Skip to content

Commit 6fc96b9

Browse files
committed
Add WiFiClient flush to clear all non-read data in RX
fixes: #119
1 parent 72ce040 commit 6fc96b9

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Diff for: libraries/WiFi/src/WiFiClient.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#define WIFI_CLIENT_MAX_WRITE_RETRY (10)
2626
#define WIFI_CLIENT_SELECT_TIMEOUT_US (100000)
27+
#define WIFI_CLIENT_FLUSH_BUFFER_SIZE (1024)
2728

2829
#undef connect
2930
#undef write
@@ -251,6 +252,32 @@ int WiFiClient::available()
251252
return count;
252253
}
253254

255+
// Though flushing means to send all pending data,
256+
// seems that in Arduino it also means to clear RX
257+
void WiFiClient::flush() {
258+
size_t a = available(), toRead = 0;
259+
if(!a){
260+
return;//nothing to flush
261+
}
262+
uint8_t * buf = (uint8_t *)malloc(WIFI_CLIENT_FLUSH_BUFFER_SIZE);
263+
if(!buf){
264+
return;//memory error
265+
}
266+
while(a){
267+
toRead = (a>WIFI_CLIENT_FLUSH_BUFFER_SIZE)?WIFI_CLIENT_FLUSH_BUFFER_SIZE:a;
268+
if(recv(fd(), buf, toRead, MSG_DONTWAIT) < 0) {
269+
if(errno != EWOULDBLOCK){
270+
log_e("%d", errno);
271+
stop();
272+
break;
273+
}
274+
delay(1);//give some time
275+
}
276+
a = available();
277+
}
278+
free(buf);
279+
}
280+
254281
uint8_t WiFiClient::connected()
255282
{
256283
uint8_t dummy = 0;

Diff for: libraries/WiFi/src/WiFiClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class WiFiClient : public Client
4949
{
5050
return 0;
5151
}
52-
void flush() {}
52+
void flush();
5353
void stop();
5454
uint8_t connected();
5555

0 commit comments

Comments
 (0)