Skip to content

Commit 06a76ee

Browse files
bbx10me-no-dev
authored andcommitted
Remote tcp disconnect not detected (#389)
* Add setNoDelay and getNoDelay to WiFiServer class * Remote TCP disconnect not detected
1 parent 51a4432 commit 06a76ee

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

Diff for: libraries/WiFi/src/WiFiClient.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,28 @@ void WiFiClient::flush() {
279279

280280
uint8_t WiFiClient::connected()
281281
{
282-
uint8_t dummy = 0;
283-
read(&dummy, 0);
282+
if (_connected) {
283+
uint8_t dummy;
284+
int res = recv(fd(), &dummy, 0, MSG_DONTWAIT);
285+
if (res <= 0) {
286+
switch (errno) {
287+
case ENOTCONN:
288+
case EPIPE:
289+
case ECONNRESET:
290+
case ECONNREFUSED:
291+
case ECONNABORTED:
292+
_connected = false;
293+
break;
294+
default:
295+
_connected = true;
296+
break;
297+
}
298+
}
299+
else {
300+
// Should never happen since requested 0 bytes
301+
_connected = true;
302+
}
303+
}
284304
return _connected;
285305
}
286306

Diff for: libraries/WiFi/src/WiFiServer.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ WiFiClient WiFiServer::available(){
4545
int client_sock = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
4646
if(client_sock >= 0){
4747
int val = 1;
48-
if(setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&val, sizeof(int)) == ESP_OK)
49-
return WiFiClient(client_sock);
48+
if(setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&val, sizeof(int)) == ESP_OK) {
49+
val = _noDelay;
50+
if(setsockopt(client_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&val, sizeof(int)) == ESP_OK)
51+
return WiFiClient(client_sock);
52+
}
5053
}
5154
return WiFiClient();
5255
}
@@ -69,6 +72,14 @@ void WiFiServer::begin(){
6972
_listening = true;
7073
}
7174

75+
void WiFiServer::setNoDelay(bool nodelay) {
76+
_noDelay = nodelay;
77+
}
78+
79+
bool WiFiServer::getNoDelay() {
80+
return _noDelay;
81+
}
82+
7283
void WiFiServer::end(){
7384
close(sockfd);
7485
sockfd = -1;

Diff for: libraries/WiFi/src/WiFiServer.h

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class WiFiServer : public Server {
2929
uint16_t _port;
3030
uint8_t _max_clients;
3131
bool _listening;
32+
bool _noDelay = false;
3233

3334
public:
3435
void listenOnLocalhost(){}
@@ -38,6 +39,8 @@ class WiFiServer : public Server {
3839
WiFiClient available();
3940
WiFiClient accept(){return available();}
4041
void begin();
42+
void setNoDelay(bool nodelay);
43+
bool getNoDelay();
4144
size_t write(const uint8_t *data, size_t len);
4245
size_t write(uint8_t data){
4346
return write(&data, 1);

0 commit comments

Comments
 (0)