From ea6d07cb79394d60b80b1c96933d879509c08598 Mon Sep 17 00:00:00 2001 From: Marcelo Aquino Date: Sat, 24 Mar 2018 04:16:44 -0300 Subject: [PATCH] Linux: Fix stability problem with ethernet (#1091) - Fix a problem where mysgw stops working and consumes a lot of cpu. - Fix bug with tcp sockets where socket descriptor wasn't being freed. --- drivers/Linux/EthernetClient.cpp | 9 +++------ drivers/Linux/EthernetServer.cpp | 4 +--- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/drivers/Linux/EthernetClient.cpp b/drivers/Linux/EthernetClient.cpp index c5686e8..446e717 100644 --- a/drivers/Linux/EthernetClient.cpp +++ b/drivers/Linux/EthernetClient.cpp @@ -220,9 +220,8 @@ void EthernetClient::stop() gettimeofday(&startTime, NULL); // wait up to a second for the connection to close - uint8_t s; do { - s = status(); + uint8_t s = status(); if (s == ETHERNETCLIENT_W5100_CLOSED) { break; // exit the loop } @@ -231,10 +230,8 @@ void EthernetClient::stop() } while (((curTime.tv_sec - startTime.tv_sec) * 1000000) + (curTime.tv_usec - startTime.tv_usec) < 1000000); - // if it hasn't closed, close it forcefully - if (s != ETHERNETCLIENT_W5100_CLOSED) { - ::close(_sock); - } + // free up the socket descriptor + ::close(_sock); _sock = -1; } diff --git a/drivers/Linux/EthernetServer.cpp b/drivers/Linux/EthernetServer.cpp index 9bbeb0e..58d0b5c 100644 --- a/drivers/Linux/EthernetServer.cpp +++ b/drivers/Linux/EthernetServer.cpp @@ -155,13 +155,11 @@ size_t EthernetServer::write(uint8_t b) size_t EthernetServer::write(const uint8_t *buffer, size_t size) { size_t n = 0; - size_t i = 0; - while (i < clients.size()) { + for (size_t i = 0; i < clients.size(); ++i) { EthernetClient client(clients[i]); if (client.status() == ETHERNETCLIENT_W5100_ESTABLISHED) { n += client.write(buffer, size); - i++; } }