Skip to content
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

fix(client): Implement readBytes in NetworkClient for faster downloads #9824

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
28 changes: 28 additions & 0 deletions libraries/Network/src/NetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,34 @@ int NetworkClient::read(uint8_t *buf, size_t size) {
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 res = -1;
if (fd() >= 0 && _rxBuffer) {
Expand Down
4 changes: 4 additions & 0 deletions libraries/Network/src/NetworkClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class NetworkClient : public ESPLwIPClient {
int available();
int read();
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();
void clear(); // clear rx
void stop();
Expand Down
Loading