code cleanup

improve readWait error handling
This commit is contained in:
Markus Sattler
2015-12-28 16:33:54 +01:00
parent b24f021d33
commit 21e092d179
3 changed files with 21 additions and 5 deletions

View File

@ -167,7 +167,7 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
*headerPtr = ((length >> 8) & 0xFF); headerPtr++; *headerPtr = ((length >> 8) & 0xFF); headerPtr++;
*headerPtr = (length & 0xFF); headerPtr++; *headerPtr = (length & 0xFF); headerPtr++;
} else { } else {
// normaly we never get here (to less memory) // Normally we never get here (to less memory)
*headerPtr |= 127; headerPtr++; *headerPtr |= 127; headerPtr++;
*headerPtr = 0x00; headerPtr++; *headerPtr = 0x00; 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(mask) {
if(useInternBuffer) { 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++) { for(uint8_t x = 0; x < sizeof(maskKey); x++) {
maskKey[x] = random(0xFF); maskKey[x] = random(0xFF);
*headerPtr = maskKey[x]; headerPtr++; *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) { if(headerToPayload) {
// header has be added to payload // 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 // 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 #ifdef WEBSOCKETS_USE_BIG_MEM
if(useInternBuffer && payloadPtr) { if(useInternBuffer && payloadPtr) {
free(payloadPtr); free(payloadPtr);
@ -275,7 +283,7 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
} }
payloadLen = buffer[0] << 8 | buffer[1]; payloadLen = buffer[0] << 8 | buffer[1];
} else if(payloadLen == 127) { } else if(payloadLen == 127) {
// read 64bit inteager as length // read 64bit integer as length
if(!readWait(client, buffer, 8)) { if(!readWait(client, buffer, 8)) {
//timeout //timeout
clientDisconnect(client, 1002); clientDisconnect(client, 1002);
@ -436,7 +444,12 @@ bool WebSockets::readWait(WSclient_t * client, uint8_t *out, size_t n) {
size_t len; size_t len;
while(n > 0) { 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"); DEBUG_WEBSOCKETS("[readWait] not connected!\n");
return false; return false;
} }

View File

@ -31,6 +31,7 @@
#ifndef DEBUG_WEBSOCKETS #ifndef DEBUG_WEBSOCKETS
#define DEBUG_WEBSOCKETS(...) #define DEBUG_WEBSOCKETS(...)
#define NODEBUG_WEBSOCKETS
#endif #endif
#ifdef ESP8266 #ifdef ESP8266

View File

@ -356,7 +356,9 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
client->cKey = base64_encode(&randomKey[0], 16); client->cKey = base64_encode(&randomKey[0], 16);
#ifndef NODEBUG_WEBSOCKETS
unsigned long start = micros(); unsigned long start = micros();
#endif
String handshake = "GET " + client->cUrl + " HTTP/1.1\r\n" String handshake = "GET " + client->cUrl + " HTTP/1.1\r\n"
"Host: " + _host + "\r\n" "Host: " + _host + "\r\n"
@ -448,7 +450,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client) {
default: ///< Server dont unterstand requrst default: ///< Server dont unterstand requrst
ok = false; ok = false;
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] serverCode is not 101 (%d)\n", client->cCode); DEBUG_WEBSOCKETS("[WS-Client][handleHeader] serverCode is not 101 (%d)\n", client->cCode);
clientDisconnect(&_client); clientDisconnect(client);
break; break;
} }
} }