From a3349f962fb30b2a3b87bbb5ef065f98e0d15386 Mon Sep 17 00:00:00 2001 From: -cr <105788188+DE-cr@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:04:40 +0100 Subject: [PATCH 1/2] stop serving client that is already gone ...at least for potentially long lasting actions, e.g. /D or /K24. This avoids unnecessarily blocking bsb-lan for other tasks (including new client requests). --- BSB_LAN/BSB_LAN.ino | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/BSB_LAN/BSB_LAN.ino b/BSB_LAN/BSB_LAN.ino index 671496dd..47a99226 100644 --- a/BSB_LAN/BSB_LAN.ino +++ b/BSB_LAN/BSB_LAN.ino @@ -4587,6 +4587,7 @@ void query(float line_start // begin at this line (ProgNr) query(line); if (decodedTelegram.prognr != -1) { if (!no_print) { // display in web client? + if (!client.connected()) return; // no need to waste time here when client is gone query_printHTML(); } } @@ -4750,14 +4751,14 @@ void transmitFile(File dataFile) { printToWebClient(PSTR("Error: Failed to read from SD card - if problem remains after reformatting, card may be incompatible.")); forcedflushToWebClient(); } - while (chars_read == logbuflen) { + while (chars_read == logbuflen && client.connected()) { client.write(buf, logbuflen); chars_read = dataFile.read(buf, logbuflen); #if defined(ESP32) esp_task_wdt_reset(); #endif } - if (chars_read > 0) client.write(buf, chars_read); + if (chars_read > 0 && client.connected()) client.write(buf, chars_read); if (buf != (byte*)bigBuff) free(buf); } @@ -6569,7 +6570,7 @@ void loop() { buf = (byte*)malloc(4096); // try to use 4 KB buffer, for improved transfer rates #endif if (buf) logbuflen=4096; else buf=(byte*)bigBuff; // fall back to static buffer, if necessary - while (nBytesToDo) { + while (nBytesToDo && client.connected()) { int n = dataFile.read(buf, nBytesToDo Date: Fri, 10 Nov 2023 08:56:13 +0100 Subject: [PATCH 2/2] abort /QD when client is gone not perfect, but at least loops are quit, thus definitely shortening unnecessary bsb-lan blocking --- BSB_LAN/BSB_LAN.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/BSB_LAN/BSB_LAN.ino b/BSB_LAN/BSB_LAN.ino index 47a99226..1d1ba7b0 100644 --- a/BSB_LAN/BSB_LAN.ino +++ b/BSB_LAN/BSB_LAN.ino @@ -5704,7 +5704,7 @@ void loop() { } else { printToWebClient(PSTR(MENU_TEXT_QFA "!")); } - for (int x=0;x<10;x++) { + for (int x=0; x<10 && client.connected(); x++) { if (found_ids[x]==0xFF) { continue; } @@ -5751,7 +5751,7 @@ void loop() { if (p[3] == 'F') { printToWebClient(PSTR(MENU_TEXT_QST "...\r\n")); flushToWebClient(); - for (int j=0;j<10000;j++) { + for (int j=0; j<10000 && client.connected(); j++) { uint32_t cc = get_cmdtbl_cmd(j); if (cc == c) { continue; @@ -5856,7 +5856,7 @@ void loop() { int IA2_max = (msg[5+bus->getBusType()*4] << 8) + msg[6+bus->getBusType()*4]; int outBufLen = strlen(outBuf); - for (int IA1_counter = 1; IA1_counter <= IA1_max; IA1_counter++) { + for (int IA1_counter = 1; IA1_counter <= IA1_max && client.connected(); IA1_counter++) { #if defined(ESP32) esp_task_wdt_reset(); #endif @@ -5870,7 +5870,7 @@ void loop() { printToWebClient(PSTR("\r\n")); flushToWebClient(); } - for (int IA2_counter = 1; IA2_counter <= IA2_max; IA2_counter++) { + for (int IA2_counter = 1; IA2_counter <= IA2_max && client.connected(); IA2_counter++) { #if defined(ESP32) esp_task_wdt_reset(); #endif