Skip to content

Commit

Permalink
deleting pbuf when partially consumed
Browse files Browse the repository at this point in the history
  • Loading branch information
andreagilardoni committed Jan 24, 2024
1 parent c595cc0 commit ec5ea83
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
16 changes: 11 additions & 5 deletions libraries/lwIpWrapper/src/lwipClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions libraries/lwIpWrapper/src/lwipClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit ec5ea83

Please sign in to comment.