Skip to content

Commit a9cdd44

Browse files
mathertelfacchinm
authored andcommitted
Optimizations: remove multiple calls to the status() function.
1 parent d92bf5b commit a9cdd44

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

libraries/Ethernet/src/EthernetClient.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,17 @@ void EthernetClient::stop() {
131131
disconnect(_sock);
132132
unsigned long start = millis();
133133

134-
// wait a second for the connection to close
135-
while (status() != SnSR::CLOSED && millis() - start < 1000)
134+
// wait up to a second for the connection to close
135+
uint8_t s;
136+
do {
137+
s = status();
138+
if (s == SnSR::CLOSED)
139+
break; // exit the loop
136140
delay(1);
141+
} while (millis() - start < 1000);
137142

138143
// if it hasn't closed, close it forcefully
139-
if (status() != SnSR::CLOSED)
144+
if (s != SnSR::CLOSED)
140145
close(_sock);
141146

142147
EthernetClass::_server_port[_sock] = 0;

libraries/Ethernet/src/EthernetServer.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ EthernetClient EthernetServer::available()
5454

5555
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
5656
EthernetClient client(sock);
57-
if (EthernetClass::_server_port[sock] == _port &&
58-
(client.status() == SnSR::ESTABLISHED ||
59-
client.status() == SnSR::CLOSE_WAIT)) {
60-
if (client.available()) {
61-
// XXX: don't always pick the lowest numbered socket.
62-
return client;
57+
if (EthernetClass::_server_port[sock] == _port) {
58+
uint8_t s = client.status();
59+
if (s == SnSR::ESTABLISHED || s == SnSR::CLOSE_WAIT) {
60+
if (client.available()) {
61+
// XXX: don't always pick the lowest numbered socket.
62+
return client;
63+
}
6364
}
6465
}
6566
}

0 commit comments

Comments
 (0)