diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp index 62a9c44901b..1733d0cede1 100644 --- a/libraries/HTTPClient/src/HTTPClient.cpp +++ b/libraries/HTTPClient/src/HTTPClient.cpp @@ -495,7 +495,7 @@ void HTTPClient::setTimeout(uint16_t timeout) { _tcpTimeout = timeout; if(connected()) { - _client->setTimeout((timeout + 500) / 1000); + _client->setTimeout(timeout); } } @@ -1165,7 +1165,7 @@ bool HTTPClient::connect(void) } // set Timeout for WiFiClient and for Stream::readBytesUntil() and Stream::readStringUntil() - _client->setTimeout((_tcpTimeout + 500) / 1000); + _client->setTimeout(_tcpTimeout); log_d(" connected to %s:%u", _host.c_str(), _port); diff --git a/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino index 2028d192c06..e528a9fdb1c 100644 --- a/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino +++ b/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -96,7 +96,7 @@ void loop() { client.setCACert(rootCACertificate); // Reading data over SSL may be slow, use an adequate timeout - client.setTimeout(12000 / 1000); // timeout argument is defined in seconds for setTimeout + client.setTimeout(12000); // timeout argument is defined in miliseconds for setTimeout // The line below is optional. It can be used to blink the LED on the board during flashing // The LED will be on during download of one buffer of data from the network. The LED will diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp index 8af04ac3f91..0d99f3680a1 100644 --- a/libraries/WebServer/src/WebServer.cpp +++ b/libraries/WebServer/src/WebServer.cpp @@ -303,7 +303,7 @@ void WebServer::handleClient() { if (_parseRequest(_currentClient)) { // because HTTP_MAX_SEND_WAIT is expressed in milliseconds, // it must be divided by 1000 - _currentClient.setTimeout(HTTP_MAX_SEND_WAIT / 1000); + _currentClient.setTimeout(HTTP_MAX_SEND_WAIT); /* / 1000 removed, WifiClient setTimeout changed to ms */ _contentLength = CONTENT_LENGTH_NOT_SET; _handleRequest(); diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp index 91247d17386..52c73b67300 100644 --- a/libraries/WiFi/src/WiFiClient.cpp +++ b/libraries/WiFi/src/WiFiClient.cpp @@ -211,6 +211,8 @@ void WiFiClient::stop() clientSocketHandle = NULL; _rxBuffer = NULL; _connected = false; + _lastReadTimeout = 0; + _lastWriteTimeout = 0; } int WiFiClient::connect(IPAddress ip, uint16_t port) @@ -331,25 +333,6 @@ int WiFiClient::getSocketOption(int level, int option, const void* value, size_t return res; } - -int WiFiClient::setTimeout(uint32_t seconds) -{ - Client::setTimeout(seconds * 1000); // This should be here? - _timeout = seconds * 1000; - if(fd() >= 0) { - struct timeval tv; - tv.tv_sec = seconds; - tv.tv_usec = 0; - 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)); @@ -418,6 +401,18 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) tv.tv_usec = WIFI_CLIENT_SELECT_TIMEOUT_US; retry--; + if(_lastWriteTimeout != _timeout){ + if(fd() >= 0){ + struct timeval timeout_tv; + timeout_tv.tv_sec = _timeout / 1000; + timeout_tv.tv_usec = (_timeout % 1000) * 1000; + if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0) + { + _lastWriteTimeout = _timeout; + } + } + } + if(select(socketFileDescriptor + 1, NULL, &set, NULL, &tv) < 0) { return 0; } @@ -477,6 +472,18 @@ size_t WiFiClient::write(Stream &stream) int WiFiClient::read(uint8_t *buf, size_t size) { + if(_lastReadTimeout != _timeout){ + if(fd() >= 0){ + struct timeval timeout_tv; + timeout_tv.tv_sec = _timeout / 1000; + timeout_tv.tv_usec = (_timeout % 1000) * 1000; + if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0) + { + _lastReadTimeout = _timeout; + } + } + } + int res = -1; if (_rxBuffer) { res = _rxBuffer->read(buf, size); diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h index cdb0e0539e9..7ff753ef3e2 100644 --- a/libraries/WiFi/src/WiFiClient.h +++ b/libraries/WiFi/src/WiFiClient.h @@ -33,7 +33,6 @@ 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 setTimeout(uint32_t seconds) = 0; }; class WiFiClient : public ESPLwIPClient @@ -43,6 +42,8 @@ class WiFiClient : public ESPLwIPClient std::shared_ptr _rxBuffer; bool _connected; int _timeout; + int _lastWriteTimeout; + int _lastReadTimeout; public: WiFiClient *next; @@ -91,7 +92,6 @@ 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 setTimeout(uint32_t seconds); int setNoDelay(bool nodelay); bool getNoDelay(); diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp index 4d4702ba84d..b3c64129d75 100644 --- a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp +++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp @@ -54,6 +54,8 @@ WiFiClientSecure::WiFiClientSecure(int sock) { _connected = false; _timeout = 30000; // Same default as ssl_client + _lastReadTimeout = 0; + _lastWriteTimeout = 0; sslclient = new sslclient_context; ssl_init(sslclient); @@ -94,6 +96,8 @@ void WiFiClientSecure::stop() sslclient->socket = -1; _connected = false; _peek = -1; + _lastReadTimeout = 0; + _lastWriteTimeout = 0; } stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key); } @@ -199,6 +203,16 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) if (!_connected) { return 0; } + if(_lastWriteTimeout != _timeout){ + struct timeval timeout_tv; + timeout_tv.tv_sec = _timeout / 1000; + timeout_tv.tv_usec = (_timeout % 1000) * 1000; + if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0) + { + _lastWriteTimeout = _timeout; + } + } + int res = send_ssl_data(sslclient, buf, size); if (res < 0) { stop(); @@ -209,6 +223,18 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) int WiFiClientSecure::read(uint8_t *buf, size_t size) { + if(_lastReadTimeout != _timeout){ + if(fd() >= 0){ + struct timeval timeout_tv; + timeout_tv.tv_sec = _timeout / 1000; + timeout_tv.tv_usec = (_timeout % 1000) * 1000; + if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0) + { + _lastReadTimeout = _timeout; + } + } + } + int peeked = 0; int avail = available(); if ((!buf && size) || avail <= 0) { @@ -396,4 +422,3 @@ int WiFiClientSecure::fd() const { return sslclient->socket; } -