diff --git a/15.pro b/15.pro index a894ce8..b4f6562 100644 --- a/15.pro +++ b/15.pro @@ -1,22 +1,37 @@ TEMPLATE = app -CONFIG += console c++14 +CONFIG += console c++1z CONFIG -= app_bundle CONFIG -= qt SOURCES += \ main.cpp \ + uSockets/src/eventing/epoll.c \ uSockets/src/context.c \ - uSockets/src/libusockets.c \ uSockets/src/socket.c \ - uSockets/src/backends/epoll.c \ - uSockets/src/backends/libuv.c \ + uSockets/src/eventing/libuv.c \ + uSockets/src/ssl.c \ + uSockets/src/loop.c \ src/Hub.cpp \ - src/Http.cpp + src/Http.cpp \ + src/Context.cpp HEADERS += \ src/Hub.h \ - src/Http.h + src/Http.h \ + src/Context.h + #uSockets/libusockets.h \ + #uSockets/internal/eventing/epoll.h \ + #uSockets/internal/networking/bsd.h \ + #uSockets/internal/eventing/libuv.h \ + #uSockets/internal/common.h \ + #uSockets/interfaces/timer.h \ + #uSockets/interfaces/socket.h \ + #uSockets/interfaces/poll.h \ + #uSockets/interfaces/context.h \ + #uSockets/interfaces/loop.h \ + #uSockets/interfaces/ssl.h \ + #uSockets/internal/loop.h INCLUDEPATH += uSockets/src src #QMAKE_CXXFLAGS += -fsanitize=address -#LIBS += -lasan +LIBS += -lasan -lssl -lcrypto diff --git a/main.cpp b/main.cpp index 30fc6ce..c56cef7 100644 --- a/main.cpp +++ b/main.cpp @@ -3,29 +3,33 @@ #include #include -//char largeBuf[] = "HTTP/1.1 200 OK\r\nContent-Length: 512\r\n\r\n"; -//int largeHttpBufSize = sizeof(largeBuf) + 512 - 1; - -char largeBuf[] = "HTTP/1.1 200 OK\r\nContent-Length: 52428800\r\n\r\n"; -int largeHttpBufSize = sizeof(largeBuf) + 52428800 - 1; - -char *largeHttpBuf; - int main() { std::cout << "HttpSocket size: " << sizeof(HttpSocket) << std::endl; - largeHttpBuf = (char *) malloc(largeHttpBufSize); - memcpy(largeHttpBuf, largeBuf, sizeof(largeBuf) - 1); + // defaultHub is per thread ready for action + // uWS::defaultHub().onHttpRequest().listen().isListening().run(); Hub h; h.onHttpConnection([](HttpSocket *s) { - //std::cout << "HTTP connection" << std::endl; + std::cout << "HTTP connection" << std::endl; }); // req = ?, res = HttpSocket + // should be s, req, nothing more or less! h.onHttpRequest([](HttpSocket *s, auto req, char *data, unsigned int length) { + // an http socket can BOTH send buffered data AND have one stream in and one stream out! + + // writeStatus(234314234) + // writeHeader(key, value) + // streamOut(asdasdasdsad) + + // if some asshole posted data, stream it in + /*s->streamIn([]() { + + });*/ + //std::cout << "Got data: " << std::string(data, length) << std::endl; // s->writeStatus(); @@ -33,13 +37,14 @@ int main() { // if url == / then stream index.htm - s->stream(largeHttpBufSize, [](int offset) { + // stream out the response + /*s->stream(largeHttpBufSize, [](int offset) { return std::make_pair(largeHttpBuf + offset, largeHttpBufSize - offset); - }); + });*/ }); h.onHttpDisconnection([](HttpSocket *s) { - //std::cout << "HTTP disconnection" << std::endl; + std::cout << "HTTP disconnection" << std::endl; }); h.listen(nullptr, 3000, 0); diff --git a/src/Context.cpp b/src/Context.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/Context.h b/src/Context.h new file mode 100644 index 0000000..ddd9dd4 --- /dev/null +++ b/src/Context.h @@ -0,0 +1,6 @@ +#ifndef CONTEXT_H +#define CONTEXT_H + + + +#endif // CONTEXT_H diff --git a/src/Http.cpp b/src/Http.cpp index 82cbd0f..e69de29 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -1,58 +0,0 @@ -#include "Http.h" -#include "Hub.h" - -#include - -void HttpContext::httpBegin(us_socket *s) { - Hub::Data *hubData = (Hub::Data *) us_loop_userdata(s->context->loop); - - //std::cout << "BeginHTTP: " << s << std::endl; - - // should not touch the socket? - new (s) HttpSocket(); - - // tester - ((HttpSocket *) s)->outStream = nullptr; - - hubData->onHttpConnection((HttpSocket *) s); -} - -void HttpContext::httpEnd(us_socket *s) { - Hub::Data *hubData = (Hub::Data *) us_loop_userdata(s->context->loop); - - hubData->onHttpDisconnection((HttpSocket *) s); -} - -void HttpContext::httpData(us_socket *s, void *data, int size) { - Hub::Data *hubData = (Hub::Data *) us_loop_userdata(s->context->loop); - - hubData->onHttpRequest((HttpSocket *) s, nullptr, (char *) data, size); -} - -void HttpContext::httpOut(us_socket *s) { - HttpSocket *httpSocket = (HttpSocket *) s; - - std::pair chunk = httpSocket->outStream(httpSocket->offset); - httpSocket->offset += us_socket_write(s, chunk.first, chunk.second); -} - -HttpContext::HttpContext() { - context.on_accepted = httpBegin; - context.on_data = httpData; - context.on_end = httpEnd; - context.on_writable = httpOut; -} - -void HttpContext::listen(const char *host, int port, int options) { - us_socket_context_listen(&context, host, port, options, sizeof(HttpSocket)); -} - -// HttpSocket -void HttpSocket::stream(int length, decltype(outStream) stream) { - outStream = stream; - offset = 0; - - // try stream - std::pair chunk = outStream(offset); - offset = us_socket_write(&s, chunk.first, chunk.second); -} diff --git a/src/Http.h b/src/Http.h index 1c26f4d..a57510a 100644 --- a/src/Http.h +++ b/src/Http.h @@ -9,7 +9,6 @@ struct HttpRequest { }; struct HttpSocket { - us_socket s; int offset = 0; HttpSocket() { @@ -20,21 +19,4 @@ struct HttpSocket { void stream(int length, decltype(outStream) stream); }; -// basically a HttpServer -struct HttpContext { - us_socket_context context; - /*struct Data { - - } *data;*/ - - static void httpBegin(us_socket *s); - // httpIn - static void httpData(us_socket *s, void *data, int size); - static void httpOut(us_socket *s); - static void httpEnd(us_socket *s); - - HttpContext(); - void listen(const char *host, int port, int options); -}; - #endif // HTTP_H diff --git a/src/Hub.cpp b/src/Hub.cpp index c6df810..8b13789 100644 --- a/src/Hub.cpp +++ b/src/Hub.cpp @@ -1,27 +1 @@ -#include "Hub.h" - -void Hub::wakeupCb(us_loop *loop) { - -} - -Hub::Hub() { - loop = us_create_loop(wakeupCb, sizeof(Data)); - new (data = (Data *) us_loop_userdata(loop)) Data(); - - // this could lie in Data constructor - data->httpContext = (HttpContext *) us_create_socket_context(loop, sizeof(HttpContext)); - new (data->httpContext) HttpContext(); -} - -void Hub::listen(const char *host, int port, int options) { - data->httpContext->listen(host, port, options); -} - -void Hub::run() { - us_loop_run(loop); -} - -Hub::~Hub() { - // us_loop_delete(loop); -} diff --git a/src/Hub.h b/src/Hub.h index 36d4089..484ca6c 100644 --- a/src/Hub.h +++ b/src/Hub.h @@ -4,23 +4,82 @@ #include "libusockets.h" #include #include +#include +#include #include "Http.h" +#include "Context.h" + +// maybe a Context is both TCP and SSL in one? +template +struct Context { -struct Hub { - us_loop *loop; struct Data { Data() { + } - HttpContext *httpContext; std::function onHttpConnection; std::function onHttpDisconnection; std::function onHttpRequest; } *data; - static void wakeupCb(us_loop *loop); + us_socket_context *httpContext; + + void init(us_loop *loop) { + httpContext = us_create_socket_context(loop, sizeof(Data)); + + new (data = (Data *) us_socket_context_ext(httpContext)) Data(); + + // register shims + us_socket_context_on_open(httpContext, [](us_socket *s) { + Data *data = (Data *) us_socket_context_ext(us_socket_get_context(s)); + + // we always give pointers to us_socket? + // same mistake as before? + + // note: this is VERY tricky to keep bug-free! + // we could give the ext here, and skip all bugs? + data->onHttpConnection((HttpSocket *) s); + }); + + us_socket_context_on_close(httpContext, [](us_socket *s) { + Data *data = (Data *) us_socket_context_ext(us_socket_get_context(s)); + + // we always give pointers to us_socket? + // same mistake as before? + + // note: this is VERY tricky to keep bug-free! + // we could give the ext here, and skip all bugs? + data->onHttpDisconnection((HttpSocket *) s); + }); + + us_socket_context_on_data(httpContext, [](us_socket *s, char *data, int length) { + //Data *data = (Data *) us_socket_context_ext(us_socket_get_context(s)); + + + + + // a HttpSocket is basically the Http state and everything needed for it, we use it to parse the data and it knows about its context + + // + + + + // here we run the HTTP parser on this data + + std::cout << std::string_view(data, length) << std::endl; + + // we always give pointers to us_socket? + // same mistake as before? + + // note: this is VERY tricky to keep bug-free! + // we could give the ext here, and skip all bugs? + //data->((HttpSocket *) s); + }); + } + void onHttpConnection(decltype(Data::onHttpConnection) handler) { data->onHttpConnection = handler; } @@ -31,10 +90,52 @@ struct Hub { data->onHttpRequest = handler; } - Hub(); - void listen(const char *host, int port, int options); - void run(); - ~Hub(); + // this should only be enabled if we are server context! + void listen(const char *host, int port, int options) { + us_socket_context_listen(httpContext, host, port, options, sizeof(HttpSocket)); + } +}; + +// are we SSL or regular hub? +struct Hub : Context { + us_loop *loop; + + using Context::onHttpConnection; + using Context::listen; + + struct Data { + Data() { + + + } + + } *data; + + static void wakeupCb(us_loop *loop) { + + } + + static void preCb(us_loop *loop) { + + } + + static void postCb(us_loop *loop) { + + } + + Hub() : loop(us_create_loop(wakeupCb, preCb, postCb, sizeof(Data))) { + new (data = (Data *) us_loop_ext(loop)) Data(); + + Context::init(loop); + } + + void run() { + us_loop_run(loop); + } + + ~Hub() { + + } }; #endif // HUB_H diff --git a/uSockets b/uSockets index 146ef52..ad3ee4c 160000 --- a/uSockets +++ b/uSockets @@ -1 +1 @@ -Subproject commit 146ef525667b653fe7fab7d66d623c32abc481d1 +Subproject commit ad3ee4c3b51ec95ddecc97a7e1f09c658e899e35