Skip to content

Handle partial socket send #503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions libraries/WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -206,22 +208,32 @@ 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
stop();
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)
Expand Down