Skip to content

Commit

Permalink
Fix read(), peek() and available() in WiFiClientSecure
Browse files Browse the repository at this point in the history
closes: #2151
  • Loading branch information
me-no-dev committed Dec 15, 2018
1 parent b37f406 commit 278fa0d
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ size_t WiFiClientSecure::write(uint8_t data)
int WiFiClientSecure::read()
{
uint8_t data = -1;

if(_peek >= 0){
data = _peek;
_peek = -1;
return data;
}

int res = read(&data, 1);
if (res < 0) {
return res;
Expand All @@ -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 peeked = 0;
if ((!buf && size) || (_peek < 0 && !available())) {
int avail = available();
if ((!buf && size) || avail <= 0) {
return -1;
}
if(!size){
Expand All @@ -196,7 +190,8 @@ int WiFiClientSecure::read(uint8_t *buf, size_t size)
buf[0] = _peek;
_peek = -1;
size--;
if(!size || !available()){
avail--;
if(!size || !avail){
return 1;
}
buf++;
Expand All @@ -206,23 +201,23 @@ int WiFiClientSecure::read(uint8_t *buf, size_t size)
int res = get_ssl_receive(sslclient, buf, size);
if (res < 0) {
stop();
return res;
return peeked?peeked:res;
}
return res + peeked;
}

int WiFiClientSecure::available()
{
int peeked = (_peek >= 0);
if (!_connected) {
return 0;
return peeked;
}
int res = data_to_read(sslclient);
if (res < 0 ) {
if (res < 0) {
stop();
} else if(_peek >= 0) {
res += 1;
return peeked?peeked:res;
}
return res;
return res+peeked;
}

uint8_t WiFiClientSecure::connected()
Expand Down

0 comments on commit 278fa0d

Please sign in to comment.