Refactor into src
This commit is contained in:
@@ -5,18 +5,18 @@ CONFIG -= qt
|
|||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
uSockets/src/context.c \
|
uSockets/src/context.c \
|
||||||
uSockets/src/libusockets.c \
|
uSockets/src/libusockets.c \
|
||||||
uSockets/src/socket.c \
|
uSockets/src/socket.c \
|
||||||
uSockets/src/backends/epoll.c \
|
uSockets/src/backends/epoll.c \
|
||||||
uSockets/src/backends/libuv.c \
|
uSockets/src/backends/libuv.c \
|
||||||
Hub.cpp \
|
src/Hub.cpp \
|
||||||
Http.cpp
|
src/Http.cpp
|
||||||
|
|
||||||
INCLUDEPATH += uSockets/src
|
|
||||||
QMAKE_CXXFLAGS += -fsanitize=address -std=c++14
|
|
||||||
LIBS += -lasan
|
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
Hub.h \
|
src/Hub.h \
|
||||||
Http.h
|
src/Http.h
|
||||||
|
|
||||||
|
INCLUDEPATH += uSockets/src src
|
||||||
|
QMAKE_CXXFLAGS += -fsanitize=address
|
||||||
|
LIBS += -lasan
|
||||||
|
|||||||
@@ -1,60 +0,0 @@
|
|||||||
#ifndef HTTP_H
|
|
||||||
#define HTTP_H
|
|
||||||
|
|
||||||
#include "libusockets.h"
|
|
||||||
|
|
||||||
struct HttpRequest {
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
struct HttpResponse {
|
|
||||||
|
|
||||||
void write() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
// 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
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
#ifndef HUB_H
|
|
||||||
#define HUB_H
|
|
||||||
|
|
||||||
#include "libusockets.h"
|
|
||||||
#include <functional>
|
|
||||||
#include <new>
|
|
||||||
|
|
||||||
#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<void(HttpRequest *, HttpResponse *)> 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
|
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
// uWS.h
|
// uWS.h
|
||||||
#include "Hub.h"
|
#include "Hub.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
Hub h;
|
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.writeStatus();
|
||||||
// res.writeHeader();
|
// res.writeHeader();
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#include "Http.h"
|
||||||
|
#include "Hub.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
+33
@@ -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
|
||||||
+23
@@ -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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef HUB_H
|
||||||
|
#define HUB_H
|
||||||
|
|
||||||
|
#include "libusockets.h"
|
||||||
|
#include <functional>
|
||||||
|
#include <new>
|
||||||
|
|
||||||
|
#include "Http.h"
|
||||||
|
|
||||||
|
struct Hub {
|
||||||
|
us_loop *loop;
|
||||||
|
struct Data {
|
||||||
|
Data(us_loop *loop) : httpContext(loop) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpContext httpContext;
|
||||||
|
std::function<void(HttpRequest *, HttpResponse *, char *data, unsigned int length)> 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
|
||||||
Reference in New Issue
Block a user