Skip to content

Commit 37ef5a4

Browse files
bbx10me-no-dev
authored andcommitted
Handle partial socket send (espressif#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.
1 parent 2eaf846 commit 37ef5a4

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

libraries/WiFi/src/WiFiClient.cpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
186186
int res =0;
187187
int retry = WIFI_CLIENT_MAX_WRITE_RETRY;
188188
int socketFileDescriptor = fd();
189+
size_t totalBytesSent = 0;
190+
size_t bytesRemaining = size;
189191

190192
if(!_connected || (socketFileDescriptor < 0)) {
191193
return 0;
@@ -206,22 +208,32 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
206208
}
207209

208210
if(FD_ISSET(socketFileDescriptor, &set)) {
209-
res = send(socketFileDescriptor, (void*) buf, size, MSG_DONTWAIT);
210-
if(res < 0) {
211+
res = send(socketFileDescriptor, (void*) buf, bytesRemaining, MSG_DONTWAIT);
212+
if(res > 0) {
213+
totalBytesSent += res;
214+
if (totalBytesSent >= size) {
215+
//completed successfully
216+
retry = 0;
217+
} else {
218+
buf += res;
219+
bytesRemaining -= res;
220+
}
221+
}
222+
else if(res < 0) {
211223
log_e("%d", errno);
212224
if(errno != EAGAIN) {
213225
//if resource was busy, can try again, otherwise give up
214226
stop();
215227
res = 0;
216228
retry = 0;
217229
}
218-
} else {
219-
//completed successfully
220-
retry = 0;
230+
}
231+
else {
232+
// Try again
221233
}
222234
}
223235
}
224-
return res;
236+
return totalBytesSent;
225237
}
226238

227239
size_t WiFiClient::write_P(PGM_P buf, size_t size)

0 commit comments

Comments
 (0)