From ec5ea837dc45651008a62d491be7f4914d2ba937 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Tue, 23 Jan 2024 17:30:46 +0100 Subject: [PATCH] deleting pbuf when partially consumed --- libraries/lwIpWrapper/src/lwipClient.cpp | 16 +++++++++++----- libraries/lwIpWrapper/src/lwipClient.h | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/libraries/lwIpWrapper/src/lwipClient.cpp b/libraries/lwIpWrapper/src/lwipClient.cpp index 02103ba1..019ea459 100644 --- a/libraries/lwIpWrapper/src/lwipClient.cpp +++ b/libraries/lwIpWrapper/src/lwipClient.cpp @@ -235,7 +235,7 @@ err_t lwipClient::recv_callback(struct tcp_pcb* tpcb, struct pbuf* p, err_t err) if (p == NULL) { // Remote host has closed the connection -> close from our side - this->stop(); + this->close_pcb(); return ERR_OK; } @@ -345,7 +345,7 @@ void lwipClient::flush() { tcp_output(this->tcp_info->pcb); } -void lwipClient::stop() { +void lwipClient::close_pcb() { if(this->tcp_info->pcb != nullptr) { tcp_recv(this->tcp_info->pcb, nullptr); tcp_sent(this->tcp_info->pcb, nullptr); @@ -360,12 +360,18 @@ void lwipClient::stop() { // FIXME if err != ERR_OK retry, there may be memory issues, retry? } +} +void lwipClient::stop() { + this->close_pcb(); // reset all the other variables in this class - // if(tcp->p != nullptr) { - // pbuf_free(tcp->p); // FIXME it happens that a pbuf, with ref == 0 is added for some reason - // } + if(this->tcp_info->pbuf_head != nullptr) { + pbuf_free(this->tcp_info->pbuf_head); // FIXME it happens that a pbuf, with ref == 0 is added for some reason + this->tcp_info->pbuf_head = nullptr; + } + this->tcp_info->pbuf_offset = 0; + if(this->tcp_info->server != nullptr) { // need to first make the server point to nullptr, then remove the client, can cause infinite recursion auto server = this->tcp_info->server; diff --git a/libraries/lwIpWrapper/src/lwipClient.h b/libraries/lwIpWrapper/src/lwipClient.h index 446c4288..d7a7c0bd 100644 --- a/libraries/lwIpWrapper/src/lwipClient.h +++ b/libraries/lwIpWrapper/src/lwipClient.h @@ -108,6 +108,7 @@ class lwipClient : public arduino::Client { friend err_t _lwip_tcp_recv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p, err_t err); friend err_t _lwip_tcp_connected_callback(void* arg, struct tcp_pcb* tpcb, err_t err); + void close_pcb(); }; inline const lwipClient CLIENT_NONE(nullptr, nullptr); \ No newline at end of file