Skip to content

Added WiFiClient::setConnectTimeout #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ extern "C" {

uint16_t WiFiClient::_srcport = 1024;

WiFiClient::WiFiClient() : _sock(NO_SOCKET_AVAIL) {
WiFiClient::WiFiClient() : _sock(NO_SOCKET_AVAIL), _connect_timeout(10000) {
}

WiFiClient::WiFiClient(uint8_t sock) : _sock(sock) {
WiFiClient::WiFiClient(uint8_t sock) : _sock(sock), _connect_timeout(10000) {
}

int WiFiClient::connect(const char* host, uint16_t port) {
Expand All @@ -63,8 +63,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) {

unsigned long start = millis();

// wait 4 second for the connection to close
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed these comments as they seem to be completely unrelated. (Copy paste accident from earlier?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may be used later on

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the comment was completely wrong. It was waiting to connect, not for the connection to close, and it waited 10 seconds not 4...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

while (!connected() && millis() - start < 10000)
while (!connected() && millis() - start < _connect_timeout)
delay(1);

if (!connected())
Expand Down Expand Up @@ -92,8 +91,7 @@ int WiFiClient::connectSSL(IPAddress ip, uint16_t port)

unsigned long start = millis();

// wait 4 second for the connection to close
while (!connected() && millis() - start < 10000)
while (!connected() && millis() - start < _connect_timeout)
delay(1);

if (!connected())
Expand Down Expand Up @@ -121,8 +119,7 @@ int WiFiClient::connectSSL(const char *host, uint16_t port)

unsigned long start = millis();

// wait 4 second for the connection to close
while (!connected() && millis() - start < 10000)
while (!connected() && millis() - start < _connect_timeout)
delay(1);

if (!connected())
Expand Down Expand Up @@ -272,3 +269,8 @@ uint16_t WiFiClient::remotePort()
uint16_t port = (_remotePort[0]<<8)+_remotePort[1];
return port;
}

void WiFiClient::setConnectTimeout(unsigned long timeout)
{
_connect_timeout = timeout;
}
3 changes: 3 additions & 0 deletions src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class WiFiClient : public Client {
virtual IPAddress remoteIP();
virtual uint16_t remotePort();

virtual void setConnectTimeout(unsigned long timeout);

friend class WiFiServer;
friend class WiFiDrv;

Expand All @@ -59,6 +61,7 @@ class WiFiClient : public Client {
static uint16_t _srcport;
uint8_t _sock; //not used
uint16_t _socket;
unsigned long _connect_timeout;
};

#endif