From e3bab887a289b4fef5ecb14a336b8558fb31137c Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Mon, 25 Dec 2023 17:32:39 +0100 Subject: [PATCH] simplify WifiESP32 --- WifiESP32.cpp | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/WifiESP32.cpp b/WifiESP32.cpp index ab5274b..1103dfc 100644 --- a/WifiESP32.cpp +++ b/WifiESP32.cpp @@ -74,12 +74,21 @@ class NetworkClient { public: NetworkClient(WiFiClient c) { wifi = c; + inUse = true; }; - bool ok() { - return (inUse && wifi.connected()); - }; + bool active(byte clientId) { + if (!inUse) + return false; + if(!wifi.connected()) { + DIAG(F("Remove client %d"), clientId); + CommandDistributor::forget(clientId); + wifi.stop(); + inUse = false; + return false; + } + return true; + } bool recycle(WiFiClient c) { - if (wifi == c) { if (inUse == true) DIAG(F("WARNING: Duplicate")); @@ -88,10 +97,16 @@ public: inUse = true; return true; } + if (inUse == false) { + wifi = c; + inUse = true; + return true; + } return false; }; WiFiClient wifi; - bool inUse = true; +private: + bool inUse; }; static std::vector clients; // a list to hold all clients @@ -281,18 +296,6 @@ void WifiESP::loop() { // really no good way to check for LISTEN especially in AP mode? wl_status_t wlStatus; if (APmode || (wlStatus = WiFi.status()) == WL_CONNECTED) { - // loop over all clients and remove inactive - for (clientId=0; clientIdhasClient()) { WiFiClient client; while (client = server->available()) { @@ -310,8 +313,9 @@ void WifiESP::loop() { } } // loop over all connected clients + // this removes as a side effect inactive clients when checking ::active() for (clientId=0; clientId 0) { // read data from client @@ -349,7 +353,7 @@ void WifiESP::loop() { } // buffer filled, end with '\0' so we can use it as C string buffer[count]='\0'; - if((unsigned int)clientId <= clients.size() && clients[clientId].ok()) { + if((unsigned int)clientId <= clients.size() && clients[clientId].active(clientId)) { if (Diag::CMD || Diag::WITHROTTLE) DIAG(F("SEND %d:%s"), clientId, buffer); clients[clientId].wifi.write(buffer,count);