More new ideas

This commit is contained in:
Alex Hultman
2018-06-18 21:36:27 +02:00
parent 460b9d6afc
commit ce15f4f87f
3 changed files with 56 additions and 26 deletions
+4 -21
View File
@@ -1,23 +1,11 @@
// uWS.h
#include "Hub.h"
#include <iostream>
#include <string.h>
int main() {
std::cout << "HttpSocket size: " << sizeof(HttpSocket) << std::endl;
// 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;
});
// req = ?, res = HttpSocket
// should be s, req, nothing more or less!
h.onHttpRequest([](HttpSocket *s, auto req, char *data, unsigned int length) {
// either use this, or use onHttpRoute but not both
uWS::defaultHub.onHttpRequest([](HttpSocket *s, HttpRequest *req) {
// an http socket can BOTH send buffered data AND have one stream in and one stream out!
@@ -41,12 +29,7 @@ int main() {
/*s->stream(largeHttpBufSize, [](int offset) {
return std::make_pair(largeHttpBuf + offset, largeHttpBufSize - offset);
});*/
});
}).listen("localhost", 3000, 0).run();
h.onHttpDisconnection([](HttpSocket *s) {
std::cout << "HTTP disconnection" << std::endl;
});
h.listen(nullptr, 3000, 0);
h.run();
// todo: important swapping to SSL should be .secureListen(same interfaces) and work out of the box!
}
+16 -1
View File
@@ -8,13 +8,28 @@ struct HttpRequest {
};
// HttpSocket is the ext of us_socket
struct HttpSocket {
int offset = 0;
// incomplete headers buffer
std::string headerBuffer;
//
HttpSocket() {
}
// the HttpParser should maybe be moved out of this into its own HttpProtocol.h like with websocket?
void parse(char *data, int length) {
std::cout << "Parsing headers: " << std::string_view(data, length) << std::endl;
}
int offset = 0;
std::function<std::pair<const char *, int>(int)> outStream;
void stream(int length, decltype(outStream) stream);
};
+36 -4
View File
@@ -10,10 +10,14 @@
#include "Http.h"
#include "Context.h"
struct Hub;
// maybe a Context is both TCP and SSL in one?
template <bool isServer, bool isSSL>
struct Context {
Hub &hub;
struct Data {
Data() {
@@ -22,11 +26,16 @@ struct Context {
std::function<void(HttpSocket *)> onHttpConnection;
std::function<void(HttpSocket *)> onHttpDisconnection;
std::function<void(HttpSocket *, HttpRequest *, char *data, unsigned int length)> onHttpRequest;
std::function<void(HttpSocket *, HttpRequest *)> onHttpRequest;
} *data;
us_socket_context *httpContext;
Context(Hub &hub) : hub(hub) {
}
// one for each? ssl and non-ssl?
void init(us_loop *loop) {
httpContext = us_create_socket_context(loop, sizeof(Data));
@@ -36,9 +45,18 @@ struct Context {
us_socket_context_on_open(httpContext, [](us_socket *s) {
Data *data = (Data *) us_socket_context_ext(us_socket_get_context(s));
// here we need to construct a HTTP socket on the ext!
// we always give pointers to us_socket?
// same mistake as before?
if (!data->onHttpConnection) {
return;
}
// note: this is VERY tricky to keep bug-free!
// we could give the ext here, and skip all bugs?
data->onHttpConnection((HttpSocket *) s);
@@ -50,6 +68,10 @@ struct Context {
// we always give pointers to us_socket?
// same mistake as before?
if (!data->onHttpDisconnection) {
return;
}
// note: this is VERY tricky to keep bug-free!
// we could give the ext here, and skip all bugs?
data->onHttpDisconnection((HttpSocket *) s);
@@ -86,13 +108,19 @@ struct Context {
void onHttpDisconnection(decltype(Data::onHttpDisconnection) handler) {
data->onHttpDisconnection = handler;
}
void onHttpRequest(decltype(Data::onHttpRequest) handler) {
Hub &onHttpRequest(decltype(Data::onHttpRequest) handler) {
data->onHttpRequest = handler;
return hub;
}
// this should only be enabled if we are server context!
void listen(const char *host, int port, int options) {
// should return Loop, not hub? or simply make it so that ALL contexts stem from some Hub? Hub is essentually the Loop abstraction? could work
Hub &listen(const char *host, int port, int options) {
us_socket_context_listen(httpContext, host, port, options, sizeof(HttpSocket));
return hub;
}
};
@@ -123,7 +151,7 @@ struct Hub : Context<true, false> {
}
Hub() : loop(us_create_loop(wakeupCb, preCb, postCb, sizeof(Data))) {
Hub() : loop(us_create_loop(wakeupCb, preCb, postCb, sizeof(Data))), Context<true, false>(*this) {
new (data = (Data *) us_loop_ext(loop)) Data();
Context<true, false>::init(loop);
@@ -138,4 +166,8 @@ struct Hub : Context<true, false> {
}
};
namespace uWS {
thread_local Hub defaultHub;
}
#endif // HUB_H