Hook up AsyncSocket with WebSocket

This commit is contained in:
Alex Hultman
2018-10-30 03:26:43 +01:00
parent f2e2b7107a
commit 57255c874d
4 changed files with 24 additions and 7 deletions
+6
View File
@@ -28,6 +28,7 @@ namespace uWS {
template <bool SSL> template <bool SSL>
struct AsyncSocket : StaticDispatch<SSL> { struct AsyncSocket : StaticDispatch<SSL> {
template <bool> friend struct HttpContext; template <bool> friend struct HttpContext;
template <bool> friend struct WebSocketContext;
protected: protected:
using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE; using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE;
using StaticDispatch<SSL>::static_dispatch; using StaticDispatch<SSL>::static_dispatch;
@@ -40,8 +41,13 @@ protected:
); );
} }
// we need a type safe realType = getData<MiddleType>
/* Get socket extension */ /* Get socket extension */
void *getExt() { 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); return static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this);
} }
+5 -6
View File
@@ -2,31 +2,30 @@
#define WEBSOCKET_H #define WEBSOCKET_H
#include "WebSocketData.h" #include "WebSocketData.h"
#include "WebSocketProtocol.h" #include "WebSocketProtocol.h"
#include "AsyncSocket.h"
#include <string_view> #include <string_view>
namespace uWS { namespace uWS {
template <bool SSL, bool isServer> template <bool SSL, bool isServer>
struct WebSocket { struct WebSocket : AsyncSocket<SSL> {
private: private:
typedef AsyncSocket<SSL> Super;
public: public:
void send(std::string_view message) { void send(std::string_view message) {
// this path should use AsyncSocket with cork and everything // if corkAllocate(size) then corkFree(unused)
// format the response // format the response
char buf[100]; char buf[100];
int writeLength = WebSocketProtocol<isServer, WebSocket<SSL, isServer>>::formatMessage(buf, message.data(), message.length(), uWS::OpCode::TEXT, message.length(), false); int writeLength = WebSocketProtocol<isServer, WebSocket<SSL, isServer>>::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);
} }
+10
View File
@@ -9,6 +9,8 @@
#include "WebSocketData.h" #include "WebSocketData.h"
#include "AsyncSocket.h"
namespace uWS { namespace uWS {
template <bool SSL> template <bool SSL>
@@ -75,12 +77,20 @@ private:
/* Handle HTTP data streams */ /* Handle HTTP data streams */
static_dispatch(us_ssl_socket_context_on_data, us_socket_context_on_data)(getSocketContext(), [](auto *s, char *data, int length) { static_dispatch(us_ssl_socket_context_on_data, us_socket_context_on_data)(getSocketContext(), [](auto *s, char *data, int length) {
AsyncSocket<SSL> *webSocket = (AsyncSocket<SSL> *) s;
webSocket->cork();
// get the data // get the data
WebSocketData *wsState = (WebSocketData *) us_socket_ext(s); WebSocketData *wsState = (WebSocketData *) us_socket_ext(s);
// this parser requires almost no time -> 215k req/sec of 215k possible // this parser requires almost no time -> 215k req/sec of 215k possible
uWS::WebSocketProtocol<true, WebSocketProtcolImplementation<true>>::consume(data, length, wsState, s); uWS::WebSocketProtocol<true, WebSocketProtcolImplementation<true>>::consume(data, length, wsState, s);
webSocket->uncork();
return s; return s;
}); });
+3 -1
View File
@@ -2,10 +2,12 @@
#define WEBSOCKETDATA_H #define WEBSOCKETDATA_H
#include "WebSocketProtocol.h" #include "WebSocketProtocol.h"
#include "AsyncSocketData.h"
namespace uWS { namespace uWS {
struct WebSocketData : WebSocketState<true> { // take care with get_ext here !
struct WebSocketData : AsyncSocketData<false>, WebSocketState<true> {
private: private:
public: public: