From 6fc96b977f5b4b110cb174fff3327ef9245ee0d6 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Thu, 2 Mar 2017 02:49:53 +0200 Subject: [PATCH] Add WiFiClient flush to clear all non-read data in RX fixes: https://github.com/espressif/arduino-esp32/issues/119 --- libraries/WiFi/src/WiFiClient.cpp | 27 +++++++++++++++++++++++++++ libraries/WiFi/src/WiFiClient.h | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp index 20041bdc..a8f81b32 100644 --- a/libraries/WiFi/src/WiFiClient.cpp +++ b/libraries/WiFi/src/WiFiClient.cpp @@ -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; diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h index 02df4319..36f66e16 100644 --- a/libraries/WiFi/src/WiFiClient.h +++ b/libraries/WiFi/src/WiFiClient.h @@ -49,7 +49,7 @@ public: { return 0; } - void flush() {} + void flush(); void stop(); uint8_t connected();