From ce15f4f87ffeff32d0bdb85955d453eb80b5b8f3 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 18 Jun 2018 21:36:27 +0200 Subject: [PATCH] More new ideas --- main.cpp | 25 ++++--------------------- src/Http.h | 17 ++++++++++++++++- src/Hub.h | 40 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 56 insertions(+), 26 deletions(-) diff --git a/main.cpp b/main.cpp index c56cef7..14e3261 100644 --- a/main.cpp +++ b/main.cpp @@ -1,23 +1,11 @@ // uWS.h #include "Hub.h" -#include -#include int main() { std::cout << "HttpSocket size: " << sizeof(HttpSocket) << std::endl; - // 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; - }); - - // req = ?, res = HttpSocket - // should be s, req, nothing more or less! - h.onHttpRequest([](HttpSocket *s, auto req, char *data, unsigned int length) { + // either use this, or use onHttpRoute but not both + uWS::defaultHub.onHttpRequest([](HttpSocket *s, HttpRequest *req) { // an http socket can BOTH send buffered data AND have one stream in and one stream out! @@ -41,12 +29,7 @@ int main() { /*s->stream(largeHttpBufSize, [](int offset) { return std::make_pair(largeHttpBuf + offset, largeHttpBufSize - offset); });*/ - }); + }).listen("localhost", 3000, 0).run(); - h.onHttpDisconnection([](HttpSocket *s) { - std::cout << "HTTP disconnection" << std::endl; - }); - - h.listen(nullptr, 3000, 0); - h.run(); + // todo: important swapping to SSL should be .secureListen(same interfaces) and work out of the box! } diff --git a/src/Http.h b/src/Http.h index a57510a..1f9a530 100644 --- a/src/Http.h +++ b/src/Http.h @@ -8,13 +8,28 @@ struct HttpRequest { }; + +// HttpSocket is the ext of us_socket struct HttpSocket { - int offset = 0; + + // incomplete headers buffer + std::string headerBuffer; + + // HttpSocket() { } + // the HttpParser should maybe be moved out of this into its own HttpProtocol.h like with websocket? + void parse(char *data, int length) { + + std::cout << "Parsing headers: " << std::string_view(data, length) << std::endl; + + } + + int offset = 0; + std::function(int)> outStream; void stream(int length, decltype(outStream) stream); }; diff --git a/src/Hub.h b/src/Hub.h index 484ca6c..e536a94 100644 --- a/src/Hub.h +++ b/src/Hub.h @@ -10,10 +10,14 @@ #include "Http.h" #include "Context.h" +struct Hub; + // maybe a Context is both TCP and SSL in one? template struct Context { + Hub &hub; + struct Data { Data() { @@ -22,11 +26,16 @@ struct Context { std::function onHttpConnection; std::function onHttpDisconnection; - std::function onHttpRequest; + std::function onHttpRequest; } *data; us_socket_context *httpContext; + Context(Hub &hub) : hub(hub) { + + } + + // one for each? ssl and non-ssl? void init(us_loop *loop) { httpContext = us_create_socket_context(loop, sizeof(Data)); @@ -36,9 +45,18 @@ struct Context { us_socket_context_on_open(httpContext, [](us_socket *s) { Data *data = (Data *) us_socket_context_ext(us_socket_get_context(s)); + + // here we need to construct a HTTP socket on the ext! + + + // we always give pointers to us_socket? // same mistake as before? + if (!data->onHttpConnection) { + return; + } + // note: this is VERY tricky to keep bug-free! // we could give the ext here, and skip all bugs? data->onHttpConnection((HttpSocket *) s); @@ -50,6 +68,10 @@ struct Context { // we always give pointers to us_socket? // same mistake as before? + if (!data->onHttpDisconnection) { + return; + } + // note: this is VERY tricky to keep bug-free! // we could give the ext here, and skip all bugs? data->onHttpDisconnection((HttpSocket *) s); @@ -86,13 +108,19 @@ struct Context { void onHttpDisconnection(decltype(Data::onHttpDisconnection) handler) { data->onHttpDisconnection = handler; } - void onHttpRequest(decltype(Data::onHttpRequest) handler) { + Hub &onHttpRequest(decltype(Data::onHttpRequest) handler) { data->onHttpRequest = handler; + + return hub; } // this should only be enabled if we are server context! - void listen(const char *host, int port, int options) { + + // should return Loop, not hub? or simply make it so that ALL contexts stem from some Hub? Hub is essentually the Loop abstraction? could work + Hub &listen(const char *host, int port, int options) { us_socket_context_listen(httpContext, host, port, options, sizeof(HttpSocket)); + + return hub; } }; @@ -123,7 +151,7 @@ struct Hub : Context { } - Hub() : loop(us_create_loop(wakeupCb, preCb, postCb, sizeof(Data))) { + Hub() : loop(us_create_loop(wakeupCb, preCb, postCb, sizeof(Data))), Context(*this) { new (data = (Data *) us_loop_ext(loop)) Data(); Context::init(loop); @@ -138,4 +166,8 @@ struct Hub : Context { } }; +namespace uWS { +thread_local Hub defaultHub; +} + #endif // HUB_H