Skip to content

Commit bed2bc8

Browse files
committed
WiFiClient.setConnectionTimeout added
1 parent 04f088b commit bed2bc8

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

src/WiFiClient.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@
2222
#include "WiFi101.h"
2323
#include "WiFiClient.h"
2424

25+
#ifndef WIFI101_CLIENT_CONNECTION_TIMEOUT
26+
#define WIFI101_CLIENT_CONNECTION_TIMEOUT 20000
27+
#endif
28+
2529
WiFiClient::WiFiClient()
2630
{
2731
_socket = -1;
32+
_timeout = WIFI101_CLIENT_CONNECTION_TIMEOUT;
2833
}
2934

3035
WiFiClient::WiFiClient(uint8_t sock)
3136
{
3237
// Spawn connected TCP client from TCP server socket:
3338
_socket = sock;
39+
_timeout = WIFI101_CLIENT_CONNECTION_TIMEOUT;
3440
}
3541

3642
int WiFiClient::connectSSL(const char* host, uint16_t port)
@@ -85,7 +91,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port, uint8_t opt, const uint8_t
8591
}
8692

8793
// Connect to remote host:
88-
if (!WiFiSocket.connect(_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) {
94+
if (!WiFiSocket.connect(_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in), _timeout)) {
8995
WiFiSocket.close(_socket);
9096
_socket = -1;
9197
return 0;

src/WiFiClient.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ class WiFiClient : public Client {
5858
virtual IPAddress remoteIP();
5959
virtual uint16_t remotePort();
6060

61+
void setConnectionTimeout(uint16_t timeout) { _timeout = timeout; }
62+
6163
private:
6264
SOCKET _socket;
65+
uint16_t _timeout;
6366

6467
int connect(const char* host, uint16_t port, uint8_t opt);
6568
int connect(IPAddress ip, uint16_t port, uint8_t opt, const uint8_t *hostname);

src/utility/WiFiSocket.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ sint8 WiFiSocketClass::setopt(SOCKET socket, uint8 u8Level, uint8 option_name, c
132132
return setsockopt(socket, u8Level, option_name, option_value, u16OptionLen);
133133
}
134134

135-
sint8 WiFiSocketClass::connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen)
135+
sint8 WiFiSocketClass::connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen, uint16 timeout)
136136
{
137137
if (::connect(sock, pstrAddr, u8AddrLen) < 0) {
138138
return 0;
@@ -142,7 +142,7 @@ sint8 WiFiSocketClass::connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8A
142142

143143
unsigned long start = millis();
144144

145-
while (_info[sock].state == SOCKET_STATE_CONNECTING && millis() - start < 20000) {
145+
while (_info[sock].state == SOCKET_STATE_CONNECTING && millis() - start < timeout) {
146146
m2m_wifi_handle_events(NULL);
147147
}
148148

src/utility/WiFiSocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class WiFiSocketClass {
3737
sint8 bind(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen);
3838
sint8 listen(SOCKET sock, uint8 backlog);
3939
sint8 setopt(SOCKET socket, uint8 u8Level, uint8 option_name, const void *option_value, uint16 u16OptionLen);
40-
sint8 connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen);
40+
sint8 connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen, uint16 timeout);
4141
uint8 connected(SOCKET sock);
4242
uint8 listening(SOCKET sock);
4343
uint8 bound(SOCKET sock);

0 commit comments

Comments
 (0)