Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(client): Implement readBytes to obey the client timeout
  • Loading branch information
me-no-dev committed Jun 12, 2024
commit 2a5a7c5810f44faaf03d50bfcc5525bb591c7688
28 changes: 24 additions & 4 deletions libraries/Network/src/NetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,31 @@ int NetworkClient::read(uint8_t *buf, size_t size) {
}

size_t NetworkClient::readBytes(char *buffer, size_t length) {
int r = read((uint8_t*)buffer, length);
if (r < 0) {
return 0;
size_t left = length, sofar = 0;
int r = 0, to = millis() + _timeout;
while (left) {
r = read((uint8_t*)buffer+sofar, left);
if (r < 0) {
// Error has occurred
break;
}
if (r > 0) {
// We got some data
left -= r;
sofar += r;
to = millis() + _timeout;
} else {
// We got no data
if (millis() >= to) {
// We have waited for data enough
log_w("Timeout waiting for data on fd %d", fd());
break;
}
// Allow other tasks to run
delay(2);
}
}
return (size_t)r;
return sofar;
}

int NetworkClient::peek() {
Expand Down