10000 code cleanup · faraday-motion/arduinoWebSockets@21e092d · GitHub
[go: up one dir, main page]

Skip to content

Commit 21e092d

Browse files
committed
code cleanup
improve readWait error handling
1 parent b24f021 commit 21e092d

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/WebSockets.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
151151
if(fin) {
152152
*headerPtr |= bit(7); ///< set Fin
153153
}
154-
*headerPtr |= opcode; ///< set opcode
154+
*headerPtr |= opcode; ///< set opcode
155155
headerPtr++;
156156

157157
// byte 1
@@ -167,7 +167,7 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
167167
*headerPtr = ((length >> 8) & 0xFF); headerPtr++;
168168
*headerPtr = (length & 0xFF); headerPtr++;
169169
} else {
170-
// normaly we never get here (to less memory)
170+
// Normally we never get here (to less memory)
171171
*headerPtr |= 127; headerPtr++;
172172
*headerPtr = 0x00; headerPtr++;
173173
*headerPtr = 0x00; headerPtr++;
@@ -181,6 +181,8 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
181181

182182
if(mask) {
183183
if(useInternBuffer) {
184+
// if we use a Intern Buffer we can modify the data
185+
// by this fact its possible the do the masking
184186
for(uint8_t x = 0; x < sizeof(maskKey); x++) {
185187
maskKey[x] = random(0xFF);
186188
*headerPtr = maskKey[x]; headerPtr++;
@@ -206,6 +208,10 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
206208
}
207209
}
208210

211+
#ifndef NODEBUG_WEBSOCKETS
212+
unsigned long start = micros();
213+
#endif
214+
209215
if(headerToPayload) {
210216
// header has be added to payload
211217
// 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
221227
}
222228
}
223229

230+
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] sending Frame Done (%uus).\n", client->num, (micros() - start));
231+
224232
#ifdef WEBSOCKETS_USE_BIG_MEM
225233
if(useInternBuffer && payloadPtr) {
226234
free(payloadPtr);
@@ -275,7 +283,7 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
275283
}
276284
payloadLen = buffer[0] << 8 | buffer[1];
277285
} else if(payloadLen == 127) {
278-
// read 64bit inteager as length
286+
// read 64bit integer as length
279287
if(!readWait(client, buffer, 8)) {
280288
//timeout
281289
clientDisconnect(client, 1002);
@@ -436,7 +444,12 @@ bool WebSockets::readWait(WSclient_t * client, uint8_t *out, size_t n) {
436444
size_t len;
437445

438446
while(n > 0) {
439-
if(client->tcp && !client->tcp->connected()) {
447+
if(!client->tcp) {
448+
DEBUG_WEBSOCKETS("[readWait] tcp is null!\n");
449+
return false;
450+
}
451+
452+
if(!client->tcp->connected()) {
440453
DEBUG_WEBSOCKETS("[readWait] not connected!\n");
441454
return false;
442455
}

src/WebSockets.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
31< E249 /code>31

3232
#ifndef DEBUG_WEBSOCKETS
3333
#define DEBUG_WEBSOCKETS(...)
34+
#define NODEBUG_WEBSOCKETS
3435
#endif
3536

3637
#ifdef ESP8266

src/WebSocketsClient.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
356356

357357
client->cKey = base64_encode(&randomKey[0], 16);
358358

359+
#ifndef NODEBUG_WEBSOCKETS
359360
unsigned long start = micros();
361+
#endif
360362

361363
String handshake = "GET " + client->cUrl + " HTTP/1.1\r\n"
362364
"Host: " + _host + "\r\n"
@@ -448,7 +450,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client) {
448450
default: ///< Server dont unterstand requrst
449451
ok = false;
450452
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] serverCode is not 101 (%d)\n", client->cCode);
451-
clientDisconnect(&_client);
453+
clientDisconnect(client);
452454
break;
453455
}
454456
}

0 commit comments

Comments
 (0)
0