Add a few more events

This commit is contained in:
Alex Hultman
2018-12-21 04:31:37 +01:00
parent 4a2cd93a22
commit 3fa0fbc5fa
7 changed files with 91 additions and 49 deletions
+7 -2
View File
@@ -54,8 +54,13 @@ public:
struct WebSocketBehavior {
bool compression = false;
int maxPayloadLength = 16 * 1024;
std::function<void(uWS::WebSocket<SSL, true> *, HttpRequest *)> open = nullptr;
std::function<void(uWS::WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> message = nullptr;
std::function<void(uWS::WebSocket<SSL, true> *)> drain = nullptr;
std::function<void(uWS::WebSocket<SSL, true> *)> ping = nullptr;
std::function<void(uWS::WebSocket<SSL, true> *)> pong = nullptr;
std::function<void(uWS::WebSocket<SSL, true> *, int, std::string_view)> close = nullptr;
};
template <class UserData>
@@ -75,6 +80,8 @@ public:
/* Copy all handlers */
webSocketContext->getExt()->messageHandler = behavior.message;
webSocketContext->getExt()->drainHandler = behavior.drain;
webSocketContext->getExt()->closeHandler = behavior.close;
return get(pattern, [webSocketContext, this, behavior](auto *res, auto *req) {
/* If we have this header set, it's a websocket */
@@ -125,8 +132,6 @@ public:
behavior.open(webSocket, req);
}
std::cout << "oh hey!" << std::endl;
} else {
/* For now we do not support having HTTP and websocket routes on the same URL */
res->close();
+7
View File
@@ -95,6 +95,13 @@ protected:
}
}
/* Returns the user space backpressure. */
int getBufferedAmount() {
AsyncSocketData<SSL> *asyncSocketData = (AsyncSocketData<SSL> *) getExt();
return asyncSocketData->buffer.size();
}
/* Write in three levels of prioritization: cork-buffer, syscall, socket-buffer. Always drain if possible.
* Returns pair of bytes written (anywhere) and wheter or not this call resulted in the polling for
* writable (or we are in a state that implies polling for writable). */
+11
View File
@@ -39,6 +39,17 @@ private:
}
public:
/* Returns pointer to the per socket user data */
//template <typename K>
void *getUserData() {
WebSocketData *webSocketData = (WebSocketData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this);
return nullptr;
}
/* See AsyncSocket */
using Super::getBufferedAmount;
/* Send or buffer a WebSocket frame, compressed or not. Returns false on increased user space backpressure. */
bool send(std::string_view message, uWS::OpCode opCode = uWS::OpCode::BINARY, bool compress = false) {
/* Transform the message to compressed domain if requested */
+10 -6
View File
@@ -47,7 +47,7 @@ private:
static bool setCompressed(uWS::WebSocketState<isServer> *wState, void *s) {
//WebSocketData *webSocketData = (WebSocketData *) us_socket_ext((us_socket *) s);
std::cout << "set compressed" << std::endl;
//std::cout << "set compressed" << std::endl;
WebSocketData *webSocketData = (WebSocketData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) s);
@@ -264,14 +264,13 @@ private:
std::cout << "close!" << std::endl;
return s;
});
/* Handle WebSocket data streams */
static_dispatch(us_ssl_socket_context_on_data, us_socket_context_on_data)(getSocketContext(), [](auto *s, char *data, int length) {
//std::cout << "websocket data" << std::endl;
/* We always cork on data */
AsyncSocket<SSL> *webSocket = (AsyncSocket<SSL> *) s;
webSocket->cork();
@@ -279,13 +278,14 @@ private:
/* We need the websocket data */
WebSocketData *wsState = (WebSocketData *) (static_dispatch(us_ssl_socket_ext, us_socket_ext)(s));
// this parser requires almost no time -> 215k req/sec of 215k possible
/* This parser has virtually no overhead */
uWS::WebSocketProtocol<isServer, WebSocketContext<SSL, isServer>>::consume(data, length, wsState, s);
// todo: check for failures here just like for HTTP
webSocket->uncork();
// I guess we need to check drain here
// are we shutdown?
if (wsState->isShuttingDown) {
webSocket->shutdown();
@@ -306,6 +306,10 @@ private:
// check for failures and shutdown just like in data event
webSocket->write(nullptr, 0); // drainage - also check for shutdown!
// call drain here
return s;
});
+3 -1
View File
@@ -28,8 +28,10 @@ template <bool, bool> struct WebSocket;
template <bool SSL>
struct WebSocketContextData {
/* The callbacks for this context */
std::function<void(WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> messageHandler;
std::function<void(uWS::WebSocket<SSL, true> *)> drainHandler;
std::function<void(uWS::WebSocket<SSL, true> *, int, std::string_view)> closeHandler;
};
}