@@ -54,6 +54,8 @@ WiFiClientSecure::WiFiClientSecure(int sock)
54
54
{
55
55
_connected = false ;
56
56
_timeout = 30000 ; // Same default as ssl_client
57
+ _lastReadTimeout = 0 ;
58
+ _lastWriteTimeout = 0 ;
57
59
58
60
sslclient = new sslclient_context;
59
61
ssl_init (sslclient);
@@ -94,6 +96,8 @@ void WiFiClientSecure::stop()
94
96
sslclient->socket = -1 ;
95
97
_connected = false ;
96
98
_peek = -1 ;
99
+ _lastReadTimeout = 0 ;
100
+ _lastWriteTimeout = 0 ;
97
101
}
98
102
stop_ssl_socket (sslclient, _CA_cert, _cert, _private_key);
99
103
}
@@ -185,6 +189,16 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
185
189
if (!_connected) {
186
190
return 0 ;
187
191
}
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
+
188
202
int res = send_ssl_data (sslclient, buf, size);
189
203
if (res < 0 ) {
190
204
stop ();
@@ -195,6 +209,18 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
195
209
196
210
int WiFiClientSecure::read (uint8_t *buf, size_t size)
197
211
{
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
+
198
224
int peeked = 0 ;
199
225
int avail = available ();
200
226
if ((!buf && size) || avail <= 0 ) {
@@ -360,22 +386,7 @@ void WiFiClientSecure::setAlpnProtocols(const char **alpn_protos)
360
386
{
361
387
_alpn_protos = alpn_protos;
362
388
}
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
+
379
390
int WiFiClientSecure::setSocketOption (int option, char * value, size_t len)
380
391
{
381
392
int res = setsockopt (sslclient->socket , SOL_SOCKET, option, value, len);
0 commit comments