Skip to content

Commit

Permalink
Optimizations: remove multiple calls to the status() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathertel authored and facchinm committed May 28, 2015
1 parent d92bf5b commit a9cdd44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
11 changes: 8 additions & 3 deletions libraries/Ethernet/src/EthernetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions libraries/Ethernet/src/EthernetServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down

0 comments on commit a9cdd44

Please sign in to comment.