Emit close event for WebSockets
This commit is contained in:
+8
-12
@@ -31,6 +31,7 @@ struct WebSocket : AsyncSocket<SSL> {
|
||||
private:
|
||||
typedef AsyncSocket<SSL> Super;
|
||||
using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE;
|
||||
using SOCKET_CONTEXT_TYPE = typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE;
|
||||
using StaticDispatch<SSL>::static_dispatch;
|
||||
|
||||
void *init(bool perMessageDeflate, bool slidingCompression) {
|
||||
@@ -89,23 +90,12 @@ public:
|
||||
|
||||
/* Emit close event, stat passive timeout */
|
||||
void close(int code, std::string_view message = {}) {
|
||||
// closing should trigger close event!
|
||||
|
||||
/*if (code == 1001) {
|
||||
std::cout << "Going away" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Closing websocket: " << code << " = " << message << std::endl;*/
|
||||
|
||||
static const int MAX_CLOSE_PAYLOAD = 123;
|
||||
int length = std::min<size_t>(MAX_CLOSE_PAYLOAD, message.length());
|
||||
|
||||
// here we start a timeout and handle it accordingly in the timeout handler
|
||||
|
||||
//WebSocketData *webSocketData = (WebSocketData *) us_socket_ext((us_socket *) this);
|
||||
// todo: here we start a timeout and handle it accordingly in the timeout handler
|
||||
|
||||
WebSocketData *webSocketData = (WebSocketData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this);
|
||||
|
||||
webSocketData->isShuttingDown = true;
|
||||
|
||||
/* Format and send the close frame */
|
||||
@@ -115,6 +105,12 @@ public:
|
||||
|
||||
// why should we fin here?
|
||||
//us_socket_shutdown((us_socket *) this);
|
||||
|
||||
/* Emit close event */
|
||||
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(
|
||||
(SOCKET_CONTEXT_TYPE *) static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *) this)
|
||||
);
|
||||
webSocketContextData->closeHandler(this, code, message);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+12
-41
@@ -45,10 +45,6 @@ private:
|
||||
|
||||
/* If we have negotiated compression, set this frame compressed */
|
||||
static bool setCompressed(uWS::WebSocketState<isServer> *wState, void *s) {
|
||||
//WebSocketData *webSocketData = (WebSocketData *) us_socket_ext((us_socket *) s);
|
||||
|
||||
//std::cout << "set compressed" << std::endl;
|
||||
|
||||
WebSocketData *webSocketData = (WebSocketData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) s);
|
||||
|
||||
if (webSocketData->compressionStatus == WebSocketData::CompressionStatus::ENABLED) {
|
||||
@@ -66,19 +62,11 @@ private:
|
||||
/* 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> *) us_socket_context_ext(us_socket_get_context((us_socket *) s));
|
||||
//WebSocketData *webSocketData = (WebSocketData *) us_socket_ext((us_socket *) 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)
|
||||
|
||||
);
|
||||
|
||||
static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *) s)
|
||||
);
|
||||
WebSocketData *webSocketData = (WebSocketData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) s);
|
||||
|
||||
//std::cout << "ho" << std::endl;
|
||||
|
||||
/* Is this a non-control frame? */
|
||||
if (opCode < 3) {
|
||||
/* Did we get everything in one go? */
|
||||
@@ -88,18 +76,11 @@ private:
|
||||
if (webSocketData->compressionStatus == WebSocketData::CompressionStatus::COMPRESSED_FRAME) {
|
||||
webSocketData->compressionStatus = WebSocketData::CompressionStatus::ENABLED;
|
||||
|
||||
//LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(us_socket_get_context((us_socket *) s)));
|
||||
|
||||
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)
|
||||
|
||||
)
|
||||
|
||||
);
|
||||
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)
|
||||
)
|
||||
);
|
||||
|
||||
std::string_view inflatedFrame = loopData->inflationStream->inflate(loopData->zlibContext, {data, length});
|
||||
if (!inflatedFrame.length()) {
|
||||
@@ -130,7 +111,7 @@ private:
|
||||
webSocketData->fragmentBuffer.append(data, length);
|
||||
|
||||
/* Are we done now? */
|
||||
// what if we don't have any remaining bytes yet we are not fin? forceclose!
|
||||
// todo: what if we don't have any remaining bytes yet we are not fin? forceclose!
|
||||
if (!remainingBytes && fin) {
|
||||
|
||||
/* Handle compression */
|
||||
@@ -140,21 +121,11 @@ private:
|
||||
// what's really the story here?
|
||||
webSocketData->fragmentBuffer.append("....");
|
||||
|
||||
//LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(us_socket_get_context((us_socket *) s)));
|
||||
|
||||
//LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(us_socket_get_context((us_socket *) s)));
|
||||
|
||||
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)
|
||||
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
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)
|
||||
)
|
||||
);
|
||||
|
||||
std::string_view inflatedFrame = loopData->inflationStream->inflate(loopData->zlibContext, {webSocketData->fragmentBuffer.data(), webSocketData->fragmentBuffer.length() - 4});
|
||||
if (!inflatedFrame.length()) {
|
||||
|
||||
Reference in New Issue
Block a user