Skip to content

Commit e32c862

Browse files
committed
WiFiSever - Arduino API available() and accept()
1 parent 32470fb commit e32c862

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

libraries/ESP8266WiFi/src/WiFiServer.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ bool WiFiServer::hasClient() {
115115
return false;
116116
}
117117

118-
WiFiClient WiFiServer::available(byte* status) {
118+
WiFiClient WiFiServer::accept(byte* status) {
119119
(void) status;
120120
if (_unclaimed) {
121121
WiFiClient result(_unclaimed);
@@ -135,6 +135,24 @@ WiFiClient WiFiServer::available(byte* status) {
135135
return WiFiClient();
136136
}
137137

138+
WiFiClient WiFiServer::available() {
139+
for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
140+
WiFiClient& client = connectedClients[i];
141+
if (client && client.status() == CLOSED) {
142+
client = WiFiClient();
143+
}
144+
if (!client) {
145+
client = accept();
146+
}
147+
}
148+
for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
149+
WiFiClient& client = connectedClients[i];
150+
if (client.available())
151+
return client;
152+
}
153+
return WiFiClient();
154+
}
155+
138156
uint8_t WiFiServer::status() {
139157
if (!_listen_pcb)
140158
return CLOSED;

libraries/ESP8266WiFi/src/WiFiServer.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ extern "C" {
6565
class ClientContext;
6666
class WiFiClient;
6767

68+
#ifndef MAX_MONITORED_CLIENTS
69+
#define MAX_MONITORED_CLIENTS 5
70+
#endif
71+
6872
class WiFiServer : public Server {
6973
// Secure server needs access to all the private entries here
7074
protected:
@@ -74,13 +78,17 @@ class WiFiServer : public Server {
7478

7579
ClientContext* _unclaimed;
7680
ClientContext* _discarded;
81+
82+
WiFiClient connectedClients[MAX_MONITORED_CLIENTS];
83+
7784
enum { _ndDefault, _ndFalse, _ndTrue } _noDelay = _ndDefault;
7885

7986
public:
8087
WiFiServer(const IPAddress& addr, uint16_t port);
8188
WiFiServer(uint16_t port);
8289
virtual ~WiFiServer() {}
83-
WiFiClient available(uint8_t* status = NULL);
90+
WiFiClient accept(uint8_t* status = NULL);
91+
WiFiClient available();
8492
bool hasClient();
8593
void begin();
8694
void begin(uint16_t port);

0 commit comments

Comments
 (0)