Skip to content

Commit

Permalink
Fix error in WiFiClient.cpp where the connect function fails for time…
Browse files Browse the repository at this point in the history
…outs below 1 second (#7686)

* Update WiFiClient.cpp

This change will allow specifying connect timeouts below 1 second. Without this change, if connect timeouts under 1 second are given, the connect defaults to 0ms and fails. 
This will also allow timeouts in fractions of seconds, e.g. 1500ms. Without this change, connect timeouts are truncated to full second increments.

* Make parameter timeout_ms clear

* Change connection timeout_ms name for clarity

---------

Co-authored-by: Rodrigo Garcia <[email protected]>
  • Loading branch information
2 people authored and me-no-dev committed Feb 7, 2023
1 parent 6cd0ebc commit 54b1ea2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions libraries/WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
{
return connect(ip,port,_timeout);
}
int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout)
int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout_ms)
{
_timeout = timeout;
_timeout = timeout_ms;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
log_e("socket: %d", errno);
Expand All @@ -231,7 +231,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout)
FD_ZERO(&fdset);
FD_SET(sockfd, &fdset);
tv.tv_sec = _timeout / 1000;
tv.tv_usec = 0;
tv.tv_usec = (_timeout % 1000) * 1000;

#ifdef ESP_IDF_VERSION_MAJOR
int res = lwip_connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
Expand Down Expand Up @@ -292,13 +292,13 @@ int WiFiClient::connect(const char *host, uint16_t port)
return connect(host,port,_timeout);
}

int WiFiClient::connect(const char *host, uint16_t port, int32_t timeout)
int WiFiClient::connect(const char *host, uint16_t port, int32_t timeout_ms)
{
IPAddress srv((uint32_t)0);
if(!WiFiGenericClass::hostByName(host, srv)){
return 0;
}
return connect(srv, port, timeout);
return connect(srv, port, timeout_ms);
}

int WiFiClient::setSocketOption(int option, char* value, size_t len)
Expand Down
4 changes: 2 additions & 2 deletions libraries/WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class WiFiClient : public ESPLwIPClient
WiFiClient(int fd);
~WiFiClient();
int connect(IPAddress ip, uint16_t port);
int connect(IPAddress ip, uint16_t port, int32_t timeout);
int connect(IPAddress ip, uint16_t port, int32_t timeout_ms);
int connect(const char *host, uint16_t port);
int connect(const char *host, uint16_t port, int32_t timeout);
int connect(const char *host, uint16_t port, int32_t timeout_ms);
size_t write(uint8_t data);
size_t write(const uint8_t *buf, size_t size);
size_t write_P(PGM_P buf, size_t size);
Expand Down

0 comments on commit 54b1ea2

Please sign in to comment.