--- a/rESP8266WiFi/BufferedWiFiClient.cpp (1.61 original) +++ b/rESP8266WiFi/BufferedWiFiClient.cpp (silent-loss fix) @@ -1,5 +1,24 @@ #include "B4RDefines.h" namespace B4R { + //============================================================================= + // Silent data loss fix (rESP8266WiFi 1.61). + // + // Original behaviour: flush() cleared the 200-byte accumulate buffer without + // looking at the return value of the wrapped client, and write() reported the + // requested length as written. A stalled socket therefore discarded up to 200 + // buffered bytes invisibly - B4RStream::WriteBytes and AsyncStreams::Write2 both + // trust the returned count, so no error, no retry and no log were produced. + // + // Rules applied below: + // * bytes leave the buffer only after the wrapped client reports them sent; + // * write() never returns more than it was given, and returns 0 when the buffer + // could not be drained, so the caller sees a short write and raises an error; + // * no extra retry loop is added: the ESP32/ESP8266 client already blocks up to + // its send timeout, repeating that here would only multiply the stall. + //============================================================================= + + //#define B4R_WIFI_BUFFER_DEBUG // uncomment to trace every transport failure on the serial port + int BufferedWiFiClient::connect(IPAddress ip, uint16_t port) { return wrappedClient->connect(ip, port); } @@ -11,19 +30,28 @@ return write(&t, 1); } size_t BufferedWiFiClient::write(const uint8_t *buf, size_t size) { - size_t total = 0; - if (size + index > MaxSize) { + if (wrappedClient == 0) + return 0; + // Make room for the new bytes. If they cannot be pushed out, this write must fail: + // accepting data on top of undelivered data is what used to hide the loss. + if (index > 0 && (size_t)index + size > MaxSize) { flush(); - total += index; + if (index > 0) { + #ifdef B4R_WIFI_BUFFER_DEBUG + Serial.print("BufferedWiFiClient: flush failed, pending = "); + Serial.println(index); + #endif + return 0; + } } if (size > MaxSize) { - return total + wrappedClient->write(buf, size); - } - else { - memcpy(buffer + index, buf, size); - index += size; - return total + size; + // Too large to buffer, so it is written through: the wrapped client reports the + // real number of transmitted bytes and a stall here is visible to the caller. + return wrappedClient->write(buf, size); } + memcpy(buffer + index, buf, size); + index += size; + return size; } int BufferedWiFiClient::available() { flush(); @@ -39,13 +67,31 @@ return wrappedClient->peek(); } void BufferedWiFiClient::flush(){ - if (index > 0) - wrappedClient->write(buffer, index); - index = 0; + if (wrappedClient == 0) + return; + while (index > 0) { + size_t sent = wrappedClient->write(buffer, index); + if (sent == 0) + break; // not accepted: keep the data buffered, do not pretend + if (sent > (size_t)index) + sent = index; // never trust an over-count from the wrapped client + if (sent < (size_t)index) + memmove(buffer, buffer + sent, index - sent); // partial send: retry the rest later + index -= (UInt)sent; + } } void BufferedWiFiClient::stop() { flush(); + if (index > 0) { + // Closing the socket makes the rest unrecoverable. The receiver will see a truncated + // body and reject it, which is the intended outcome - but it must not pass silently. + #ifdef B4R_WIFI_BUFFER_DEBUG + Serial.print("BufferedWiFiClient: discarding pending bytes on stop: "); + Serial.println(index); + #endif + index = 0; + } wrappedClient->stop(); } uint8_t BufferedWiFiClient::connected() { @@ -58,4 +104,4 @@ wrappedClient = client; index = 0; } -} \ No newline at end of file +}