Skip to content

Commit e4c187b

Browse files
committed
update flush()
1 parent d0036a1 commit e4c187b

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

libraries/ESP8266WiFi/src/WiFiClient.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,21 @@ size_t WiFiClient::peekBytes(uint8_t *buffer, size_t length) {
285285
return _client->peekBytes((char *)buffer, count);
286286
}
287287

288-
void WiFiClient::flush()
288+
bool WiFiClient::flush(int maxWaitMs)
289289
{
290290
if (_client)
291-
_client->wait_until_sent();
291+
return !_client || _client->wait_until_sent(maxWaitMs);
292+
return true;
292293
}
293294

294-
void WiFiClient::stop()
295+
bool WiFiClient::stop(int maxWaitMs)
295296
{
296297
if (!_client)
297-
return;
298+
return true;
298299

299-
_client->close();
300+
bool ok = _client->wait_until_sent(maxWaitMs);
301+
ok &= _client->close() == ERR_OK;
302+
return ok;
300303
}
301304

302305
uint8_t WiFiClient::connected()

libraries/ESP8266WiFi/src/WiFiClient.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
#include "IPAddress.h"
2929
#include "include/slist.h"
3030

31-
#define WIFICLIENT_MAX_PACKET_SIZE 1460
31+
#ifndef TCP_MSS
32+
#define TCP_MSS 1460 // lwip1.4
33+
#endif
34+
35+
#define WIFICLIENT_MAX_PACKET_SIZE TCP_MSS
36+
#define WIFICLIENT_MAX_FLUSH_WAIT_MS 100
3237

3338
#define TCP_DEFAULT_KEEPALIVE_IDLE_SEC 7200 // 2 hours
3439
#define TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC 75 // 75 sec
@@ -67,8 +72,10 @@ class WiFiClient : public Client, public SList<WiFiClient> {
6772
size_t peekBytes(char *buffer, size_t length) {
6873
return peekBytes((uint8_t *) buffer, length);
6974
}
70-
virtual void flush();
71-
virtual void stop();
75+
bool flush(int maxWaitMs);
76+
bool stop(int maxWaitMs);
77+
virtual void flush() { flush(WIFICLIENT_MAX_FLUSH_WAIT_MS); }
78+
virtual void stop() { stop(WIFICLIENT_MAX_FLUSH_WAIT_MS); }
7279
virtual uint8_t connected();
7380
virtual operator bool();
7481

libraries/ESP8266WiFi/src/include/ClientContext.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,30 @@ class ClientContext
296296
_rx_buf_offset = 0;
297297
}
298298

299-
void wait_until_sent()
299+
bool wait_until_sent(int max_wait_ms = WIFICLIENT_MAX_FLUSH_WAIT_MS)
300300
{
301-
// fix option 1 in
302-
// https://github.com/esp8266/Arduino/pull/3967#pullrequestreview-83451496
303-
// TODO: option 2
301+
if (!_pcb)
302+
return;
303+
tcp_output(_pcb);
304304

305-
#define WAIT_TRIES_MS 10 // at most 10ms
305+
// https://github.com/esp8266/Arduino/pull/3967#pullrequestreview-83451496
306+
// option 1 done
307+
// option 2 / _write_some() not necessary since _datasource is always nullptr here
306308

307-
int tries = 1+ WAIT_TRIES_MS;
309+
max_wait_ms++;
308310

309-
while (state() == ESTABLISHED && tcp_sndbuf(_pcb) != TCP_SND_BUF && --tries) {
310-
//_write_some();
311+
// wait for peer's acks flushing lwIP's output buffer
312+
while (state() == ESTABLISHED && tcp_sndbuf(_pcb) != TCP_SND_BUF && --max_wait_ms)
311313
delay(1); // esp_ schedule+yield
314+
315+
#ifdef DEBUGV
316+
if (max_wait_ms == 0) {
317+
// wait until sent: timeout
318+
DEBUGV(":wustmo\n");
312319
}
320+
#endif
321+
322+
return max_wait_ms > 0;
313323
}
314324

315325
uint8_t state() const

0 commit comments

Comments
 (0)