diff --git a/RF24Client.cpp b/RF24Client.cpp index 7463328..5d676b3 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,6 +282,10 @@ err_t RF24Client::srecv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p return ERR_OK; } + if (tpcb != myPcb) { + return ERR_MEM; + } + bool id = state->stateActiveID; IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print("Server: Copy data to "); Serial.println(state->stateActiveID);); @@ -385,20 +423,19 @@ err_t RF24Client::serverTimeouts(void* arg, struct tcp_pcb* tpcb) state->result = tcp_close(tpcb); state->closeTimer = millis(); - dataSize[activeState] = 0; + 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; } - myPcb = nullptr; - return state->result; + return ERR_OK; // } } @@ -414,60 +451,66 @@ 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 (tpcb->refused_data != nullptr) { + tcp_set_flags(tpcb, TF_ACK_NOW); + tcp_output(tpcb); + } - if (state->backlogWasAccepted == false) { - IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println("Server: With backlog accepted");); - tcp_backlog_accepted(tpcb); - state->backlogWasAccepted = true; - accepts--; - } + if (millis() - state->connectTimestamp > state->sConnectionTimeout * 2) { - state->result = tcp_close(tpcb); - state->connected = false; - 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; } } @@ -488,27 +531,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; @@ -599,16 +664,33 @@ 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 /*************************************************************/ +#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()) { + return 0; + } if (gState[activeState] != nullptr) { return gState[activeState]->connected || dataSize[activeState] > 0; } @@ -784,6 +866,9 @@ int RF24Client::connect(IPAddress ip, uint16_t port) memset(incomingData[activeState], 0, INCOMING_DATA_SIZE); gState[activeState]->connected = false; gState[activeState]->result = 0; + simpleCounter += 1; + gState[activeState]->identifier = simpleCounter; + connectionId = simpleCounter; tcp_arg(myPcb, gState[activeState]); tcp_err(myPcb, error_callback); tcp_recv(myPcb, recv_callback); @@ -1025,7 +1110,9 @@ void RF24Client::stop() RF24Ethernet.update(); #elif USE_LWIP == 1 - _stop(); + if (ownsConnection()) { + _stop(); + } #elif USE_LWIP == 2 if (_socket >= 0) { @@ -1041,6 +1128,7 @@ void RF24Client::stop() #if USE_LWIP == 1 void RF24Client::_stop() { + const uint32_t stoppingId = gState[activeState]->identifier; const uint32_t drainStart = millis(); while (myPcb != nullptr && gState[activeState]->connected && (myPcb->unsent != nullptr || myPcb->unacked != nullptr)) { @@ -1061,6 +1149,10 @@ void RF24Client::_stop() Ethernet.update(); } + if (gState[activeState]->identifier != stoppingId) { + return; + } + tcp_pcb* pcb = myPcb; myPcb = nullptr; @@ -1116,6 +1208,9 @@ RF24Client::operator bool() #if USE_LWIP < 1 return data && (!(data->state & UIP_CLIENT_REMOTECLOSED) || data->packets_in != 0); #elif USE_LWIP == 1 + if (!ownsConnection()) { + return false; + } return dataSize[activeState] > 0 ? true : false; #elif USE_LWIP == 2 @@ -1127,6 +1222,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 +1234,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); } @@ -1210,20 +1315,27 @@ size_t RF24Client::_write(uint8_t* data, const uint8_t* buf, size_t size) 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 (myPcb == nullptr) + 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; - _stop(); + if (stillWriting()) { + gState[initialActiveState]->result = write_err; + gState[initialActiveState]->connected = false; + _stop(); + } return 0; } position += chunk; @@ -1232,15 +1344,17 @@ size_t RF24Client::_write(uint8_t* data, const uint8_t* buf, size_t size) Ethernet.update(); } - if (myPcb == nullptr) + 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; - _stop(); + if (stillWriting()) { + gState[initialActiveState]->result = write_err; + gState[initialActiveState]->connected = false; + _stop(); + } return 0; } @@ -1538,6 +1652,10 @@ int RF24Client::available() { return _available(data); } +#elif USE_LWIP == 1 + if (ownsConnection()) { + return _available(data); + } #else return _available(data); #endif @@ -1565,9 +1683,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); @@ -1757,7 +1873,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..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; @@ -259,6 +261,9 @@ class RF24Client : public Client RF24Client(uint32_t data); RF24Client(uint8_t data); uint8_t* data; + + uint32_t connectionId = 0; + bool ownsConnection() const; static size_t _write(uint8_t* data, const uint8_t* buf, size_t size); static int _available(uint8_t* data); @@ -281,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);