From c115c441e41f2a797eceab0dadaf6cfab03c17d0 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 7 Aug 2022 01:24:41 +0200 Subject: [PATCH] ESP32 introduce NetworkClient class to hold state associated to WiFiClient --- GITHUB_SHA.h | 2 +- WifiESP32.cpp | 107 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 72 insertions(+), 37 deletions(-) diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index 9ea001a..16bd176 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "PORTX-HAL-20220806" +#define GITHUB_SHA "PORTX-HAL-20220807" diff --git a/WifiESP32.cpp b/WifiESP32.cpp index 917785e..3f9f7e5 100644 --- a/WifiESP32.cpp +++ b/WifiESP32.cpp @@ -67,7 +67,25 @@ void disableCoreWDT(byte core){ } */ -static std::vector clients; // a list to hold all clients +class NetworkClient { +public: + NetworkClient(WiFiClient c) { + wifi = c; + }; + bool ok() { + return (inUse && wifi.connected()); + }; + bool recycle(WiFiClient c) { + if (inUse == true) return false; + wifi = c; + inUse = true; + return true; + }; + WiFiClient wifi; + bool inUse = true; +}; + +static std::vector clients; // a list to hold all clients static WiFiServer *server = NULL; static RingStream *outboundRing = new RingStream(2048); static bool APmode = false; @@ -197,29 +215,40 @@ void WifiESP::loop() { // loop over all clients and remove inactive for (clientId=0; clientIdhasClient()) { WiFiClient client; while (client = server->available()) { - clients.push_back(client); - DIAG(F("New client %s"), client.remoteIP().toString().c_str()); + for (clientId=0; clientId=clients.size()) { + NetworkClient nc(client); + clients.push_back(nc); + DIAG(F("New client %d, %s"), clientId, client.remoteIP().toString().c_str()); + } } } // loop over all connected clients for (clientId=0; clientId 0) { + if ((len = clients[clientId].wifi.available()) > 0) { // read data from client byte cmd[len+1]; for(int i=0; imark(clientId); @@ -238,35 +267,41 @@ void WifiESP::loop() { // something is wrong with the ringbuffer position // or client has disconnected outboundRing->info(); - // try to recover by reading out to nowhere - int count=outboundRing->count(); - for(int i=0;iread(); - if (c < 0) { - DIAG(F("Ringread fail at %d"),i); - break; - } - } - outboundRing->info(); - } else { - // we have data to send in outboundRing - if(clients[clientId].connected()) { - outboundRing->read(); // read over peek() + if ((unsigned int)clientId < 8) { + // try to recover by reading out to nowhere int count=outboundRing->count(); - { - char buffer[count+1]; - for(int i=0;iread(); - if (c >= 0) - buffer[i] = (char)c; - else { - DIAG(F("Ringread fail at %d"),i); - break; - } + for(int i=0;iread(); + if (c < 0) { + DIAG(F("Ringread fail at %d"),i); + break; } - buffer[count]=0; - clients[clientId].write(buffer,count); } + outboundRing->info(); + } + DIAG(F("Ring beyond rescue")); + } else { + // We have data to send in outboundRing + // and we have a valid clientId. + // First read it out to buffer + // and then look if it can be sent because + // we can not leave it in the ring for ever + outboundRing->read(); // read over peek() + int count=outboundRing->count(); + { + char buffer[count+1]; + for(int i=0;iread(); + if (c >= 0) + buffer[i] = (char)c; + else { + DIAG(F("Ringread fail at %d"),i); + break; + } + } + buffer[count]=0; + if(clients[clientId].ok()) + clients[clientId].wifi.write(buffer,count); } } }