Skip to content

Commit 8fd56ad

Browse files
Adjust tcp_write size when memory is tight (#729)
Increases the MEM_SIZE outstanding write buffer to 8K Allows the ClientContext to attempt to send smaller buffer chunks in the case where MEM_SIZE won't allow the full tcp_sndbuf() transfer. Fixes #725
1 parent 5ab19e9 commit 8fd56ad

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

include/lwipopts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern void interrupts();
2121
#define MEM_LIBC_MALLOC 0
2222

2323
#define MEM_ALIGNMENT 4
24-
#define MEM_SIZE 4000
24+
#define MEM_SIZE 8192
2525
#define MEMP_NUM_TCP_SEG 32
2626
#define MEMP_NUM_ARP_QUEUE 10
2727
#define PBUF_POOL_SIZE 24

lib/libpico-ipv6.a

0 Bytes
Binary file not shown.

lib/libpico.a

0 Bytes
Binary file not shown.

libraries/WiFi/src/include/ClientContext.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ class ClientContext {
491491
DEBUGV(":wr %d %d\r\n", _datalen - _written, _written);
492492

493493
bool has_written = false;
494+
int scale = 0;
494495

495496
while (_written < _datalen) {
496497
if (state() == CLOSED) {
@@ -501,6 +502,10 @@ class ClientContext {
501502
{
502503
LWIPMutex m; // Block the timer sys_check_timeouts call, just for this call
503504
next_chunk_size = std::min((size_t)tcp_sndbuf(_pcb), remaining);
505+
// Potentially reduce transmit size if we are tight on memory, but only if it doesn't return a 0 chunk size
506+
if (next_chunk_size > (size_t)(1 << scale)) {
507+
next_chunk_size >>= scale;
508+
}
504509
}
505510
if (!next_chunk_size) {
506511
break;
@@ -531,6 +536,13 @@ class ClientContext {
531536
if (err == ERR_OK) {
532537
_written += next_chunk_size;
533538
has_written = true;
539+
} else if (err == ERR_MEM) {
540+
if (scale < 4) {
541+
// Retry sending at 1/2 the chunk size
542+
scale ++;
543+
} else {
544+
break;
545+
}
534546
} else {
535547
// ERR_MEM(-1) is a valid error meaning
536548
// "come back later". It leaves state() opened

tools/libpico/lwipopts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern void interrupts();
2121
#define MEM_LIBC_MALLOC 0
2222

2323
#define MEM_ALIGNMENT 4
24-
#define MEM_SIZE 4000
24+
#define MEM_SIZE 8192
2525
#define MEMP_NUM_TCP_SEG 32
2626
#define MEMP_NUM_ARP_QUEUE 10
2727
#define PBUF_POOL_SIZE 24

0 commit comments

Comments
 (0)