@@ -250,19 +250,32 @@ void WiFiClientSecureCtx::_freeSSL() {
250
250
}
251
251
252
252
bool WiFiClientSecureCtx::_clientConnected () {
253
- return (_client && _client->state () == ESTABLISHED);
253
+ if (!_client || (_client->state () == CLOSED)) {
254
+ return false ;
255
+ }
256
+
257
+ return _client->state () == ESTABLISHED;
258
+ }
259
+
260
+ bool WiFiClientSecureCtx::_engineConnected () {
261
+ return _clientConnected () && _handshake_done && _eng && (br_ssl_engine_current_state (_eng) != BR_SSL_CLOSED);
254
262
}
255
263
256
264
uint8_t WiFiClientSecureCtx::connected () {
257
- if (available () || (_clientConnected () && _handshake_done && (br_ssl_engine_current_state (_eng) != BR_SSL_CLOSED))) {
265
+ if (!_engineConnected ()) {
266
+ return false ;
267
+ }
268
+
269
+ if (_pollRecvBuffer () > 0 ) {
258
270
return true ;
259
271
}
260
- return false ;
272
+
273
+ return _engineConnected ();
261
274
}
262
275
263
276
int WiFiClientSecureCtx::availableForWrite () {
264
- // code taken from ::_write()
265
- if (!connected () || !_handshake_done ) {
277
+ // Can't write things when there's no connection or br_ssl engine is closed
278
+ if (!_engineConnected () ) {
266
279
return 0 ;
267
280
}
268
281
// Get BearSSL to a state where we can send
@@ -284,7 +297,7 @@ int WiFiClientSecureCtx::availableForWrite () {
284
297
size_t WiFiClientSecureCtx::_write (const uint8_t *buf, size_t size, bool pmem) {
285
298
size_t sent_bytes = 0 ;
286
299
287
- if (!connected () || ! size || !_handshake_done ) {
300
+ if (!size || !_engineConnected () ) {
288
301
return 0 ;
289
302
}
290
303
@@ -331,10 +344,11 @@ size_t WiFiClientSecureCtx::write_P(PGM_P buf, size_t size) {
331
344
}
332
345
333
346
size_t WiFiClientSecureCtx::write (Stream& stream) {
334
- if (!connected () || !_handshake_done ) {
335
- DEBUG_BSSL (" write: Connect/handshake not completed yet \n " );
347
+ if (!_engineConnected () ) {
348
+ DEBUG_BSSL (" write: no br_ssl engine to work with \n " );
336
349
return 0 ;
337
350
}
351
+
338
352
return stream.sendAll (this );
339
353
}
340
354
@@ -343,12 +357,20 @@ int WiFiClientSecureCtx::read(uint8_t *buf, size_t size) {
343
357
return -1 ;
344
358
}
345
359
346
- int avail = available ();
347
- bool conn = connected ();
348
- if (!avail && conn) {
349
- return 0 ; // We're still connected, but nothing to read
360
+ // will either check the internal buffer, or try to wait for some data
361
+ // *may* attempt to write some pending ::write() data b/c of _run_until
362
+ int avail = _pollRecvBuffer ();
363
+
364
+ // internal buffer might still be available for some time
365
+ bool engine = _engineConnected ();
366
+
367
+ // we're still connected, but nothing to read
368
+ if (!avail && engine) {
369
+ return 0 ;
350
370
}
351
- if (!avail && !conn) {
371
+
372
+ // or, available failed to assign the internal buffer and we are already disconnected
373
+ if (!avail && !engine) {
352
374
DEBUG_BSSL (" read: Not connected, none left available\n " );
353
375
return -1 ;
354
376
}
@@ -363,10 +385,11 @@ int WiFiClientSecureCtx::read(uint8_t *buf, size_t size) {
363
385
return to_copy;
364
386
}
365
387
366
- if (!conn ) {
388
+ if (!engine ) {
367
389
DEBUG_BSSL (" read: Not connected\n " );
368
390
return -1 ;
369
391
}
392
+
370
393
return 0 ; // If we're connected, no error but no read.
371
394
}
372
395
@@ -395,7 +418,7 @@ int WiFiClientSecureCtx::read() {
395
418
return -1 ;
396
419
}
397
420
398
- int WiFiClientSecureCtx::available () {
421
+ int WiFiClientSecureCtx::_pollRecvBuffer () {
399
422
if (_recvapp_buf) {
400
423
return _recvapp_len; // Anything from last call?
401
424
}
@@ -416,8 +439,12 @@ int WiFiClientSecureCtx::available() {
416
439
return 0 ;
417
440
}
418
441
442
+ int WiFiClientSecureCtx::available () {
443
+ return _pollRecvBuffer ();
444
+ }
445
+
419
446
int WiFiClientSecureCtx::peek () {
420
- if (!ctx_present () || ! available ( )) {
447
+ if (!ctx_present () || ( 0 == _pollRecvBuffer () )) {
421
448
DEBUG_BSSL (" peek: Not connected, none left available\n " );
422
449
return -1 ;
423
450
}
@@ -436,7 +463,7 @@ size_t WiFiClientSecureCtx::peekBytes(uint8_t *buffer, size_t length) {
436
463
}
437
464
438
465
_startMillis = millis ();
439
- while ((available () < (int ) length) && ((millis () - _startMillis) < 5000 )) {
466
+ while ((_pollRecvBuffer () < (int ) length) && ((millis () - _startMillis) < 5000 )) {
440
467
yield ();
441
468
}
442
469
0 commit comments