Skip to content
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

WiFiClient.setConnectionTimeout added #329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@
#include "WiFi101.h"
#include "WiFiClient.h"

#ifndef WIFI101_CLIENT_CONNECTION_TIMEOUT
#define WIFI101_CLIENT_CONNECTION_TIMEOUT 20000
#endif

WiFiClient::WiFiClient()
{
_socket = -1;
_timeout = WIFI101_CLIENT_CONNECTION_TIMEOUT;
}

WiFiClient::WiFiClient(uint8_t sock)
{
// Spawn connected TCP client from TCP server socket:
_socket = sock;
_timeout = WIFI101_CLIENT_CONNECTION_TIMEOUT;
}

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

// Connect to remote host:
if (!WiFiSocket.connect(_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) {
if (!WiFiSocket.connect(_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in), _timeout)) {
WiFiSocket.close(_socket);
_socket = -1;
return 0;
Expand Down
3 changes: 3 additions & 0 deletions src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ class WiFiClient : public Client {
virtual IPAddress remoteIP();
virtual uint16_t remotePort();

void setConnectionTimeout(uint16_t timeout) { _timeout = timeout; }

private:
SOCKET _socket;
uint16_t _timeout;

int connect(const char* host, uint16_t port, uint8_t opt);
int connect(IPAddress ip, uint16_t port, uint8_t opt, const uint8_t *hostname);
Expand Down
4 changes: 2 additions & 2 deletions src/utility/WiFiSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ sint8 WiFiSocketClass::setopt(SOCKET socket, uint8 u8Level, uint8 option_name, c
return setsockopt(socket, u8Level, option_name, option_value, u16OptionLen);
}

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

unsigned long start = millis();

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

Expand Down
2 changes: 1 addition & 1 deletion src/utility/WiFiSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class WiFiSocketClass {
sint8 bind(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen);
sint8 listen(SOCKET sock, uint8 backlog);
sint8 setopt(SOCKET socket, uint8 u8Level, uint8 option_name, const void *option_value, uint16 u16OptionLen);
sint8 connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen);
sint8 connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen, uint16 timeout);
uint8 connected(SOCKET sock);
uint8 listening(SOCKET sock);
uint8 bound(SOCKET sock);
Expand Down