From f1fff71e882d8682b4ccbccd2e2f5684855f03d4 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 22 Nov 2020 20:02:32 +0100 Subject: [PATCH 1/4] use native contructor and destructor to initialize WSclient_t --- src/WebSockets.h | 16 ++++++++++-- src/WebSockets4WebServer.h | 2 ++ src/WebSocketsServer.cpp | 50 ++++++-------------------------------- 3 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/WebSockets.h b/src/WebSockets.h index 91d95eb..fa200b0 100644 --- a/src/WebSockets.h +++ b/src/WebSockets.h @@ -258,7 +258,19 @@ typedef struct { uint8_t * maskKey; } WSMessageHeader_t; -typedef struct { +struct WSclient_t { + + WSclient_t() = default; + + WSclient_t(uint8_t num, uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount): + num(num), + status(WSC_NOT_CONNECTED), + pingInterval(pingInterval), + pongTimeout(pongTimeout), + disconnectTimeoutCount(disconnectTimeoutCount) + { + } + uint8_t num; ///< connection number WSclientsStatus_t status; @@ -309,7 +321,7 @@ typedef struct { String cHttpLine; ///< HTTP header lines #endif -} WSclient_t; +}; class WebSockets { protected: diff --git a/src/WebSockets4WebServer.h b/src/WebSockets4WebServer.h index 01a7f8a..ba2b020 100644 --- a/src/WebSockets4WebServer.h +++ b/src/WebSockets4WebServer.h @@ -41,6 +41,8 @@ class WebSockets4WebServer : public WebSocketsServerCore { onEvent(event); return [&, wsRootDir](const String & method, const String & url, WiFiClient * tcpClient, ESP8266WebServer::ContentTypeFunction contentType) { + (void)contentType; + if(!(method == "GET" && url.indexOf(wsRootDir) == 0)) { return ESP8266WebServer::CLIENT_REQUEST_CAN_CONTINUE; } diff --git a/src/WebSocketsServer.cpp b/src/WebSocketsServer.cpp index 20b2373..1b85ecb 100644 --- a/src/WebSocketsServer.cpp +++ b/src/WebSocketsServer.cpp @@ -38,8 +38,6 @@ WebSocketsServerCore::WebSocketsServerCore(const String & origin, const String & _httpHeaderValidationFunc = NULL; _mandatoryHttpHeaders = NULL; _mandatoryHttpHeaderCount = 0; - - memset(&_clients[0], 0x00, (sizeof(WSclient_t) * WEBSOCKETS_SERVER_CLIENT_MAX)); } WebSocketsServer::WebSocketsServer(uint16_t port, const String & origin, const String & protocol) @@ -73,47 +71,15 @@ WebSocketsServer::~WebSocketsServer() { * called to initialize the Websocket server */ void WebSocketsServerCore::begin(void) { - WSclient_t * client; - // init client storage - for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) { - client = &_clients[i]; - - client->num = i; - client->status = WSC_NOT_CONNECTED; - client->tcp = NULL; -#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) - client->isSSL = false; - client->ssl = NULL; -#endif - client->cUrl = ""; - client->cCode = 0; - - client->cIsClient = false; - client->cIsUpgrade = false; - client->cIsWebsocket = false; - - client->cSessionId = ""; - client->cKey = ""; - client->cAccept = ""; - client->cProtocol = ""; - client->cExtensions = ""; - client->cVersion = 0; - - client->cWsRXsize = 0; - - client->base64Authorization = ""; - client->plainAuthorization = ""; - - client->extraHeaders = ""; - -#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) - client->cHttpLine = ""; -#endif - - client->pingInterval = _pingInterval; - client->pongTimeout = _pongTimeout; - client->disconnectTimeoutCount = _disconnectTimeoutCount; + for (int i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) { + WSclient_t * client = &_clients[i]; + + // reset instance: + // destructor in place + client->~WSclient_t(); + // constructor in place + new (client) WSclient_t(i, _pingInterval, _pongTimeout, _disconnectTimeoutCount); } #ifdef ESP8266 From 5662cd32604c8e63a47e105cce6c37674ad15220 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 22 Nov 2020 21:42:20 +0100 Subject: [PATCH 2/4] fix clients init logic --- src/WebSockets.h | 20 ++++++++++++-------- src/WebSocketsServer.cpp | 26 ++++++++++++++++++-------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/WebSockets.h b/src/WebSockets.h index fa200b0..35d7d8c 100644 --- a/src/WebSockets.h +++ b/src/WebSockets.h @@ -260,17 +260,21 @@ typedef struct { struct WSclient_t { - WSclient_t() = default; - - WSclient_t(uint8_t num, uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount): - num(num), - status(WSC_NOT_CONNECTED), - pingInterval(pingInterval), - pongTimeout(pongTimeout), - disconnectTimeoutCount(disconnectTimeoutCount) + WSclient_t (): status(WSC_NOT_CONNECTED) { } + void init (uint8_t num, + uint32_t pingInterval, + uint32_t pongTimeout, + uint8_t disconnectTimeoutCount) + { + this->num = num; + this->pingInterval = pingInterval; + this->pongTimeout = pongTimeout; + this->disconnectTimeoutCount = disconnectTimeoutCount; + } + uint8_t num; ///< connection number WSclientsStatus_t status; diff --git a/src/WebSocketsServer.cpp b/src/WebSocketsServer.cpp index 1b85ecb..50d92ed 100644 --- a/src/WebSocketsServer.cpp +++ b/src/WebSocketsServer.cpp @@ -71,15 +71,14 @@ WebSocketsServer::~WebSocketsServer() { * called to initialize the Websocket server */ void WebSocketsServerCore::begin(void) { - // init client storage - for (int i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) { - WSclient_t * client = &_clients[i]; - // reset instance: - // destructor in place - client->~WSclient_t(); - // constructor in place - new (client) WSclient_t(i, _pingInterval, _pongTimeout, _disconnectTimeoutCount); + // adjust clients storage: + // _clients[i]'s constructor are already called, + // all its members are initialized to their default value, + // except the ones explicitly detailed in WSclient_t() constructor. + // Then we need to initialize some members to non-trivial values: + for (int i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) { + _clients[i].init(i, _pingInterval, _pongTimeout, _disconnectTimeoutCount); } #ifdef ESP8266 @@ -98,6 +97,17 @@ void WebSocketsServerCore::begin(void) { void WebSocketsServerCore::close(void) { _runnning = false; disconnect(); + + // reset _clients[] + for (int i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) { + WSclient_t * client = &_clients[i]; + + // reset instance: + // destructor in place + client->~WSclient_t(); + // constructor in place (reset Strings, set scalars to 0) + new (client) WSclient_t; + } } /** From 4920056bc9088af4b86d2bc3ecbb43b64d2c3165 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 22 Nov 2020 22:06:18 +0100 Subject: [PATCH 3/4] fix clearing _Client[] --- src/WebSocketsServer.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/WebSocketsServer.cpp b/src/WebSocketsServer.cpp index 50d92ed..c8ac2ef 100644 --- a/src/WebSocketsServer.cpp +++ b/src/WebSocketsServer.cpp @@ -98,15 +98,10 @@ void WebSocketsServerCore::close(void) { _runnning = false; disconnect(); - // reset _clients[] + // restore _clients[] to their initial state + // before next call to ::begin() for (int i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) { - WSclient_t * client = &_clients[i]; - - // reset instance: - // destructor in place - client->~WSclient_t(); - // constructor in place (reset Strings, set scalars to 0) - new (client) WSclient_t; + _clients[i] = WSclient_t(); } } From 9d9f892e55cb078190b9739a5115fb05bd077e7e Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 22 Nov 2020 22:47:10 +0100 Subject: [PATCH 4/4] + constructors for scalars --- src/WebSockets.h | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/src/WebSockets.h b/src/WebSockets.h index 35d7d8c..00d65b2 100644 --- a/src/WebSockets.h +++ b/src/WebSockets.h @@ -258,11 +258,7 @@ typedef struct { uint8_t * maskKey; } WSMessageHeader_t; -struct WSclient_t { - - WSclient_t (): status(WSC_NOT_CONNECTED) - { - } +typedef struct { void init (uint8_t num, uint32_t pingInterval, @@ -275,34 +271,34 @@ struct WSclient_t { this->disconnectTimeoutCount = disconnectTimeoutCount; } - uint8_t num; ///< connection number + uint8_t num = 0; ///< connection number - WSclientsStatus_t status; + WSclientsStatus_t status = WSC_NOT_CONNECTED; - WEBSOCKETS_NETWORK_CLASS * tcp; + WEBSOCKETS_NETWORK_CLASS * tcp = nullptr; - bool isSocketIO; ///< client for socket.io server + bool isSocketIO = false; ///< client for socket.io server #if defined(HAS_SSL) - bool isSSL; ///< run in ssl mode + bool isSSL = false; ///< run in ssl mode WEBSOCKETS_NETWORK_SSL_CLASS * ssl; #endif - String cUrl; ///< http url - uint16_t cCode; ///< http code + String cUrl; ///< http url + uint16_t cCode = 0; ///< http code bool cIsClient = false; ///< will be used for masking - bool cIsUpgrade; ///< Connection == Upgrade - bool cIsWebsocket; ///< Upgrade == websocket + bool cIsUpgrade = false; ///< Connection == Upgrade + bool cIsWebsocket = false; ///< Upgrade == websocket String cSessionId; ///< client Set-Cookie (session id) String cKey; ///< client Sec-WebSocket-Key String cAccept; ///< client Sec-WebSocket-Accept String cProtocol; ///< client Sec-WebSocket-Protocol String cExtensions; ///< client Sec-WebSocket-Extensions - uint16_t cVersion; ///< client Sec-WebSocket-Version + uint16_t cVersion = 0; ///< client Sec-WebSocket-Version - uint8_t cWsRXsize; ///< State of the RX + uint8_t cWsRXsize = 0; ///< State of the RX uint8_t cWsHeader[WEBSOCKETS_MAX_HEADER_SIZE]; ///< RX WS Message buffer WSMessageHeader_t cWsHeaderDecode; @@ -311,21 +307,21 @@ struct WSclient_t { String extraHeaders; - bool cHttpHeadersValid; ///< non-websocket http header validity indicator + bool cHttpHeadersValid = false; ///< non-websocket http header validity indicator size_t cMandatoryHeadersCount; ///< non-websocket mandatory http headers present count - bool pongReceived; - uint32_t pingInterval; // how often ping will be sent, 0 means "heartbeat is not active" - uint32_t lastPing; // millis when last pong has been received - uint32_t pongTimeout; // interval in millis after which pong is considered to timeout - uint8_t disconnectTimeoutCount; // after how many subsequent pong timeouts discconnect will happen, 0 means "do not disconnect" - uint8_t pongTimeoutCount; // current pong timeout count + bool pongReceived = false; + uint32_t pingInterval = 0; // how often ping will be sent, 0 means "heartbeat is not active" + uint32_t lastPing = 0; // millis when last pong has been received + uint32_t pongTimeout = 0; // interval in millis after which pong is considered to timeout + uint8_t disconnectTimeoutCount = 0; // after how many subsequent pong timeouts discconnect will happen, 0 means "do not disconnect" + uint8_t pongTimeoutCount = 0; // current pong timeout count #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) String cHttpLine; ///< HTTP header lines #endif -}; +} WSclient_t; class WebSockets { protected: