Skip to content

Commit bc79feb

Browse files
fix(client): Implement readBytes in NetworkClient for faster downloads (#9824)
* fix(client): Implement readBytes in NetworkClient for faster downloads * fix(client): Implement readBytes to obey the client timeout * fix(clieant): use getTimeout() instead * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 1ef2208 commit bc79feb

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: libraries/Network/src/NetworkClient.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,34 @@ int NetworkClient::read(uint8_t *buf, size_t size) {
479479
return res;
480480
}
481481

482+
size_t NetworkClient::readBytes(char *buffer, size_t length) {
483+
size_t left = length, sofar = 0;
484+
int r = 0, to = millis() + getTimeout();
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() + getTimeout();
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+
}
506+
}
507+
return sofar;
508+
}
509+
482510
int NetworkClient::peek() {
483511
int res = -1;
484512
if (fd() >= 0 && _rxBuffer) {

Diff for: libraries/Network/src/NetworkClient.h

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class NetworkClient : public ESPLwIPClient {
6060
int available();
6161
int read();
6262
int read(uint8_t *buf, size_t size);
63+
size_t readBytes(char *buffer, size_t length);
64+
size_t readBytes(uint8_t *buffer, size_t length) {
65+
return readBytes((char *)buffer, length);
66+
}
6367
int peek();
6468
void clear(); // clear rx
6569
void stop();

0 commit comments

Comments
 (0)