From 37ef5a43e360bbb95def6be8a997d0c7a75ccf33 Mon Sep 17 00:00:00 2001 From: bbx10 Date: Mon, 31 Jul 2017 09:51:41 -1000 Subject: [PATCH] Handle partial socket send (#503) send() can return a value > 0 but less than size indicating it was able to accept some of the data in buffer. The caller must try again after updating the buffer pointer and size remaining. --- libraries/WiFi/src/WiFiClient.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp index f6f4c300..336f12b0 100644 --- a/libraries/WiFi/src/WiFiClient.cpp +++ b/libraries/WiFi/src/WiFiClient.cpp @@ -186,6 +186,8 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) int res =0; int retry = WIFI_CLIENT_MAX_WRITE_RETRY; int socketFileDescriptor = fd(); + size_t totalBytesSent = 0; + size_t bytesRemaining = size; if(!_connected || (socketFileDescriptor < 0)) { return 0; @@ -206,8 +208,18 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) } if(FD_ISSET(socketFileDescriptor, &set)) { - res = send(socketFileDescriptor, (void*) buf, size, MSG_DONTWAIT); - if(res < 0) { + res = send(socketFileDescriptor, (void*) buf, bytesRemaining, MSG_DONTWAIT); + if(res > 0) { + totalBytesSent += res; + if (totalBytesSent >= size) { + //completed successfully + retry = 0; + } else { + buf += res; + bytesRemaining -= res; + } + } + else if(res < 0) { log_e("%d", errno); if(errno != EAGAIN) { //if resource was busy, can try again, otherwise give up @@ -215,13 +227,13 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) res = 0; retry = 0; } - } else { - //completed successfully - retry = 0; + } + else { + // Try again } } } - return res; + return totalBytesSent; } size_t WiFiClient::write_P(PGM_P buf, size_t size)