Skip to content

Commit bf73a74

Browse files
rtpmsysSuGlider
authored andcommitted
Fix error in WiFiClient.cpp where the connect function fails for timeouts 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]>
1 parent 52a4462 commit bf73a74

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

libraries/WiFi/src/WiFiClient.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
210210
{
211211
return connect(ip,port,_timeout);
212212
}
213-
int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout)
213+
int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout_ms)
214214
{
215-
_timeout = timeout;
215+
_timeout = timeout_ms;
216216
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
217217
if (sockfd < 0) {
218218
log_e("socket: %d", errno);
@@ -231,7 +231,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout)
231231
FD_ZERO(&fdset);
232232
FD_SET(sockfd, &fdset);
233233
tv.tv_sec = _timeout / 1000;
234-
tv.tv_usec = 0;
234+
tv.tv_usec = (_timeout % 1000) * 1000;
235235

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

295-
int WiFiClient::connect(const char *host, uint16_t port, int32_t timeout)
295+
int WiFiClient::connect(const char *host, uint16_t port, int32_t timeout_ms)
296296
{
297297
IPAddress srv((uint32_t)0);
298298
if(!WiFiGenericClass::hostByName(host, srv)){
299299
return 0;
300300
}
301-
return connect(srv, port, timeout);
301+
return connect(srv, port, timeout_ms);
302302
}
303303

304304
int WiFiClient::setSocketOption(int option, char* value, size_t len)

libraries/WiFi/src/WiFiClient.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class WiFiClient : public ESPLwIPClient
5050
WiFiClient(int fd);
5151
~WiFiClient();
5252
int connect(IPAddress ip, uint16_t port);
53-
int connect(IPAddress ip, uint16_t port, int32_t timeout);
53+
int connect(IPAddress ip, uint16_t port, int32_t timeout_ms);
5454
int connect(const char *host, uint16_t port);
55-
int connect(const char *host, uint16_t port, int32_t timeout);
55+
int connect(const char *host, uint16_t port, int32_t timeout_ms);
5656
size_t write(uint8_t data);
5757
size_t write(const uint8_t *buf, size_t size);
5858
size_t write_P(PGM_P buf, size_t size);

0 commit comments

Comments
 (0)