Wrap Hub and Http

This commit is contained in:
Alex Hultman
2018-05-06 18:29:33 +02:00
parent 11e0482d04
commit d43e9fc265
6 changed files with 127 additions and 45 deletions
+9 -2
View File
@@ -9,7 +9,14 @@ SOURCES += \
uSockets/src/libusockets.c \
uSockets/src/socket.c \
uSockets/src/backends/epoll.c \
uSockets/src/backends/libuv.c
uSockets/src/backends/libuv.c \
Hub.cpp \
Http.cpp
INCLUDEPATH += uSockets/src
QMAKE_CXXFLAGS += -std=c++14
QMAKE_CXXFLAGS += -fsanitize=address -std=c++14
LIBS += -lasan
HEADERS += \
Hub.h \
Http.h
+2
View File
@@ -0,0 +1,2 @@
#include "Http.h"
+60
View File
@@ -0,0 +1,60 @@
#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
+2
View File
@@ -0,0 +1,2 @@
#include "Hub.h"
+50
View File
@@ -0,0 +1,50 @@
#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
+4 -43
View File
@@ -1,45 +1,5 @@
#include "libusockets.h"
#include <iostream>
#include <functional>
using namespace std;
struct HttpRequest {
};
struct HttpResponse {
void write() {
}
};
struct Hub {
us_loop *loop;
static void wakeup_cb(us_loop *loop) {
}
struct Data {
std::function<void(HttpRequest *, HttpResponse *)> onHttpRequest;
} *data;
void onHttpRequest(decltype(Data::onHttpRequest) handler) {
data->onHttpRequest = handler;
}
Hub() {
loop = us_create_loop(wakeup_cb, sizeof(Data));
data = (Data *) us_loop_userdata(loop);
}
~Hub() {
// us_loop_delete(loop);
}
};
// uWS.h
#include "Hub.h"
int main() {
@@ -55,5 +15,6 @@ int main() {
});
h.listen(nullptr, 3000, 0);
h.run();
}