@@ -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
196210int 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+
379390int WiFiClientSecure::setSocketOption (int option, char * value, size_t len)
380391{
381392 int res = setsockopt (sslclient->socket , SOL_SOCKET, option, value, len);
0 commit comments