From 2714ffe8d62e0eb828fd78dc1be5b216c5de59bb Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Tue, 11 Jun 2019 19:16:46 +0200 Subject: [PATCH] Upgrade to uSockets 0.2.0a1 (experimental) --- src/App.h | 20 +++++------ src/AsyncSocket.h | 20 +++++------ src/HttpContext.h | 76 +++++++++++++++++++++--------------------- src/Loop.h | 37 ++++++++++---------- src/WebSocket.h | 18 +++++----- src/WebSocketContext.h | 66 ++++++++++++++++++------------------ uSockets | 2 +- 7 files changed, 119 insertions(+), 120 deletions(-) diff --git a/src/App.h b/src/App.h index 98394f0..e8fa4df 100644 --- a/src/App.h +++ b/src/App.h @@ -77,7 +77,7 @@ public: webSocketContexts = std::move(other.webSocketContexts); } - TemplatedApp(us_new_socket_context_options_t options = {}) { + TemplatedApp(us_socket_context_options_t options = {}) { httpContext = uWS::HttpContext::create(uWS::Loop::get(), options); } @@ -104,7 +104,7 @@ public: "µWebSockets cannot satisfy UserData alignment requirements. You need to recompile µSockets with LIBUS_EXT_ALIGNMENT adjusted accordingly."); /* Every route has its own websocket context with its own behavior and user data type */ - auto *webSocketContext = WebSocketContext::create(Loop::get(), (us_new_socket_context_t *) httpContext); + auto *webSocketContext = WebSocketContext::create(Loop::get(), (us_socket_context_t *) httpContext); /* We need to clear this later on */ webSocketContexts.push_back(webSocketContext); @@ -116,7 +116,7 @@ public: /* If we are the first one to use compression, initialize it */ if (behavior.compression) { - LoopData *loopData = (LoopData *) us_loop_ext(us_new_socket_context_loop(SSL, webSocketContext->getSocketContext())); + LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(SSL, webSocketContext->getSocketContext())); /* Initialize loop's deflate inflate streams */ if (!loopData->zlibContext) { @@ -198,8 +198,8 @@ public: res->getHttpResponseData()->~HttpResponseData(); /* Adopting a socket invalidates it, do not rely on it directly to carry any data */ - WebSocket *webSocket = (WebSocket *) us_new_socket_context_adopt_socket(SSL, - (us_new_socket_context_t *) webSocketContext, (us_new_socket_t *) res, sizeof(WebSocketData) + sizeof(UserData)); + WebSocket *webSocket = (WebSocket *) us_socket_context_adopt_socket(SSL, + (us_socket_context_t *) webSocketContext, (us_socket_t *) res, sizeof(WebSocketData) + sizeof(UserData)); /* Update corked socket in case we got a new one (assuming we always are corked in handlers). */ webSocket->cork(); @@ -211,7 +211,7 @@ public: /* Emit open event and start the timeout */ if (behavior.open) { - us_new_socket_timeout(SSL, (us_new_socket_t *) webSocket, behavior.idleTimeout); + us_socket_timeout(SSL, (us_socket_t *) webSocket, behavior.idleTimeout); behavior.open(webSocket, req); } @@ -278,7 +278,7 @@ public: } /* Host, port, callback */ - TemplatedApp &&listen(std::string host, int port, fu2::unique_function &&handler) { + TemplatedApp &&listen(std::string host, int port, fu2::unique_function &&handler) { if (!host.length()) { return listen(port, std::move(handler)); } @@ -287,7 +287,7 @@ public: } /* Host, port, options, callback */ - TemplatedApp &&listen(std::string host, int port, int options, fu2::unique_function &&handler) { + TemplatedApp &&listen(std::string host, int port, int options, fu2::unique_function &&handler) { if (!host.length()) { return listen(port, options, std::move(handler)); } @@ -296,13 +296,13 @@ public: } /* Port, callback */ - TemplatedApp &&listen(int port, fu2::unique_function &&handler) { + TemplatedApp &&listen(int port, fu2::unique_function &&handler) { handler(httpContext->listen(nullptr, port, 0)); return std::move(*this); } /* Port, options, callback */ - TemplatedApp &&listen(int port, int options, fu2::unique_function &&handler) { + TemplatedApp &&listen(int port, int options, fu2::unique_function &&handler) { handler(httpContext->listen(nullptr, port, options)); return std::move(*this); } diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index 7204a83..451cf25 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -36,27 +36,27 @@ struct AsyncSocket { protected: /* Get loop data for socket */ LoopData *getLoopData() { - return (LoopData *) us_loop_ext(us_new_socket_context_loop(SSL, us_new_socket_context(SSL, (us_new_socket_t *) this))); + return (LoopData *) us_loop_ext(us_socket_context_loop(SSL, us_socket_context(SSL, (us_socket_t *) this))); } /* Get socket extension */ AsyncSocketData *getAsyncSocketData() { - return (AsyncSocketData *) us_new_socket_ext(SSL, (us_new_socket_t *) this); + return (AsyncSocketData *) us_socket_ext(SSL, (us_socket_t *) this); } /* Socket timeout */ void timeout(unsigned int seconds) { - us_new_socket_timeout(SSL, (us_new_socket_t *) this, seconds); + us_socket_timeout(SSL, (us_socket_t *) this, seconds); } /* Shutdown socket without any automatic drainage */ void shutdown() { - us_new_socket_shutdown(SSL, (us_new_socket_t *) this); + us_socket_shutdown(SSL, (us_socket_t *) this); } /* Immediately close socket */ - us_new_socket_t *close() { - return us_new_socket_close(SSL, (us_new_socket_t *) this); + us_socket_t *close() { + return us_socket_close(SSL, (us_socket_t *) this); } /* Cork this socket. Only one socket may ever be corked per-loop at any given time */ @@ -98,7 +98,7 @@ protected: std::string_view getRemoteAddress() { static thread_local char buf[16]; int ipLength = 16; - us_new_socket_remote_address(SSL, (us_new_socket_t *) this, buf, &ipLength); + us_socket_remote_address(SSL, (us_socket_t *) this, buf, &ipLength); return std::string_view(buf, ipLength); } @@ -107,7 +107,7 @@ protected: * writable (or we are in a state that implies polling for writable). */ std::pair write(const char *src, int length, bool optionally = false, int nextLength = 0) { /* Fake success if closed, simple fix to allow uncork of closed socket to succeed */ - if (us_new_socket_is_closed(SSL, (us_new_socket_t *) this)) { + if (us_socket_is_closed(SSL, (us_socket_t *) this)) { return {length, false}; } @@ -117,7 +117,7 @@ protected: /* We are limited if we have a per-socket buffer */ if (asyncSocketData->buffer.length()) { /* Write off as much as we can */ - int written = us_new_socket_write(SSL, (us_new_socket_t *) this, asyncSocketData->buffer.data(), asyncSocketData->buffer.length(), /*nextLength != 0 | */length); + int written = us_socket_write(SSL, (us_socket_t *) this, asyncSocketData->buffer.data(), asyncSocketData->buffer.length(), /*nextLength != 0 | */length); /* On failure return, otherwise continue down the function */ if (written < asyncSocketData->buffer.length()) { @@ -165,7 +165,7 @@ protected: } } else { /* We are not corked */ - int written = us_new_socket_write(SSL, (us_new_socket_t *) this, src, length, nextLength != 0); + int written = us_socket_write(SSL, (us_socket_t *) this, src, length, nextLength != 0); /* Did we fail? */ if (written < length) { diff --git a/src/HttpContext.h b/src/HttpContext.h index d6ef0be..5512a09 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -40,31 +40,31 @@ private: /* Maximum delay allowed until an HTTP connection is terminated due to outstanding request or rejected data (slow loris protection) */ static const int HTTP_IDLE_TIMEOUT_S = 10; - us_new_socket_context_t *getSocketContext() { - return (us_new_socket_context_t *) this; + us_socket_context_t *getSocketContext() { + return (us_socket_context_t *) this; } - static us_new_socket_context_t *getSocketContext(us_new_socket_t *s) { - return (us_new_socket_context_t *) us_new_socket_context(SSL, s); + static us_socket_context_t *getSocketContext(us_socket_t *s) { + return (us_socket_context_t *) us_socket_context(SSL, s); } HttpContextData *getSocketContextData() { - return (HttpContextData *) us_new_socket_context_ext(SSL, getSocketContext()); + return (HttpContextData *) us_socket_context_ext(SSL, getSocketContext()); } - static HttpContextData *getSocketContextDataS(us_new_socket_t *s) { - return (HttpContextData *) us_new_socket_context_ext(SSL, getSocketContext(s)); + static HttpContextData *getSocketContextDataS(us_socket_t *s) { + return (HttpContextData *) us_socket_context_ext(SSL, getSocketContext(s)); } /* Init the HttpContext by registering libusockets event handlers */ HttpContext *init() { /* Handle socket connections */ - us_new_socket_context_on_open(SSL, getSocketContext(), [](us_new_socket_t *s, int is_client, char *ip, int ip_length) { + us_socket_context_on_open(SSL, getSocketContext(), [](us_socket_t *s, int is_client, char *ip, int ip_length) { /* Any connected socket should timeout until it has a request */ - us_new_socket_timeout(SSL, s, HTTP_IDLE_TIMEOUT_S); + us_socket_timeout(SSL, s, HTTP_IDLE_TIMEOUT_S); /* Init socket ext */ - new (us_new_socket_ext(SSL, s)) HttpResponseData; + new (us_socket_ext(SSL, s)) HttpResponseData; /* Call filter */ HttpContextData *httpContextData = getSocketContextDataS(s); @@ -76,9 +76,9 @@ private: }); /* Handle socket disconnections */ - us_new_socket_context_on_close(SSL, getSocketContext(), [](us_new_socket_t *s) { + us_socket_context_on_close(SSL, getSocketContext(), [](us_socket_t *s) { /* Get socket ext */ - HttpResponseData *httpResponseData = (HttpResponseData *) us_new_socket_ext(SSL, s); + HttpResponseData *httpResponseData = (HttpResponseData *) us_socket_ext(SSL, s); /* Call filter */ HttpContextData *httpContextData = getSocketContextDataS(s); @@ -98,7 +98,7 @@ private: }); /* Handle HTTP data streams */ - us_new_socket_context_on_data(SSL, getSocketContext(), [](us_new_socket_t *s, char *data, int length) { + us_socket_context_on_data(SSL, getSocketContext(), [](us_socket_t *s, char *data, int length) { // total overhead is about 210k down to 180k // ~210k req/sec is the original perf with write in data @@ -109,11 +109,11 @@ private: HttpContextData *httpContextData = getSocketContextDataS(s); /* Do not accept any data while in shutdown state */ - if (us_new_socket_is_shut_down(SSL, (us_new_socket_t *) s)) { + if (us_socket_is_shut_down(SSL, (us_socket_t *) s)) { return s; } - HttpResponseData *httpResponseData = (HttpResponseData *) us_new_socket_ext(SSL, s); + HttpResponseData *httpResponseData = (HttpResponseData *) us_socket_ext(SSL, s); /* Cork this socket */ ((AsyncSocket *) s)->cork(); @@ -125,15 +125,15 @@ private: void *returnedSocket = httpResponseData->consumePostPadded(data, length, s, [httpContextData](void *s, uWS::HttpRequest *httpRequest) -> void * { /* For every request we reset the timeout and hang until user makes action */ /* Warning: if we are in shutdown state, resetting the timer is a security issue! */ - us_new_socket_timeout(SSL, (us_new_socket_t *) s, 0); + us_socket_timeout(SSL, (us_socket_t *) s, 0); /* Reset httpResponse */ - HttpResponseData *httpResponseData = (HttpResponseData *) us_new_socket_ext(SSL, (us_new_socket_t *) s); + HttpResponseData *httpResponseData = (HttpResponseData *) us_socket_ext(SSL, (us_socket_t *) s); httpResponseData->offset = 0; /* Are we not ready for another request yet? Terminate the connection. */ if (httpResponseData->state & HttpResponseData::HTTP_RESPONSE_PENDING) { - us_new_socket_close(SSL, (us_new_socket_t *) s); + us_socket_close(SSL, (us_socket_t *) s); return nullptr; } @@ -146,7 +146,7 @@ private: /* If first pass failed, we try and match by "any" method */ if (!httpContextData->router.route("*", httpRequest->getUrl(), routerData)) { /* If second pass fail, we have to force close this socket as we have no handler for it */ - us_new_socket_close(SSL, (us_new_socket_t *) s); + us_socket_close(SSL, (us_socket_t *) s); return nullptr; } } @@ -158,12 +158,12 @@ private: } /* Was the socket closed? */ - if (us_new_socket_is_closed(SSL, (struct us_new_socket_t *) s)) { + if (us_socket_is_closed(SSL, (struct us_socket_t *) s)) { return nullptr; } /* We absolutely have to terminate parsing if shutdown */ - if (us_new_socket_is_shut_down(SSL, (us_new_socket_t *) s)) { + if (us_socket_is_shut_down(SSL, (us_socket_t *) s)) { return nullptr; } @@ -176,7 +176,7 @@ private: /* If we have not responded and we have a data handler, we need to timeout to enfore client sending the data */ if (!((HttpResponse *) s)->hasResponded() && httpResponseData->inStream) { - us_new_socket_timeout(SSL, (us_new_socket_t *) s, HTTP_IDLE_TIMEOUT_S); + us_socket_timeout(SSL, (us_socket_t *) s, HTTP_IDLE_TIMEOUT_S); } /* Continue parsing */ @@ -188,25 +188,25 @@ private: /* Getting a chunk of data while having a data handler should reset timeout (todo: if last, short timeout, if not last, bigger timeout) */ /* Really, we only need to reset timeout to the larger delay if we are not fin */ - us_new_socket_timeout(SSL, (struct us_new_socket_t *) user, HTTP_IDLE_TIMEOUT_S); + us_socket_timeout(SSL, (struct us_socket_t *) user, HTTP_IDLE_TIMEOUT_S); /* We might respond in the handler, so do not change timeout after this */ httpResponseData->inStream(data, fin); /* Was the socket closed? */ - if (us_new_socket_is_closed(SSL, (struct us_new_socket_t *) user)) { + if (us_socket_is_closed(SSL, (struct us_socket_t *) user)) { return nullptr; } /* We absolutely have to terminate parsing if shutdown */ - if (us_new_socket_is_shut_down(SSL, (us_new_socket_t *) user)) { + if (us_socket_is_shut_down(SSL, (us_socket_t *) user)) { return nullptr; } } return user; }, [](void *user) { /* Close any socket on HTTP errors */ - us_new_socket_close(SSL, (us_new_socket_t *) user); + us_socket_close(SSL, (us_socket_t *) user); return nullptr; }); @@ -219,7 +219,7 @@ private: ((AsyncSocket *) s)->timeout(HTTP_IDLE_TIMEOUT_S); } - return (us_new_socket_t *) returnedSocket; + return (us_socket_t *) returnedSocket; } /* If we upgraded, check here (differ between nullptr close and nullptr upgrade) */ @@ -234,7 +234,7 @@ private: httpContextData->upgradedWebSocket = nullptr; /* Return the new upgraded websocket */ - return (us_new_socket_t *) asyncSocket; + return (us_socket_t *) asyncSocket; } /* We cannot return nullptr to the underlying stack in any case */ @@ -242,7 +242,7 @@ private: }); /* Handle HTTP write out (note: SSL_read may trigger this spuriously, the app need to handle spurious calls) */ - us_new_socket_context_on_writable(SSL, getSocketContext(), [](us_new_socket_t *s) { + us_socket_context_on_writable(SSL, getSocketContext(), [](us_socket_t *s) { AsyncSocket *asyncSocket = (AsyncSocket *) s; HttpResponseData *httpResponseData = (HttpResponseData *) asyncSocket->getAsyncSocketData(); @@ -250,7 +250,7 @@ private: /* Ask the developer to write data and return success (true) or failure (false), OR skip sending anything and return success (true). */ if (httpResponseData->onWritable) { /* We are now writable, so hang timeout again, the user does not have to do anything so we should hang until end or tryEnd rearms timeout */ - us_new_socket_timeout(SSL, s, 0); + us_socket_timeout(SSL, s, 0); /* We expect the developer to return whether or not write was successful (true). * If write was never called, the developer should still return true so that we may drain. */ @@ -277,7 +277,7 @@ private: }); /* Handle FIN, HTTP does not support half-closed sockets, so simply close */ - us_new_socket_context_on_end(SSL, getSocketContext(), [](us_new_socket_t *s) { + us_socket_context_on_end(SSL, getSocketContext(), [](us_socket_t *s) { /* We do not care for half closed sockets */ AsyncSocket *asyncSocket = (AsyncSocket *) s; @@ -286,7 +286,7 @@ private: }); /* Handle socket timeouts, simply close them so to not confuse client with FIN */ - us_new_socket_context_on_timeout(SSL, getSocketContext(), [](us_new_socket_t *s) { + us_socket_context_on_timeout(SSL, getSocketContext(), [](us_socket_t *s) { /* Force close rather than gracefully shutdown and risk confusing the client with a complete download */ AsyncSocket *asyncSocket = (AsyncSocket *) s; @@ -306,17 +306,17 @@ private: public: /* Construct a new HttpContext using specified loop */ - static HttpContext *create(Loop *loop, us_new_socket_context_options_t options = {}) { + static HttpContext *create(Loop *loop, us_socket_context_options_t options = {}) { HttpContext *httpContext; - httpContext = (HttpContext *) us_new_create_socket_context(SSL, (us_loop *) loop, sizeof(HttpContextData), options); + httpContext = (HttpContext *) us_create_socket_context(SSL, (us_loop_t *) loop, sizeof(HttpContextData), options); if (!httpContext) { return nullptr; } /* Init socket context data */ - new ((HttpContextData *) us_new_socket_context_ext(SSL, (us_new_socket_context_t *) httpContext)) HttpContextData(); + new ((HttpContextData *) us_socket_context_ext(SSL, (us_socket_context_t *) httpContext)) HttpContextData(); return httpContext->init(); } @@ -327,7 +327,7 @@ public: httpContextData->~HttpContextData(); /* Free the socket context in whole */ - us_new_socket_context_free(SSL, getSocketContext()); + us_socket_context_free(SSL, getSocketContext()); } void filter(fu2::unique_function *, int)> &&filterHandler) { @@ -352,8 +352,8 @@ public: } /* Listen to port using this HttpContext */ - us_listen_socket *listen(const char *host, int port, int options) { - return us_new_socket_context_listen(SSL, getSocketContext(), host, port, options, sizeof(HttpResponseData)); + us_listen_socket_t *listen(const char *host, int port, int options) { + return us_socket_context_listen(SSL, getSocketContext(), host, port, options, sizeof(HttpResponseData)); } }; diff --git a/src/Loop.h b/src/Loop.h index c071e70..918520d 100644 --- a/src/Loop.h +++ b/src/Loop.h @@ -21,12 +21,12 @@ /* The loop is lazily created per-thread and run with uWS::run() */ #include "LoopData.h" -#include +#include namespace uWS { struct Loop { private: - static void wakeupCb(us_loop *loop) { + static void wakeupCb(us_loop_t *loop) { LoopData *loopData = (LoopData *) us_loop_ext(loop); /* Swap current deferQueue */ @@ -42,7 +42,7 @@ private: loopData->deferQueues[oldDeferQueue].clear(); } - static void preCb(us_loop *loop) { + static void preCb(us_loop_t *loop) { LoopData *loopData = (LoopData *) us_loop_ext(loop); if (loopData->preHandler) { @@ -55,7 +55,7 @@ private: } } - static void postCb(us_loop *loop) { + static void postCb(us_loop_t *loop) { LoopData *loopData = (LoopData *) us_loop_ext(loop); /* We should move over to using only these */ @@ -72,13 +72,12 @@ private: ~Loop() = default; Loop *init() { - new (us_loop_ext((us_loop *) this)) LoopData; + new (us_loop_ext((us_loop_t *) this)) LoopData; return this; } - /* Todo: should take void ptr */ - static Loop *create(bool defaultLoop) { - return ((Loop *) us_create_loop(defaultLoop, wakeupCb, preCb, postCb, sizeof(LoopData)))->init(); + static Loop *create(void *hint) { + return ((Loop *) us_create_loop(hint, wakeupCb, preCb, postCb, sizeof(LoopData)))->init(); } public: @@ -90,10 +89,10 @@ public: /* If we are given a native loop pointer we pass that to uSockets and let it deal with it */ if (existingNativeLoop) { /* Todo: here we want to pass the pointer, not a boolean */ - lazyLoop = create(true); + lazyLoop = create(existingNativeLoop); /* We cannot register automatic free here, must be manually done */ } else { - lazyLoop = create(false); + lazyLoop = create(nullptr); std::atexit([]() { Loop::get()->free(); }); @@ -105,53 +104,53 @@ public: /* Freeing the default loop should be done once */ void free() { - LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this); + LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this); loopData->~LoopData(); /* uSockets will track whether this loop is owned by us or a borrowed alien loop */ - us_loop_free((us_loop *) this); + us_loop_free((us_loop_t *) this); } /* We want to have multiple of these */ void addPostHandler(fu2::unique_function &&handler) { - LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this); + LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this); loopData->postHandlers.emplace_back(std::move(handler)); } /* Set postCb callback */ void setPostHandler(fu2::unique_function &&handler) { - LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this); + LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this); loopData->postHandler = std::move(handler); } void setPreHandler(fu2::unique_function &&handler) { - LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this); + LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this); loopData->preHandler = std::move(handler); } /* Defer this callback on Loop's thread of execution */ void defer(fu2::unique_function &&cb) { - LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this); + LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this); //if (std::thread::get_id() == ) // todo: add fast path for same thread id loopData->deferMutex.lock(); loopData->deferQueues[loopData->currentDeferQueue].emplace_back(std::move(cb)); loopData->deferMutex.unlock(); - us_wakeup_loop((us_loop *) this); + us_wakeup_loop((us_loop_t *) this); } /* Actively block and run this loop */ void run() { - us_loop_run((us_loop *) this); + us_loop_run((us_loop_t *) this); } /* Passively integrate with the underlying default loop */ /* Used to seamlessly integrate with third parties such as Node.js */ void integrate() { - us_loop_integrate((us_loop *) this); + us_loop_integrate((us_loop_t *) this); } }; diff --git a/src/WebSocket.h b/src/WebSocket.h index fc4822f..8f047ef 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -34,14 +34,14 @@ private: typedef AsyncSocket Super; void *init(bool perMessageDeflate, bool slidingCompression, std::string &&backpressure) { - new (us_new_socket_ext(SSL, (us_new_socket_t *) this)) WebSocketData(perMessageDeflate, slidingCompression, std::move(backpressure)); + new (us_socket_ext(SSL, (us_socket_t *) this)) WebSocketData(perMessageDeflate, slidingCompression, std::move(backpressure)); return this; } public: /* Returns pointer to the per socket user data */ void *getUserData() { - WebSocketData *webSocketData = (WebSocketData *) us_new_socket_ext(SSL, (us_new_socket_t *) this); + WebSocketData *webSocketData = (WebSocketData *) us_socket_ext(SSL, (us_socket_t *) this); /* We just have it overallocated by sizeof type */ return (webSocketData + 1); } @@ -94,7 +94,7 @@ public: /* Send websocket close frame, emit close event, send FIN if successful */ void end(int code, std::string_view message = {}) { /* Check if we already called this one */ - WebSocketData *webSocketData = (WebSocketData *) us_new_socket_ext(SSL, (us_new_socket_t *) this); + WebSocketData *webSocketData = (WebSocketData *) us_socket_ext(SSL, (us_socket_t *) this); if (webSocketData->isShuttingDown) { return; } @@ -120,8 +120,8 @@ public: } /* Emit close event */ - WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_new_socket_context_ext(SSL, - (us_new_socket_context_t *) us_new_socket_context(SSL, (us_new_socket_t *) this) + WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, + (us_socket_context_t *) us_socket_context(SSL, (us_socket_t *) this) ); if (webSocketContextData->closeHandler) { webSocketContextData->closeHandler(this, code, message); @@ -133,8 +133,8 @@ public: /* Subscribe to a topic according to MQTT rules and syntax */ void subscribe(std::string_view topic) { - WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_new_socket_context_ext(SSL, - (us_new_socket_context_t *) us_new_socket_context(SSL, (us_new_socket_t *) this) + WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, + (us_socket_context_t *) us_socket_context(SSL, (us_socket_t *) this) ); /* Fix this up */ @@ -145,8 +145,8 @@ public: /* Publish a message to a topic according to MQTT rules and syntax */ void publish(std::string_view topic, std::string_view message) { - WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_new_socket_context_ext(SSL, - (us_new_socket_context_t *) us_new_socket_context(SSL, (us_new_socket_t *) this) + WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, + (us_socket_context_t *) us_socket_context(SSL, (us_socket_t *) this) ); /* We frame the message right here and only pass raw bytes to the pub/subber */ diff --git a/src/WebSocketContext.h b/src/WebSocketContext.h index 66bd338..52abd78 100644 --- a/src/WebSocketContext.h +++ b/src/WebSocketContext.h @@ -32,17 +32,17 @@ struct WebSocketContext { private: WebSocketContext() = delete; - us_new_socket_context_t *getSocketContext() { - return (us_new_socket_context_t *) this; + us_socket_context_t *getSocketContext() { + return (us_socket_context_t *) this; } WebSocketContextData *getExt() { - return (WebSocketContextData *) us_new_socket_context_ext(SSL, (us_new_socket_context_t *) this); + return (WebSocketContextData *) us_socket_context_ext(SSL, (us_socket_context_t *) this); } /* If we have negotiated compression, set this frame compressed */ static bool setCompressed(uWS::WebSocketState *wState, void *s) { - WebSocketData *webSocketData = (WebSocketData *) us_new_socket_ext(SSL, (us_new_socket_t *) s); + WebSocketData *webSocketData = (WebSocketData *) us_socket_ext(SSL, (us_socket_t *) s); if (webSocketData->compressionStatus == WebSocketData::CompressionStatus::ENABLED) { webSocketData->compressionStatus = WebSocketData::CompressionStatus::COMPRESSED_FRAME; @@ -53,14 +53,14 @@ private: } static void forceClose(uWS::WebSocketState *wState, void *s) { - us_new_socket_close(SSL, (us_new_socket_t *) s); + us_socket_close(SSL, (us_socket_t *) s); } /* Returns true on breakage */ static bool handleFragment(char *data, size_t length, unsigned int remainingBytes, int opCode, bool fin, uWS::WebSocketState *webSocketState, void *s) { /* WebSocketData and WebSocketContextData */ - WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_new_socket_context_ext(SSL, us_new_socket_context(SSL, (us_new_socket_t *) s)); - WebSocketData *webSocketData = (WebSocketData *) us_new_socket_ext(SSL, (us_new_socket_t *) s); + WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, us_socket_context(SSL, (us_socket_t *) s)); + WebSocketData *webSocketData = (WebSocketData *) us_socket_ext(SSL, (us_socket_t *) s); /* Is this a non-control frame? */ if (opCode < 3) { @@ -71,7 +71,7 @@ private: if (webSocketData->compressionStatus == WebSocketData::CompressionStatus::COMPRESSED_FRAME) { webSocketData->compressionStatus = WebSocketData::CompressionStatus::ENABLED; - LoopData *loopData = (LoopData *) us_loop_ext(us_new_socket_context_loop(SSL, us_new_socket_context(SSL, (us_new_socket_t *) s))); + LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(SSL, us_socket_context(SSL, (us_socket_t *) s))); std::string_view inflatedFrame = loopData->inflationStream->inflate(loopData->zlibContext, {data, length}, webSocketContextData->maxPayloadLength); if (!inflatedFrame.length()) { forceClose(webSocketState, s); @@ -91,7 +91,7 @@ private: /* Emit message event & break if we are closed or shut down when returning */ if (webSocketContextData->messageHandler) { webSocketContextData->messageHandler((WebSocket *) s, std::string_view(data, length), (uWS::OpCode) opCode); - if (us_new_socket_is_closed(SSL, (us_new_socket_t *) s) || webSocketData->isShuttingDown) { + if (us_socket_is_closed(SSL, (us_socket_t *) s) || webSocketData->isShuttingDown) { return true; } } @@ -114,8 +114,8 @@ private: webSocketData->fragmentBuffer.append("...."); LoopData *loopData = (LoopData *) us_loop_ext( - us_new_socket_context_loop(SSL, - us_new_socket_context(SSL, (us_new_socket_t *) s) + us_socket_context_loop(SSL, + us_socket_context(SSL, (us_socket_t *) s) ) ); @@ -144,7 +144,7 @@ private: /* Emit message and check for shutdown or close */ if (webSocketContextData->messageHandler) { webSocketContextData->messageHandler((WebSocket *) s, std::string_view(data, length), (uWS::OpCode) opCode); - if (us_new_socket_is_closed(SSL, (us_new_socket_t *) s) || webSocketData->isShuttingDown) { + if (us_socket_is_closed(SSL, (us_socket_t *) s) || webSocketData->isShuttingDown) { return true; } } @@ -212,7 +212,7 @@ private: } static bool refusePayloadLength(uint64_t length, uWS::WebSocketState *wState, void *s) { - auto *webSocketContextData = (WebSocketContextData *) us_new_socket_context_ext(SSL, us_new_socket_context(SSL, (us_new_socket_t *) s)); + auto *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, us_socket_context(SSL, (us_socket_t *) s)); /* Return true for refuse, false for accept */ return webSocketContextData->maxPayloadLength < length; @@ -224,13 +224,13 @@ private: * any backpressure from HTTP state kept. */ /* Handle socket disconnections */ - us_new_socket_context_on_close(SSL, getSocketContext(), [](auto *s) { + us_socket_context_on_close(SSL, getSocketContext(), [](auto *s) { /* For whatever reason, if we already have emitted close event, do not emit it again */ - WebSocketData *webSocketData = (WebSocketData *) (us_new_socket_ext(SSL, s)); + WebSocketData *webSocketData = (WebSocketData *) (us_socket_ext(SSL, s)); if (!webSocketData->isShuttingDown) { /* Emit close event */ - auto *webSocketContextData = (WebSocketContextData *) us_new_socket_context_ext(SSL, us_new_socket_context(SSL, (us_new_socket_t *) s)); + auto *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, us_socket_context(SSL, (us_socket_t *) s)); if (webSocketContextData->closeHandler) { webSocketContextData->closeHandler((WebSocket *) s, 1006, {}); @@ -247,10 +247,10 @@ private: }); /* Handle WebSocket data streams */ - us_new_socket_context_on_data(SSL, getSocketContext(), [](auto *s, char *data, int length) { + us_socket_context_on_data(SSL, getSocketContext(), [](auto *s, char *data, int length) { /* We need the websocket data */ - WebSocketData *webSocketData = (WebSocketData *) (us_new_socket_ext(SSL, s)); + WebSocketData *webSocketData = (WebSocketData *) (us_socket_ext(SSL, s)); /* When in websocket shutdown mode, we do not care for ANY message, whether responding close frame or not. * We only care for the TCP FIN really, not emitting any message after closing is key */ @@ -258,7 +258,7 @@ private: return s; } - auto *webSocketContextData = (WebSocketContextData *) us_new_socket_context_ext(SSL, us_new_socket_context(SSL, (us_new_socket_t *) s)); + auto *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, us_socket_context(SSL, (us_socket_t *) s)); auto *asyncSocket = (AsyncSocket *) s; /* Every time we get data and not in shutdown state we simply reset the timeout */ @@ -286,15 +286,15 @@ private: }); /* Handle HTTP write out (note: SSL_read may trigger this spuriously, the app need to handle spurious calls) */ - us_new_socket_context_on_writable(SSL, getSocketContext(), [](auto *s) { + us_socket_context_on_writable(SSL, getSocketContext(), [](auto *s) { /* It makes sense to check for us_is_shut_down here and return if so, to avoid shutting down twice */ - if (us_new_socket_is_shut_down(SSL, (us_new_socket_t *) s)) { + if (us_socket_is_shut_down(SSL, (us_socket_t *) s)) { return s; } AsyncSocket *asyncSocket = (AsyncSocket *) s; - WebSocketData *webSocketData = (WebSocketData *)(us_new_socket_ext(SSL, s)); + WebSocketData *webSocketData = (WebSocketData *)(us_socket_ext(SSL, s)); /* We store old backpressure since it is unclear whether write drained anything */ int backpressure = asyncSocket->getBufferedAmount(); @@ -304,7 +304,7 @@ private: /* Behavior: if we actively drain backpressure, always reset timeout (even if we are in shutdown) */ if (backpressure < asyncSocket->getBufferedAmount()) { - auto *webSocketContextData = (WebSocketContextData *) us_new_socket_context_ext(SSL, us_new_socket_context(SSL, (us_new_socket_t *) s)); + auto *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, us_socket_context(SSL, (us_socket_t *) s)); asyncSocket->timeout(webSocketContextData->idleTimeout); } @@ -317,7 +317,7 @@ private: } } else if (backpressure > asyncSocket->getBufferedAmount()) { /* Only call drain if we actually drained backpressure */ - auto *webSocketContextData = (WebSocketContextData *) us_new_socket_context_ext(SSL, us_new_socket_context(SSL, (us_new_socket_t *) s)); + auto *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, us_socket_context(SSL, (us_socket_t *) s)); if (webSocketContextData->drainHandler) { webSocketContextData->drainHandler((WebSocket *) s); } @@ -328,19 +328,19 @@ private: }); /* Handle FIN, HTTP does not support half-closed sockets, so simply close */ - us_new_socket_context_on_end(SSL, getSocketContext(), [](auto *s) { + us_socket_context_on_end(SSL, getSocketContext(), [](auto *s) { /* If we get a fin, we just close I guess */ - us_new_socket_close(SSL, (us_new_socket_t *) s); + us_socket_close(SSL, (us_socket_t *) s); return s; }); /* Handle socket timeouts, simply close them so to not confuse client with FIN */ - us_new_socket_context_on_timeout(SSL, getSocketContext(), [](auto *s) { + us_socket_context_on_timeout(SSL, getSocketContext(), [](auto *s) { /* Timeout is very simple; we just close it */ - us_new_socket_close(SSL, (us_new_socket_t *) s); + us_socket_close(SSL, (us_socket_t *) s); return s; }); @@ -349,22 +349,22 @@ private: } void free() { - WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_new_socket_context_ext(SSL, (us_new_socket_context_t *) this); + WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, (us_socket_context_t *) this); webSocketContextData->~WebSocketContextData(); - us_new_socket_context_free(SSL, (us_new_socket_context_t *) this); + us_socket_context_free(SSL, (us_socket_context_t *) this); } public: /* WebSocket contexts are always child contexts to a HTTP context so no SSL options are needed as they are inherited */ - static WebSocketContext *create(Loop *loop, us_new_socket_context_t *parentSocketContext) { - WebSocketContext *webSocketContext = (WebSocketContext *) us_new_create_child_socket_context(SSL, parentSocketContext, sizeof(WebSocketContextData)); + static WebSocketContext *create(Loop *loop, us_socket_context_t *parentSocketContext) { + WebSocketContext *webSocketContext = (WebSocketContext *) us_create_child_socket_context(SSL, parentSocketContext, sizeof(WebSocketContextData)); if (!webSocketContext) { return nullptr; } /* Init socket context data */ - new ((WebSocketContextData *) us_new_socket_context_ext(SSL, (us_new_socket_context_t *)webSocketContext)) WebSocketContextData; + new ((WebSocketContextData *) us_socket_context_ext(SSL, (us_socket_context_t *)webSocketContext)) WebSocketContextData; return webSocketContext->init(); } }; diff --git a/uSockets b/uSockets index f1cae03..1fa56f7 160000 --- a/uSockets +++ b/uSockets @@ -1 +1 @@ -Subproject commit f1cae03ecdc0539f4155b964726065288b15c7a4 +Subproject commit 1fa56f7f6fff7e116534b45d48d9dd372e507038