3
3
using namespace std ;
4
4
5
5
/* -------------------------------------------------------------------------- */
6
- WiFiClient::WiFiClient () : _sock(-1 ), destroy_at_distructor(true ), rx_buffer(nullptr ) {
6
+ WiFiClient::WiFiClient () : _sock(-1 ), destroy_at_distructor(true ), rx_buffer(nullptr ) {
7
7
rx_buffer = std::shared_ptr<FifoBuffer<uint8_t ,RX_BUFFER_DIM>>(new FifoBuffer<uint8_t ,RX_BUFFER_DIM>());
8
8
}
9
9
/* -------------------------------------------------------------------------- */
10
-
10
+
11
11
/* -------------------------------------------------------------------------- */
12
12
WiFiClient::WiFiClient (int s) : _sock(s), destroy_at_distructor(false ), rx_buffer(nullptr ) {
13
13
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
18
18
WiFiClient::~WiFiClient () { }
19
19
/* -------------------------------------------------------------------------- */
20
20
21
- /* -------------------------------------------------------------------------- */
21
+ /* -------------------------------------------------------------------------- */
22
22
WiFiClient::WiFiClient (const WiFiClient& c) {
23
- /* -------------------------------------------------------------------------- */
23
+ /* -------------------------------------------------------------------------- */
24
24
_sock = c._sock ;
25
25
rx_buffer = c.rx_buffer ;
26
26
}
27
27
28
28
/* -------------------------------------------------------------------------- */
29
29
void WiFiClient::getSocket () {
30
30
/* -------------------------------------------------------------------------- */
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
+
31
37
if (_sock == -1 ) {
32
38
string res = " " ;
33
39
modem.begin ();
34
40
if (modem.write (string (PROMPT (_BEGINCLIENT)),res, " %s" , CMD (_BEGINCLIENT))) {
35
41
_sock = atoi (res.c_str ());
36
42
}
37
- }
43
+ }
38
44
}
39
45
40
46
/* -------------------------------------------------------------------------- */
@@ -45,7 +51,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port){
45
51
46
52
/* -------------------------------------------------------------------------- */
47
53
int WiFiClient::connect (const char *host, uint16_t port){
48
- /* -------------------------------------------------------------------------- */
54
+ /* -------------------------------------------------------------------------- */
49
55
getSocket ();
50
56
if (_sock >= 0 ) {
51
57
string res = " " ;
@@ -55,17 +61,18 @@ int WiFiClient::connect(const char *host, uint16_t port){
55
61
return 1 ;
56
62
}
57
63
} 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
+ }
61
67
}
62
68
}
69
+
63
70
return 0 ;
64
71
}
65
72
66
73
/* -------------------------------------------------------------------------- */
67
74
size_t WiFiClient::write (uint8_t b){
68
- /* -------------------------------------------------------------------------- */
75
+ /* -------------------------------------------------------------------------- */
69
76
return write (&b, 1 );
70
77
}
71
78
@@ -79,15 +86,14 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size){
79
86
if (modem.passthrough (buf,size)) {
80
87
return size;
81
88
}
82
-
83
89
}
84
90
return 0 ;
85
91
86
92
}
87
93
88
94
/* -------------------------------------------------------------------------- */
89
95
int WiFiClient::available () {
90
- /* -------------------------------------------------------------------------- */
96
+ /* -------------------------------------------------------------------------- */
91
97
int rv = 0 ;
92
98
if (_sock >= 0 && rx_buffer != nullptr ) {
93
99
if (rx_buffer->available () > 0 ) {
@@ -109,17 +115,17 @@ int WiFiClient::available() {
109
115
110
116
/* -------------------------------------------------------------------------- */
111
117
int WiFiClient::_read () {
112
- /* -------------------------------------------------------------------------- */
118
+ /* -------------------------------------------------------------------------- */
113
119
int rv = -1 ;
114
120
if (_sock >= 0 && rx_buffer != nullptr ) {
115
121
string res = " " ;
116
122
uint32_t size = rx_buffer->freePositions () - 1 ;
117
123
modem.begin ();
118
-
124
+
119
125
/* important - it works one shot */
120
126
modem.avoid_trim_results ();
121
127
modem.read_using_size ();
122
-
128
+
123
129
if (modem.write (string (PROMPT (_CLIENTRECEIVE)),res, " %s%d,%d\r\n " , CMD_WRITE (_CLIENTRECEIVE), _sock, size)) {
124
130
if (res.size () > 0 ) {
125
131
for (int i = 0 , rv = 0 ; i < size && i < res.size (); i++) {
@@ -133,11 +139,11 @@ int WiFiClient::_read() {
133
139
}
134
140
}
135
141
return rv;
136
- }
142
+ }
137
143
138
144
/* -------------------------------------------------------------------------- */
139
145
void WiFiClient::read_if_needed (size_t s) {
140
- /* -------------------------------------------------------------------------- */
146
+ /* -------------------------------------------------------------------------- */
141
147
if (rx_buffer != nullptr ) {
142
148
if ((size_t )rx_buffer->available () < s) {
143
149
_read ();
@@ -147,12 +153,12 @@ void WiFiClient::read_if_needed(size_t s) {
147
153
148
154
/* -------------------------------------------------------------------------- */
149
155
int WiFiClient::read () {
150
- /* -------------------------------------------------------------------------- */
156
+ /* -------------------------------------------------------------------------- */
151
157
uint8_t b;
152
158
if (read (&b, 1 ) == 1 ) {
153
159
return b;
154
160
}
155
- return -1 ;
161
+ return -1 ;
156
162
}
157
163
158
164
/* -------------------------------------------------------------------------- */
@@ -173,27 +179,27 @@ int WiFiClient::read(uint8_t *buf, size_t size) {
173
179
}
174
180
}
175
181
}
176
- return rv;
182
+ return rv;
177
183
}
178
184
179
185
/* -------------------------------------------------------------------------- */
180
186
int WiFiClient::peek () {
181
- /* -------------------------------------------------------------------------- */
187
+ /* -------------------------------------------------------------------------- */
182
188
int rv = -1 ;
183
189
if (_sock >= 0 ) {
184
190
string res = " " ;
185
191
modem.begin ();
186
192
if (modem.write (string (PROMPT (_PEEK)),res, " %s%d\r\n " , CMD_WRITE (_PEEK), _sock)) {
187
193
rv = atoi (res.c_str ());
188
- }
194
+ }
189
195
}
190
196
return rv;
191
197
}
192
198
193
199
194
200
/* -------------------------------------------------------------------------- */
195
201
void WiFiClient::flush () {
196
- /* -------------------------------------------------------------------------- */
202
+ /* -------------------------------------------------------------------------- */
197
203
if (_sock >= 0 ) {
198
204
string res = " " ;
199
205
modem.begin ();
@@ -203,18 +209,20 @@ void WiFiClient::flush() {
203
209
204
210
/* -------------------------------------------------------------------------- */
205
211
void WiFiClient::stop () {
206
- /* -------------------------------------------------------------------------- */
212
+ /* -------------------------------------------------------------------------- */
207
213
if (_sock >= 0 ) {
208
214
string res = " " ;
209
215
modem.begin ();
210
216
modem.write (string (PROMPT (_CLIENTCLOSE)),res, " %s%d\r\n " , CMD_WRITE (_CLIENTCLOSE), _sock);
211
217
_sock = -1 ;
212
218
}
219
+
220
+ rx_buffer->clear ();
213
221
}
214
222
215
223
/* -------------------------------------------------------------------------- */
216
224
uint8_t WiFiClient::connected () {
217
- /* -------------------------------------------------------------------------- */
225
+ /* -------------------------------------------------------------------------- */
218
226
uint8_t rv = 0 ;
219
227
if (this ->available () > 0 ) {
220
228
return 1 ;
@@ -224,16 +232,16 @@ uint8_t WiFiClient::connected() {
224
232
modem.begin ();
225
233
if (modem.write (string (PROMPT (_CLIENTCONNECTED)),res, " %s%d\r\n " , CMD_WRITE (_CLIENTCONNECTED), _sock)) {
226
234
rv = atoi (res.c_str ());
227
- }
235
+ }
228
236
}
237
+
229
238
return rv;
230
239
}
231
240
232
241
/* -------------------------------------------------------------------------- */
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 ;
237
245
}
238
246
239
247
/* -------------------------------------------------------------------------- */
@@ -246,22 +254,22 @@ IPAddress WiFiClient::remoteIP() {
246
254
if (modem.write (string (PROMPT (_REMOTEIP)),res, " %s%d\r\n " , CMD_WRITE (_REMOTEIP), _sock)) {
247
255
ip.fromString (res.c_str ());
248
256
return ip;
249
- }
257
+ }
250
258
}
251
259
return IPAddress (0 ,0 ,0 ,0 );
252
260
}
253
261
254
262
/* -------------------------------------------------------------------------- */
255
263
uint16_t WiFiClient::remotePort (){
256
- /* -------------------------------------------------------------------------- */
264
+ /* -------------------------------------------------------------------------- */
257
265
uint16_t rv = 0 ;
258
266
if (_sock >= 0 ) {
259
267
string res = " " ;
260
268
modem.begin ();
261
269
if (modem.write (string (PROMPT (_REMOTEPORT)),res, " %s%d\r\n " , CMD_WRITE (_REMOTEPORT), _sock)) {
262
270
rv = atoi (res.c_str ());
263
271
return rv;
264
- }
272
+ }
265
273
}
266
- return rv;
274
+ return rv;
267
275
}
0 commit comments