Skip to content

Commit 28ca40b

Browse files
committed
Providing a more fair servicing of multiple tcp clients.
I modified `EthernetServer::available()` such that it will not always pick the socket with the lowest number that has data available. Instead when the method returned the client at socket i, it will check socket (i+1) % MAX_SOCK_NUM first when then method is called the next time. The problem with the previous implementation is that if there is a client connected to a socket with a low number (e.g. the first one with number 0) and the peer constantly sends data. In that case `EthernetServer::available()` always returns that client. The clients, which are connected to a socket with a higher number (e.g. 1, 2, or 3), can only be returned when the ones with lower numbers have no data available. This problem is fixed with this commit. This commit also implements the changes suggested by @matthijskooijman.
1 parent 19a9e4d commit 28ca40b

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

libraries/Ethernet/src/EthernetServer.cpp

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

55-
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
56-
EthernetClient client(sock);
57-
if (EthernetClass::_server_port[sock] == _port) {
55+
for (int i = 0; i < MAX_SOCK_NUM; i++) {
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) {
5861
uint8_t s = client.status();
5962
if (s == SnSR::ESTABLISHED || s == SnSR::CLOSE_WAIT) {
6063
if (client.available()) {
61-
// XXX: don't always pick the lowest numbered socket.
64+
// doesn't always pick the lowest numbered socket, because of
65+
// _lastReturnedSocket + 1 at the beginning
6266
return client;
6367
}
6468
}

libraries/Ethernet/src/EthernetServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public Server {
1010
private:
1111
uint16_t _port;
1212
void accept();
13+
int _lastReturnedSocket = -1;
1314
public:
1415
EthernetServer(uint16_t);
1516
EthernetClient available();

0 commit comments

Comments
 (0)