Skip to content

Commit d717b58

Browse files
Speed up ClientContext::write(Stream) by chunking (#1017)
Instead of sending a single packet per byte, send out larger chunks to TCP/IP while doing a ::write(Stream). Fixes #999
1 parent 861da4c commit d717b58

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

libraries/WiFi/src/include/ClientContext.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,22 @@ class ClientContext {
374374
return 0;
375375
}
376376
size_t sent = 0;
377+
uint8_t buff[128];
377378
while (stream.available()) {
378-
char b;
379-
b = stream.read();
380-
if (write(&b, 1)) {
381-
sent ++;
379+
// Stream only lets you read 1 byte at a time, so buffer in local copy
380+
size_t i;
381+
for (i = 0; (i < sizeof(buff)) && stream.available(); i++) {
382+
buff[i] = stream.read();
383+
}
384+
if (i) {
385+
// Send as a single packet
386+
int len = write((const char *)buff, i);
387+
sent += len;
388+
if (len != (int)i) {
389+
break; // Write error...
390+
}
382391
} else {
392+
// Out of data...
383393
break;
384394
}
385395
}

0 commit comments

Comments
 (0)