From 048ba3fd1e9891768b596333da8b4d9278e78a2a Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sat, 17 Aug 2024 20:18:59 +0200 Subject: [PATCH] replace socket.connected() with check for return value of socket.read() --- EthernetInterface.cpp | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/EthernetInterface.cpp b/EthernetInterface.cpp index 3e1a51a..e87316c 100644 --- a/EthernetInterface.cpp +++ b/EthernetInterface.cpp @@ -208,28 +208,24 @@ void EthernetInterface::loop2() { int count = clients[socket].read(buffer, MAX_ETH_BUFFER); looptimer(8000, F("Ethloop2 read")); if (count > 0) { - if (Diag::ETHERNET) DIAG(F("Ethernet: available socket=%d,count=%d"), socket, count); - buffer[count] = '\0'; // terminate the string properly - if (Diag::ETHERNET) DIAG(F("buffer:%e"), buffer); - // execute with data going directly back - CommandDistributor::parse(socket,buffer,outboundRing); - looptimer(2000, F("Ethloop2 parse")); - return; // limit the amount of processing that takes place within 1 loop() cycle. - } - } + if (Diag::ETHERNET) DIAG(F("Ethernet: available socket=%d,count=%d"), socket, count); + buffer[count] = '\0'; // terminate the string properly + if (Diag::ETHERNET) DIAG(F("buffer:%e"), buffer); + // execute with data going directly back + CommandDistributor::parse(socket,buffer,outboundRing); + looptimer(2000, F("Ethloop2 parse")); + return; // limit the amount of processing that takes place within 1 loop() cycle. + } else if (count == 0) { + // The client has disconnected + clients[socket].stop(); + CommandDistributor::forget(socket); + if (Diag::ETHERNET) DIAG(F("Ethernet: disconnect %d "), socket); + } + // fall through if count = -1 (no bytes available) + } } looptimer(8000, F("Ethloop2 after incoming")); - // stop any clients which disconnect - for (int socket = 0; socket