forked from espressif/arduino-esp32
Add WiFiServer hasClient and WiFiTelnetToSerial example (#394)
WiFiTelnetToSerial is also a test for hasClient().
This commit is contained in:
@ -40,9 +40,16 @@ void WiFiServer::stopAll(){}
|
||||
WiFiClient WiFiServer::available(){
|
||||
if(!_listening)
|
||||
return WiFiClient();
|
||||
int client_sock;
|
||||
if (_accepted_sockfd >= 0) {
|
||||
client_sock = _accepted_sockfd;
|
||||
_accepted_sockfd = -1;
|
||||
}
|
||||
else {
|
||||
struct sockaddr_in _client;
|
||||
int cs = sizeof(struct sockaddr_in);
|
||||
int client_sock = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
|
||||
client_sock = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
|
||||
}
|
||||
if(client_sock >= 0){
|
||||
int val = 1;
|
||||
if(setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&val, sizeof(int)) == ESP_OK) {
|
||||
@ -70,6 +77,8 @@ void WiFiServer::begin(){
|
||||
return;
|
||||
fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||
_listening = true;
|
||||
_noDelay = false;
|
||||
_accepted_sockfd = -1;
|
||||
}
|
||||
|
||||
void WiFiServer::setNoDelay(bool nodelay) {
|
||||
@ -80,6 +89,19 @@ bool WiFiServer::getNoDelay() {
|
||||
return _noDelay;
|
||||
}
|
||||
|
||||
bool WiFiServer::hasClient() {
|
||||
if (_accepted_sockfd >= 0) {
|
||||
return true;
|
||||
}
|
||||
struct sockaddr_in _client;
|
||||
int cs = sizeof(struct sockaddr_in);
|
||||
_accepted_sockfd = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
|
||||
if (_accepted_sockfd >= 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void WiFiServer::end(){
|
||||
close(sockfd);
|
||||
sockfd = -1;
|
||||
|
Reference in New Issue
Block a user