Skip to content

Commit

Permalink
WiFiClients - setTimeout replaced with setConnectionTimeout
Browse files Browse the repository at this point in the history
to not shadow Stream::setTimeout which has a different purpose
  • Loading branch information
JAndrassy committed Dec 13, 2023
1 parent 29cde94 commit 49ea525
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,7 @@ bool HTTPClient::connect(void)

// set Timeout for WiFiClient and for Stream::readBytesUntil() and Stream::readStringUntil()
_client->setTimeout(_tcpTimeout);
_client->setConnectionTimeout(_tcpTimeout);

log_d(" connected to %s:%u", _host.c_str(), _port);

Expand Down
17 changes: 17 additions & 0 deletions libraries/WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,23 @@ int WiFiClient::getSocketOption(int level, int option, const void* value, size_t
return res;
}

int WiFiClient::setConnectionTimeout(uint32_t milliseconds)
{
_timeout = milliseconds;
if(fd() >= 0) {
struct timeval tv;
tv.tv_sec = _timeout / 1000;
tv.tv_usec = (_timeout % 1000) * 1000;
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
return -1;
}
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
}
else {
return 0;
}
}

int WiFiClient::setOption(int option, int *value)
{
return setSocketOption(IPPROTO_TCP, option, (const void*)value, sizeof(int));
Expand Down
2 changes: 2 additions & 0 deletions libraries/WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ESPLwIPClient : public Client
public:
virtual int connect(IPAddress ip, uint16_t port, int32_t timeout) = 0;
virtual int connect(const char *host, uint16_t port, int32_t timeout) = 0;
virtual int setConnectionTimeout(uint32_t milliseconds) = 0;
};

class WiFiClient : public ESPLwIPClient
Expand Down Expand Up @@ -92,6 +93,7 @@ class WiFiClient : public ESPLwIPClient
int getSocketOption(int level, int option, const void* value, size_t size);
int setOption(int option, int *value);
int getOption(int option, int *value);
int setConnectionTimeout(uint32_t milliseconds);
int setNoDelay(bool nodelay);
bool getNoDelay();

Expand Down
8 changes: 4 additions & 4 deletions libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,13 @@ void WiFiClientSecure::setAlpnProtocols(const char **alpn_protos)
{
_alpn_protos = alpn_protos;
}
int WiFiClientSecure::setTimeout(uint32_t seconds)
int WiFiClientSecure::setConnectionTimeout(uint32_t milliseconds)
{
_timeout = seconds * 1000;
_timeout = milliseconds;
if (sslclient->socket >= 0) {
struct timeval tv;
tv.tv_sec = seconds;
tv.tv_usec = 0;
tv.tv_sec = _timeout / 1000;
tv.tv_usec = (_timeout % 1000) * 1000;
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/WiFiClientSecure/src/WiFiClientSecure.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class WiFiClientSecure : public WiFiClient
void setAlpnProtocols(const char **alpn_protos);
const mbedtls_x509_crt* getPeerCertificate() { return mbedtls_ssl_get_peer_cert(&sslclient->ssl_ctx); };
bool getFingerprintSHA256(uint8_t sha256_result[32]) { return get_peer_fingerprint(sslclient, sha256_result); };
int setTimeout(uint32_t seconds);
int setConnectionTimeout(uint32_t milliseconds);
int fd() const;

operator bool()
Expand Down

0 comments on commit 49ea525

Please sign in to comment.