diff --git a/libraries/Ethernet/src/EthernetClient.h b/libraries/Ethernet/src/EthernetClient.h index dbfa0012d..07e997582 100644 --- a/libraries/Ethernet/src/EthernetClient.h +++ b/libraries/Ethernet/src/EthernetClient.h @@ -8,6 +8,7 @@ class EthernetClient : public lwipClient { public: EthernetClient() {} EthernetClient(struct tcp_struct *tcpClient) : lwipClient(tcpClient) {} + EthernetClient(lwipClient& client) : lwipClient(client) {} }; #endif \ No newline at end of file diff --git a/libraries/Ethernet/src/EthernetServer.h b/libraries/Ethernet/src/EthernetServer.h index 2afb5060c..71f75abed 100644 --- a/libraries/Ethernet/src/EthernetServer.h +++ b/libraries/Ethernet/src/EthernetServer.h @@ -12,24 +12,8 @@ class EthernetServer : public lwipServer { EthernetServer(uint16_t port) : lwipServer(port) {} EthernetClient available() { - accept(); - - for (int n = 0; n < MAX_CLIENT; n++) { - if (_tcp_client[n] != NULL) { - if (_tcp_client[n]->pcb != NULL) { - EthernetClient client(_tcp_client[n]); - uint8_t s = client.status(); - if (s == TCP_ACCEPTED) { - if (client.available()) { - return client; - } - } - } - } - } - - struct tcp_struct *default_client = NULL; - return EthernetClient(default_client); + lwipClient client = lwipServer::available(); + return EthernetClient(client); } }; diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h index 19c7e83d4..4f297ef4e 100644 --- a/libraries/WiFi/src/WiFiClient.h +++ b/libraries/WiFi/src/WiFiClient.h @@ -8,6 +8,7 @@ class WiFiClient : public lwipClient { public: WiFiClient() {} WiFiClient(struct tcp_struct *tcpClient) : lwipClient(tcpClient) {} + WiFiClient(lwipClient& client) : lwipClient(client) {} }; #endif diff --git a/libraries/WiFi/src/WiFiServer.h b/libraries/WiFi/src/WiFiServer.h index 1199814fd..eae24f349 100644 --- a/libraries/WiFi/src/WiFiServer.h +++ b/libraries/WiFi/src/WiFiServer.h @@ -11,24 +11,8 @@ class WiFiServer : public lwipServer { WiFiServer(uint16_t port) : lwipServer(port) {} WiFiClient available() { - accept(); - - for (int n = 0; n < MAX_CLIENT; n++) { - if (_tcp_client[n] != NULL) { - if (_tcp_client[n]->pcb != NULL) { - WiFiClient client(_tcp_client[n]); - uint8_t s = client.status(); - if (s == TCP_ACCEPTED) { - if (client.available()) { - return client; - } - } - } - } - } - - struct tcp_struct *default_client = NULL; - return WiFiClient(default_client); + lwipClient client = lwipServer::available(); + return WiFiClient(client); } }; diff --git a/libraries/lwIpWrapper/src/lwipClient.cpp b/libraries/lwIpWrapper/src/lwipClient.cpp index 8facb2525..ee7565890 100644 --- a/libraries/lwIpWrapper/src/lwipClient.cpp +++ b/libraries/lwIpWrapper/src/lwipClient.cpp @@ -6,9 +6,13 @@ extern "C" { #include "lwipClient.h" +static void memPoolDeleter(struct tcp_struct* tcpClient) +{ + mem_free(tcpClient); +} + /* -------------------------------------------------------------------------- */ lwipClient::lwipClient() - : _tcp_client(NULL) { } /* -------------------------------------------------------------------------- */ @@ -17,15 +21,14 @@ lwipClient::lwipClient() sketches but sock is ignored. */ /* -------------------------------------------------------------------------- */ lwipClient::lwipClient(uint8_t sock) - : _tcp_client(NULL) { } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ lwipClient::lwipClient(struct tcp_struct* tcpClient) + : _tcp_client(tcpClient, memPoolDeleter) { - _tcp_client = tcpClient; } /* -------------------------------------------------------------------------- */ @@ -49,7 +52,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port) /* -------------------------------------------------------------------------- */ if (_tcp_client == NULL) { /* Allocates memory for client */ - _tcp_client = (struct tcp_struct*)mem_malloc(sizeof(struct tcp_struct)); + _tcp_client.reset((struct tcp_struct*)mem_malloc(sizeof(struct tcp_struct)), memPoolDeleter); if (_tcp_client == NULL) { return 0; @@ -69,7 +72,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port) uint32_t startTime = millis(); ip_addr_t ipaddr; - tcp_arg(_tcp_client->pcb, _tcp_client); + tcp_arg(_tcp_client->pcb, _tcp_client.get()); if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) { stop(); return 0; @@ -215,7 +218,7 @@ void lwipClient::stop() // close tcp connection if not closed yet if (status() != TCP_CLOSING) { - tcp_connection_close(_tcp_client->pcb, _tcp_client); + tcp_connection_close(_tcp_client->pcb, _tcp_client.get()); } } @@ -243,7 +246,7 @@ uint8_t lwipClient::status() lwipClient::operator bool() { /* -------------------------------------------------------------------------- */ - return (_tcp_client && (_tcp_client->state != TCP_CLOSING)); + return (_tcp_client != nullptr); } /* -------------------------------------------------------------------------- */ diff --git a/libraries/lwIpWrapper/src/lwipClient.h b/libraries/lwIpWrapper/src/lwipClient.h index 511b3c7f1..0dc282d3c 100644 --- a/libraries/lwIpWrapper/src/lwipClient.h +++ b/libraries/lwIpWrapper/src/lwipClient.h @@ -9,6 +9,7 @@ #include "lwipMem.h" #include "lwipTcp.h" #include "lwipTypes.h" +#include class lwipClient : public Client { @@ -66,7 +67,7 @@ class lwipClient : public Client { using Print::write; private: - struct tcp_struct* _tcp_client; + std::shared_ptr _tcp_client; uint16_t _timeout = 10000; }; diff --git a/libraries/lwIpWrapper/src/lwipServer.cpp b/libraries/lwIpWrapper/src/lwipServer.cpp index be42b2a43..70cbf0e1e 100644 --- a/libraries/lwIpWrapper/src/lwipServer.cpp +++ b/libraries/lwIpWrapper/src/lwipServer.cpp @@ -9,9 +9,6 @@ extern "C" { lwipServer::lwipServer(uint16_t port) { _port = port; - for (int i = 0; i < MAX_CLIENT; i++) { - _tcp_client[i] = {}; - } _tcp_server = {}; } @@ -50,12 +47,9 @@ void lwipServer::accept() { /* Free client if disconnected */ for (int n = 0; n < MAX_CLIENT; n++) { - if (_tcp_client[n] != NULL) { - lwipClient client(_tcp_client[n]); - if (client.status() == TCP_CLOSING) { - mem_free(_tcp_client[n]); - _tcp_client[n] = NULL; - } + if (_tcp_client[n] && !_tcp_client[n].connected()) { + Serial.println("clear"); + _tcp_client[n] = lwipClient(); } } } @@ -65,21 +59,12 @@ lwipClient lwipServer::available() accept(); for (int n = 0; n < MAX_CLIENT; n++) { - if (_tcp_client[n] != NULL) { - if (_tcp_client[n]->pcb != NULL) { - lwipClient client(_tcp_client[n]); - uint8_t s = client.status(); - if (s == TCP_ACCEPTED) { - if (client.available()) { - return client; - } - } - } + if (_tcp_client[n].available()) { + return _tcp_client[n]; } } - struct tcp_struct* default_client = NULL; - return lwipClient(default_client); + return lwipClient(); } size_t lwipServer::write(uint8_t b) @@ -94,14 +79,8 @@ size_t lwipServer::write(const uint8_t* buffer, size_t size) accept(); for (int n = 0; n < MAX_CLIENT; n++) { - if (_tcp_client[n] != NULL) { - if (_tcp_client[n]->pcb != NULL) { - lwipClient client(_tcp_client[n]); - uint8_t s = client.status(); - if (s == TCP_ACCEPTED) { - n += client.write(buffer, size); - } - } + if (_tcp_client[n].status() == TCP_ACCEPTED) { + n += _tcp_client[n].write(buffer, size); } } diff --git a/libraries/lwIpWrapper/src/lwipServer.h b/libraries/lwIpWrapper/src/lwipServer.h index e7d4456b0..983a86ca2 100644 --- a/libraries/lwIpWrapper/src/lwipServer.h +++ b/libraries/lwIpWrapper/src/lwipServer.h @@ -10,7 +10,7 @@ class lwipServer : public Server { protected: uint16_t _port; struct tcp_struct _tcp_server; - struct tcp_struct* _tcp_client[MAX_CLIENT]; + lwipClient _tcp_client[MAX_CLIENT]; void accept(void); diff --git a/libraries/lwIpWrapper/src/lwipTcp.cpp b/libraries/lwIpWrapper/src/lwipTcp.cpp index 680262790..3b28d3b2e 100644 --- a/libraries/lwIpWrapper/src/lwipTcp.cpp +++ b/libraries/lwIpWrapper/src/lwipTcp.cpp @@ -1,4 +1,5 @@ #include "lwipTcp.h" +#include "lwipClient.h" #if LWIP_TCP static err_t tcp_recv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p, err_t err); @@ -53,7 +54,7 @@ err_t tcp_accept_callback(void* arg, struct tcp_pcb* newpcb, err_t err) { err_t ret_err; uint8_t accepted; - struct tcp_struct** tcpClient = (struct tcp_struct**)arg; + lwipClient* tcpClient = (lwipClient*)arg; /* set priority for the newly accepted tcp connection newpcb */ tcp_setprio(newpcb, TCP_PRIO_MIN); @@ -69,8 +70,8 @@ err_t tcp_accept_callback(void* arg, struct tcp_pcb* newpcb, err_t err) /* Looking for an empty socket */ for (uint16_t i = 0; i < MAX_CLIENT; i++) { - if (tcpClient[i] == NULL) { - tcpClient[i] = client; + if (!tcpClient[i]) { + tcpClient[i] = lwipClient(client); accepted = 1; break; }