diff --git a/15.pro b/15.pro index 0a8e482..3f8dca8 100644 --- a/15.pro +++ b/15.pro @@ -5,18 +5,18 @@ CONFIG -= qt SOURCES += \ main.cpp \ - uSockets/src/context.c \ - uSockets/src/libusockets.c \ - uSockets/src/socket.c \ - uSockets/src/backends/epoll.c \ - uSockets/src/backends/libuv.c \ - Hub.cpp \ - Http.cpp - -INCLUDEPATH += uSockets/src -QMAKE_CXXFLAGS += -fsanitize=address -std=c++14 -LIBS += -lasan + uSockets/src/context.c \ + uSockets/src/libusockets.c \ + uSockets/src/socket.c \ + uSockets/src/backends/epoll.c \ + uSockets/src/backends/libuv.c \ + src/Hub.cpp \ + src/Http.cpp HEADERS += \ - Hub.h \ - Http.h + src/Hub.h \ + src/Http.h + +INCLUDEPATH += uSockets/src src +QMAKE_CXXFLAGS += -fsanitize=address +LIBS += -lasan diff --git a/Http.cpp b/Http.cpp deleted file mode 100644 index 590e691..0000000 --- a/Http.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "Http.h" - diff --git a/Http.h b/Http.h deleted file mode 100644 index 424cb46..0000000 --- a/Http.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef HTTP_H -#define HTTP_H - -#include "libusockets.h" - -struct HttpRequest { - -}; - -struct HttpResponse { - - void write() { - - } - -}; - -#include - -// basically a HttpServer -struct HttpContext { - us_socket_context *context; - struct Data { - - } *data; - - static void httpBegin(us_socket *s) { - // yep - std::cout << "Accepted a connection" << std::endl; - } - - static void httpData(us_socket *s, void *data, int size) { - // yep - std::cout << "got data" << std::endl; - - //us_loop_userdata(s->context->loop) - - // call into Hub::Data::onHttpRequest - // us_loop_userdata(s->context->loop) = Hub::Data - } - - static void httpEnd(us_socket *s) { - // yep - std::cout << "Disconnection of a connection" << std::endl; - } - - HttpContext(us_loop *loop) { - context = us_create_socket_context(loop, sizeof(us_socket_context)); - - context->on_accepted = httpBegin; - context->on_data = httpData; - context->on_end = httpEnd; - } - - void listen(const char *host, int port, int options) { - us_socket_context_listen(context, host, port, options, sizeof(us_socket)); - } -}; - -#endif // HTTP_H diff --git a/Hub.cpp b/Hub.cpp deleted file mode 100644 index dd97762..0000000 --- a/Hub.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "Hub.h" - diff --git a/Hub.h b/Hub.h deleted file mode 100644 index 0f63b78..0000000 --- a/Hub.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef HUB_H -#define HUB_H - -#include "libusockets.h" -#include -#include - -#include "Http.h" - -struct Hub { - us_loop *loop; - - static void wakeupCb(us_loop *loop) { - - } - - struct Data { - Data(us_loop *loop) : httpContext(loop) { - - } - - HttpContext httpContext; - std::function onHttpRequest; - } *data; - - void onHttpRequest(decltype(Data::onHttpRequest) handler) { - data->onHttpRequest = handler; - } - - Hub() { - loop = us_create_loop(wakeupCb, sizeof(Data)); - new (data = (Data *) us_loop_userdata(loop)) Data(loop); - } - - // should we expose us_listen_socket here? - // us_listen_socket_close(listen_socket) is easy so why not? - void listen(const char *host, int port, int options) { - data->httpContext.listen(host, port, options); - } - - void run() { - us_loop_run(loop); - } - - ~Hub() { - // us_loop_delete(loop); - } -}; - -#endif // HUB_H diff --git a/main.cpp b/main.cpp index c32bb1a..b642a79 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,14 @@ // uWS.h #include "Hub.h" +#include int main() { - Hub h; - h.onHttpRequest([](auto req, auto res) { + // req = ?, res = HttpSocket + h.onHttpRequest([](auto req, auto res, char *data, unsigned int length) { + + std::cout << "Got data: " << std::string(data, length) << std::endl; // res.writeStatus(); // res.writeHeader(); diff --git a/src/Http.cpp b/src/Http.cpp new file mode 100644 index 0000000..daa4b15 --- /dev/null +++ b/src/Http.cpp @@ -0,0 +1,33 @@ +#include "Http.h" +#include "Hub.h" + +#include + +void HttpContext::httpBegin(us_socket *s) { + // yep + std::cout << "Accepted a connection" << std::endl; +} + +void HttpContext::httpData(us_socket *s, void *data, int size) { + Hub::Data *hubData = (Hub::Data *) us_loop_userdata(s->context->loop); + + + hubData->onHttpRequest(nullptr, nullptr, (char *) data, size); +} + +void HttpContext::httpEnd(us_socket *s) { + // yep + std::cout << "Disconnection of a connection" << std::endl; +} + +HttpContext::HttpContext(us_loop *loop) { + context = us_create_socket_context(loop, sizeof(us_socket_context)); + + context->on_accepted = httpBegin; + context->on_data = httpData; + context->on_end = httpEnd; +} + +void HttpContext::listen(const char *host, int port, int options) { + us_socket_context_listen(context, host, port, options, sizeof(us_socket)); +} diff --git a/src/Http.h b/src/Http.h new file mode 100644 index 0000000..2d412af --- /dev/null +++ b/src/Http.h @@ -0,0 +1,33 @@ +#ifndef HTTP_H +#define HTTP_H + +#include "libusockets.h" + +struct HttpRequest { + +}; + +struct HttpResponse { + + void write() { + + } + +}; + +// basically a HttpServer +struct HttpContext { + us_socket_context *context; + struct Data { + + } *data; + + static void httpBegin(us_socket *s); + static void httpData(us_socket *s, void *data, int size); + static void httpEnd(us_socket *s); + + HttpContext(us_loop *loop); + void listen(const char *host, int port, int options); +}; + +#endif // HTTP_H diff --git a/src/Hub.cpp b/src/Hub.cpp new file mode 100644 index 0000000..d480923 --- /dev/null +++ b/src/Hub.cpp @@ -0,0 +1,23 @@ +#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(loop); +} + +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 new file mode 100644 index 0000000..0606064 --- /dev/null +++ b/src/Hub.h @@ -0,0 +1,32 @@ +#ifndef HUB_H +#define HUB_H + +#include "libusockets.h" +#include +#include + +#include "Http.h" + +struct Hub { + us_loop *loop; + struct Data { + Data(us_loop *loop) : httpContext(loop) { + + } + + HttpContext httpContext; + std::function onHttpRequest; + } *data; + + static void wakeupCb(us_loop *loop); + void onHttpRequest(decltype(Data::onHttpRequest) handler) { + data->onHttpRequest = handler; + } + + Hub(); + void listen(const char *host, int port, int options); + void run(); + ~Hub(); +}; + +#endif // HUB_H