Skip to content

Commit febcda0

Browse files
copercinime-no-dev
authored andcommitted
add WiFiClientSecure::peek(); (#1310)
1 parent 69f72ec commit febcda0

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

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

+27-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ WiFiClientSecure::WiFiClientSecure()
3939
_CA_cert = NULL;
4040
_cert = NULL;
4141
_private_key = NULL;
42-
next = NULL;
42+
next = NULL;
4343
}
4444

4545

@@ -58,13 +58,13 @@ WiFiClientSecure::WiFiClientSecure(int sock)
5858
_CA_cert = NULL;
5959
_cert = NULL;
6060
_private_key = NULL;
61-
next = NULL;
61+
next = NULL;
6262
}
6363

6464
WiFiClientSecure::~WiFiClientSecure()
6565
{
6666
stop();
67-
delete sslclient;
67+
delete sslclient;
6868
}
6969

7070
WiFiClientSecure &WiFiClientSecure::operator=(const WiFiClientSecure &other)
@@ -113,14 +113,29 @@ int WiFiClientSecure::connect(const char *host, uint16_t port, const char *_CA_c
113113
return 1;
114114
}
115115

116+
int WiFiClientSecure::peek(){
117+
if(_peek >= 0){
118+
return _peek;
119+
}
120+
_peek = read();
121+
return _peek;
122+
}
123+
116124
size_t WiFiClientSecure::write(uint8_t data)
117125
{
118126
return write(&data, 1);
119127
}
120128

121129
int WiFiClientSecure::read()
122130
{
123-
uint8_t data = 0;
131+
uint8_t data = -1;
132+
133+
if(_peek >= 0){
134+
data = _peek;
135+
_peek = -1;
136+
return data;
137+
}
138+
124139
int res = read(&data, 1);
125140
if (res < 0) {
126141
return res;
@@ -143,6 +158,13 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
143158

144159
int WiFiClientSecure::read(uint8_t *buf, size_t size)
145160
{
161+
if(_peek >= 0){
162+
uint8_t data = -1;
163+
data = _peek;
164+
_peek = -1;
165+
return data;
166+
}
167+
146168
if (!available()) {
147169
return -1;
148170
}
@@ -161,7 +183,7 @@ int WiFiClientSecure::available()
161183
int res = data_to_read(sslclient);
162184
if (res < 0 ) {
163185
stop();
164-
}
186+
}
165187
return res;
166188
}
167189

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class WiFiClientSecure : public WiFiClient
3131
sslclient_context *sslclient;
3232

3333
int _lastError = 0;
34+
int _peek = -1;
3435
const char *_CA_cert;
3536
const char *_cert;
3637
const char *_private_key;
@@ -44,15 +45,12 @@ class WiFiClientSecure : public WiFiClient
4445
int connect(const char *host, uint16_t port);
4546
int connect(IPAddress ip, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key);
4647
int connect(const char *host, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key);
48+
int peek();
4749
size_t write(uint8_t data);
4850
size_t write(const uint8_t *buf, size_t size);
4951
int available();
5052
int read();
5153
int read(uint8_t *buf, size_t size);
52-
int peek()
53-
{
54-
return 0;
55-
}
5654
void flush() {}
5755
void stop();
5856
uint8_t connected();

0 commit comments

Comments
 (0)