Skip to content

Commit be84c82

Browse files
authored
[WiFiClient] Default connection timeout, when no timeout provided (#5487)
## The problem WiFiClient's connect method variant where no timeout is passed can block esp32 MCU and may then cause watchdog to kick in and reset the device. This behavior is different from that, what is in arduino-esp8266 core. ## Summary Some cross-esp libraries (working both on esp32 and 8266), like PubSubClient simply call connect method on WiFiClient, to get connected to remote server. However, connect behavior varies betwen esp arduino 8266 and esp arduino 32 cores. This pull request tries introduce same behavior - to make connect method non-blocking on esp32, like it is with 8266 arduino core. ## Proposed solution Introduce default fixed timeout that can be changed by #define - by default set to 3 seconds. ### Affected components: WiFiClient ### Affected methods: ```c++ int connect(IPAddress ip, uint16_t port); int connect(const char *host, uint16_t port); ``` ### Impact May impact projects or libraries using connect method variant without specified timeout, where: - remote is located far away or - connection is heavily limited, or - remote is slow, when it comes to accept the connection
1 parent 31127f4 commit be84c82

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

libraries/WiFi/src/WiFiClient.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <lwip/netdb.h>
2424
#include <errno.h>
2525

26+
#define WIFI_CLIENT_DEF_CONN_TIMEOUT (3)
2627
#define WIFI_CLIENT_MAX_WRITE_RETRY (10)
2728
#define WIFI_CLIENT_SELECT_TIMEOUT_US (1000000)
2829
#define WIFI_CLIENT_FLUSH_BUFFER_SIZE (1024)
@@ -207,7 +208,7 @@ void WiFiClient::stop()
207208

208209
int WiFiClient::connect(IPAddress ip, uint16_t port)
209210
{
210-
return connect(ip,port,-1);
211+
return connect(ip,port,WIFI_CLIENT_DEF_CONN_TIMEOUT);
211212
}
212213
int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout)
213214
{
@@ -278,7 +279,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout)
278279

279280
int WiFiClient::connect(const char *host, uint16_t port)
280281
{
281-
return connect(host,port,-1);
282+
return connect(host,port,WIFI_CLIENT_DEF_CONN_TIMEOUT);
282283
}
283284
int WiFiClient::connect(const char *host, uint16_t port, int32_t timeout)
284285
{

0 commit comments

Comments
 (0)