Skip to content

Commit 26980b3

Browse files
d-a-vdevyte
authored andcommitted
fix #1002 ::Flush() wait for empty send buffer (#3967)
* fix #1002 ::Flush() wait for empty send buffer * 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 affd1b3 commit 26980b3

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

libraries/ESP8266WiFi/src/WiFiClient.cpp

+1-1
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->flush();
262+
_client->wait_until_sent();
263263
}
264264

265265
void WiFiClient::stop()

libraries/ESP8266WiFi/src/WiFiUdp.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ int WiFiUDP::peek()
243243

244244
void WiFiUDP::flush()
245245
{
246-
if (_ctx)
247-
_ctx->flush();
246+
endPacket();
248247
}
249248

250249
IPAddress WiFiUDP::remoteIP()

libraries/ESP8266WiFi/src/include/ClientContext.h

+18-2
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,6 +290,22 @@ class ClientContext
290290
_rx_buf_offset = 0;
291291
}
292292

293+
void wait_until_sent()
294+
{
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) {
304+
_write_some();
305+
delay(1); // esp_ schedule+yield
306+
}
307+
}
308+
293309
uint8_t state() const
294310
{
295311
if(!_pcb) {

libraries/Ethernet/src/EthernetUdp.cpp

+7-2
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-
flush();
121+
clear_remaining();
122122

123123
if (recvAvailable(_sock) > 0)
124124
{
@@ -204,7 +204,7 @@ int EthernetUDP::peek()
204204
return b;
205205
}
206206

207-
void EthernetUDP::flush()
207+
void EthernetUDP::clear_remaining()
208208
{
209209
// could this fail (loop endlessly) if _remaining > 0 and recv in read fails?
210210
// should only occur if recv fails after telling us the data is there, lets
@@ -216,3 +216,8 @@ void EthernetUDP::flush()
216216
}
217217
}
218218

219+
void EthernetUDP::flush()
220+
{
221+
endPacket();
222+
}
223+

libraries/Ethernet/src/EthernetUdp.h

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class EthernetUDP : public UDP {
4949
uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
5050
uint16_t _offset; // offset into the packet being sent
5151
uint16_t _remaining; // remaining bytes of incoming packet yet to be processed
52+
53+
protected:
54+
void clear_remaining();
5255

5356
public:
5457
EthernetUDP(); // Constructor

0 commit comments

Comments
 (0)