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.
This commit is contained in:
parent
2eaf846134
commit
37ef5a43e3
@ -186,6 +186,8 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
|
|||||||
int res =0;
|
int res =0;
|
||||||
int retry = WIFI_CLIENT_MAX_WRITE_RETRY;
|
int retry = WIFI_CLIENT_MAX_WRITE_RETRY;
|
||||||
int socketFileDescriptor = fd();
|
int socketFileDescriptor = fd();
|
||||||
|
size_t totalBytesSent = 0;
|
||||||
|
size_t bytesRemaining = size;
|
||||||
|
|
||||||
if(!_connected || (socketFileDescriptor < 0)) {
|
if(!_connected || (socketFileDescriptor < 0)) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -206,8 +208,18 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(FD_ISSET(socketFileDescriptor, &set)) {
|
if(FD_ISSET(socketFileDescriptor, &set)) {
|
||||||
res = send(socketFileDescriptor, (void*) buf, size, MSG_DONTWAIT);
|
res = send(socketFileDescriptor, (void*) buf, bytesRemaining, MSG_DONTWAIT);
|
||||||
if(res < 0) {
|
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);
|
log_e("%d", errno);
|
||||||
if(errno != EAGAIN) {
|
if(errno != EAGAIN) {
|
||||||
//if resource was busy, can try again, otherwise give up
|
//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;
|
res = 0;
|
||||||
retry = 0;
|
retry = 0;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
//completed successfully
|
else {
|
||||||
retry = 0;
|
// Try again
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return totalBytesSent;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t WiFiClient::write_P(PGM_P buf, size_t size)
|
size_t WiFiClient::write_P(PGM_P buf, size_t size)
|
||||||
|
Loading…
Reference in New Issue
Block a user