From 21e092d179c79d00b4ff53544b94624c597ee8a2 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Mon, 28 Dec 2015 16:33:54 +0100 Subject: [PATCH] code cleanup improve readWait error handling --- src/WebSockets.cpp | 21 +++++++++++++++++---- src/WebSockets.h | 1 + src/WebSocketsClient.cpp | 4 +++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/WebSockets.cpp b/src/WebSockets.cpp index d270af4..95809d3 100644 --- a/src/WebSockets.cpp +++ b/src/WebSockets.cpp @@ -151,7 +151,7 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay if(fin) { *headerPtr |= bit(7); ///< set Fin } - *headerPtr |= opcode; ///< set opcode + *headerPtr |= opcode; ///< set opcode headerPtr++; // byte 1 @@ -167,7 +167,7 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay *headerPtr = ((length >> 8) & 0xFF); headerPtr++; *headerPtr = (length & 0xFF); headerPtr++; } else { - // normaly we never get here (to less memory) + // Normally we never get here (to less memory) *headerPtr |= 127; headerPtr++; *headerPtr = 0x00; headerPtr++; *headerPtr = 0x00; headerPtr++; @@ -181,6 +181,8 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay if(mask) { if(useInternBuffer) { + // if we use a Intern Buffer we can modify the data + // by this fact its possible the do the masking for(uint8_t x = 0; x < sizeof(maskKey); x++) { maskKey[x] = random(0xFF); *headerPtr = maskKey[x]; headerPtr++; @@ -206,6 +208,10 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay } } +#ifndef NODEBUG_WEBSOCKETS + unsigned long start = micros(); +#endif + if(headerToPayload) { // header has be added to payload // payload is forced to reserved 14 Byte but we may not need all based on the length and mask settings @@ -221,6 +227,8 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay } } + DEBUG_WEBSOCKETS("[WS][%d][sendFrame] sending Frame Done (%uus).\n", client->num, (micros() - start)); + #ifdef WEBSOCKETS_USE_BIG_MEM if(useInternBuffer && payloadPtr) { free(payloadPtr); @@ -275,7 +283,7 @@ void WebSockets::handleWebsocket(WSclient_t * client) { } payloadLen = buffer[0] << 8 | buffer[1]; } else if(payloadLen == 127) { - // read 64bit inteager as length + // read 64bit integer as length if(!readWait(client, buffer, 8)) { //timeout clientDisconnect(client, 1002); @@ -436,7 +444,12 @@ bool WebSockets::readWait(WSclient_t * client, uint8_t *out, size_t n) { size_t len; while(n > 0) { - if(client->tcp && !client->tcp->connected()) { + if(!client->tcp) { + DEBUG_WEBSOCKETS("[readWait] tcp is null!\n"); + return false; + } + + if(!client->tcp->connected()) { DEBUG_WEBSOCKETS("[readWait] not connected!\n"); return false; } diff --git a/src/WebSockets.h b/src/WebSockets.h index 002f141..ffccf80 100644 --- a/src/WebSockets.h +++ b/src/WebSockets.h @@ -31,6 +31,7 @@ #ifndef DEBUG_WEBSOCKETS #define DEBUG_WEBSOCKETS(...) +#define NODEBUG_WEBSOCKETS #endif #ifdef ESP8266 diff --git a/src/WebSocketsClient.cpp b/src/WebSocketsClient.cpp index a22fd33..3a0a039 100644 --- a/src/WebSocketsClient.cpp +++ b/src/WebSocketsClient.cpp @@ -356,7 +356,9 @@ void WebSocketsClient::sendHeader(WSclient_t * client) { client->cKey = base64_encode(&randomKey[0], 16); +#ifndef NODEBUG_WEBSOCKETS unsigned long start = micros(); +#endif String handshake = "GET " + client->cUrl + " HTTP/1.1\r\n" "Host: " + _host + "\r\n" @@ -448,7 +450,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client) { default: ///< Server dont unterstand requrst ok = false; DEBUG_WEBSOCKETS("[WS-Client][handleHeader] serverCode is not 101 (%d)\n", client->cCode); - clientDisconnect(&_client); + clientDisconnect(client); break; } }