Skip to content

Commit 715e306

Browse files
authoredMay 27, 2024··
Merge pull request #309 from andreagilardoni/wificlient-issues
WifiS3 Wificlient issues
2 parents 0620eb3 + c9a7485 commit 715e306

File tree

4 files changed

+79
-66
lines changed

4 files changed

+79
-66
lines changed
 

‎libraries/WiFiS3/src/WiFiClient.cpp

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
using namespace std;
44

55
/* -------------------------------------------------------------------------- */
6-
WiFiClient::WiFiClient() : _sock(-1), destroy_at_distructor(true), rx_buffer(nullptr) {
6+
WiFiClient::WiFiClient() : _sock(-1), destroy_at_distructor(true), rx_buffer(nullptr) {
77
rx_buffer = std::shared_ptr<FifoBuffer<uint8_t,RX_BUFFER_DIM>>(new FifoBuffer<uint8_t,RX_BUFFER_DIM>());
88
}
99
/* -------------------------------------------------------------------------- */
10-
10+
1111
/* -------------------------------------------------------------------------- */
1212
WiFiClient::WiFiClient(int s) : _sock(s), destroy_at_distructor(false), rx_buffer(nullptr) {
1313
rx_buffer = std::shared_ptr<FifoBuffer<uint8_t,RX_BUFFER_DIM>>(new FifoBuffer<uint8_t,RX_BUFFER_DIM>());
@@ -18,23 +18,29 @@ WiFiClient::WiFiClient(int s) : _sock(s), destroy_at_distructor(false), rx_buffe
1818
WiFiClient::~WiFiClient() { }
1919
/* -------------------------------------------------------------------------- */
2020

21-
/* -------------------------------------------------------------------------- */
21+
/* -------------------------------------------------------------------------- */
2222
WiFiClient::WiFiClient(const WiFiClient& c) {
23-
/* -------------------------------------------------------------------------- */
23+
/* -------------------------------------------------------------------------- */
2424
_sock = c._sock;
2525
rx_buffer = c.rx_buffer;
2626
}
2727

2828
/* -------------------------------------------------------------------------- */
2929
void WiFiClient::getSocket() {
3030
/* -------------------------------------------------------------------------- */
31+
if(_sock >= 0 && !connected()) {
32+
// if sock >= 0 -> it means we were connected, but something happened and we need
33+
// to reset this socket in order to be able to connect again
34+
stop();
35+
}
36+
3137
if(_sock == -1) {
3238
string res = "";
3339
modem.begin();
3440
if(modem.write(string(PROMPT(_BEGINCLIENT)),res, "%s" , CMD(_BEGINCLIENT))) {
3541
_sock = atoi(res.c_str());
3642
}
37-
}
43+
}
3844
}
3945

4046
/* -------------------------------------------------------------------------- */
@@ -45,7 +51,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port){
4551

4652
/* -------------------------------------------------------------------------- */
4753
int WiFiClient::connect(const char *host, uint16_t port){
48-
/* -------------------------------------------------------------------------- */
54+
/* -------------------------------------------------------------------------- */
4955
getSocket();
5056
if(_sock >= 0) {
5157
string res = "";
@@ -55,17 +61,18 @@ int WiFiClient::connect(const char *host, uint16_t port){
5561
return 1;
5662
}
5763
} else {
58-
if(modem.write(string(PROMPT(_CLIENTCONNECTNAME)),res, "%s%d,%s,%d\r\n" , CMD_WRITE(_CLIENTCONNECTNAME), _sock, host,port)) {
59-
return 1;
60-
}
64+
if(modem.write(string(PROMPT(_CLIENTCONNECTNAME)),res, "%s%d,%s,%d\r\n" , CMD_WRITE(_CLIENTCONNECTNAME), _sock, host,port)) {
65+
return 1;
66+
}
6167
}
6268
}
69+
6370
return 0;
6471
}
6572

6673
/* -------------------------------------------------------------------------- */
6774
size_t WiFiClient::write(uint8_t b){
68-
/* -------------------------------------------------------------------------- */
75+
/* -------------------------------------------------------------------------- */
6976
return write(&b, 1);
7077
}
7178

@@ -79,15 +86,14 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size){
7986
if(modem.passthrough(buf,size)) {
8087
return size;
8188
}
82-
8389
}
8490
return 0;
8591

8692
}
8793

8894
/* -------------------------------------------------------------------------- */
8995
int WiFiClient::available() {
90-
/* -------------------------------------------------------------------------- */
96+
/* -------------------------------------------------------------------------- */
9197
int rv = 0;
9298
if(_sock >= 0 && rx_buffer != nullptr) {
9399
if(rx_buffer->available() > 0) {
@@ -109,17 +115,17 @@ int WiFiClient::available() {
109115

110116
/* -------------------------------------------------------------------------- */
111117
int WiFiClient::_read() {
112-
/* -------------------------------------------------------------------------- */
118+
/* -------------------------------------------------------------------------- */
113119
int rv = -1;
114120
if(_sock >= 0 && rx_buffer != nullptr) {
115121
string res = "";
116122
uint32_t size = rx_buffer->freePositions() - 1;
117123
modem.begin();
118-
124+
119125
/* important - it works one shot */
120126
modem.avoid_trim_results();
121127
modem.read_using_size();
122-
128+
123129
if(modem.write(string(PROMPT(_CLIENTRECEIVE)),res, "%s%d,%d\r\n" , CMD_WRITE(_CLIENTRECEIVE), _sock, size)) {
124130
if(res.size() > 0) {
125131
for(int i = 0, rv = 0; i < size && i < res.size(); i++) {
@@ -133,11 +139,11 @@ int WiFiClient::_read() {
133139
}
134140
}
135141
return rv;
136-
}
142+
}
137143

138144
/* -------------------------------------------------------------------------- */
139145
void WiFiClient::read_if_needed(size_t s) {
140-
/* -------------------------------------------------------------------------- */
146+
/* -------------------------------------------------------------------------- */
141147
if(rx_buffer != nullptr) {
142148
if((size_t)rx_buffer->available() < s) {
143149
_read();
@@ -147,12 +153,12 @@ void WiFiClient::read_if_needed(size_t s) {
147153

148154
/* -------------------------------------------------------------------------- */
149155
int WiFiClient::read() {
150-
/* -------------------------------------------------------------------------- */
156+
/* -------------------------------------------------------------------------- */
151157
uint8_t b;
152158
if(read(&b, 1) == 1) {
153159
return b;
154160
}
155-
return -1;
161+
return -1;
156162
}
157163

158164
/* -------------------------------------------------------------------------- */
@@ -173,27 +179,27 @@ int WiFiClient::read(uint8_t *buf, size_t size) {
173179
}
174180
}
175181
}
176-
return rv;
182+
return rv;
177183
}
178184

179185
/* -------------------------------------------------------------------------- */
180186
int WiFiClient::peek() {
181-
/* -------------------------------------------------------------------------- */
187+
/* -------------------------------------------------------------------------- */
182188
int rv = -1;
183189
if(_sock >= 0) {
184190
string res = "";
185191
modem.begin();
186192
if(modem.write(string(PROMPT(_PEEK)),res, "%s%d\r\n" , CMD_WRITE(_PEEK), _sock)) {
187193
rv = atoi(res.c_str());
188-
}
194+
}
189195
}
190196
return rv;
191197
}
192198

193199

194200
/* -------------------------------------------------------------------------- */
195201
void WiFiClient::flush() {
196-
/* -------------------------------------------------------------------------- */
202+
/* -------------------------------------------------------------------------- */
197203
if(_sock >= 0) {
198204
string res = "";
199205
modem.begin();
@@ -203,18 +209,20 @@ void WiFiClient::flush() {
203209

204210
/* -------------------------------------------------------------------------- */
205211
void WiFiClient::stop() {
206-
/* -------------------------------------------------------------------------- */
212+
/* -------------------------------------------------------------------------- */
207213
if(_sock >= 0) {
208214
string res = "";
209215
modem.begin();
210216
modem.write(string(PROMPT(_CLIENTCLOSE)),res, "%s%d\r\n" , CMD_WRITE(_CLIENTCLOSE), _sock);
211217
_sock = -1;
212218
}
219+
220+
rx_buffer->clear();
213221
}
214222

215223
/* -------------------------------------------------------------------------- */
216224
uint8_t WiFiClient::connected() {
217-
/* -------------------------------------------------------------------------- */
225+
/* -------------------------------------------------------------------------- */
218226
uint8_t rv = 0;
219227
if(this->available() > 0) {
220228
return 1;
@@ -224,16 +232,16 @@ uint8_t WiFiClient::connected() {
224232
modem.begin();
225233
if(modem.write(string(PROMPT(_CLIENTCONNECTED)),res, "%s%d\r\n" , CMD_WRITE(_CLIENTCONNECTED), _sock)) {
226234
rv = atoi(res.c_str());
227-
}
235+
}
228236
}
237+
229238
return rv;
230239
}
231240

232241
/* -------------------------------------------------------------------------- */
233-
bool WiFiClient::operator==(const WiFiClient& whs)
234-
{
235-
/* -------------------------------------------------------------------------- */
236-
return _sock == whs._sock;
242+
bool WiFiClient::operator==(const WiFiClient& whs) {
243+
/* -------------------------------------------------------------------------- */
244+
return _sock == whs._sock;
237245
}
238246

239247
/* -------------------------------------------------------------------------- */
@@ -246,22 +254,22 @@ IPAddress WiFiClient::remoteIP() {
246254
if(modem.write(string(PROMPT(_REMOTEIP)),res, "%s%d\r\n" , CMD_WRITE(_REMOTEIP), _sock)) {
247255
ip.fromString(res.c_str());
248256
return ip;
249-
}
257+
}
250258
}
251259
return IPAddress(0,0,0,0);
252260
}
253261

254262
/* -------------------------------------------------------------------------- */
255263
uint16_t WiFiClient::remotePort(){
256-
/* -------------------------------------------------------------------------- */
264+
/* -------------------------------------------------------------------------- */
257265
uint16_t rv = 0;
258266
if(_sock >= 0) {
259267
string res = "";
260268
modem.begin();
261269
if(modem.write(string(PROMPT(_REMOTEPORT)),res, "%s%d\r\n" , CMD_WRITE(_REMOTEPORT), _sock)) {
262270
rv = atoi(res.c_str());
263271
return rv;
264-
}
272+
}
265273
}
266-
return rv;
274+
return rv;
267275
}

‎libraries/WiFiS3/src/WiFiClient.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class WiFiClient : public Client {
6565
}
6666

6767
friend class WiFiServer;
68-
68+
6969
using Print::write;
7070

7171
protected:
@@ -76,7 +76,6 @@ class WiFiClient : public Client {
7676
std::shared_ptr<FifoBuffer<uint8_t,RX_BUFFER_DIM>> rx_buffer;
7777
int _read();
7878
void read_if_needed(size_t s);
79-
void clear_buffer();
8079
bool destroy_at_distructor;
8180

8281

‎libraries/WiFiS3/src/WiFiSSLClient.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
using namespace std;
44

55
/* -------------------------------------------------------------------------- */
6-
WiFiSSLClient::WiFiSSLClient() : _sock(-1) {
6+
WiFiSSLClient::WiFiSSLClient() {
77
/* -------------------------------------------------------------------------- */
8-
rx_buffer = std::shared_ptr<FifoBuffer<uint8_t,RX_BUFFER_DIM>>(new FifoBuffer<uint8_t,RX_BUFFER_DIM>());
98
}
109

1110
/* -------------------------------------------------------------------------- */
@@ -17,13 +16,19 @@ WiFiSSLClient::~WiFiSSLClient() {
1716
/* -------------------------------------------------------------------------- */
1817
void WiFiSSLClient::getSocket() {
1918
/* -------------------------------------------------------------------------- */
19+
if(_sock >= 0 && !connected()) {
20+
// if sock >= 0 -> it means we were connected, but something happened and we need
21+
// to reset this socket in order to be able to connect again
22+
stop();
23+
}
24+
2025
if(_sock == -1) {
2126
string res = "";
2227
modem.begin();
2328
if(modem.write(string(PROMPT(_SSLBEGINCLIENT)),res, "%s" , CMD(_SSLBEGINCLIENT))) {
2429
_sock = atoi(res.c_str());
2530
}
26-
}
31+
}
2732
}
2833

2934
/* -------------------------------------------------------------------------- */
@@ -87,7 +92,7 @@ void WiFiSSLClient::setEccSlot(int ecc508KeySlot, const byte cert[], int certLen
8792

8893
/* -------------------------------------------------------------------------- */
8994
size_t WiFiSSLClient::write(uint8_t b){
90-
/* -------------------------------------------------------------------------- */
95+
/* -------------------------------------------------------------------------- */
9196
return write(&b, 1);
9297
}
9398

@@ -101,15 +106,15 @@ size_t WiFiSSLClient::write(const uint8_t *buf, size_t size){
101106
if(modem.passthrough(buf,size)) {
102107
return size;
103108
}
104-
109+
105110
}
106111
return 0;
107112

108113
}
109114

110115
/* -------------------------------------------------------------------------- */
111116
int WiFiSSLClient::available(){
112-
/* -------------------------------------------------------------------------- */
117+
/* -------------------------------------------------------------------------- */
113118
int rv = 0;
114119
if(_sock >= 0 && rx_buffer != nullptr) {
115120
if(rx_buffer->available() > 0) {
@@ -123,21 +128,21 @@ int WiFiSSLClient::available(){
123128
if (rv < 0) {
124129
return 0;
125130
}
126-
}
131+
}
127132
}
128133
}
129134
return rv;
130135
}
131136

132137
/* -------------------------------------------------------------------------- */
133138
int WiFiSSLClient::_read() {
134-
/* -------------------------------------------------------------------------- */
139+
/* -------------------------------------------------------------------------- */
135140
int rv = -1;
136-
if(_sock >= 0) {
141+
if(_sock >= 0 && rx_buffer != nullptr) {
137142
string res = "";
138143
uint32_t size = rx_buffer->freePositions() - 1;
139144
modem.begin();
140-
145+
141146
/* important - it works one shot */
142147
modem.avoid_trim_results();
143148
modem.read_using_size();
@@ -154,24 +159,24 @@ int WiFiSSLClient::_read() {
154159
}
155160
}
156161
return rv;
157-
}
162+
}
158163

159164
/* -------------------------------------------------------------------------- */
160165
void WiFiSSLClient::read_if_needed(size_t s) {
161-
/* -------------------------------------------------------------------------- */
166+
/* -------------------------------------------------------------------------- */
162167
if((size_t)rx_buffer->available() < s) {
163168
_read();
164169
}
165170
}
166171

167172
/* -------------------------------------------------------------------------- */
168173
int WiFiSSLClient::read() {
169-
/* -------------------------------------------------------------------------- */
174+
/* -------------------------------------------------------------------------- */
170175
uint8_t b;
171176
if(read(&b, 1) == 1) {
172177
return b;
173178
}
174-
return -1;
179+
return -1;
175180
}
176181

177182
/* -------------------------------------------------------------------------- */
@@ -190,27 +195,27 @@ int WiFiSSLClient::read(uint8_t *buf, size_t size) {
190195
go_on = false;
191196
}
192197
}
193-
return rv;
198+
return rv;
194199
}
195200

196201
/* -------------------------------------------------------------------------- */
197202
int WiFiSSLClient::peek() {
198-
/* -------------------------------------------------------------------------- */
203+
/* -------------------------------------------------------------------------- */
199204
int rv = -1;
200205
if(_sock >= 0) {
201206
string res = "";
202207
modem.begin();
203208
if(modem.write(string(PROMPT(_SSLPEEK)),res, "%s%d\r\n" , CMD_WRITE(_SSLPEEK), _sock)) {
204209
rv = atoi(res.c_str());
205-
}
210+
}
206211
}
207212
return rv;
208213
}
209214

210215

211216
/* -------------------------------------------------------------------------- */
212217
void WiFiSSLClient::flush() {
213-
/* -------------------------------------------------------------------------- */
218+
/* -------------------------------------------------------------------------- */
214219
if(_sock >= 0) {
215220
string res = "";
216221
modem.begin();
@@ -220,31 +225,33 @@ void WiFiSSLClient::flush() {
220225

221226
/* -------------------------------------------------------------------------- */
222227
void WiFiSSLClient::stop() {
223-
/* -------------------------------------------------------------------------- */
228+
/* -------------------------------------------------------------------------- */
224229
if(_sock >= 0) {
225230
string res = "";
226231
modem.begin();
227232
modem.write(string(PROMPT(_SSLCLIENTCLOSE)),res, "%s%d\r\n" , CMD_WRITE(_SSLCLIENTCLOSE), _sock);
228233
_sock = -1;
229234
}
235+
236+
rx_buffer->clear();
230237
}
231238

232239
/* -------------------------------------------------------------------------- */
233240
uint8_t WiFiSSLClient::connected() {
234-
/* -------------------------------------------------------------------------- */
241+
/* -------------------------------------------------------------------------- */
235242
uint8_t rv = 0;
236243
if(_sock >= 0) {
237244
string res = "";
238245
modem.begin();
239246
if(modem.write(string(PROMPT(_SSLCLIENTCONNECTED)),res, "%s%d\r\n" , CMD_WRITE(_SSLCLIENTCONNECTED), _sock)) {
240247
rv = atoi(res.c_str());
241-
}
248+
}
242249
}
243250
return rv;
244251
}
245-
bool WiFiSSLClient::operator==(const WiFiSSLClient& whs)
246-
{
247-
return _sock == whs._sock;
252+
253+
bool WiFiSSLClient::operator==(const WiFiSSLClient& whs) {
254+
return _sock == whs._sock;
248255
}
249256

250257
/* -------------------------------------------------------------------------- */
@@ -257,22 +264,22 @@ IPAddress WiFiSSLClient::remoteIP() {
257264
if(modem.write(string(PROMPT(_SSLREMOTEIP)),res, "%s%d\r\n" , CMD_WRITE(_SSLREMOTEIP), _sock)) {
258265
ip.fromString(res.c_str());
259266
return ip;
260-
}
267+
}
261268
}
262269
return IPAddress(0,0,0,0);
263270
}
264271

265272
/* -------------------------------------------------------------------------- */
266273
uint16_t WiFiSSLClient::remotePort(){
267-
/* -------------------------------------------------------------------------- */
274+
/* -------------------------------------------------------------------------- */
268275
uint16_t rv = 0;
269276
if(_sock >= 0) {
270277
string res = "";
271278
modem.begin();
272279
if(modem.write(string(PROMPT(_SSLREMOTEPORT)),res, "%s%d\r\n" , CMD_WRITE(_SSLREMOTEPORT), _sock)) {
273280
rv = atoi(res.c_str());
274281
return rv;
275-
}
282+
}
276283
}
277-
return rv;
284+
return rv;
278285
}

‎libraries/WiFiS3/src/WiFiSSLClient.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class WiFiSSLClient : public WiFiClient {
4545
virtual void stop();
4646
virtual uint8_t connected();
4747
virtual operator bool() {
48-
return _sock != -1;
48+
return _sock != -1;
4949
}
5050
virtual bool operator==(const WiFiSSLClient&);
5151
virtual bool operator!=(const WiFiSSLClient& whs)
@@ -60,7 +60,6 @@ class WiFiSSLClient : public WiFiClient {
6060
using Print::write;
6161

6262
private:
63-
int _sock;
6463
void getSocket();
6564
int _read();
6665
void read_if_needed(size_t s);

0 commit comments

Comments
 (0)
Please sign in to comment.