From 023cca11187d10c60e507ebe80e874a6553dc112 Mon Sep 17 00:00:00 2001 From: Justin Mitchell Date: Thu, 15 Jan 2026 10:33:40 -0500 Subject: [PATCH] fixed files stalling at 90% --- .../network/CalibreWirelessActivity.cpp | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/activities/network/CalibreWirelessActivity.cpp b/src/activities/network/CalibreWirelessActivity.cpp index 6a701696..d6df713a 100644 --- a/src/activities/network/CalibreWirelessActivity.cpp +++ b/src/activities/network/CalibreWirelessActivity.cpp @@ -235,17 +235,29 @@ void CalibreWirelessActivity::listenForDiscovery() { } void CalibreWirelessActivity::handleTcpClient() { + // In binary mode, keep reading even if connection closed - data may still be buffered + if (inBinaryMode) { + // Check if there's still data to read, even if connection is closing + if (tcpClient.available() > 0 || tcpClient.connected()) { + receiveBinaryData(); + return; + } + // Connection closed and no more data - check if transfer was complete + if (binaryBytesRemaining > 0) { + Serial.printf("[%lu] [CAL] Connection lost with %zu bytes remaining\n", millis(), binaryBytesRemaining); + currentFile.close(); + inBinaryMode = false; + setError("Transfer incomplete - connection lost"); + return; + } + } + if (!tcpClient.connected()) { setState(WirelessState::DISCONNECTED); setStatus("Calibre disconnected"); return; } - if (inBinaryMode) { - receiveBinaryData(); - return; - } - std::string message; if (readJsonMessage(message)) { size_t start = message.find('['); @@ -532,8 +544,9 @@ void CalibreWirelessActivity::receiveBinaryData() { int available = tcpClient.available(); if (available <= 0) { - // Brief wait for more data - vTaskDelay(1); + // Wait longer for data - TCP buffers may not be immediately available + // especially near end of transfer when connection is closing + vTaskDelay(10 / portTICK_PERIOD_MS); return; }