Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2a5a7c5

Browse files
committedJun 12, 2024·
fix(client): Implement readBytes to obey the client timeout
1 parent 29df67b commit 2a5a7c5

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed
 

‎libraries/Network/src/NetworkClient.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,31 @@ int NetworkClient::read(uint8_t *buf, size_t size) {
480480
}
481481

482482
size_t NetworkClient::readBytes(char *buffer, size_t length) {
483-
int r = read((uint8_t*)buffer, length);
484-
if (r < 0) {
485-
return 0;
483+
size_t left = length, sofar = 0;
484+
int r = 0, to = millis() + _timeout;
485+
while (left) {
486+
r = read((uint8_t*)buffer+sofar, left);
487+
if (r < 0) {
488+
// Error has occurred
489+
break;
490+
}
491+
if (r > 0) {
492+
// We got some data
493+
left -= r;
494+
sofar += r;
495+
to = millis() + _timeout;
496+
} else {
497+
// We got no data
498+
if (millis() >= to) {
499+
// We have waited for data enough
500+
log_w("Timeout waiting for data on fd %d", fd());
501+
break;
502+
}
503+
// Allow other tasks to run
504+
delay(2);
505+
}
486506
}
487-
return (size_t)r;
507+
return sofar;
488508
}
489509

490510
int NetworkClient::peek() {

0 commit comments

Comments
 (0)
Please sign in to comment.