Skip to content

Commit 715e306

Browse files
authored
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

+42-34
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

+1-2
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

0 commit comments

Comments
 (0)