Skip to content

Commit 8b055de

Browse files
committed
implemented changes suggested by @matthijskooijman
- renaming _sock to _lastReturnedSocket - move increment line up The changes make that the clients returned by `EthernetServer::available()` are more evenly distributed over the 4 sockets of the w5100 chip. In the previous impelmenatation there was a problem if there is a client connected to socket number 0 and the client is permanently sending data. In that case `EthernetServer::available()` always returned that client, even if other clients are connected and have data available.
1 parent 95d8d8e commit 8b055de

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

libraries/Ethernet/src/EthernetServer.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,21 @@ EthernetClient EthernetServer::available()
5252
{
5353
accept();
5454

55-
// increment _sock to avoid returning the same socket again
56-
_sock = (_sock + 1) % MAX_SOCK_NUM;
57-
5855
for (int i = 0; i < MAX_SOCK_NUM; i++) {
59-
EthernetClient client(_sock);
60-
if (EthernetClass::_server_port[_sock] == _port) {
56+
// increment _lastReturnedSocket to avoid returning the same socket again
57+
_lastReturnedSocket = (_lastReturnedSocket + 1) % MAX_SOCK_NUM;
58+
59+
EthernetClient client(_lastReturnedSocket);
60+
if (EthernetClass::_server_port[_lastReturnedSocket] == _port) {
6161
uint8_t s = client.status();
6262
if (s == SnSR::ESTABLISHED || s == SnSR::CLOSE_WAIT) {
6363
if (client.available()) {
64-
// doesn't always pick the lowest numbered socket, because of _sock +
65-
// 1 at the beginning
64+
// doesn't always pick the lowest numbered socket, because of
65+
// _lastReturnedSocket + 1 at the beginning
6666
return client;
6767
}
6868
}
6969
}
70-
_sock = (_sock + 1) % MAX_SOCK_NUM;
7170
}
7271

7372
return EthernetClient(MAX_SOCK_NUM);

libraries/Ethernet/src/EthernetServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public Server {
1010
private:
1111
uint16_t _port;
1212
void accept();
13-
int _sock = -1;
13+
int _lastReturnedSocket = -1;
1414
public:
1515
EthernetServer(uint16_t);
1616
EthernetClient available();

0 commit comments

Comments
 (0)