Skip to content

fix WSclient_t initialization in server #581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions src/WebSockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,34 +259,46 @@ typedef struct {
} WSMessageHeader_t;

typedef struct {
uint8_t num; ///< connection number

WSclientsStatus_t status;
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 = 0; ///< connection number

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;

Expand All @@ -295,15 +307,15 @@ typedef struct {

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
Expand Down
2 changes: 2 additions & 0 deletions src/WebSockets4WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
55 changes: 13 additions & 42 deletions src/WebSocketsServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -73,47 +71,14 @@ 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;
// 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
Expand All @@ -132,6 +97,12 @@ void WebSocketsServerCore::begin(void) {
void WebSocketsServerCore::close(void) {
_runnning = false;
disconnect();

// restore _clients[] to their initial state
// before next call to ::begin()
for (int i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
_clients[i] = WSclient_t();
}
}

/**
Expand Down