From d5ec8c18b624452395497c794408d037464fabcc Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Mon, 30 Nov 2026 14:51:05 -0600 Subject: [PATCH 1/5] Testing fix: Wrong data sent to clients - When running headless server example, found users can still receive incorrect data, this should fix that --- RF24Client.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++-------- RF24Client.h | 4 +++ 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/RF24Client.cpp b/RF24Client.cpp index 7463328..93dccac 100644 --- a/RF24Client.cpp +++ b/RF24Client.cpp @@ -248,6 +248,12 @@ err_t RF24Client::srecv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p return ERR_OK; } + // Waiting connections would share the spare buffer and could mix up their requests. lwIP + // keeps refused data and offers it again once this connection is the active one. + if (tpcb != myPcb) { + return ERR_MEM; + } + bool id = state->stateActiveID; IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: Copy data to "); Serial.println(state->stateActiveID);); @@ -383,9 +389,12 @@ err_t RF24Client::serverTimeouts(void* arg, struct tcp_pcb* tpcb) //if (tpcb->state == ESTABLISHED || tpcb->state == SYN_SENT || tpcb->state == SYN_RCVD) { IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println("Server: Closed Server PCB TIMEOUT ");); + // Detach first: its late callbacks must not touch a slot a newer connection may use, + // and tcp_close() frees the pcb at once if unread data is left + tcp_arg(tpcb, nullptr); state->result = tcp_close(tpcb); state->closeTimer = millis(); - dataSize[activeState] = 0; + dataSize[state->stateActiveID] = 0; state->connected = false; if (state->result != ERR_OK) { if (myPcb == tpcb) { @@ -397,7 +406,9 @@ err_t RF24Client::serverTimeouts(void* arg, struct tcp_pcb* tpcb) tpcb = nullptr; return ERR_ABRT; } - myPcb = nullptr; + if (myPcb == tpcb) { + myPcb = nullptr; + } return state->result; // } @@ -599,6 +610,11 @@ RF24Client::RF24Client(uip_userdata_t* conn_data) : data(conn_data) /** \cond */ RF24Client::RF24Client(uint32_t data) : data(0) { + #if USE_LWIP == 1 + if (gState[activeState] != nullptr) { + connectionId = gState[activeState]->identifier; + } + #endif } /** \endcond */ #endif @@ -609,7 +625,7 @@ uint8_t RF24Client::connected() #if USE_LWIP < 1 return (data && (data->packets_in != 0 || (data->state & UIP_CLIENT_CONNECTED))) ? 1 : 0; #elif USE_LWIP == 1 - if (gState[activeState] != nullptr) { + if (ownsConnection()) { return gState[activeState]->connected || dataSize[activeState] > 0; } return 0; @@ -784,6 +800,8 @@ int RF24Client::connect(IPAddress ip, uint16_t port) memset(incomingData[activeState], 0, INCOMING_DATA_SIZE); gState[activeState]->connected = false; gState[activeState]->result = 0; + gState[activeState]->identifier = ++simpleCounter; + connectionId = simpleCounter; tcp_arg(myPcb, gState[activeState]); tcp_err(myPcb, error_callback); tcp_recv(myPcb, recv_callback); @@ -1025,7 +1043,9 @@ void RF24Client::stop() RF24Ethernet.update(); #elif USE_LWIP == 1 - _stop(); + if (ownsConnection()) { + _stop(); + } #elif USE_LWIP == 2 if (_socket >= 0) { @@ -1042,8 +1062,10 @@ void RF24Client::stop() void RF24Client::_stop() { + tcp_pcb* const stopping = myPcb; // Ethernet.update() below can close it or make another one active + const uint32_t drainStart = millis(); - while (myPcb != nullptr && gState[activeState]->connected && (myPcb->unsent != nullptr || myPcb->unacked != nullptr)) { + while (myPcb != nullptr && myPcb == stopping && gState[activeState]->connected && (myPcb->unsent != nullptr || myPcb->unacked != nullptr)) { if (millis() - drainStart > 5000) { break; } @@ -1061,6 +1083,10 @@ void RF24Client::_stop() Ethernet.update(); } + if (myPcb != stopping) { + return; + } + tcp_pcb* pcb = myPcb; myPcb = nullptr; @@ -1116,7 +1142,7 @@ RF24Client::operator bool() #if USE_LWIP < 1 return data && (!(data->state & UIP_CLIENT_REMOTECLOSED) || data->packets_in != 0); #elif USE_LWIP == 1 - return dataSize[activeState] > 0 ? true : false; + return ownsConnection() && dataSize[activeState] > 0; #elif USE_LWIP == 2 return available(); @@ -1127,6 +1153,11 @@ RF24Client::operator bool() size_t RF24Client::write(uint8_t c) { +#if USE_LWIP == 1 + if (!ownsConnection()) { + return 0; + } +#endif return _write(data, &c, 1); } @@ -1134,6 +1165,11 @@ size_t RF24Client::write(uint8_t c) size_t RF24Client::write(const uint8_t* buf, size_t size) { +#if USE_LWIP == 1 + if (!ownsConnection()) { + return 0; + } +#endif return _write(data, buf, size); } @@ -1207,6 +1243,8 @@ size_t RF24Client::_write(uint8_t* data, const uint8_t* buf, size_t size) #elif USE_LWIP == 1 bool initialActiveState = activeState; + // blocking_write() and Ethernet.update() can close this connection and make another one active + tcp_pcb* const writing = myPcb; size_t chunk = (MAX_PAYLOAD_SIZE - 14) < TCP_SND_BUF ? (MAX_PAYLOAD_SIZE - 14) : TCP_SND_BUF; // Max chunk size is equal to RF24Network MAX_PAYLOAD_SIZE capped by TCP_SND_BUF size size_t position = 0; @@ -1215,7 +1253,7 @@ size_t RF24Client::_write(uint8_t* data, const uint8_t* buf, size_t size) if (millis() - timeout > 3000) { break; } - if (myPcb == nullptr) + if (writing == nullptr || myPcb != writing) return 0; err_t write_err = blocking_write(myPcb, gState[initialActiveState], reinterpret_cast(&buf[position]), chunk); @@ -1223,7 +1261,9 @@ size_t RF24Client::_write(uint8_t* data, const uint8_t* buf, size_t size) if (write_err != ERR_OK) { gState[initialActiveState]->result = write_err; gState[initialActiveState]->connected = false; - _stop(); + if (myPcb == writing) { + _stop(); + } return 0; } position += chunk; @@ -1232,7 +1272,7 @@ size_t RF24Client::_write(uint8_t* data, const uint8_t* buf, size_t size) Ethernet.update(); } - if (myPcb == nullptr) + if (writing == nullptr || myPcb != writing) return 0; err_t write_err = blocking_write(myPcb, gState[initialActiveState], reinterpret_cast(&buf[position]), size); @@ -1240,7 +1280,9 @@ size_t RF24Client::_write(uint8_t* data, const uint8_t* buf, size_t size) if (write_err != ERR_OK) { gState[initialActiveState]->result = write_err; gState[initialActiveState]->connected = false; - _stop(); + if (myPcb == writing) { + _stop(); + } return 0; } @@ -1538,6 +1580,10 @@ int RF24Client::available() { return _available(data); } +#elif USE_LWIP == 1 + if (ownsConnection()) { + return _available(data); + } #else return _available(data); #endif @@ -1757,7 +1803,9 @@ void RF24Client::flush() #endif } #elif USE_LWIP == 1 - dataSize[activeState] = 0; + if (ownsConnection()) { + dataSize[activeState] = 0; + } #elif USE_LWIP == 2 while (available()) { read(); diff --git a/RF24Client.h b/RF24Client.h index 8cf94df..bfd70d1 100644 --- a/RF24Client.h +++ b/RF24Client.h @@ -259,6 +259,10 @@ class RF24Client : public Client RF24Client(uint32_t data); RF24Client(uint8_t data); uint8_t* data; + // The ConnectState::identifier of this client's connection. If the active connection is a + // different one (it closed and a waiting one took over), this client must not touch it. + uint32_t connectionId = 0; + bool ownsConnection() const { return gState[activeState] != nullptr && gState[activeState]->identifier == connectionId; } static size_t _write(uint8_t* data, const uint8_t* buf, size_t size); static int _available(uint8_t* data); From 33a4638b0f3e1f8e4c6cca4a89d93b8c51272064 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Tue, 1 Dec 2026 03:16:50 -0600 Subject: [PATCH 2/5] Clear callbacks on close --- RF24Client.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/RF24Client.cpp b/RF24Client.cpp index 93dccac..3be7f26 100644 --- a/RF24Client.cpp +++ b/RF24Client.cpp @@ -391,11 +391,15 @@ err_t RF24Client::serverTimeouts(void* arg, struct tcp_pcb* tpcb) // Detach first: its late callbacks must not touch a slot a newer connection may use, // and tcp_close() frees the pcb at once if unread data is left + state->connected = false; tcp_arg(tpcb, nullptr); + tcp_poll(tpcb, nullptr, 0); + tcp_err(tpcb, nullptr); + tcp_recv(tpcb, nullptr); + state->result = tcp_close(tpcb); state->closeTimer = millis(); dataSize[state->stateActiveID] = 0; - state->connected = false; if (state->result != ERR_OK) { if (myPcb == tpcb) { tcp_arg(myPcb, nullptr); @@ -464,9 +468,13 @@ err_t RF24Client::closed_port(void* arg, struct tcp_pcb* tpcb) state->backlogWasAccepted = true; accepts--; } - - state->result = tcp_close(tpcb); state->connected = false; + tcp_arg(tpcb, nullptr); + tcp_poll(tpcb, nullptr, 0); + tcp_err(tpcb, nullptr); + tcp_recv(tpcb, nullptr); + state->result = tcp_close(tpcb); + if (state->result == ERR_OK) { state->closeTimer = millis(); } From 7815bdc7c0ec932573f3a764a9e9131e174355d2 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Tue, 1 Dec 2026 03:18:31 -0600 Subject: [PATCH 3/5] Update RF24Client.cpp Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- RF24Client.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/RF24Client.cpp b/RF24Client.cpp index 3be7f26..809c27a 100644 --- a/RF24Client.cpp +++ b/RF24Client.cpp @@ -396,7 +396,6 @@ err_t RF24Client::serverTimeouts(void* arg, struct tcp_pcb* tpcb) tcp_poll(tpcb, nullptr, 0); tcp_err(tpcb, nullptr); tcp_recv(tpcb, nullptr); - state->result = tcp_close(tpcb); state->closeTimer = millis(); dataSize[state->stateActiveID] = 0; From dee7974b2db847da825931344c301b5c2ec90fd2 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Tue, 1 Dec 2026 04:51:43 -0600 Subject: [PATCH 4/5] Fixes for last commits Fix what broke in the last commits --- RF24Client.cpp | 258 ++++++++++++++++++++++++++++++------------------- RF24Client.h | 9 +- 2 files changed, 164 insertions(+), 103 deletions(-) diff --git a/RF24Client.cpp b/RF24Client.cpp index 809c27a..7ccecf3 100644 --- a/RF24Client.cpp +++ b/RF24Client.cpp @@ -59,6 +59,7 @@ uip_userdata_t RF24Client::all_data[UIP_CONNS]; #include "lwip/include/lwip/tcp.h" #include "lwip/include/lwip/tcpip.h" #endif + #include #include "RF24Ethernet.h" /** \cond */ @@ -71,6 +72,22 @@ uint32_t RF24Client::serverConnectionTimeout; uint32_t RF24Client::simpleCounter; bool RF24Client::activeState; int32_t RF24Client::accepts; +RF24Client::ConnectState* RF24Client::waitingHead; + +/***************************************************************************************************/ + +void RF24Client::removeWaiting(ConnectState* state) +{ + ConnectState** link = &waitingHead; + while (*link != nullptr) { + if (*link == state) { + *link = state->nextWaiting; + break; + } + link = &(*link)->nextWaiting; + } + state->nextWaiting = nullptr; +} /***************************************************************************************************/ @@ -161,7 +178,6 @@ err_t RF24Client::blocking_write(struct tcp_pcb* fpcb, ConnectState* fstate, con return err; } - // ERR_MEM: lwIP's send queue is full. That is not an error: send what is queued and wait tcp_output(fpcb); #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING @@ -200,6 +216,13 @@ void RF24Client::error_callback(void* arg, err_t err) { ConnectState* state = (ConnectState*)arg; + if (state != nullptr && state->waiting) { + IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: Err cb on waiting ID: "); Serial.println(state->identifier);); + removeWaiting(state); + accepts--; + delete state; + return; + } if (state != nullptr) { state->result = err; state->connected = false; @@ -223,10 +246,21 @@ err_t RF24Client::srecv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p } if (p == nullptr) { + if (state != nullptr && state->waiting) { + removeWaiting(state); + accepts--; + delete state; + state = nullptr; + if (tpcb != nullptr) { + tcp_backlog_accepted(tpcb); + } + } if (state != nullptr) { state->connected = false; } if (tpcb != nullptr) { + tcp_arg(tpcb, nullptr); + tcp_poll(tpcb, nullptr, 0); if (tcp_close(tpcb) != ERR_OK) { if (myPcb == tpcb) { myPcb = nullptr; @@ -248,8 +282,6 @@ err_t RF24Client::srecv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p return ERR_OK; } - // Waiting connections would share the spare buffer and could mix up their requests. lwIP - // keeps refused data and offers it again once this connection is the active one. if (tpcb != myPcb) { return ERR_MEM; } @@ -389,30 +421,21 @@ err_t RF24Client::serverTimeouts(void* arg, struct tcp_pcb* tpcb) //if (tpcb->state == ESTABLISHED || tpcb->state == SYN_SENT || tpcb->state == SYN_RCVD) { IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println("Server: Closed Server PCB TIMEOUT ");); - // Detach first: its late callbacks must not touch a slot a newer connection may use, - // and tcp_close() frees the pcb at once if unread data is left - state->connected = false; - tcp_arg(tpcb, nullptr); - tcp_poll(tpcb, nullptr, 0); - tcp_err(tpcb, nullptr); - tcp_recv(tpcb, nullptr); state->result = tcp_close(tpcb); state->closeTimer = millis(); dataSize[state->stateActiveID] = 0; + state->connected = false; + if (myPcb == tpcb) { + myPcb = nullptr; + } + tcp_arg(tpcb, nullptr); + tcp_poll(tpcb, nullptr, 0); if (state->result != ERR_OK) { - if (myPcb == tpcb) { - tcp_arg(myPcb, nullptr); - myPcb = nullptr; - } - tcp_arg(tpcb, nullptr); tcp_abort(tpcb); tpcb = nullptr; return ERR_ABRT; } - if (myPcb == tpcb) { - myPcb = nullptr; - } - return state->result; + return ERR_OK; // } } @@ -428,64 +451,61 @@ err_t RF24Client::closed_port(void* arg, struct tcp_pcb* tpcb) ConnectState* state = (ConnectState*)arg; - if (state != nullptr) { - IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: Client Poll Cb ID: "); Serial.println(state->identifier)); + if (state == nullptr || tpcb == nullptr) { + return ERR_OK; } - if (myPcb == nullptr) { - if (state != nullptr && tpcb != nullptr) { + IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: Client Poll Cb ID: "); Serial.print(state->identifier); Serial.print(" pcb state: "); + Serial.println((int)tpcb->state);); - if ((tpcb->state == ESTABLISHED || tpcb->state == SYN_SENT || tpcb->state == SYN_RCVD)) { - if (state->backlogWasAccepted == false) { + if (!state->waiting) { + tcp_poll(tpcb, nullptr, 0); + return ERR_OK; + } - state->backlogWasAccepted = true; - state->connectTimestamp = millis(); - state->connected = true; - accepts--; - myPcb = tpcb; - IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: ACCEPT delayed PCB "); Serial.println(state->identifier);); + if (myPcb == nullptr && state == waitingHead && (tpcb->state == ESTABLISHED || tpcb->state == SYN_RCVD)) { - tcp_backlog_accepted(tpcb); - activeState = state->stateActiveID; - return ERR_OK; - } - } - } - } + const bool slot = !activeState; + ConnectState* active = gState[slot]; + active->identifier = state->identifier; + active->stateActiveID = slot; + active->sConnectionTimeout = state->sConnectionTimeout; + active->backlogWasAccepted = true; + active->connected = true; + active->result = ERR_OK; + active->connectTimestamp = millis(); + active->serverTimer = millis(); + active->waiting = false; + active->nextWaiting = nullptr; - if (tpcb != nullptr) { - if (state != nullptr) { - if (millis() - state->connectTimestamp > state->sConnectionTimeout * 2) { + removeWaiting(state); + delete state; + accepts--; - if ((tpcb->state == ESTABLISHED || tpcb->state == SYN_SENT || tpcb->state == SYN_RCVD)) { + dataSize[slot] = 0; + myPcb = tpcb; + activeState = slot; + tcp_arg(tpcb, active); + tcp_backlog_accepted(tpcb); + tcp_poll(tpcb, serverTimeouts, 8); + IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: ACCEPT delayed PCB "); Serial.println(active->identifier);); + return ERR_OK; + } - IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: Close off delayed PCB function 1, ID: "); Serial.println(state->identifier);); + if (millis() - state->connectTimestamp > state->sConnectionTimeout * 2) { - if (state->backlogWasAccepted == false) { - IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println("Server: With backlog accepted");); - tcp_backlog_accepted(tpcb); - state->backlogWasAccepted = true; - accepts--; - } - state->connected = false; - tcp_arg(tpcb, nullptr); - tcp_poll(tpcb, nullptr, 0); - tcp_err(tpcb, nullptr); - tcp_recv(tpcb, nullptr); - state->result = tcp_close(tpcb); - - if (state->result == ERR_OK) { - state->closeTimer = millis(); - } - else { - tcp_abort(tpcb); - tpcb = nullptr; - return ERR_ABRT; - } + IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: Close off delayed PCB function 1, ID: "); Serial.println(state->identifier);); - return state->result; - } - } + removeWaiting(state); + delete state; + accepts--; + + tcp_arg(tpcb, nullptr); + tcp_poll(tpcb, nullptr, 0); + tcp_backlog_accepted(tpcb); + if (tcp_close(tpcb) != ERR_OK) { + tcp_abort(tpcb); + return ERR_ABRT; } } @@ -506,27 +526,49 @@ err_t RF24Client::accept(void* arg, struct tcp_pcb* tpcb, err_t err) if (tpcb != nullptr) { IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: Client connect from: "); Serial.println(ip4addr_ntoa(ip_2_ip4(&tpcb->remote_ip)));); } - bool actState = activeState; - if (myPcb != nullptr) { + ConnectState* waitState = new (std::nothrow) ConnectState; + if (waitState == nullptr) { + IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println("Server: No memory for waiting connection");); + tcp_arg(tpcb, nullptr); + tcp_abort(tpcb); + return ERR_ABRT; + } + IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: Accept w/already connected: Accepted_Conns - Delayed_Conns == "); Serial.println(accepts);); tcp_backlog_delayed(tpcb); accepts++; + + simpleCounter += 1; + waitState->waiting = true; + waitState->identifier = simpleCounter; + waitState->sConnectionTimeout = serverConnectionTimeout; + waitState->connectTimestamp = millis(); + waitState->serverTimer = millis(); + + ConnectState** link = &waitingHead; + while (*link != nullptr) { + link = &(*link)->nextWaiting; + } + *link = waitState; + + tcp_arg(tpcb, waitState); + tcp_recv(tpcb, srecv_callback); + tcp_sent(tpcb, sent_callback); + tcp_err(tpcb, error_callback); tcp_poll(tpcb, closed_port, 5); - actState = !activeState; - gState[actState]->connected = false; - gState[actState]->backlogWasAccepted = false; - } - else { - myPcb = tpcb; - tcp_poll(tpcb, serverTimeouts, 8); - activeState = !activeState; - actState = activeState; - gState[actState]->connected = true; - gState[actState]->backlogWasAccepted = true; + return ERR_OK; } + myPcb = tpcb; + tcp_poll(tpcb, serverTimeouts, 8); + activeState = !activeState; + const bool actState = activeState; + gState[actState]->connected = true; + gState[actState]->backlogWasAccepted = true; + gState[actState]->waiting = false; + dataSize[actState] = 0; simpleCounter += 1; @@ -627,12 +669,24 @@ RF24Client::RF24Client(uint32_t data) : data(0) #endif /*************************************************************/ +#if USE_LWIP == 1 +bool RF24Client::ownsConnection() const +{ + return gState[activeState] != nullptr && gState[activeState]->identifier == connectionId; +} +#endif + +/*************************************************************/ + uint8_t RF24Client::connected() { #if USE_LWIP < 1 return (data && (data->packets_in != 0 || (data->state & UIP_CLIENT_CONNECTED))) ? 1 : 0; #elif USE_LWIP == 1 - if (ownsConnection()) { + if (!ownsConnection()) { + return 0; + } + if (gState[activeState] != nullptr) { return gState[activeState]->connected || dataSize[activeState] > 0; } return 0; @@ -807,7 +861,8 @@ int RF24Client::connect(IPAddress ip, uint16_t port) memset(incomingData[activeState], 0, INCOMING_DATA_SIZE); gState[activeState]->connected = false; gState[activeState]->result = 0; - gState[activeState]->identifier = ++simpleCounter; + simpleCounter += 1; + gState[activeState]->identifier = simpleCounter; connectionId = simpleCounter; tcp_arg(myPcb, gState[activeState]); tcp_err(myPcb, error_callback); @@ -1068,11 +1123,10 @@ void RF24Client::stop() #if USE_LWIP == 1 void RF24Client::_stop() { - - tcp_pcb* const stopping = myPcb; // Ethernet.update() below can close it or make another one active + const uint32_t stoppingId = gState[activeState]->identifier; const uint32_t drainStart = millis(); - while (myPcb != nullptr && myPcb == stopping && gState[activeState]->connected && (myPcb->unsent != nullptr || myPcb->unacked != nullptr)) { + while (myPcb != nullptr && gState[activeState]->connected && (myPcb->unsent != nullptr || myPcb->unacked != nullptr)) { if (millis() - drainStart > 5000) { break; } @@ -1090,7 +1144,7 @@ void RF24Client::_stop() Ethernet.update(); } - if (myPcb != stopping) { + if (gState[activeState]->identifier != stoppingId) { return; } @@ -1149,7 +1203,10 @@ RF24Client::operator bool() #if USE_LWIP < 1 return data && (!(data->state & UIP_CLIENT_REMOTECLOSED) || data->packets_in != 0); #elif USE_LWIP == 1 - return ownsConnection() && dataSize[activeState] > 0; + if (!ownsConnection()) { + return false; + } + return dataSize[activeState] > 0 ? true : false; #elif USE_LWIP == 2 return available(); @@ -1250,25 +1307,28 @@ size_t RF24Client::_write(uint8_t* data, const uint8_t* buf, size_t size) #elif USE_LWIP == 1 bool initialActiveState = activeState; - // blocking_write() and Ethernet.update() can close this connection and make another one active - tcp_pcb* const writing = myPcb; size_t chunk = (MAX_PAYLOAD_SIZE - 14) < TCP_SND_BUF ? (MAX_PAYLOAD_SIZE - 14) : TCP_SND_BUF; // Max chunk size is equal to RF24Network MAX_PAYLOAD_SIZE capped by TCP_SND_BUF size size_t position = 0; + const uint32_t writingId = gState[initialActiveState]->identifier; + auto stillWriting = [&]() { + return myPcb != nullptr && activeState == initialActiveState && gState[initialActiveState]->identifier == writingId; + }; + uint32_t timeout = millis(); while (size > chunk) { if (millis() - timeout > 3000) { break; } - if (writing == nullptr || myPcb != writing) + if (!stillWriting()) return 0; err_t write_err = blocking_write(myPcb, gState[initialActiveState], reinterpret_cast(&buf[position]), chunk); if (write_err != ERR_OK) { - gState[initialActiveState]->result = write_err; - gState[initialActiveState]->connected = false; - if (myPcb == writing) { + if (stillWriting()) { + gState[initialActiveState]->result = write_err; + gState[initialActiveState]->connected = false; _stop(); } return 0; @@ -1279,15 +1339,15 @@ size_t RF24Client::_write(uint8_t* data, const uint8_t* buf, size_t size) Ethernet.update(); } - if (writing == nullptr || myPcb != writing) + if (!stillWriting()) return 0; err_t write_err = blocking_write(myPcb, gState[initialActiveState], reinterpret_cast(&buf[position]), size); if (write_err != ERR_OK) { - gState[initialActiveState]->result = write_err; - gState[initialActiveState]->connected = false; - if (myPcb == writing) { + if (stillWriting()) { + gState[initialActiveState]->result = write_err; + gState[initialActiveState]->connected = false; _stop(); } return 0; @@ -1618,9 +1678,7 @@ int RF24Client::_available(uint8_t* data) return 0; // First: is there readable/hup state? - struct zsock_pollfd pfd - { - }; + struct zsock_pollfd pfd {}; pfd.fd = self->_socket; pfd.events = ZSOCK_POLLIN; int pr = zsock_poll(&pfd, 1, 0); diff --git a/RF24Client.h b/RF24Client.h index bfd70d1..a223467 100644 --- a/RF24Client.h +++ b/RF24Client.h @@ -217,6 +217,8 @@ class RF24Client : public Client volatile uint32_t identifier = 0; volatile bool connected = false; + bool waiting = false; + ConnectState* nextWaiting = nullptr; } __attribute__((packed)); @@ -259,10 +261,9 @@ class RF24Client : public Client RF24Client(uint32_t data); RF24Client(uint8_t data); uint8_t* data; - // The ConnectState::identifier of this client's connection. If the active connection is a - // different one (it closed and a waiting one took over), this client must not touch it. + uint32_t connectionId = 0; - bool ownsConnection() const { return gState[activeState] != nullptr && gState[activeState]->identifier == connectionId; } + bool ownsConnection() const; static size_t _write(uint8_t* data, const uint8_t* buf, size_t size); static int _available(uint8_t* data); @@ -285,6 +286,8 @@ class RF24Client : public Client static char* incomingData[2]; static uint32_t simpleCounter; static int32_t accepts; + static ConnectState* waitingHead; + static void removeWaiting(ConnectState* state); #elif USE_LWIP == 2 RF24Client(uint32_t data); From 5199d3acea307a1fd86b9bca3e43b4db45f3def9 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Tue, 1 Dec 2026 05:51:05 -0600 Subject: [PATCH 5/5] More fixes for last commits --- RF24Client.cpp | 5 +++++ RF24Client.h | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/RF24Client.cpp b/RF24Client.cpp index 7ccecf3..5d676b3 100644 --- a/RF24Client.cpp +++ b/RF24Client.cpp @@ -492,6 +492,11 @@ err_t RF24Client::closed_port(void* arg, struct tcp_pcb* tpcb) return ERR_OK; } + if (tpcb->refused_data != nullptr) { + tcp_set_flags(tpcb, TF_ACK_NOW); + tcp_output(tpcb); + } + if (millis() - state->connectTimestamp > state->sConnectionTimeout * 2) { IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: Close off delayed PCB function 1, ID: "); Serial.println(state->identifier);); diff --git a/RF24Client.h b/RF24Client.h index a223467..b8f4f2e 100644 --- a/RF24Client.h +++ b/RF24Client.h @@ -202,6 +202,8 @@ class RF24Client : public Client */ struct ConnectState { + ConnectState* nextWaiting = nullptr; + bool waiting = false; volatile bool backlogWasAccepted = false; volatile bool clientPollingSetup = 0; @@ -217,8 +219,6 @@ class RF24Client : public Client volatile uint32_t identifier = 0; volatile bool connected = false; - bool waiting = false; - ConnectState* nextWaiting = nullptr; } __attribute__((packed));