-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathWiFiClient.cpp
425 lines (345 loc) · 8.19 KB
/
WiFiClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
WiFiClient.cpp - TCP/IP client for esp8266, mostly compatible
with Arduino WiFi shield library
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define LWIP_INTERNAL
extern "C"
{
#include "include/wl_definitions.h"
#include "osapi.h"
#include "ets_sys.h"
}
#include "debug.h"
#include "ESP8266WiFi.h"
#include "WiFiClient.h"
#include "WiFiServer.h"
#include "lwip/opt.h"
#include "lwip/ip.h"
#include "lwip/tcp.h"
#include "lwip/inet.h"
#include "lwip/netif.h"
#include <include/ClientContext.h>
#include "c_types.h"
uint16_t WiFiClient::_localPort = 0;
static bool defaultNoDelay = false; // false == Nagle enabled by default
static bool defaultSync = false;
bool getDefaultPrivateGlobalSyncValue ()
{
return defaultSync;
}
void WiFiClient::setDefaultNoDelay (bool noDelay)
{
defaultNoDelay = noDelay;
}
void WiFiClient::setDefaultSync (bool sync)
{
defaultSync = sync;
}
bool WiFiClient::getDefaultNoDelay ()
{
return defaultNoDelay;
}
bool WiFiClient::getDefaultSync ()
{
return defaultSync;
}
template<>
WiFiClient* SList<WiFiClient>::_s_first = 0;
WiFiClient::WiFiClient()
: _client(0)
{
_timeout = 5000;
WiFiClient::_add(this);
}
WiFiClient::WiFiClient(ClientContext* client)
: _client(client)
{
_timeout = 5000;
_client->ref();
WiFiClient::_add(this);
setSync(defaultSync);
setNoDelay(defaultNoDelay);
}
WiFiClient::~WiFiClient()
{
WiFiClient::_remove(this);
if (_client)
_client->unref();
}
WiFiClient::WiFiClient(const WiFiClient& other)
{
_client = other._client;
_timeout = other._timeout;
_localPort = other._localPort;
if (_client)
_client->ref();
WiFiClient::_add(this);
}
WiFiClient& WiFiClient::operator=(const WiFiClient& other)
{
if (_client)
_client->unref();
_client = other._client;
_timeout = other._timeout;
_localPort = other._localPort;
if (_client)
_client->ref();
return *this;
}
int WiFiClient::connect(const char* host, uint16_t port)
{
IPAddress remote_addr;
if (WiFi.hostByName(host, remote_addr, _timeout))
{
return connect(remote_addr, port);
}
return 0;
}
int WiFiClient::connect(const String& host, uint16_t port)
{
return connect(host.c_str(), port);
}
int WiFiClient::connect(IPAddress ip, uint16_t port)
{
if (_client) {
stop();
_client->unref();
_client = nullptr;
}
#if LWIP_VERSION_MAJOR == 1
// if the default interface is down, tcp_connect exits early without
// ever calling tcp_err
// http://lists.gnu.org/archive/html/lwip-devel/2010-05/msg00001.html
netif* interface = ip_route(ip);
if (!interface) {
DEBUGV("no route to host\r\n");
return 0;
}
#endif
tcp_pcb* pcb = tcp_new();
if (!pcb)
return 0;
if (_localPort > 0) {
pcb->local_port = _localPort++;
}
_client = new ClientContext(pcb, nullptr, nullptr);
_client->ref();
_client->setTimeout(_timeout);
int res = _client->connect(ip, port);
if (res == 0) {
_client->unref();
_client = nullptr;
return 0;
}
setSync(defaultSync);
setNoDelay(defaultNoDelay);
return 1;
}
void WiFiClient::setNoDelay(bool nodelay) {
if (!_client)
return;
_client->setNoDelay(nodelay);
}
bool WiFiClient::getNoDelay() const {
if (!_client)
return false;
return _client->getNoDelay();
}
void WiFiClient::setSync(bool sync)
{
if (!_client)
return;
_client->setSync(sync);
}
bool WiFiClient::getSync() const
{
if (!_client)
return false;
return _client->getSync();
}
size_t WiFiClient::availableForWrite ()
{
return _client? _client->availableForWrite(): 0;
}
size_t WiFiClient::write(uint8_t b)
{
return write(&b, 1);
}
size_t WiFiClient::write(const uint8_t *buf, size_t size)
{
if (!_client || !size)
{
return 0;
}
_client->setTimeout(_timeout);
return _client->write(buf, size);
}
size_t WiFiClient::write(Stream& stream, size_t unused)
{
(void) unused;
return WiFiClient::write(stream);
}
size_t WiFiClient::write(Stream& stream)
{
if (!_client || !stream.available())
{
return 0;
}
_client->setTimeout(_timeout);
return _client->write(stream);
}
size_t WiFiClient::write_P(PGM_P buf, size_t size)
{
if (!_client || !size)
{
return 0;
}
_client->setTimeout(_timeout);
return _client->write_P(buf, size);
}
int WiFiClient::available()
{
if (!_client)
return false;
int result = _client->getSize();
if (!result) {
optimistic_yield(100);
}
return result;
}
int WiFiClient::read()
{
if (!available())
return -1;
return _client->read();
}
int WiFiClient::read(uint8_t* buf, size_t size)
{
return (int) _client->read(reinterpret_cast<char*>(buf), size);
}
int WiFiClient::peek()
{
if (!available())
return -1;
return _client->peek();
}
size_t WiFiClient::peekBytes(uint8_t *buffer, size_t length) {
size_t count = 0;
if(!_client) {
return 0;
}
_startMillis = millis();
while((available() < (int) length) && ((millis() - _startMillis) < _timeout)) {
yield();
}
if(available() < (int) length) {
count = available();
} else {
count = length;
}
return _client->peekBytes((char *)buffer, count);
}
bool WiFiClient::flush(unsigned int maxWaitMs)
{
if (!_client)
return true;
if (maxWaitMs == 0)
maxWaitMs = WIFICLIENT_MAX_FLUSH_WAIT_MS;
return _client->wait_until_sent(maxWaitMs);
}
bool WiFiClient::stop(unsigned int maxWaitMs)
{
if (!_client)
return true;
bool ret = flush(maxWaitMs); // virtual, may be ssl's
if (_client->close() != ERR_OK)
ret = false;
return ret;
}
uint8_t WiFiClient::connected()
{
if (!_client || _client->state() == CLOSED)
return 0;
return _client->state() == ESTABLISHED || available();
}
uint8_t WiFiClient::status()
{
if (!_client)
return CLOSED;
return _client->state();
}
WiFiClient::operator bool()
{
return connected();
}
IPAddress WiFiClient::remoteIP()
{
if (!_client || !_client->getRemoteAddress())
return IPAddress(0U);
return _client->getRemoteAddress();
}
uint16_t WiFiClient::remotePort()
{
if (!_client)
return 0;
return _client->getRemotePort();
}
IPAddress WiFiClient::localIP()
{
if (!_client)
return IPAddress(0U);
return IPAddress(_client->getLocalAddress());
}
uint16_t WiFiClient::localPort()
{
if (!_client)
return 0;
return _client->getLocalPort();
}
void WiFiClient::stopAll()
{
for (WiFiClient* it = _s_first; it; it = it->_next) {
it->stop();
}
}
void WiFiClient::stopAllExcept(WiFiClient* except)
{
for (WiFiClient* it = _s_first; it; it = it->_next) {
if (it != except) {
it->stop();
}
}
}
void WiFiClient::keepAlive (uint16_t idle_sec, uint16_t intv_sec, uint8_t count)
{
_client->keepAlive(idle_sec, intv_sec, count);
}
bool WiFiClient::isKeepAliveEnabled () const
{
return _client->isKeepAliveEnabled();
}
uint16_t WiFiClient::getKeepAliveIdle () const
{
return _client->getKeepAliveIdle();
}
uint16_t WiFiClient::getKeepAliveInterval () const
{
return _client->getKeepAliveInterval();
}
uint8_t WiFiClient::getKeepAliveCount () const
{
return _client->getKeepAliveCount();
}