From d1982482408d2ff61a6da6a87bec4a6a95d8086d Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Sun, 13 Oct 2024 11:28:15 +0200 Subject: [PATCH] Fix _recv method --- examples/Client/Client.ino | 8 ++++---- platformio.ini | 2 +- src/AsyncTCP.cpp | 27 ++++++--------------------- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/examples/Client/Client.ino b/examples/Client/Client.ino index cf3de05..f2f7c28 100644 --- a/examples/Client/Client.ino +++ b/examples/Client/Client.ino @@ -20,7 +20,7 @@ // 16 slots on esp32 (CONFIG_LWIP_MAX_ACTIVE_TCP) #define MAX_CLIENTS CONFIG_LWIP_MAX_ACTIVE_TCP -// #define MAX_CLIENTS 3 +// #define MAX_CLIENTS 1 size_t permits = MAX_CLIENTS; @@ -52,8 +52,7 @@ void makeRequest() { }); client->onData([](void* arg, AsyncClient* client, void* data, size_t len) { - Serial.printf("** data received by client: %" PRIu16 "\n", client->localPort()); - // Serial.write((uint8_t*)data, len); + Serial.printf("** data received by client: %" PRIu16 ": len=%u\n", client->localPort(), len); }); client->write("GET / HTTP/1.1\r\nHost: " HOST "\r\nUser-Agent: ESP\r\nConnection: close\r\n\r\n"); @@ -84,5 +83,6 @@ void setup() { } void loop() { - delay(500); + delay(1000); + Serial.printf("** free heap: %" PRIu32 "\n", ESP.getFreeHeap()); } diff --git a/platformio.ini b/platformio.ini index 0de5c49..387a5b8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,7 +13,7 @@ build_flags = -D CONFIG_ASYNC_TCP_RUNNING_CORE=1 -D CONFIG_ASYNC_TCP_STACK_SIZE=4096 -D CONFIG_ARDUHAL_LOG_COLORS - -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO + -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG upload_protocol = esptool monitor_speed = 115200 monitor_filters = esp32_exception_decoder, log2file diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index 75a631d..8b4bc16 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -447,19 +447,8 @@ static esp_err_t _tcp_write(tcp_pcb * pcb, int8_t closed_slot, const char* data, static err_t _tcp_recved_api(struct tcpip_api_call_data *api_call_msg){ tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg; msg->err = ERR_CONN; - // original code: - // causes the issue: assert failed: tcp_update_rcv_ann_wnd IDF/components/lwip/lwip/src/core/tcp.c:951 (new_rcv_ann_wnd <= 0xffff) - // if(msg->closed_slot == INVALID_CLOSED_SLOT || !_closed_slots[msg->closed_slot]) { - - // attempted fix 1, which was rolled back since non reproductible after middleware support in ESPAsyncWebServer but works in some cases: - // - https://github.com/tbnobody/OpenDTU/discussions/2326 - // - https://github.com/mathieucarbou/AsyncTCP/pull/24 - if(msg->closed_slot != INVALID_CLOSED_SLOT && !_closed_slots[msg->closed_slot]) { - - // attempted fix 2, - // - OpenDTU testing: https://github.com/tbnobody/OpenDTU/discussions/2326 - // - Original fix https://github.com/me-no-dev/AsyncTCP/pull/173/files#diff-5312944d5b5f39741f3827dd678a8e828e2624ceab236264c025ef2bdf400d6aR422-R428 - // Seems to have side effects: https://github.com/me-no-dev/ESPAsyncWebServer/issues/1437 + if(msg->closed_slot == INVALID_CLOSED_SLOT || !_closed_slots[msg->closed_slot]) { + // if(msg->closed_slot != INVALID_CLOSED_SLOT && !_closed_slots[msg->closed_slot]) { // if(msg->closed_slot != INVALID_CLOSED_SLOT) { msg->err = 0; tcp_recved(msg->pcb, msg->received); @@ -996,19 +985,13 @@ int8_t AsyncClient::_sent(tcp_pcb* pcb, uint16_t len) { } int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) { - if(!_pcb || pcb != _pcb){ - log_d("0x%08x != 0x%08x", (uint32_t)pcb, (uint32_t)_pcb); - return ERR_OK; - } - size_t total = 0; - while((pb != NULL) && (ERR_OK == err)) { + while(pb != NULL) { _rx_last_packet = millis(); //we should not ack before we assimilate the data _ack_pcb = true; pbuf *b = pb; pb = b->next; b->next = NULL; - total += b->len; if(_pb_cb){ _pb_cb(_pb_cb_arg, this, b); } else { @@ -1017,11 +1000,13 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) { } if(!_ack_pcb) { _rx_ack_len += b->len; + } else if(_pcb) { + _tcp_recved(_pcb, _closed_slot, b->len); } } pbuf_free(b); } - return _tcp_recved(pcb, _closed_slot, total); + return ERR_OK; } int8_t AsyncClient::_poll(tcp_pcb* pcb){