Skip to content

Commit 6a584ef

Browse files
committed
Merge commit 'refs/pull/6676/head' of https://github.com/espressif/arduino-esp32 into pr/7043
2 parents 8161e78 + 17adb9d commit 6a584ef

File tree

7 files changed

+59
-41
lines changed

7 files changed

+59
-41
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

@@ -1151,7 +1151,7 @@ bool HTTPClient::connect(void)
11511151
}
11521152

11531153
// set Timeout for WiFiClient and for Stream::readBytesUntil() and Stream::readStringUntil()
1154-
_client->setTimeout((_tcpTimeout + 500) / 1000);
1154+
_client->setTimeout(_tcpTimeout);
11551155

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

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
@@ -312,7 +312,7 @@ void WebServer::handleClient() {
312312
if (_parseRequest(_currentClient)) {
313313
// because HTTP_MAX_SEND_WAIT is expressed in milliseconds,
314314
// it must be divided by 1000
315-
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT / 1000);
315+
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT); /* / 1000 removed, WifiClient setTimeout changed to ms */
316316
_contentLength = CONTENT_LENGTH_NOT_SET;
317317
_handleRequest();
318318

libraries/WiFi/src/WiFiClient.cpp

+26-18
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ void WiFiClient::stop()
204204
clientSocketHandle = NULL;
205205
_rxBuffer = NULL;
206206
_connected = false;
207+
_lastReadTimeout = 0;
208+
_lastWriteTimeout = 0;
207209
}
208210

209211
int WiFiClient::connect(IPAddress ip, uint16_t port)
@@ -310,24 +312,6 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
310312
return res;
311313
}
312314

313-
int WiFiClient::setTimeout(uint32_t seconds)
314-
{
315-
Client::setTimeout(seconds * 1000); // This should be here?
316-
_timeout = seconds * 1000;
317-
if(fd() >= 0) {
318-
struct timeval tv;
319-
tv.tv_sec = seconds;
320-
tv.tv_usec = 0;
321-
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
322-
return -1;
323-
}
324-
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
325-
}
326-
else {
327-
return 0;
328-
}
329-
}
330-
331315
int WiFiClient::setOption(int option, int *value)
332316
{
333317
int res = setsockopt(fd(), IPPROTO_TCP, option, (char *) value, sizeof(int));
@@ -400,6 +384,18 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
400384
tv.tv_usec = WIFI_CLIENT_SELECT_TIMEOUT_US;
401385
retry--;
402386

387+
if(_lastWriteTimeout != _timeout){
388+
if(fd() >= 0){
389+
struct timeval timeout_tv;
390+
timeout_tv.tv_sec = _timeout / 1000;
391+
timeout_tv.tv_usec = (_timeout % 1000) * 1000;
392+
if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
393+
{
394+
_lastWriteTimeout = _timeout;
395+
}
396+
}
397+
}
398+
403399
if(select(socketFileDescriptor + 1, NULL, &set, NULL, &tv) < 0) {
404400
return 0;
405401
}
@@ -459,6 +455,18 @@ size_t WiFiClient::write(Stream &stream)
459455

460456
int WiFiClient::read(uint8_t *buf, size_t size)
461457
{
458+
if(_lastReadTimeout != _timeout){
459+
if(fd() >= 0){
460+
struct timeval timeout_tv;
461+
timeout_tv.tv_sec = _timeout / 1000;
462+
timeout_tv.tv_usec = (_timeout % 1000) * 1000;
463+
if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
464+
{
465+
_lastReadTimeout = _timeout;
466+
}
467+
}
468+
}
469+
462470
int res = -1;
463471
if (_rxBuffer) {
464472
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;
@@ -89,7 +90,6 @@ class WiFiClient : public ESPLwIPClient
8990
int setSocketOption(int option, char* value, size_t len);
9091
int setOption(int option, int *value);
9192
int getOption(int option, int *value);
92-
int setTimeout(uint32_t seconds);
9393
int setNoDelay(bool nodelay);
9494
bool getNoDelay();
9595

libraries/WiFiClientSecure/src/WiFiClientSecure.cpp

+27-16
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
}
@@ -185,6 +189,16 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
185189
if (!_connected) {
186190
return 0;
187191
}
192+
if(_lastWriteTimeout != _timeout){
193+
struct timeval timeout_tv;
194+
timeout_tv.tv_sec = _timeout / 1000;
195+
timeout_tv.tv_usec = (_timeout % 1000) * 1000;
196+
if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
197+
{
198+
_lastWriteTimeout = _timeout;
199+
}
200+
}
201+
188202
int res = send_ssl_data(sslclient, buf, size);
189203
if (res < 0) {
190204
stop();
@@ -195,6 +209,18 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
195209

196210
int WiFiClientSecure::read(uint8_t *buf, size_t size)
197211
{
212+
if(_lastReadTimeout != _timeout){
213+
if(fd() >= 0){
214+
struct timeval timeout_tv;
215+
timeout_tv.tv_sec = _timeout / 1000;
216+
timeout_tv.tv_usec = (_timeout % 1000) * 1000;
217+
if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
218+
{
219+
_lastReadTimeout = _timeout;
220+
}
221+
}
222+
}
223+
198224
int peeked = 0;
199225
int avail = available();
200226
if ((!buf && size) || avail <= 0) {
@@ -360,22 +386,7 @@ void WiFiClientSecure::setAlpnProtocols(const char **alpn_protos)
360386
{
361387
_alpn_protos = alpn_protos;
362388
}
363-
int WiFiClientSecure::setTimeout(uint32_t seconds)
364-
{
365-
_timeout = seconds * 1000;
366-
if (sslclient->socket >= 0) {
367-
struct timeval tv;
368-
tv.tv_sec = seconds;
369-
tv.tv_usec = 0;
370-
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
371-
return -1;
372-
}
373-
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
374-
}
375-
else {
376-
return 0;
377-
}
378-
}
389+
379390
int WiFiClientSecure::setSocketOption(int option, char* value, size_t len)
380391
{
381392
int res = setsockopt(sslclient->socket, SOL_SOCKET, option, value, len);

libraries/WiFiClientSecure/src/WiFiClientSecure.h

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class WiFiClientSecure : public WiFiClient
7979
void setAlpnProtocols(const char **alpn_protos);
8080
const mbedtls_x509_crt* getPeerCertificate() { return mbedtls_ssl_get_peer_cert(&sslclient->ssl_ctx); };
8181
bool getFingerprintSHA256(uint8_t sha256_result[32]) { return get_peer_fingerprint(sslclient, sha256_result); };
82-
int setTimeout(uint32_t seconds);
8382
int setSocketOption(int option, char* value, size_t len);
8483

8584
operator bool()

0 commit comments

Comments
 (0)