Refactor using new uSockets API
This commit is contained in:
+43
-47
@@ -18,7 +18,6 @@
|
||||
#ifndef WEBSOCKETCONTEXT_H
|
||||
#define WEBSOCKETCONTEXT_H
|
||||
|
||||
#include "StaticDispatch.h"
|
||||
#include "WebSocketContextData.h"
|
||||
#include "WebSocketProtocol.h"
|
||||
#include "WebSocketData.h"
|
||||
@@ -27,26 +26,23 @@
|
||||
namespace uWS {
|
||||
|
||||
template <bool SSL, bool isServer>
|
||||
struct WebSocketContext : StaticDispatch<SSL> {
|
||||
struct WebSocketContext {
|
||||
template <bool> friend struct TemplatedApp;
|
||||
template <bool, class> friend class WebSocketProtocol;
|
||||
private:
|
||||
using SOCKET_CONTEXT_TYPE = typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE;
|
||||
using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE;
|
||||
using StaticDispatch<SSL>::static_dispatch;
|
||||
WebSocketContext() = delete;
|
||||
|
||||
SOCKET_CONTEXT_TYPE *getSocketContext() {
|
||||
return (SOCKET_CONTEXT_TYPE *) this;
|
||||
us_new_socket_context_t *getSocketContext() {
|
||||
return (us_new_socket_context_t *) this;
|
||||
}
|
||||
|
||||
WebSocketContextData<SSL> *getExt() {
|
||||
return (WebSocketContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)((SOCKET_CONTEXT_TYPE *) this);
|
||||
return (WebSocketContextData<SSL> *) us_new_socket_context_ext(SSL, (us_new_socket_context_t *) this);
|
||||
}
|
||||
|
||||
/* If we have negotiated compression, set this frame compressed */
|
||||
static bool setCompressed(uWS::WebSocketState<isServer> *wState, void *s) {
|
||||
WebSocketData *webSocketData = (WebSocketData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) s);
|
||||
WebSocketData *webSocketData = (WebSocketData *) us_new_socket_ext(SSL, (us_new_socket_t *) s);
|
||||
|
||||
if (webSocketData->compressionStatus == WebSocketData::CompressionStatus::ENABLED) {
|
||||
webSocketData->compressionStatus = WebSocketData::CompressionStatus::COMPRESSED_FRAME;
|
||||
@@ -57,16 +53,16 @@ private:
|
||||
}
|
||||
|
||||
static void forceClose(uWS::WebSocketState<isServer> *wState, void *s) {
|
||||
us_socket_close((us_socket *) s);
|
||||
us_new_socket_close(SSL, (us_new_socket_t *) s);
|
||||
}
|
||||
|
||||
/* Returns true on breakage */
|
||||
static bool handleFragment(char *data, size_t length, unsigned int remainingBytes, int opCode, bool fin, uWS::WebSocketState<isServer> *webSocketState, void *s) {
|
||||
/* WebSocketData and WebSocketContextData */
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(
|
||||
static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *) s)
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) us_new_socket_context_ext(SSL,
|
||||
us_new_socket_context(SSL, (us_new_socket_t *) s)
|
||||
);
|
||||
WebSocketData *webSocketData = (WebSocketData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) s);
|
||||
WebSocketData *webSocketData = (WebSocketData *) us_new_socket_ext(SSL, (us_new_socket_t *) s);
|
||||
|
||||
/* Is this a non-control frame? */
|
||||
if (opCode < 3) {
|
||||
@@ -78,8 +74,8 @@ private:
|
||||
webSocketData->compressionStatus = WebSocketData::CompressionStatus::ENABLED;
|
||||
|
||||
LoopData *loopData = (LoopData *)us_loop_ext(
|
||||
static_dispatch(us_ssl_socket_context_loop, us_socket_context_loop)(
|
||||
static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *)s)
|
||||
us_new_socket_context_loop(SSL,
|
||||
us_new_socket_context(SSL, (us_new_socket_t *)s)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -102,7 +98,7 @@ private:
|
||||
/* Emit message event & break if we are closed or shut down when returning */
|
||||
if (webSocketContextData->messageHandler) {
|
||||
webSocketContextData->messageHandler((WebSocket<SSL, isServer> *) s, std::string_view(data, length), (uWS::OpCode) opCode);
|
||||
if (us_socket_is_closed((us_socket *)s) || webSocketData->isShuttingDown) {
|
||||
if (us_new_socket_is_closed(SSL, (us_new_socket_t *)s) || webSocketData->isShuttingDown) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -125,8 +121,8 @@ private:
|
||||
webSocketData->fragmentBuffer.append("....");
|
||||
|
||||
LoopData *loopData = (LoopData *) us_loop_ext(
|
||||
static_dispatch(us_ssl_socket_context_loop, us_socket_context_loop)(
|
||||
static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *) s)
|
||||
us_new_socket_context_loop(SSL,
|
||||
us_new_socket_context(SSL, (us_new_socket_t *) s)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -155,7 +151,7 @@ private:
|
||||
/* Emit message and check for shutdown or close */
|
||||
if (webSocketContextData->messageHandler) {
|
||||
webSocketContextData->messageHandler((WebSocket<SSL, isServer> *) s, std::string_view(data, length), (uWS::OpCode) opCode);
|
||||
if (us_socket_is_closed((us_socket *)s) || webSocketData->isShuttingDown) {
|
||||
if (us_new_socket_is_closed(SSL, (us_new_socket_t *)s) || webSocketData->isShuttingDown) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -223,8 +219,8 @@ private:
|
||||
}
|
||||
|
||||
static bool refusePayloadLength(uint64_t length, uWS::WebSocketState<isServer> *wState, void *s) {
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(
|
||||
static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *)s)
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) us_new_socket_context_ext(SSL,
|
||||
us_new_socket_context(SSL, (us_new_socket_t *)s)
|
||||
);
|
||||
|
||||
/* Return true for refuse, false for accept */
|
||||
@@ -237,14 +233,14 @@ private:
|
||||
* any backpressure from HTTP state kept. */
|
||||
|
||||
/* Handle socket disconnections */
|
||||
static_dispatch(us_ssl_socket_context_on_close, us_socket_context_on_close)(getSocketContext(), [](auto *s) {
|
||||
us_new_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 *) (static_dispatch(us_ssl_socket_ext, us_socket_ext)(s));
|
||||
WebSocketData *webSocketData = (WebSocketData *) (us_new_socket_ext(SSL, s));
|
||||
if (!webSocketData->isShuttingDown) {
|
||||
/* Emit close event */
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(
|
||||
static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *)s)
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) us_new_socket_context_ext(SSL,
|
||||
us_new_socket_context(SSL, (us_new_socket_t *)s)
|
||||
);
|
||||
|
||||
if (webSocketContextData->closeHandler) {
|
||||
@@ -259,18 +255,18 @@ private:
|
||||
});
|
||||
|
||||
/* Handle WebSocket data streams */
|
||||
static_dispatch(us_ssl_socket_context_on_data, us_socket_context_on_data)(getSocketContext(), [](auto *s, char *data, int length) {
|
||||
us_new_socket_context_on_data(SSL, getSocketContext(), [](auto *s, char *data, int length) {
|
||||
|
||||
/* Everytime we get data, we reset the timeout to our idleTimeout, that's the only timer we have */
|
||||
|
||||
/* If not in websocket shutdown state, for every */
|
||||
|
||||
// återställ inte om vi är i shutdown state, dvs, ge den inte massa tid på sig att skicka massa skit-frames mellan och upphålla oss!
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(
|
||||
static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *) s)
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) us_new_socket_context_ext(SSL,
|
||||
us_new_socket_context(SSL, (us_new_socket_t *) s)
|
||||
);
|
||||
|
||||
static_dispatch(us_ssl_socket_timeout, us_socket_timeout)((SOCKET_TYPE *) s, webSocketContextData->idleTimeout);
|
||||
us_new_socket_timeout(SSL, (us_new_socket_t *) s, webSocketContextData->idleTimeout);
|
||||
|
||||
|
||||
/* We always cork on data */
|
||||
@@ -278,7 +274,7 @@ private:
|
||||
webSocket->cork();
|
||||
|
||||
/* We need the websocket data */
|
||||
WebSocketData *webSocketData = (WebSocketData *) (static_dispatch(us_ssl_socket_ext, us_socket_ext)(s));
|
||||
WebSocketData *webSocketData = (WebSocketData *) (us_new_socket_ext(SSL, s));
|
||||
|
||||
/* This parser has virtually no overhead */
|
||||
uWS::WebSocketProtocol<isServer, WebSocketContext<SSL, isServer>>::consume(data, length, (WebSocketState<isServer> *) webSocketData, s);
|
||||
@@ -289,7 +285,7 @@ private:
|
||||
webSocket->uncork();
|
||||
|
||||
// cannot do anything else if closed
|
||||
if (us_socket_is_closed((us_socket *) s)) {
|
||||
if (us_new_socket_is_closed(SSL, (us_new_socket_t *) s)) {
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -304,15 +300,15 @@ private:
|
||||
});
|
||||
|
||||
/* Handle HTTP write out (note: SSL_read may trigger this spuriously, the app need to handle spurious calls) */
|
||||
static_dispatch(us_ssl_socket_context_on_writable, us_socket_context_on_writable)(getSocketContext(), [](auto *s) {
|
||||
us_new_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 (static_dispatch(us_ssl_socket_is_shut_down, us_socket_is_shut_down)((SOCKET_TYPE *) s)) {
|
||||
if (us_new_socket_is_shut_down(SSL, (us_new_socket_t *) s)) {
|
||||
return s;
|
||||
}
|
||||
|
||||
AsyncSocket<SSL> *webSocket = (AsyncSocket<SSL> *) s;
|
||||
WebSocketData *webSocketData = (WebSocketData *)(static_dispatch(us_ssl_socket_ext, us_socket_ext)(s));
|
||||
WebSocketData *webSocketData = (WebSocketData *)(us_new_socket_ext(SSL, s));
|
||||
|
||||
/* We store old backpressure since it is unclear whether write drained anything */
|
||||
int backpressure = webSocket->getBufferedAmount();
|
||||
@@ -322,8 +318,8 @@ private:
|
||||
|
||||
/* Behavior: if we actively drain backpressure, always reset timeout (even if we are in shutdown) */
|
||||
if (backpressure < webSocket->getBufferedAmount()) {
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(
|
||||
static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *)s)
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) us_new_socket_context_ext(SSL,
|
||||
us_new_socket_context(SSL, (us_new_socket_t *)s)
|
||||
);
|
||||
webSocket->timeout(webSocketContextData->idleTimeout);
|
||||
}
|
||||
@@ -337,8 +333,8 @@ private:
|
||||
}
|
||||
} else if (backpressure > webSocket->getBufferedAmount()) {
|
||||
/* Only call drain if we actually drained backpressure */
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(
|
||||
static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *)s)
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) us_new_socket_context_ext(SSL,
|
||||
us_new_socket_context(SSL, (us_new_socket_t *)s)
|
||||
);
|
||||
if (webSocketContextData->drainHandler) {
|
||||
webSocketContextData->drainHandler((WebSocket<SSL, isServer> *) s);
|
||||
@@ -350,19 +346,19 @@ private:
|
||||
});
|
||||
|
||||
/* Handle FIN, HTTP does not support half-closed sockets, so simply close */
|
||||
static_dispatch(us_ssl_socket_context_on_end, us_socket_context_on_end)(getSocketContext(), [](auto *s) {
|
||||
us_new_socket_context_on_end(SSL, getSocketContext(), [](auto *s) {
|
||||
|
||||
/* If we get a fin, we just close I guess */
|
||||
static_dispatch(us_ssl_socket_close, us_socket_close)((SOCKET_TYPE *) s);
|
||||
us_new_socket_close(SSL, (us_new_socket_t *) s);
|
||||
|
||||
return s;
|
||||
});
|
||||
|
||||
/* Handle socket timeouts, simply close them so to not confuse client with FIN */
|
||||
static_dispatch(us_ssl_socket_context_on_timeout, us_socket_context_on_timeout)(getSocketContext(), [](auto *s) {
|
||||
us_new_socket_context_on_timeout(SSL, getSocketContext(), [](auto *s) {
|
||||
|
||||
/* Timeout is very simple; we just close it */
|
||||
static_dispatch(us_ssl_socket_close, us_socket_close)((SOCKET_TYPE *) s);
|
||||
us_new_socket_close(SSL, (us_new_socket_t *) s);
|
||||
|
||||
return s;
|
||||
});
|
||||
@@ -371,22 +367,22 @@ private:
|
||||
}
|
||||
|
||||
void free() {
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)((SOCKET_CONTEXT_TYPE *) this);
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) us_new_socket_context_ext(SSL, (us_new_socket_context_t *) this);
|
||||
webSocketContextData->~WebSocketContextData();
|
||||
|
||||
static_dispatch(us_ssl_socket_context_free, us_socket_context_free)((SOCKET_CONTEXT_TYPE *) this);
|
||||
us_new_socket_context_free(SSL, (us_new_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, SOCKET_CONTEXT_TYPE *parentSocketContext) {
|
||||
WebSocketContext *webSocketContext = (WebSocketContext *)static_dispatch(us_create_child_ssl_socket_context, us_create_child_socket_context)(parentSocketContext, sizeof(WebSocketContextData<SSL>));
|
||||
static WebSocketContext *create(Loop *loop, us_new_socket_context_t *parentSocketContext) {
|
||||
WebSocketContext *webSocketContext = (WebSocketContext *) us_new_create_child_socket_context(SSL, parentSocketContext, sizeof(WebSocketContextData<SSL>));
|
||||
if (!webSocketContext) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* Init socket context data */
|
||||
new ((WebSocketContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)((SOCKET_CONTEXT_TYPE *)webSocketContext)) WebSocketContextData<SSL>;
|
||||
new ((WebSocketContextData<SSL> *) us_new_socket_context_ext(SSL, (us_new_socket_context_t *)webSocketContext)) WebSocketContextData<SSL>;
|
||||
return webSocketContext->init();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user