Refactor into src

This commit is contained in:
Alex Hultman
2018-05-06 19:38:08 +02:00
parent d43e9fc265
commit 9cf5042a57
10 changed files with 139 additions and 129 deletions
+33
View File
@@ -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
View File
@@ -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
View File
@@ -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);
}
+32
View File
@@ -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