Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit 8607ac1

Browse files
committed
Stop accept()-ing connections if already at max connection limit
The constant CONFIG_LWIP_MAX_SOCKETS, if available, tells how many sockets can be opened at once. If already at this limit, and an attempt is made to accept() an additional incoming connection from a listening socket, the accept() call will fail. Fix this by checking the socket list and refusing to add the listening socket to the set of readable sockets to check in the select() call. This will cause connections to remain in the listening backlog until some other socket is closed.
1 parent 2895bb8 commit 8607ac1

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

src/AsyncTCP.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,19 @@ void _asynctcpsock_task(void *)
9292
FD_ZERO(&sockSet_r); FD_ZERO(&sockSet_w);
9393
for (it = _socketBaseList.begin(); it != _socketBaseList.end(); it++) {
9494
if ((*it)->_socket != -1) {
95-
FD_SET((*it)->_socket, &sockSet_r);
95+
#ifdef CONFIG_LWIP_MAX_SOCKETS
96+
if (!(*it)->_isServer() || _socketBaseList.size() < CONFIG_LWIP_MAX_SOCKETS) {
97+
#endif
98+
FD_SET((*it)->_socket, &sockSet_r);
99+
if (max_sock <= (*it)->_socket) max_sock = (*it)->_socket + 1;
100+
#ifdef CONFIG_LWIP_MAX_SOCKETS
101+
}
102+
#endif
96103
if ((*it)->_pendingWrite()) {
97104
FD_SET((*it)->_socket, &sockSet_w);
105+
if (max_sock <= (*it)->_socket) max_sock = (*it)->_socket + 1;
98106
}
99107
(*it)->_selected = true;
100-
if (max_sock <= (*it)->_socket) max_sock = (*it)->_socket + 1;
101108
}
102109
}
103110

src/AsyncTCP.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class AsyncSocketBase
7171
virtual void _sockDelayedConnect(void) {} // Action to take on DNS-resolve finished
7272

7373
virtual bool _pendingWrite(void) { return false; } // Test if there is data pending to be written
74+
virtual bool _isServer(void) { return false; } // Will a read from this socket result in one more client?
7475

7576
public:
7677
AsyncSocketBase(void);
@@ -239,6 +240,9 @@ class AsyncServer : public AsyncSocketBase
239240

240241
// Listening socket is readable on incoming connection
241242
void _sockIsReadable(void);
243+
244+
// Mark this class as a server
245+
bool _isServer(void) { return true; }
242246
};
243247

244248

0 commit comments

Comments
 (0)