Skip to content

Commit

Permalink
Add WiFiClient flush to clear all non-read data in RX
Browse files Browse the repository at this point in the history
fixes: #119
  • Loading branch information
me-no-dev committed Mar 2, 2017
1 parent 72ce040 commit 6fc96b9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions libraries/WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#define WIFI_CLIENT_MAX_WRITE_RETRY (10)
#define WIFI_CLIENT_SELECT_TIMEOUT_US (100000)
#define WIFI_CLIENT_FLUSH_BUFFER_SIZE (1024)

#undef connect
#undef write
Expand Down Expand Up @@ -251,6 +252,32 @@ int WiFiClient::available()
return count;
}

// Though flushing means to send all pending data,
// seems that in Arduino it also means to clear RX
void WiFiClient::flush() {
size_t a = available(), toRead = 0;
if(!a){
return;//nothing to flush
}
uint8_t * buf = (uint8_t *)malloc(WIFI_CLIENT_FLUSH_BUFFER_SIZE);
if(!buf){
return;//memory error
}
while(a){
toRead = (a>WIFI_CLIENT_FLUSH_BUFFER_SIZE)?WIFI_CLIENT_FLUSH_BUFFER_SIZE:a;
if(recv(fd(), buf, toRead, MSG_DONTWAIT) < 0) {
if(errno != EWOULDBLOCK){
log_e("%d", errno);
stop();
break;
}
delay(1);//give some time
}
a = available();
}
free(buf);
}

uint8_t WiFiClient::connected()
{
uint8_t dummy = 0;
Expand Down
2 changes: 1 addition & 1 deletion libraries/WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class WiFiClient : public Client
{
return 0;
}
void flush() {}
void flush();
void stop();
uint8_t connected();

Expand Down

0 comments on commit 6fc96b9

Please sign in to comment.