Skip to content

Commit 0114f39

Browse files
committed
WiFiClient connect timeout
1 parent 12c33f1 commit 0114f39

File tree

4 files changed

+14
-41
lines changed

4 files changed

+14
-41
lines changed

src/WiFiClient.cpp

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) {
5959
_sock = ServerDrv::getSocket();
6060
if (_sock != NO_SOCKET_AVAIL)
6161
{
62-
ServerDrv::startClient(uint32_t(ip), port, _sock);
63-
64-
unsigned long start = millis();
65-
66-
// wait 4 second for the connection to close
67-
while (!connected() && millis() - start < 10000)
68-
delay(1);
62+
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TCP_MODE, _connTimeout);
6963

7064
if (!connected())
7165
{
@@ -88,13 +82,7 @@ int WiFiClient::connectSSL(IPAddress ip, uint16_t port)
8882
_sock = ServerDrv::getSocket();
8983
if (_sock != NO_SOCKET_AVAIL)
9084
{
91-
ServerDrv::startClient(uint32_t(ip), port, _sock, TLS_MODE);
92-
93-
unsigned long start = millis();
94-
95-
// wait 4 second for the connection to close
96-
while (!connected() && millis() - start < 10000)
97-
delay(1);
85+
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TLS_MODE, _connTimeout);
9886

9987
if (!connected())
10088
{
@@ -117,13 +105,7 @@ int WiFiClient::connectSSL(const char *host, uint16_t port)
117105
_sock = ServerDrv::getSocket();
118106
if (_sock != NO_SOCKET_AVAIL)
119107
{
120-
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_MODE);
121-
122-
unsigned long start = millis();
123-
124-
// wait 4 second for the connection to close
125-
while (!connected() && millis() - start < 10000)
126-
delay(1);
108+
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_MODE, _connTimeout);
127109

128110
if (!connected())
129111
{
@@ -146,13 +128,7 @@ int WiFiClient::connectBearSSL(IPAddress ip, uint16_t port)
146128
_sock = ServerDrv::getSocket();
147129
if (_sock != NO_SOCKET_AVAIL)
148130
{
149-
ServerDrv::startClient(uint32_t(ip), port, _sock, TLS_BEARSSL_MODE);
150-
151-
unsigned long start = millis();
152-
153-
// wait 4 second for the connection to close
154-
while (!connected() && millis() - start < 10000)
155-
delay(1);
131+
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TLS_BEARSSL_MODE, _connTimeout);
156132

157133
if (!connected())
158134
{
@@ -175,13 +151,7 @@ int WiFiClient::connectBearSSL(const char *host, uint16_t port)
175151
_sock = ServerDrv::getSocket();
176152
if (_sock != NO_SOCKET_AVAIL)
177153
{
178-
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_BEARSSL_MODE);
179-
180-
unsigned long start = millis();
181-
182-
// wait 4 second for the connection to close
183-
while (!connected() && millis() - start < 10000)
184-
delay(1);
154+
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_BEARSSL_MODE, _connTimeout);
185155

186156
if (!connected())
187157
{

src/WiFiClient.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class WiFiClient : public Client {
3232
WiFiClient(uint8_t sock);
3333

3434
uint8_t status();
35+
void setConnectionTimeout(uint16_t timeout) {_connTimeout = timeout;}
36+
3537
virtual int connect(IPAddress ip, uint16_t port);
3638
virtual int connect(const char *host, uint16_t port);
3739
virtual int connectSSL(IPAddress ip, uint16_t port);
@@ -61,8 +63,8 @@ class WiFiClient : public Client {
6163

6264
private:
6365
static uint16_t _srcport;
64-
uint8_t _sock; //not used
65-
uint16_t _socket;
66+
uint8_t _sock;
67+
uint16_t _connTimeout = 0;
6668
bool _retrySend;
6769
};
6870

src/utility/server_drv.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,17 @@ void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uin
110110
SpiDrv::spiSlaveDeselect();
111111
}
112112

113-
void ServerDrv::startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode)
113+
void ServerDrv::startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode, uint16_t timeout)
114114
{
115115
WAIT_FOR_SLAVE_SELECT();
116116
// Send Command
117-
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_5);
117+
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_6);
118118
SpiDrv::sendParam((uint8_t*)host, host_len);
119119
SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress));
120120
SpiDrv::sendParam(port);
121121
SpiDrv::sendParam(&sock, 1);
122-
SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
122+
SpiDrv::sendParam(&protMode, 1);
123+
SpiDrv::sendParam(timeout, LAST_PARAM);
123124

124125
// pad to multiple of 4
125126
int commandSize = 17 + host_len;

src/utility/server_drv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ServerDrv
3737

3838
static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
3939

40-
static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
40+
static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE, uint16_t timeout = 0);
4141

4242
static void stopClient(uint8_t sock);
4343

0 commit comments

Comments
 (0)