From 5827505a3a47b5bf1618b7ed325b906a878f92b2 Mon Sep 17 00:00:00 2001 From: sandtec65 <41759949+sandtec65@users.noreply.github.com> Date: Fri, 27 Jul 2018 14:59:30 +0200 Subject: Prevent disconnect before file is fully received In the doStore function, the data.available return value may be less than the complete file size, if all tcp packets have not been received when the call is made. The problem is then that closeTransfer() will be called regardless, i.e. before the entire file is received, and the transfer will be interrupted. The code changes will allow additional doStore() iterations before closeTransfer is called. Tested OK on both ESP32 and ESP8266. Note: on ESP32, use arduino lib newer than 10 july, 2018. --- ESP8266FtpServer.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ESP8266FtpServer.cpp b/ESP8266FtpServer.cpp index 7d25e9a..98401c6 100644 --- a/ESP8266FtpServer.cpp +++ b/ESP8266FtpServer.cpp @@ -782,11 +782,11 @@ return false; boolean FtpServer::doStore() { - if( data.connected() ) + // Avoid blocking by never reading more bytes than are available + int navail = data.available(); + + if (navail > 0) { - // Avoid blocking by never reading more bytes than are available - int navail = data.available(); - if (navail <= 0) return true; // And be sure not to overflow buf. if (navail > FTP_BUF_SIZE) navail = FTP_BUF_SIZE; int16_t nb = data.read((uint8_t*) buf, navail ); @@ -797,10 +797,16 @@ boolean FtpServer::doStore() file.write((uint8_t*) buf, nb ); bytesTransfered += nb; } + } + if( !data.connected() && (navail <= 0) ) + { + closeTransfer(); + return false; + } + else + { return true; } - closeTransfer(); - return false; } void FtpServer::closeTransfer() -- cgit v1.2.3