Remote tcp disconnect not detected (#389)

* Add setNoDelay and getNoDelay to WiFiServer class

* Remote TCP disconnect not detected
This commit is contained in:
bbx10 2017-05-22 02:16:07 -10:00 committed by Me No Dev
parent 51a4432ca8
commit 06a76eebe8
3 changed files with 38 additions and 4 deletions

View File

@ -279,8 +279,28 @@ void WiFiClient::flush() {
uint8_t WiFiClient::connected() uint8_t WiFiClient::connected()
{ {
uint8_t dummy = 0; if (_connected) {
read(&dummy, 0); uint8_t dummy;
int res = recv(fd(), &dummy, 0, MSG_DONTWAIT);
if (res <= 0) {
switch (errno) {
case ENOTCONN:
case EPIPE:
case ECONNRESET:
case ECONNREFUSED:
case ECONNABORTED:
_connected = false;
break;
default:
_connected = true;
break;
}
}
else {
// Should never happen since requested 0 bytes
_connected = true;
}
}
return _connected; return _connected;
} }

View File

@ -45,9 +45,12 @@ WiFiClient WiFiServer::available(){
int client_sock = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs); int client_sock = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
if(client_sock >= 0){ if(client_sock >= 0){
int val = 1; int val = 1;
if(setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&val, sizeof(int)) == ESP_OK) if(setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&val, sizeof(int)) == ESP_OK) {
val = _noDelay;
if(setsockopt(client_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&val, sizeof(int)) == ESP_OK)
return WiFiClient(client_sock); return WiFiClient(client_sock);
} }
}
return WiFiClient(); return WiFiClient();
} }
@ -69,6 +72,14 @@ void WiFiServer::begin(){
_listening = true; _listening = true;
} }
void WiFiServer::setNoDelay(bool nodelay) {
_noDelay = nodelay;
}
bool WiFiServer::getNoDelay() {
return _noDelay;
}
void WiFiServer::end(){ void WiFiServer::end(){
close(sockfd); close(sockfd);
sockfd = -1; sockfd = -1;

View File

@ -29,6 +29,7 @@ class WiFiServer : public Server {
uint16_t _port; uint16_t _port;
uint8_t _max_clients; uint8_t _max_clients;
bool _listening; bool _listening;
bool _noDelay = false;
public: public:
void listenOnLocalhost(){} void listenOnLocalhost(){}
@ -38,6 +39,8 @@ class WiFiServer : public Server {
WiFiClient available(); WiFiClient available();
WiFiClient accept(){return available();} WiFiClient accept(){return available();}
void begin(); void begin();
void setNoDelay(bool nodelay);
bool getNoDelay();
size_t write(const uint8_t *data, size_t len); size_t write(const uint8_t *data, size_t len);
size_t write(uint8_t data){ size_t write(uint8_t data){
return write(&data, 1); return write(&data, 1);