From a9cdd44d27a87a424e628a38f80a54504dea645c Mon Sep 17 00:00:00 2001 From: Matthias Hertel Date: Sat, 16 May 2015 20:30:59 +0200 Subject: [PATCH] Optimizations: remove multiple calls to the status() function. --- libraries/Ethernet/src/EthernetClient.cpp | 11 ++++++++--- libraries/Ethernet/src/EthernetServer.cpp | 13 +++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/libraries/Ethernet/src/EthernetClient.cpp b/libraries/Ethernet/src/EthernetClient.cpp index a592bfdc956..1feed4c424a 100644 --- a/libraries/Ethernet/src/EthernetClient.cpp +++ b/libraries/Ethernet/src/EthernetClient.cpp @@ -131,12 +131,17 @@ void EthernetClient::stop() { disconnect(_sock); unsigned long start = millis(); - // wait a second for the connection to close - while (status() != SnSR::CLOSED && millis() - start < 1000) + // wait up to a second for the connection to close + uint8_t s; + do { + s = status(); + if (s == SnSR::CLOSED) + break; // exit the loop delay(1); + } while (millis() - start < 1000); // if it hasn't closed, close it forcefully - if (status() != SnSR::CLOSED) + if (s != SnSR::CLOSED) close(_sock); EthernetClass::_server_port[_sock] = 0; diff --git a/libraries/Ethernet/src/EthernetServer.cpp b/libraries/Ethernet/src/EthernetServer.cpp index 6d6ce8c8027..cfa813eb7be 100644 --- a/libraries/Ethernet/src/EthernetServer.cpp +++ b/libraries/Ethernet/src/EthernetServer.cpp @@ -54,12 +54,13 @@ EthernetClient EthernetServer::available() for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { EthernetClient client(sock); - if (EthernetClass::_server_port[sock] == _port && - (client.status() == SnSR::ESTABLISHED || - client.status() == SnSR::CLOSE_WAIT)) { - if (client.available()) { - // XXX: don't always pick the lowest numbered socket. - return client; + if (EthernetClass::_server_port[sock] == _port) { + uint8_t s = client.status(); + if (s == SnSR::ESTABLISHED || s == SnSR::CLOSE_WAIT) { + if (client.available()) { + // XXX: don't always pick the lowest numbered socket. + return client; + } } } }