Fix peek, read and available logic in WiFiClientSecure

This commit is contained in:
me-no-dev 2018-06-27 22:09:21 +02:00
parent 058645264c
commit 873876d94c

View File

@ -81,6 +81,7 @@ void WiFiClientSecure::stop()
close(sslclient->socket); close(sslclient->socket);
sslclient->socket = -1; sslclient->socket = -1;
_connected = false; _connected = false;
_peek = -1;
} }
stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key); stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key);
} }
@ -105,7 +106,7 @@ int WiFiClientSecure::connect(const char *host, uint16_t port, const char *_CA_c
int ret = start_ssl_client(sslclient, host, port, _CA_cert, _cert, _private_key); int ret = start_ssl_client(sslclient, host, port, _CA_cert, _cert, _private_key);
_lastError = ret; _lastError = ret;
if (ret < 0) { if (ret < 0) {
log_e("lwip_connect_r: %d", errno); log_e("start_ssl_client: %d", ret);
stop(); stop();
return 0; return 0;
} }
@ -117,7 +118,7 @@ int WiFiClientSecure::peek(){
if(_peek >= 0){ if(_peek >= 0){
return _peek; return _peek;
} }
_peek = read(); _peek = timedRead();
return _peek; return _peek;
} }
@ -158,21 +159,30 @@ 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)
{ {
if(_peek >= 0){ int peeked = 0;
uint8_t data = -1; if ((!buf && size) || (_peek < 0 && !available())) {
data = _peek;
_peek = -1;
return data;
}
if (!available()) {
return -1; return -1;
} }
if(!size){
return 0;
}
if(_peek >= 0){
buf[0] = _peek;
_peek = -1;
size--;
if(!size || !available()){
return 1;
}
buf++;
peeked = 1;
}
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 res;
}
return res + peeked;
} }
int WiFiClientSecure::available() int WiFiClientSecure::available()
@ -183,6 +193,8 @@ int WiFiClientSecure::available()
int res = data_to_read(sslclient); int res = data_to_read(sslclient);
if (res < 0 ) { if (res < 0 ) {
stop(); stop();
} else if(_peek >= 0) {
res += 1;
} }
return res; return res;
} }