Skip to content

Commit 278fa0d

Browse files
committed
Fix read(), peek() and available() in WiFiClientSecure
closes: espressif#2151
1 parent b37f406 commit 278fa0d

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

Diff for: libraries/WiFiClientSecure/src/WiFiClientSecure.cpp

+10-15
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,6 @@ size_t WiFiClientSecure::write(uint8_t data)
156156
int WiFiClientSecure::read()
157157
{
158158
uint8_t data = -1;
159-
160-
if(_peek >= 0){
161-
data = _peek;
162-
_peek = -1;
163-
return data;
164-
}
165-
166159
int res = read(&data, 1);
167160
if (res < 0) {
168161
return res;
@@ -186,7 +179,8 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
186179
int WiFiClientSecure::read(uint8_t *buf, size_t size)
187180
{
188181
int peeked = 0;
189-
if ((!buf && size) || (_peek < 0 && !available())) {
182+
int avail = available();
183+
if ((!buf && size) || avail <= 0) {
190184
return -1;
191185
}
192186
if(!size){
@@ -196,7 +190,8 @@ int WiFiClientSecure::read(uint8_t *buf, size_t size)
196190
buf[0] = _peek;
197191
_peek = -1;
198192
size--;
199-
if(!size || !available()){
193+
avail--;
194+
if(!size || !avail){
200195
return 1;
201196
}
202197
buf++;
@@ -206,23 +201,23 @@ int WiFiClientSecure::read(uint8_t *buf, size_t size)
206201
int res = get_ssl_receive(sslclient, buf, size);
207202
if (res < 0) {
208203
stop();
209-
return res;
204+
return peeked?peeked:res;
210205
}
211206
return res + peeked;
212207
}
213208

214209
int WiFiClientSecure::available()
215210
{
211+
int peeked = (_peek >= 0);
216212
if (!_connected) {
217-
return 0;
213+
return peeked;
218214
}
219215
int res = data_to_read(sslclient);
220-
if (res < 0 ) {
216+
if (res < 0) {
221217
stop();
222-
} else if(_peek >= 0) {
223-
res += 1;
218+
return peeked?peeked:res;
224219
}
225-
return res;
220+
return res+peeked;
226221
}
227222

228223
uint8_t WiFiClientSecure::connected()

0 commit comments

Comments
 (0)