Add WiFiClient flush to clear all non-read data in RX

fixes: https://github.com/espressif/arduino-esp32/issues/119
This commit is contained in:
me-no-dev 2017-03-02 02:49:53 +02:00
parent 72ce040785
commit 6fc96b977f
2 changed files with 28 additions and 1 deletions

View File

@ -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
@ -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;

View File

@ -49,7 +49,7 @@ public:
{
return 0;
}
void flush() {}
void flush();
void stop();
uint8_t connected();