Skip to content

Commit 899dff9

Browse files
Jason2866me-no-devlucasssvaz
authored
New client flush (#274)
* Bugfix/wificlient (#273) * WiFiClient - Properly initialize and check _rxBuffer --------- Co-authored-by: Me No Dev <[email protected]> Co-authored-by: Lucas Saavedra Vaz <[email protected]>
1 parent 415063b commit 899dff9

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

libraries/WiFi/src/WiFiClient.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ class WiFiClientRxBuffer {
158158
size_t available(){
159159
return _fill - _pos + r_available();
160160
}
161+
162+
void flush(){
163+
if(r_available()){
164+
fillBuffer();
165+
}
166+
_pos = _fill;
167+
}
161168
};
162169

163170
class WiFiClientSocketHandle {
@@ -180,7 +187,7 @@ class WiFiClientSocketHandle {
180187
}
181188
};
182189

183-
WiFiClient::WiFiClient():_connected(false),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS),next(NULL)
190+
WiFiClient::WiFiClient():_rxBuffer(nullptr),_connected(false),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS),next(NULL)
184191
{
185192
}
186193

@@ -529,26 +536,9 @@ int WiFiClient::available()
529536
// Though flushing means to send all pending data,
530537
// seems that in Arduino it also means to clear RX
531538
void WiFiClient::flush() {
532-
int res;
533-
size_t a = available(), toRead = 0;
534-
if(!a){
535-
return;//nothing to flush
536-
}
537-
uint8_t * buf = (uint8_t *)malloc(WIFI_CLIENT_FLUSH_BUFFER_SIZE);
538-
if(!buf){
539-
return;//memory error
539+
if (_rxBuffer != nullptr) {
540+
_rxBuffer->flush();
540541
}
541-
while(a){
542-
toRead = (a>WIFI_CLIENT_FLUSH_BUFFER_SIZE)?WIFI_CLIENT_FLUSH_BUFFER_SIZE:a;
543-
res = recv(fd(), buf, toRead, MSG_DONTWAIT);
544-
if(res < 0) {
545-
log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
546-
stop();
547-
break;
548-
}
549-
a -= res;
550-
}
551-
free(buf);
552542
}
553543

554544
uint8_t WiFiClient::connected()

0 commit comments

Comments
 (0)