Skip to content

Commit 61787b2

Browse files
joelucidigrr
authored andcommitted
ClientConnection uses too much heap when streaming files esp8266#2871 (esp8266#2874)
* ClientConnection uses too much heap when streaming files esp8266#2871 * make write_chunk_size a member variable * untabify
1 parent 90729ea commit 61787b2

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

libraries/ESP8266WiFi/src/include/ClientContext.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ class ClientContext
333333
return _written;
334334
}
335335

336-
337336
void _write_some()
338337
{
339338
if (!_datasource || !_pcb) {
@@ -346,15 +345,20 @@ class ClientContext
346345
can_send = 0;
347346
}
348347
size_t will_send = (can_send < left) ? can_send : left;
349-
if (will_send) {
350-
const uint8_t* buf = _datasource->get_buffer(will_send);
351-
err_t err = tcp_write(_pcb, buf, will_send, TCP_WRITE_FLAG_COPY);
352-
_datasource->release_buffer(buf, will_send);
348+
bool did_write = false;
349+
while( will_send ) {
350+
size_t next_chunk =
351+
will_send > _write_chunk_size ? _write_chunk_size : will_send;
352+
const uint8_t* buf = _datasource->get_buffer(next_chunk);
353+
err_t err = tcp_write(_pcb, buf, next_chunk, TCP_WRITE_FLAG_COPY);
354+
_datasource->release_buffer(buf, next_chunk);
353355
if (err == ERR_OK) {
354-
_written += will_send;
355-
tcp_output(_pcb);
356+
_written += next_chunk;
357+
did_write = true;
356358
}
359+
will_send -= next_chunk;
357360
}
361+
if( did_write ) tcp_output(_pcb);
358362

359363
if (!_datasource->available() || _noblock) {
360364
delete _datasource;
@@ -479,6 +483,7 @@ class ClientContext
479483

480484
DataSource* _datasource = nullptr;
481485
size_t _written = 0;
486+
size_t _write_chunk_size = 256;
482487
bool _noblock = false;
483488
bool _send_waiting = false;
484489
};

0 commit comments

Comments
 (0)