Skip to content

Commit 29cde94

Browse files
authored
ESPLwIPClient::setTimeout conflict fix with Stream::setTimeout (#6676)
* Removed virtual + moved socketOptions ot read/write * Removed no needed code + edit * removed Client::getTimeout * removed setTimeout from WifiClient - read/write timeouts in constructor now * Changed seconds to miliseconds in other classes relaed + examples * Applied same changes for WifiClientSecure * Added 0 init values to constructor * Seconds are not rounded now * removed +500 for previous rounding + unnecessary comments removed. * fix rebased code in WifiClientSecure * Fix rebased code * Fix rebase code
1 parent c5297bf commit 29cde94

File tree

6 files changed

+58
-26
lines changed

6 files changed

+58
-26
lines changed

libraries/HTTPClient/src/HTTPClient.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ void HTTPClient::setTimeout(uint16_t timeout)
495495
{
496496
_tcpTimeout = timeout;
497497
if(connected()) {
498-
_client->setTimeout((timeout + 500) / 1000);
498+
_client->setTimeout(timeout);
499499
}
500500
}
501501

@@ -1165,7 +1165,7 @@ bool HTTPClient::connect(void)
11651165
}
11661166

11671167
// set Timeout for WiFiClient and for Stream::readBytesUntil() and Stream::readStringUntil()
1168-
_client->setTimeout((_tcpTimeout + 500) / 1000);
1168+
_client->setTimeout(_tcpTimeout);
11691169

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

libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void loop() {
9696
client.setCACert(rootCACertificate);
9797

9898
// Reading data over SSL may be slow, use an adequate timeout
99-
client.setTimeout(12000 / 1000); // timeout argument is defined in seconds for setTimeout
99+
client.setTimeout(12000); // timeout argument is defined in miliseconds for setTimeout
100100

101101
// The line below is optional. It can be used to blink the LED on the board during flashing
102102
// The LED will be on during download of one buffer of data from the network. The LED will

libraries/WebServer/src/WebServer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void WebServer::handleClient() {
303303
if (_parseRequest(_currentClient)) {
304304
// because HTTP_MAX_SEND_WAIT is expressed in milliseconds,
305305
// it must be divided by 1000
306-
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT / 1000);
306+
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT); /* / 1000 removed, WifiClient setTimeout changed to ms */
307307
_contentLength = CONTENT_LENGTH_NOT_SET;
308308
_handleRequest();
309309

libraries/WiFi/src/WiFiClient.cpp

+26-19
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ void WiFiClient::stop()
211211
clientSocketHandle = NULL;
212212
_rxBuffer = NULL;
213213
_connected = false;
214+
_lastReadTimeout = 0;
215+
_lastWriteTimeout = 0;
214216
}
215217

216218
int WiFiClient::connect(IPAddress ip, uint16_t port)
@@ -331,25 +333,6 @@ int WiFiClient::getSocketOption(int level, int option, const void* value, size_t
331333
return res;
332334
}
333335

334-
335-
int WiFiClient::setTimeout(uint32_t seconds)
336-
{
337-
Client::setTimeout(seconds * 1000); // This should be here?
338-
_timeout = seconds * 1000;
339-
if(fd() >= 0) {
340-
struct timeval tv;
341-
tv.tv_sec = seconds;
342-
tv.tv_usec = 0;
343-
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
344-
return -1;
345-
}
346-
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
347-
}
348-
else {
349-
return 0;
350-
}
351-
}
352-
353336
int WiFiClient::setOption(int option, int *value)
354337
{
355338
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)
418401
tv.tv_usec = WIFI_CLIENT_SELECT_TIMEOUT_US;
419402
retry--;
420403

404+
if(_lastWriteTimeout != _timeout){
405+
if(fd() >= 0){
406+
struct timeval timeout_tv;
407+
timeout_tv.tv_sec = _timeout / 1000;
408+
timeout_tv.tv_usec = (_timeout % 1000) * 1000;
409+
if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
410+
{
411+
_lastWriteTimeout = _timeout;
412+
}
413+
}
414+
}
415+
421416
if(select(socketFileDescriptor + 1, NULL, &set, NULL, &tv) < 0) {
422417
return 0;
423418
}
@@ -477,6 +472,18 @@ size_t WiFiClient::write(Stream &stream)
477472

478473
int WiFiClient::read(uint8_t *buf, size_t size)
479474
{
475+
if(_lastReadTimeout != _timeout){
476+
if(fd() >= 0){
477+
struct timeval timeout_tv;
478+
timeout_tv.tv_sec = _timeout / 1000;
479+
timeout_tv.tv_usec = (_timeout % 1000) * 1000;
480+
if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
481+
{
482+
_lastReadTimeout = _timeout;
483+
}
484+
}
485+
}
486+
480487
int res = -1;
481488
if (_rxBuffer) {
482489
res = _rxBuffer->read(buf, size);

libraries/WiFi/src/WiFiClient.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class ESPLwIPClient : public Client
3333
public:
3434
virtual int connect(IPAddress ip, uint16_t port, int32_t timeout) = 0;
3535
virtual int connect(const char *host, uint16_t port, int32_t timeout) = 0;
36-
virtual int setTimeout(uint32_t seconds) = 0;
3736
};
3837

3938
class WiFiClient : public ESPLwIPClient
@@ -43,6 +42,8 @@ class WiFiClient : public ESPLwIPClient
4342
std::shared_ptr<WiFiClientRxBuffer> _rxBuffer;
4443
bool _connected;
4544
int _timeout;
45+
int _lastWriteTimeout;
46+
int _lastReadTimeout;
4647

4748
public:
4849
WiFiClient *next;
@@ -91,7 +92,6 @@ class WiFiClient : public ESPLwIPClient
9192
int getSocketOption(int level, int option, const void* value, size_t size);
9293
int setOption(int option, int *value);
9394
int getOption(int option, int *value);
94-
int setTimeout(uint32_t seconds);
9595
int setNoDelay(bool nodelay);
9696
bool getNoDelay();
9797

libraries/WiFiClientSecure/src/WiFiClientSecure.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ WiFiClientSecure::WiFiClientSecure(int sock)
5454
{
5555
_connected = false;
5656
_timeout = 30000; // Same default as ssl_client
57+
_lastReadTimeout = 0;
58+
_lastWriteTimeout = 0;
5759

5860
sslclient = new sslclient_context;
5961
ssl_init(sslclient);
@@ -94,6 +96,8 @@ void WiFiClientSecure::stop()
9496
sslclient->socket = -1;
9597
_connected = false;
9698
_peek = -1;
99+
_lastReadTimeout = 0;
100+
_lastWriteTimeout = 0;
97101
}
98102
stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key);
99103
}
@@ -199,6 +203,16 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
199203
if (!_connected) {
200204
return 0;
201205
}
206+
if(_lastWriteTimeout != _timeout){
207+
struct timeval timeout_tv;
208+
timeout_tv.tv_sec = _timeout / 1000;
209+
timeout_tv.tv_usec = (_timeout % 1000) * 1000;
210+
if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
211+
{
212+
_lastWriteTimeout = _timeout;
213+
}
214+
}
215+
202216
int res = send_ssl_data(sslclient, buf, size);
203217
if (res < 0) {
204218
stop();
@@ -209,6 +223,18 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
209223

210224
int WiFiClientSecure::read(uint8_t *buf, size_t size)
211225
{
226+
if(_lastReadTimeout != _timeout){
227+
if(fd() >= 0){
228+
struct timeval timeout_tv;
229+
timeout_tv.tv_sec = _timeout / 1000;
230+
timeout_tv.tv_usec = (_timeout % 1000) * 1000;
231+
if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
232+
{
233+
_lastReadTimeout = _timeout;
234+
}
235+
}
236+
}
237+
212238
int peeked = 0;
213239
int avail = available();
214240
if ((!buf && size) || avail <= 0) {
@@ -396,4 +422,3 @@ int WiFiClientSecure::fd() const
396422
{
397423
return sslclient->socket;
398424
}
399-

0 commit comments

Comments
 (0)