@@ -165,18 +165,32 @@ IoT_Error_t iot_tls_read(Network *pNetwork, unsigned char *pMsg, size_t len, Tim
165
165
}
166
166
167
167
while (len > 0 ) {
168
- /* Make sure we never block on read for longer than timer has left,
169
- but also that we don't block indefinitely (ie read_timeout > 0) */
170
- read_timeout = MAX (1 , MIN (read_timeout , left_ms (timer )));
171
- timeout .tv_sec = read_timeout / 1000 ;
172
- timeout .tv_usec = (read_timeout % 1000 ) * 1000 ;
168
+ if (esp_tls_get_bytes_avail (tls ) <= 0 ) {
169
+ fd_set read_fs , error_fs ;
173
170
174
- ret = setsockopt (tls -> sockfd , SOL_SOCKET , SO_RCVTIMEO , (char * )& timeout , sizeof (struct timeval ));
175
- ret = esp_tls_conn_read (tls , pMsg , len );
171
+ /* Make sure we never block on read for longer than timer has left,
172
+ but also that we don't block indefinitely (ie read_timeout > 0) */
173
+ read_timeout = MAX (1 , MIN (read_timeout , left_ms (timer )));
174
+ timeout .tv_sec = read_timeout / 1000 ;
175
+ timeout .tv_usec = (read_timeout % 1000 ) * 1000 ;
176
+
177
+ FD_CLR (tls -> sockfd , & read_fs );
178
+ FD_CLR (tls -> sockfd , & error_fs );
179
+
180
+ FD_SET (tls -> sockfd , & read_fs );
181
+ FD_SET (tls -> sockfd , & error_fs );
176
182
177
- /* Restore the old timeout */
178
- tlsDataParams -> timeout = read_timeout ;
183
+ ret = select (tls -> sockfd + 1 , & read_fs , NULL , & error_fs , & timeout );
184
+ if (!ret )
185
+ goto check_time ;
186
+ else if (ret < 0 )
187
+ return NETWORK_SSL_READ_ERROR ;
179
188
189
+ if (FD_ISSET (tls -> sockfd , & error_fs ))
190
+ return NETWORK_SSL_READ_ERROR ;
191
+ }
192
+
193
+ ret = esp_tls_conn_read (tls , pMsg , len );
180
194
if (ret > 0 ) {
181
195
rxLen += ret ;
182
196
pMsg += ret ;
@@ -185,6 +199,7 @@ IoT_Error_t iot_tls_read(Network *pNetwork, unsigned char *pMsg, size_t len, Tim
185
199
return NETWORK_SSL_READ_ERROR ;
186
200
}
187
201
202
+ check_time :
188
203
// Evaluate timeout after the read to make sure read is done at least once
189
204
if (has_timer_expired (timer )) {
190
205
break ;
0 commit comments