mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-30 03:26:13 +01:00
simplify WifiESP32
This commit is contained in:
parent
041a6534da
commit
e3bab887a2
|
@ -74,12 +74,21 @@ class NetworkClient {
|
||||||
public:
|
public:
|
||||||
NetworkClient(WiFiClient c) {
|
NetworkClient(WiFiClient c) {
|
||||||
wifi = c;
|
wifi = c;
|
||||||
|
inUse = true;
|
||||||
};
|
};
|
||||||
bool ok() {
|
bool active(byte clientId) {
|
||||||
return (inUse && wifi.connected());
|
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) {
|
bool recycle(WiFiClient c) {
|
||||||
|
|
||||||
if (wifi == c) {
|
if (wifi == c) {
|
||||||
if (inUse == true)
|
if (inUse == true)
|
||||||
DIAG(F("WARNING: Duplicate"));
|
DIAG(F("WARNING: Duplicate"));
|
||||||
|
@ -88,10 +97,16 @@ public:
|
||||||
inUse = true;
|
inUse = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (inUse == false) {
|
||||||
|
wifi = c;
|
||||||
|
inUse = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
bool inUse = true;
|
private:
|
||||||
|
bool inUse;
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::vector<NetworkClient> clients; // a list to hold all clients
|
static std::vector<NetworkClient> 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?
|
// really no good way to check for LISTEN especially in AP mode?
|
||||||
wl_status_t wlStatus;
|
wl_status_t wlStatus;
|
||||||
if (APmode || (wlStatus = WiFi.status()) == WL_CONNECTED) {
|
if (APmode || (wlStatus = WiFi.status()) == WL_CONNECTED) {
|
||||||
// loop over all clients and remove inactive
|
|
||||||
for (clientId=0; clientId<clients.size(); clientId++){
|
|
||||||
// check if client is there and alive
|
|
||||||
if(clients[clientId].inUse && !clients[clientId].wifi.connected()) {
|
|
||||||
DIAG(F("Remove client %d"), clientId);
|
|
||||||
CommandDistributor::forget(clientId);
|
|
||||||
clients[clientId].wifi.stop();
|
|
||||||
clients[clientId].inUse = false;
|
|
||||||
//Do NOT clients.erase(clients.begin()+clientId) as
|
|
||||||
//that would mix up clientIds for later.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (server->hasClient()) {
|
if (server->hasClient()) {
|
||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
while (client = server->available()) {
|
while (client = server->available()) {
|
||||||
|
@ -310,8 +313,9 @@ void WifiESP::loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// loop over all connected clients
|
// loop over all connected clients
|
||||||
|
// this removes as a side effect inactive clients when checking ::active()
|
||||||
for (clientId=0; clientId<clients.size(); clientId++){
|
for (clientId=0; clientId<clients.size(); clientId++){
|
||||||
if(clients[clientId].ok()) {
|
if(clients[clientId].active(clientId)) {
|
||||||
int len;
|
int len;
|
||||||
if ((len = clients[clientId].wifi.available()) > 0) {
|
if ((len = clients[clientId].wifi.available()) > 0) {
|
||||||
// read data from client
|
// 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 filled, end with '\0' so we can use it as C string
|
||||||
buffer[count]='\0';
|
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)
|
if (Diag::CMD || Diag::WITHROTTLE)
|
||||||
DIAG(F("SEND %d:%s"), clientId, buffer);
|
DIAG(F("SEND %d:%s"), clientId, buffer);
|
||||||
clients[clientId].wifi.write(buffer,count);
|
clients[clientId].wifi.write(buffer,count);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user