Skip to content

Commit a6a7eb5

Browse files
committed
WiFiClient::Flush() guarantees that the data has been delivered
option 1 of #3967 (comment) 10ms max wait according to loaded tcp echo/reply scheme
1 parent 6a933ef commit a6a7eb5

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

libraries/ESP8266WiFi/src/WiFiClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ size_t WiFiClient::peekBytes(uint8_t *buffer, size_t length) {
259259
void WiFiClient::flush()
260260
{
261261
if (_client)
262-
_client->arduinoFlush();
262+
_client->wait_until_sent();
263263
}
264264

265265
void WiFiClient::stop()

libraries/ESP8266WiFi/src/include/ClientContext.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class ClientContext
107107
if(this != 0) {
108108
DEBUGV(":ur %d\r\n", _refcnt);
109109
if(--_refcnt == 0) {
110-
flush();
110+
discard_received();
111111
close();
112112
if(_discard_cb) {
113113
_discard_cb(_discard_cb_arg, this);
@@ -277,7 +277,7 @@ class ClientContext
277277
return copy_size;
278278
}
279279

280-
void flush()
280+
void discard_received()
281281
{
282282
if(!_rx_buf) {
283283
return;
@@ -290,15 +290,20 @@ class ClientContext
290290
_rx_buf_offset = 0;
291291
}
292292

293-
void arduinoFlush()
293+
void wait_until_sent()
294294
{
295-
#if 1
296-
// this is useless since _write_from_source() always exits with _datasource==NULL
297-
while (state() == ESTABLISHED && _datasource && _datasource->available()) {
295+
// fix option 1 in
296+
// https://github.com/esp8266/Arduino/pull/3967#pullrequestreview-83451496
297+
// TODO: option 2
298+
299+
#define WAIT_TRIES_MS 10 // at most 10ms
300+
301+
int tries = 1+ WAIT_TRIES_MS;
302+
303+
while (state() == ESTABLISHED && tcp_sndbuf(_pcb) != TCP_SND_BUF && --tries) {
298304
_write_some();
299305
delay(1); // esp_ schedule+yield
300306
}
301-
#endif
302307
}
303308

304309
uint8_t state() const

libraries/Ethernet/src/EthernetUdp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ size_t EthernetUDP::write(const uint8_t *buffer, size_t size)
118118
int EthernetUDP::parsePacket()
119119
{
120120
// discard any remaining bytes in the last packet
121-
clear_incoming();
121+
clear_remaining();
122122

123123
if (recvAvailable(_sock) > 0)
124124
{

0 commit comments

Comments
 (0)