From 9c0b0bc583b3bc2d69379f8c1d8bba49aab22c45 Mon Sep 17 00:00:00 2001 From: Thorsten Laux Date: Tue, 17 Jan 2017 09:06:51 +0100 Subject: [PATCH 1/3] ClientConnection uses too much heap when streaming files #2871 --- .../ESP8266WiFi/src/include/ClientContext.h | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/libraries/ESP8266WiFi/src/include/ClientContext.h b/libraries/ESP8266WiFi/src/include/ClientContext.h index d2ee0903b3..2d4437ee9c 100644 --- a/libraries/ESP8266WiFi/src/include/ClientContext.h +++ b/libraries/ESP8266WiFi/src/include/ClientContext.h @@ -333,6 +333,7 @@ class ClientContext return _written; } +#define TCP_WRITE_CHUNK_SIZE 256 void _write_some() { @@ -345,16 +346,21 @@ class ClientContext if (_pcb->snd_queuelen >= TCP_SND_QUEUELEN) { can_send = 0; } - size_t will_send = (can_send < left) ? can_send : left; - if (will_send) { - const uint8_t* buf = _datasource->get_buffer(will_send); - err_t err = tcp_write(_pcb, buf, will_send, TCP_WRITE_FLAG_COPY); - _datasource->release_buffer(buf, will_send); + size_t will_send = (can_send < left) ? can_send : left; + bool did_write = false; + while( will_send ) { + size_t next_chunk = + will_send > TCP_WRITE_CHUNK_SIZE ? TCP_WRITE_CHUNK_SIZE : will_send; + const uint8_t* buf = _datasource->get_buffer(next_chunk); + err_t err = tcp_write(_pcb, buf, next_chunk, TCP_WRITE_FLAG_COPY); + _datasource->release_buffer(buf, next_chunk); if (err == ERR_OK) { - _written += will_send; - tcp_output(_pcb); + _written += next_chunk; + did_write = true; } + will_send -= next_chunk; } + if( did_write ) tcp_output(_pcb); if (!_datasource->available() || _noblock) { delete _datasource; From 091ec1f7a939bd65b6e3985b2608e92c34ed9813 Mon Sep 17 00:00:00 2001 From: Thorsten Laux Date: Tue, 17 Jan 2017 09:24:46 +0100 Subject: [PATCH 2/3] make write_chunk_size a member variable --- libraries/ESP8266WiFi/src/include/ClientContext.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266WiFi/src/include/ClientContext.h b/libraries/ESP8266WiFi/src/include/ClientContext.h index 2d4437ee9c..bd29475276 100644 --- a/libraries/ESP8266WiFi/src/include/ClientContext.h +++ b/libraries/ESP8266WiFi/src/include/ClientContext.h @@ -333,8 +333,6 @@ class ClientContext return _written; } -#define TCP_WRITE_CHUNK_SIZE 256 - void _write_some() { if (!_datasource || !_pcb) { @@ -350,7 +348,7 @@ class ClientContext bool did_write = false; while( will_send ) { size_t next_chunk = - will_send > TCP_WRITE_CHUNK_SIZE ? TCP_WRITE_CHUNK_SIZE : will_send; + will_send > _write_chunk_size ? _write_chunk_size : will_send; const uint8_t* buf = _datasource->get_buffer(next_chunk); err_t err = tcp_write(_pcb, buf, next_chunk, TCP_WRITE_FLAG_COPY); _datasource->release_buffer(buf, next_chunk); @@ -480,6 +478,7 @@ class ClientContext DataSource* _datasource = nullptr; size_t _written = 0; + size_t _write_chunk_size = 256; bool _noblock = false; bool _send_waiting = false; }; From 174bdf4ccb2c849c064f82407b7a94d4d325a2d6 Mon Sep 17 00:00:00 2001 From: Thorsten Laux Date: Tue, 17 Jan 2017 09:27:28 +0100 Subject: [PATCH 3/3] untabify --- .../ESP8266WiFi/src/include/ClientContext.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libraries/ESP8266WiFi/src/include/ClientContext.h b/libraries/ESP8266WiFi/src/include/ClientContext.h index bd29475276..caef03cb0f 100644 --- a/libraries/ESP8266WiFi/src/include/ClientContext.h +++ b/libraries/ESP8266WiFi/src/include/ClientContext.h @@ -344,21 +344,21 @@ class ClientContext if (_pcb->snd_queuelen >= TCP_SND_QUEUELEN) { can_send = 0; } - size_t will_send = (can_send < left) ? can_send : left; - bool did_write = false; - while( will_send ) { - size_t next_chunk = - will_send > _write_chunk_size ? _write_chunk_size : will_send; + size_t will_send = (can_send < left) ? can_send : left; + bool did_write = false; + while( will_send ) { + size_t next_chunk = + will_send > _write_chunk_size ? _write_chunk_size : will_send; const uint8_t* buf = _datasource->get_buffer(next_chunk); err_t err = tcp_write(_pcb, buf, next_chunk, TCP_WRITE_FLAG_COPY); _datasource->release_buffer(buf, next_chunk); if (err == ERR_OK) { _written += next_chunk; - did_write = true; + did_write = true; } - will_send -= next_chunk; + will_send -= next_chunk; } - if( did_write ) tcp_output(_pcb); + if( did_write ) tcp_output(_pcb); if (!_datasource->available() || _noblock) { delete _datasource; @@ -478,7 +478,7 @@ class ClientContext DataSource* _datasource = nullptr; size_t _written = 0; - size_t _write_chunk_size = 256; + size_t _write_chunk_size = 256; bool _noblock = false; bool _send_waiting = false; };