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>
This commit is contained in:
parent
1ef2208349
commit
bc79feb217
2 changed files with 32 additions and 0 deletions
|
|
@ -479,6 +479,34 @@ int NetworkClient::read(uint8_t *buf, size_t size) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t NetworkClient::readBytes(char *buffer, size_t length) {
|
||||||
|
size_t left = length, sofar = 0;
|
||||||
|
int r = 0, to = millis() + getTimeout();
|
||||||
|
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() + getTimeout();
|
||||||
|
} 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 sofar;
|
||||||
|
}
|
||||||
|
|
||||||
int NetworkClient::peek() {
|
int NetworkClient::peek() {
|
||||||
int res = -1;
|
int res = -1;
|
||||||
if (fd() >= 0 && _rxBuffer) {
|
if (fd() >= 0 && _rxBuffer) {
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,10 @@ public:
|
||||||
int available();
|
int available();
|
||||||
int read();
|
int read();
|
||||||
int read(uint8_t *buf, size_t size);
|
int read(uint8_t *buf, size_t size);
|
||||||
|
size_t readBytes(char *buffer, size_t length);
|
||||||
|
size_t readBytes(uint8_t *buffer, size_t length) {
|
||||||
|
return readBytes((char *)buffer, length);
|
||||||
|
}
|
||||||
int peek();
|
int peek();
|
||||||
void clear(); // clear rx
|
void clear(); // clear rx
|
||||||
void stop();
|
void stop();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue