Skip to content

Commit 0937b07

Browse files
authored
ClientContext: break timeout delays also on error while writing or connecting (esp8266#6454)
This PR stops the 1ms-delay loop also when a tcp error occurs (previously this was done only when tcp had just connected or a write/send had succeeded). The tcp error can be any, in this case with pubsubclient it is "connection refused" after the mqtt server disappeared and pubsubclient tries to reconnect.
1 parent e56aed6 commit 0937b07

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

libraries/ESP8266WiFi/src/include/ClientContext.h

+18-18
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ class ClientContext
128128
if (err != ERR_OK) {
129129
return 0;
130130
}
131-
_connect_pending = 1;
131+
_delaying = true;
132132
_op_start_time = millis();
133133
// Following delay will be interrupted by connect callback
134-
for (decltype(_timeout_ms) i = 0; _connect_pending && i < _timeout_ms; i++) {
134+
for (decltype(_timeout_ms) i = 0; _delaying && i < _timeout_ms; i++) {
135135
// Give scheduled functions a chance to run (e.g. Ethernet uses recurrent)
136136
delay(1);
137-
}
138-
_connect_pending = 0;
137+
}
138+
_delaying = false;
139139
if (!_pcb) {
140140
DEBUGV(":cabrt\r\n");
141141
return 0;
@@ -432,15 +432,16 @@ class ClientContext
432432

433433
void _notify_error()
434434
{
435-
if (_connect_pending || _send_waiting) {
436-
esp_schedule();
435+
if (_delaying) {
436+
_delaying = false;
437+
esp_schedule(); // break current delay()
437438
}
438439
}
439440

440441
size_t _write_from_source(DataSource* ds)
441442
{
442443
assert(_datasource == nullptr);
443-
assert(!_send_waiting);
444+
assert(!_delaying);
444445
_datasource = ds;
445446
_written = 0;
446447
_op_start_time = millis();
@@ -458,14 +459,14 @@ class ClientContext
458459
break;
459460
}
460461

461-
_send_waiting = true;
462+
_delaying = true;
462463
// Following delay will be interrupted by on next received ack
463-
for (decltype(_timeout_ms) i = 0; _send_waiting && i < _timeout_ms; i++) {
464+
for (decltype(_timeout_ms) i = 0; _delaying && i < _timeout_ms; i++) {
464465
// Give scheduled functions a chance to run (e.g. Ethernet uses recurrent)
465466
delay(1);
466467
}
468+
_delaying = false;
467469
} while(true);
468-
_send_waiting = false;
469470

470471
if (_sync)
471472
wait_until_sent();
@@ -532,9 +533,9 @@ class ClientContext
532533

533534
void _write_some_from_cb()
534535
{
535-
if (_send_waiting) {
536-
_send_waiting = false;
537-
esp_schedule();
536+
if (_delaying) {
537+
_delaying = false;
538+
esp_schedule(); // break current delay()
538539
}
539540
}
540541

@@ -608,9 +609,9 @@ class ClientContext
608609
(void) err;
609610
(void) pcb;
610611
assert(pcb == _pcb);
611-
assert(_connect_pending);
612-
_connect_pending = 0;
613-
esp_schedule();
612+
assert(_delaying);
613+
_delaying = false;
614+
esp_schedule(); // break current delay()
614615
return ERR_OK;
615616
}
616617

@@ -658,8 +659,7 @@ class ClientContext
658659
size_t _written = 0;
659660
uint32_t _timeout_ms = 5000;
660661
uint32_t _op_start_time = 0;
661-
bool _send_waiting = false;
662-
uint8_t _connect_pending = 0;
662+
bool _delaying = false;
663663

664664
int8_t _refcnt;
665665
ClientContext* _next;

0 commit comments

Comments
 (0)