From 57255c874d79f2a13cad497f54a3a0f49bdec6d3 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Tue, 30 Oct 2018 03:26:43 +0100 Subject: [PATCH] Hook up AsyncSocket with WebSocket --- src/AsyncSocket.h | 6 ++++++ src/WebSocket.h | 11 +++++------ src/WebSocketContext.h | 10 ++++++++++ src/WebSocketData.h | 4 +++- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index 253e1ad..64b05e3 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -28,6 +28,7 @@ namespace uWS { template struct AsyncSocket : StaticDispatch { template friend struct HttpContext; + template friend struct WebSocketContext; protected: using SOCKET_TYPE = typename StaticDispatch::SOCKET_TYPE; using StaticDispatch::static_dispatch; @@ -40,8 +41,13 @@ protected: ); } + // we need a type safe realType = getData + /* Get socket extension */ void *getExt() { + + // we might have multiple inheritance so need to know the middle type + return static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this); } diff --git a/src/WebSocket.h b/src/WebSocket.h index c033023..434d292 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -2,31 +2,30 @@ #define WEBSOCKET_H #include "WebSocketData.h" - #include "WebSocketProtocol.h" +#include "AsyncSocket.h" #include namespace uWS { template -struct WebSocket { +struct WebSocket : AsyncSocket { private: - + typedef AsyncSocket Super; public: void send(std::string_view message) { - // this path should use AsyncSocket with cork and everything + // if corkAllocate(size) then corkFree(unused) // format the response char buf[100]; int writeLength = WebSocketProtocol>::formatMessage(buf, message.data(), message.length(), uWS::OpCode::TEXT, message.length(), false); - us_socket_write((us_socket *) this, buf, writeLength, false); - + Super::write(buf, writeLength); } diff --git a/src/WebSocketContext.h b/src/WebSocketContext.h index 9d22d1a..e2ad05c 100644 --- a/src/WebSocketContext.h +++ b/src/WebSocketContext.h @@ -9,6 +9,8 @@ #include "WebSocketData.h" +#include "AsyncSocket.h" + namespace uWS { template @@ -75,12 +77,20 @@ private: /* Handle HTTP data streams */ static_dispatch(us_ssl_socket_context_on_data, us_socket_context_on_data)(getSocketContext(), [](auto *s, char *data, int length) { + + AsyncSocket *webSocket = (AsyncSocket *) s; + + webSocket->cork(); + // get the data WebSocketData *wsState = (WebSocketData *) us_socket_ext(s); // this parser requires almost no time -> 215k req/sec of 215k possible uWS::WebSocketProtocol>::consume(data, length, wsState, s); + + webSocket->uncork(); + return s; }); diff --git a/src/WebSocketData.h b/src/WebSocketData.h index ed72956..316e0e5 100644 --- a/src/WebSocketData.h +++ b/src/WebSocketData.h @@ -2,10 +2,12 @@ #define WEBSOCKETDATA_H #include "WebSocketProtocol.h" +#include "AsyncSocketData.h" namespace uWS { -struct WebSocketData : WebSocketState { +// take care with get_ext here ! +struct WebSocketData : AsyncSocketData, WebSocketState { private: public: