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.
This commit is contained in:
Ben Hencke
2020-09-16 11:02:02 -07:00
parent 05ec18e49b
commit 083683425f
3 changed files with 14 additions and 6 deletions

View File

@ -501,7 +501,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
reasonCode = payload[0] << 8 | payload[1]; reasonCode = payload[0] << 8 | payload[1];
} }
#endif #endif
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get ask for close. Code: %d", client->num, reasonCode); DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get ask for close. Code: %d\n", client->num, reasonCode);
if(header->payloadLen > 2) { if(header->payloadLen > 2) {
DEBUG_WEBSOCKETS(" (%s)\n", (payload + 2)); DEBUG_WEBSOCKETS(" (%s)\n", (payload + 2));
} else { } else {
@ -510,6 +510,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
clientDisconnect(client, 1000); clientDisconnect(client, 1000);
} break; } break;
default: default:
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] got unknown opcode: %d\n", client->num, header->opCode);
clientDisconnect(client, 1002); clientDisconnect(client, 1002);
break; break;
} }
@ -630,7 +631,7 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
} }
if(!client->tcp->available()) { if(!client->tcp->available()) {
WEBSOCKETS_YIELD(); WEBSOCKETS_YIELD_MORE();
continue; continue;
} }
@ -643,7 +644,9 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
} else { } else {
//DEBUG_WEBSOCKETS("Receive %d left %d!\n", len, n); //DEBUG_WEBSOCKETS("Receive %d left %d!\n", len, n);
} }
WEBSOCKETS_YIELD(); if (n > 0) {
WEBSOCKETS_YIELD();
}
} }
if(cb) { if(cb) {
cb(client, true); cb(client, true);
@ -693,9 +696,11 @@ size_t WebSockets::write(WSclient_t * client, uint8_t * out, size_t n) {
total += len; total += len;
//DEBUG_WEBSOCKETS("write %d left %d!\n", len, n); //DEBUG_WEBSOCKETS("write %d left %d!\n", len, n);
} else { } else {
//DEBUG_WEBSOCKETS("write %d failed left %d!\n", len, n); DEBUG_WEBSOCKETS("WS write %d failed left %d!\n", len, n);
}
if (n > 0) {
WEBSOCKETS_YIELD();
} }
WEBSOCKETS_YIELD();
} }
WEBSOCKETS_YIELD(); WEBSOCKETS_YIELD();
return total; return total;

View File

@ -65,8 +65,10 @@
#if defined(ESP8266) #if defined(ESP8266)
#define WEBSOCKETS_YIELD() delay(0) #define WEBSOCKETS_YIELD() delay(0)
#define WEBSOCKETS_YIELD_MORE() delay(1)
#elif defined(ESP32) #elif defined(ESP32)
#define WEBSOCKETS_YIELD() yield() #define WEBSOCKETS_YIELD() yield()
#define WEBSOCKETS_YIELD_MORE() delay(1)
#endif #endif
#elif defined(STM32_DEVICE) #elif defined(STM32_DEVICE)

View File

@ -567,7 +567,7 @@ void WebSocketsServer::clientDisconnect(WSclient_t * client) {
if(client->tcp) { if(client->tcp) {
if(client->tcp->connected()) { if(client->tcp->connected()) {
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC) #if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC) && (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP32)
client->tcp->flush(); client->tcp->flush();
#endif #endif
client->tcp->stop(); client->tcp->stop();
@ -694,6 +694,7 @@ void WebSocketsServer::handleClientData(void) {
WebSockets::handleWebsocket(client); WebSockets::handleWebsocket(client);
break; break;
default: default:
DEBUG_WEBSOCKETS("[WS-Server][%d][handleClientData] unknown client status %d\n", client->num, client->status);
WebSockets::clientDisconnect(client, 1002); WebSockets::clientDisconnect(client, 1002);
break; break;
} }