Skip to content

Commit 0836834

Browse files
committed
avoid flush on esp32, add/fix debugs, longer yield when waiting for data
flush causes a bunch of reads as we try to close the socket on esp32. I think flush is broken on that platform. the comments indicate confusion. added some debug logs for important cases that were missing them, some missing newlines to exisitng logs. added a longer yield when waiting for data, in some super busy cases it could trigger a task watchdog or otherwise starve the system. (yield alone doesn't always switch to lower priority tasks) make some other yields conditional to avoid some waste when it would double-yield.
1 parent 05ec18e commit 0836834

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/WebSockets.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
501501
reasonCode = payload[0] << 8 | payload[1];
502502
}
503503
#endif
504-
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get ask for close. Code: %d", client->num, reasonCode);
504+
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get ask for close. Code: %d\n", client->num, reasonCode);
505505
if(header->payloadLen > 2) {
506506
DEBUG_WEBSOCKETS(" (%s)\n", (payload + 2));
507507
} else {
@@ -510,6 +510,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
510510
clientDisconnect(client, 1000);
511511
} break;
512512
default:
513+
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] got unknown opcode: %d\n", client->num, header->opCode);
513514
clientDisconnect(client, 1002);
514515
break;
515516
}
@@ -630,7 +631,7 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
630631
}
631632

632633
if(!client->tcp->available()) {
633-
WEBSOCKETS_YIELD();
634+
WEBSOCKETS_YIELD_MORE();
634635
continue;
635636
}
636637

@@ -643,7 +644,9 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
643644
} else {
644645
//DEBUG_WEBSOCKETS("Receive %d left %d!\n", len, n);
645646
}
646-
WEBSOCKETS_YIELD();
647+
if (n > 0) {
648+
WEBSOCKETS_YIELD();
649+
}
647650
}
648651
if(cb) {
649652
cb(client, true);
@@ -693,9 +696,11 @@ size_t WebSockets::write(WSclient_t * client, uint8_t * out, size_t n) {
693696
total += len;
694697
//DEBUG_WEBSOCKETS("write %d left %d!\n", len, n);
695698
} else {
696-
//DEBUG_WEBSOCKETS("write %d failed left %d!\n", len, n);
699+
DEBUG_WEBSOCKETS("WS write %d failed left %d!\n", len, n);
700+
}
701+
if (n > 0) {
702+
WEBSOCKETS_YIELD();
697703
}
698-
WEBSOCKETS_YIELD();
699704
}
700705
WEBSOCKETS_YIELD();
701706
return total;

src/WebSockets.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@
6565

6666
#if defined(ESP8266)
6767
#define WEBSOCKETS_YIELD() delay(0)
68+
#define WEBSOCKETS_YIELD_MORE() delay(1)
6869
#elif defined(ESP32)
6970
#define WEBSOCKETS_YIELD() yield()
71+
#define WEBSOCKETS_YIELD_MORE() delay(1)
7072
#endif
7173

7274
#elif defined(STM32_DEVICE)

src/WebSocketsServer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ void WebSocketsServer::clientDisconnect(WSclient_t * client) {
567567

568568
if(client->tcp) {
569569
if(client->tcp->connected()) {
570-
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
570+
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC) && (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP32)
571571
client->tcp->flush();
572572
#endif
573573
client->tcp->stop();
@@ -694,6 +694,7 @@ void WebSocketsServer::handleClientData(void) {
694694
WebSockets::handleWebsocket(client);
695695
break;
696696
default:
697+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleClientData] unknown client status %d\n", client->num, client->status);
697698
WebSockets::clientDisconnect(client, 1002);
698699
break;
699700
}

0 commit comments

Comments
 (0)