Update a few ideas
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
#ifndef CONTEXT_H
|
||||
#define CONTEXT_H
|
||||
|
||||
|
||||
|
||||
#endif // CONTEXT_H
|
||||
@@ -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
@@ -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
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user