Allow faster reuse of socket, to be able to restart WifiServer. (#4306)

See #3960 for more details of the problem and the solution. I only implemented what was proposed in this ticket, as it solves my problem, which was the same as in this ticket. Credits for the code going to @etrinh ;-)

This also is a more consistence behaviour compared to esp8266, where it also is possible to restart the wifiserver immediately on the same port.
This commit is contained in:
ahorn42 2020-11-06 13:16:50 +01:00 committed by GitHub
parent dd1a15478f
commit c6a8da61f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 0 deletions

View File

@ -63,6 +63,10 @@ WiFiClient WiFiServer::available(){
} }
void WiFiServer::begin(uint16_t port){ void WiFiServer::begin(uint16_t port){
begin(port, 1);
}
void WiFiServer::begin(uint16_t port, int enable){
if(_listening) if(_listening)
return; return;
if(port){ if(port){
@ -72,6 +76,7 @@ void WiFiServer::begin(uint16_t port){
sockfd = socket(AF_INET , SOCK_STREAM, 0); sockfd = socket(AF_INET , SOCK_STREAM, 0);
if (sockfd < 0) if (sockfd < 0)
return; return;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
server.sin_family = AF_INET; server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY; server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(_port); server.sin_port = htons(_port);

View File

@ -40,6 +40,7 @@ class WiFiServer : public Server {
WiFiClient available(); WiFiClient available();
WiFiClient accept(){return available();} WiFiClient accept(){return available();}
void begin(uint16_t port=0); void begin(uint16_t port=0);
void begin(uint16_t port, int reuse_enable);
void setNoDelay(bool nodelay); void setNoDelay(bool nodelay);
bool getNoDelay(); bool getNoDelay();
bool hasClient(); bool hasClient();