Update a few ideas

This commit is contained in:
Alex Hultman
2018-06-18 21:07:33 +02:00
parent 10ce6df701
commit 460b9d6afc
9 changed files with 157 additions and 132 deletions
+22 -7
View File
@@ -1,22 +1,37 @@
TEMPLATE = app
CONFIG += console c++14
CONFIG += console c++1z
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp \
uSockets/src/eventing/epoll.c \
uSockets/src/context.c \
uSockets/src/libusockets.c \
uSockets/src/socket.c \
uSockets/src/backends/epoll.c \
uSockets/src/backends/libuv.c \
uSockets/src/eventing/libuv.c \
uSockets/src/ssl.c \
uSockets/src/loop.c \
src/Hub.cpp \
src/Http.cpp
src/Http.cpp \
src/Context.cpp
HEADERS += \
src/Hub.h \
src/Http.h
src/Http.h \
src/Context.h
#uSockets/libusockets.h \
#uSockets/internal/eventing/epoll.h \
#uSockets/internal/networking/bsd.h \
#uSockets/internal/eventing/libuv.h \
#uSockets/internal/common.h \
#uSockets/interfaces/timer.h \
#uSockets/interfaces/socket.h \
#uSockets/interfaces/poll.h \
#uSockets/interfaces/context.h \
#uSockets/interfaces/loop.h \
#uSockets/interfaces/ssl.h \
#uSockets/internal/loop.h
INCLUDEPATH += uSockets/src src
#QMAKE_CXXFLAGS += -fsanitize=address
#LIBS += -lasan
LIBS += -lasan -lssl -lcrypto
+19 -14
View File
@@ -3,29 +3,33 @@
#include <iostream>
#include <string.h>
//char largeBuf[] = "HTTP/1.1 200 OK\r\nContent-Length: 512\r\n\r\n";
//int largeHttpBufSize = sizeof(largeBuf) + 512 - 1;
char largeBuf[] = "HTTP/1.1 200 OK\r\nContent-Length: 52428800\r\n\r\n";
int largeHttpBufSize = sizeof(largeBuf) + 52428800 - 1;
char *largeHttpBuf;
int main() {
std::cout << "HttpSocket size: " << sizeof(HttpSocket) << std::endl;
largeHttpBuf = (char *) malloc(largeHttpBufSize);
memcpy(largeHttpBuf, largeBuf, sizeof(largeBuf) - 1);
// 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;
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) {
// an http socket can BOTH send buffered data AND have one stream in and one stream out!
// writeStatus(234314234)
// writeHeader(key, value)
// streamOut(asdasdasdsad)
// if some asshole posted data, stream it in
/*s->streamIn([]() {
});*/
//std::cout << "Got data: " << std::string(data, length) << std::endl;
// s->writeStatus();
@@ -33,13 +37,14 @@ int main() {
// if url == / then stream index.htm
s->stream(largeHttpBufSize, [](int offset) {
// stream out the response
/*s->stream(largeHttpBufSize, [](int offset) {
return std::make_pair(largeHttpBuf + offset, largeHttpBufSize - offset);
});
});*/
});
h.onHttpDisconnection([](HttpSocket *s) {
//std::cout << "HTTP disconnection" << std::endl;
std::cout << "HTTP disconnection" << std::endl;
});
h.listen(nullptr, 3000, 0);
View File
+6
View File
@@ -0,0 +1,6 @@
#ifndef CONTEXT_H
#define CONTEXT_H
#endif // CONTEXT_H
-58
View File
@@ -1,58 +0,0 @@
#include "Http.h"
#include "Hub.h"
#include <iostream>
void HttpContext::httpBegin(us_socket *s) {
Hub::Data *hubData = (Hub::Data *) us_loop_userdata(s->context->loop);
//std::cout << "BeginHTTP: " << s << std::endl;
// should not touch the socket?
new (s) HttpSocket();
// tester
((HttpSocket *) s)->outStream = nullptr;
hubData->onHttpConnection((HttpSocket *) s);
}
void HttpContext::httpEnd(us_socket *s) {
Hub::Data *hubData = (Hub::Data *) us_loop_userdata(s->context->loop);
hubData->onHttpDisconnection((HttpSocket *) s);
}
void HttpContext::httpData(us_socket *s, void *data, int size) {
Hub::Data *hubData = (Hub::Data *) us_loop_userdata(s->context->loop);
hubData->onHttpRequest((HttpSocket *) s, nullptr, (char *) data, size);
}
void HttpContext::httpOut(us_socket *s) {
HttpSocket *httpSocket = (HttpSocket *) s;
std::pair<const char *, int> chunk = httpSocket->outStream(httpSocket->offset);
httpSocket->offset += us_socket_write(s, chunk.first, chunk.second);
}
HttpContext::HttpContext() {
context.on_accepted = httpBegin;
context.on_data = httpData;
context.on_end = httpEnd;
context.on_writable = httpOut;
}
void HttpContext::listen(const char *host, int port, int options) {
us_socket_context_listen(&context, host, port, options, sizeof(HttpSocket));
}
// HttpSocket
void HttpSocket::stream(int length, decltype(outStream) stream) {
outStream = stream;
offset = 0;
// try stream
std::pair<const char *, int> chunk = outStream(offset);
offset = us_socket_write(&s, chunk.first, chunk.second);
}
-18
View File
@@ -9,7 +9,6 @@ struct HttpRequest {
};
struct HttpSocket {
us_socket s;
int offset = 0;
HttpSocket() {
@@ -20,21 +19,4 @@ struct HttpSocket {
void stream(int length, decltype(outStream) stream);
};
// basically a HttpServer
struct HttpContext {
us_socket_context context;
/*struct Data {
} *data;*/
static void httpBegin(us_socket *s);
// httpIn
static void httpData(us_socket *s, void *data, int size);
static void httpOut(us_socket *s);
static void httpEnd(us_socket *s);
HttpContext();
void listen(const char *host, int port, int options);
};
#endif // HTTP_H
-26
View File
@@ -1,27 +1 @@
#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();
// this could lie in Data constructor
data->httpContext = (HttpContext *) us_create_socket_context(loop, sizeof(HttpContext));
new (data->httpContext) HttpContext();
}
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);
}
+109 -8
View File
@@ -4,23 +4,82 @@
#include "libusockets.h"
#include <functional>
#include <new>
#include <string_view>
#include <iostream>
#include "Http.h"
#include "Context.h"
// maybe a Context is both TCP and SSL in one?
template <bool isServer, bool isSSL>
struct Context {
struct Hub {
us_loop *loop;
struct Data {
Data() {
}
HttpContext *httpContext;
std::function<void(HttpSocket *)> onHttpConnection;
std::function<void(HttpSocket *)> onHttpDisconnection;
std::function<void(HttpSocket *, HttpRequest *, char *data, unsigned int length)> onHttpRequest;
} *data;
static void wakeupCb(us_loop *loop);
us_socket_context *httpContext;
void init(us_loop *loop) {
httpContext = us_create_socket_context(loop, sizeof(Data));
new (data = (Data *) us_socket_context_ext(httpContext)) Data();
// register shims
us_socket_context_on_open(httpContext, [](us_socket *s) {
Data *data = (Data *) us_socket_context_ext(us_socket_get_context(s));
// we always give pointers to us_socket?
// same mistake as before?
// note: this is VERY tricky to keep bug-free!
// we could give the ext here, and skip all bugs?
data->onHttpConnection((HttpSocket *) s);
});
us_socket_context_on_close(httpContext, [](us_socket *s) {
Data *data = (Data *) us_socket_context_ext(us_socket_get_context(s));
// we always give pointers to us_socket?
// same mistake as before?
// note: this is VERY tricky to keep bug-free!
// we could give the ext here, and skip all bugs?
data->onHttpDisconnection((HttpSocket *) s);
});
us_socket_context_on_data(httpContext, [](us_socket *s, char *data, int length) {
//Data *data = (Data *) us_socket_context_ext(us_socket_get_context(s));
// a HttpSocket is basically the Http state and everything needed for it, we use it to parse the data and it knows about its context
//
// here we run the HTTP parser on this data
std::cout << std::string_view(data, length) << std::endl;
// we always give pointers to us_socket?
// same mistake as before?
// note: this is VERY tricky to keep bug-free!
// we could give the ext here, and skip all bugs?
//data->((HttpSocket *) s);
});
}
void onHttpConnection(decltype(Data::onHttpConnection) handler) {
data->onHttpConnection = handler;
}
@@ -31,10 +90,52 @@ struct Hub {
data->onHttpRequest = handler;
}
Hub();
void listen(const char *host, int port, int options);
void run();
~Hub();
// this should only be enabled if we are server context!
void listen(const char *host, int port, int options) {
us_socket_context_listen(httpContext, host, port, options, sizeof(HttpSocket));
}
};
// are we SSL or regular hub?
struct Hub : Context<true, false> {
us_loop *loop;
using Context::onHttpConnection;
using Context<true, false>::listen;
struct Data {
Data() {
}
} *data;
static void wakeupCb(us_loop *loop) {
}
static void preCb(us_loop *loop) {
}
static void postCb(us_loop *loop) {
}
Hub() : loop(us_create_loop(wakeupCb, preCb, postCb, sizeof(Data))) {
new (data = (Data *) us_loop_ext(loop)) Data();
Context<true, false>::init(loop);
}
void run() {
us_loop_run(loop);
}
~Hub() {
}
};
#endif // HUB_H