Fix read(), peek() and available() in WiFiClientSecure

closes: https://github.com/espressif/arduino-esp32/pull/2151
This commit is contained in:
me-no-dev 2018-12-15 18:14:38 +01:00
parent b37f4069e4
commit 278fa0d87a

View File

@ -156,13 +156,6 @@ size_t WiFiClientSecure::write(uint8_t data)
int WiFiClientSecure::read() int WiFiClientSecure::read()
{ {
uint8_t data = -1; uint8_t data = -1;
if(_peek >= 0){
data = _peek;
_peek = -1;
return data;
}
int res = read(&data, 1); int res = read(&data, 1);
if (res < 0) { if (res < 0) {
return res; return res;
@ -186,7 +179,8 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
int WiFiClientSecure::read(uint8_t *buf, size_t size) int WiFiClientSecure::read(uint8_t *buf, size_t size)
{ {
int peeked = 0; int peeked = 0;
if ((!buf && size) || (_peek < 0 && !available())) { int avail = available();
if ((!buf && size) || avail <= 0) {
return -1; return -1;
} }
if(!size){ if(!size){
@ -196,7 +190,8 @@ int WiFiClientSecure::read(uint8_t *buf, size_t size)
buf[0] = _peek; buf[0] = _peek;
_peek = -1; _peek = -1;
size--; size--;
if(!size || !available()){ avail--;
if(!size || !avail){
return 1; return 1;
} }
buf++; buf++;
@ -206,23 +201,23 @@ int WiFiClientSecure::read(uint8_t *buf, size_t size)
int res = get_ssl_receive(sslclient, buf, size); int res = get_ssl_receive(sslclient, buf, size);
if (res < 0) { if (res < 0) {
stop(); stop();
return res; return peeked?peeked:res;
} }
return res + peeked; return res + peeked;
} }
int WiFiClientSecure::available() int WiFiClientSecure::available()
{ {
int peeked = (_peek >= 0);
if (!_connected) { if (!_connected) {
return 0; return peeked;
} }
int res = data_to_read(sslclient); int res = data_to_read(sslclient);
if (res < 0 ) { if (res < 0) {
stop(); stop();
} else if(_peek >= 0) { return peeked?peeked:res;
res += 1;
} }
return res; return res+peeked;
} }
uint8_t WiFiClientSecure::connected() uint8_t WiFiClientSecure::connected()