Skip to content

Commit 06a76ee

Browse files
bbx10me-no-dev
authored andcommitted
Remote tcp disconnect not detected (espressif#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

‎libraries/WiFi/src/WiFiClient.cpp‎

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

280280
uint8_tWiFiClient::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

‎libraries/WiFi/src/WiFiServer.cpp‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ WiFiClient WiFiServer::available(){
4545
int client_sock = accept(sockfd, (structsockaddr *)&_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-
returnWiFiClient(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+
returnWiFiClient(client_sock);
52+
}
5053
}
5154
returnWiFiClient();
5255
}
@@ -69,6 +72,14 @@ void WiFiServer::begin(){
6972
_listening = true;
7073
}
7174

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

‎libraries/WiFi/src/WiFiServer.h‎

Lines changed: 3 additions & 0 deletions
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
voidlistenOnLocalhost(){}
@@ -38,6 +39,8 @@ class WiFiServer : public Server{
3839
WiFiClient available();
3940
WiFiClient accept(){returnavailable()}
4041
voidbegin();
42+
voidsetNoDelay(bool nodelay);
43+
boolgetNoDelay();
4144
size_twrite(constuint8_t *data, size_t len);
4245
size_twrite(uint8_t data){
4346
returnwrite(&data, 1);

0 commit comments

Comments
(0)