Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion arduino/libraries/WiFi/src/WiFiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ uint8_t WiFiServer::begin(uint16_t port)

WiFiClient WiFiServer::available(uint8_t* status)
{
int result = lwip_accept(_socket, NULL, 0);
int result = -1;
if (_accepted_sock >= 0) {
result = _accepted_sock;
_accepted_sock = -1;
} else {
result = lwip_accept(_socket, NULL, 0);
}

if (status) {
*status = (result != -1);
Expand Down Expand Up @@ -109,6 +115,25 @@ WiFiClient WiFiServer::available(uint8_t* status)
return WiFiClient(result);
}

WiFiClient WiFiServer::accept()
{
int result = -1;
if (_accepted_sock >= 0) {
result = _accepted_sock;
_accepted_sock = -1;
} else {
result = lwip_accept(_socket, NULL, 0);
}
return WiFiClient(result);
}

bool WiFiServer::hasClient() {
if (_accepted_sock != -1)
return true;
_accepted_sock = lwip_accept(_socket, NULL, 0);
return (_accepted_sock != -1);
}

uint8_t WiFiServer::status() {
// Deprecated.
return 0;
Expand Down
3 changes: 3 additions & 0 deletions arduino/libraries/WiFi/src/WiFiServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class WiFiServer /*: public Server*/ {
public:
WiFiServer();
WiFiClient available(uint8_t* status = NULL);
WiFiClient accept();
bool hasClient();
uint8_t begin(uint16_t port);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
Expand All @@ -44,6 +46,7 @@ class WiFiServer /*: public Server*/ {
uint16_t _port;
int _socket;
int _spawnedSockets[CONFIG_LWIP_MAX_SOCKETS];
int _accepted_sock = -1;
};

#endif // WIFISERVER_H
19 changes: 17 additions & 2 deletions main/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,24 @@ int availDataTcp(const uint8_t command[], uint8_t response[])

if (socketTypes[socket] == 0x00) {
if (tcpServers[socket]) {
WiFiClient client = tcpServers[socket].available();

uint8_t accept = command[6];
available = 255;

if (accept) {
for (int i = 0; i < MAX_SOCKETS; i++) {
if (socketTypes[i] == 255) {
WiFiClient client = tcpServers[socket].accept();
if (client) {
socketTypes[i] = 0x00;
tcpClients[i] = client;
available = i;
}
break;
}
}
} else {
WiFiClient client = tcpServers[socket].available();
if (client) {
// try to find existing socket slot
for (int i = 0; i < MAX_SOCKETS; i++) {
Expand Down Expand Up @@ -491,6 +505,7 @@ int availDataTcp(const uint8_t command[], uint8_t response[])
}
}
}
}
} else {
available = tcpClients[socket].available();
}
Expand Down Expand Up @@ -2153,7 +2168,7 @@ void CommandHandlerClass::updateGpio0Pin()

for (int i = 0; i < MAX_SOCKETS; i++) {
if (socketTypes[i] == 0x00) {
if (tcpServers[i] && tcpServers[i].available()) {
if (tcpServers[i] && (tcpServers[i].hasClient() || tcpServers[i].available())) {
available = 1;
break;
} else if (tcpClients[i] && tcpClients[i].connected() && tcpClients[i].available()) {
Expand Down