forked from me-no-dev/ESPAsyncTCP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSyncClient.cpp
414 lines (370 loc) · 10.6 KB
/
SyncClient.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
/*
Asynchronous TCP library for Espressif MCUs
Copyright (c) 2016 Hristo Gochkov. 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
*/
#include "Arduino.h"
#include "SyncClient.h"
#include "ESPAsyncTCP.h"
#include "cbuf.h"
#include <interrupts.h>
#define DEBUG_ESP_SYNC_CLIENT
#if defined(DEBUG_ESP_SYNC_CLIENT) && !defined(SYNC_CLIENT_DEBUG)
#define SYNC_CLIENT_DEBUG( format, ...) DEBUG_GENERIC_P("[SYNC_CLIENT]", format, ##__VA_ARGS__)
#endif
#ifndef SYNC_CLIENT_DEBUG
#define SYNC_CLIENT_DEBUG(...) do { (void)0;} while(false)
#endif
/*
Without LWIP_NETIF_TX_SINGLE_PBUF, all tcp_writes default to "no copy".
Referenced data must be preserved and free-ed from the specified tcp_sent()
callback. Alternative, tcp_writes need to use the TCP_WRITE_FLAG_COPY
attribute.
*/
static_assert(LWIP_NETIF_TX_SINGLE_PBUF, "Required, tcp_write() must always copy.");
SyncClient::SyncClient(size_t txBufLen)
: _client(NULL)
, _tx_buffer(NULL)
, _tx_buffer_size(txBufLen)
, _rx_buffer(NULL)
, _ref(NULL)
{
ref();
}
SyncClient::SyncClient(AsyncClient *client, size_t txBufLen)
: _client(client)
, _tx_buffer(new (std::nothrow) cbuf(txBufLen))
, _tx_buffer_size(txBufLen)
, _rx_buffer(NULL)
, _ref(NULL)
{
if(ref() > 0 && _client != NULL)
_attachCallbacks();
}
SyncClient::~SyncClient(){
if (0 == unref())
_release();
}
void SyncClient::_release(){
if(_client != NULL){
_client->onData(NULL, NULL);
_client->onAck(NULL, NULL);
_client->onPoll(NULL, NULL);
_client->abort();
_client = NULL;
}
if(_tx_buffer != NULL){
cbuf *b = _tx_buffer;
_tx_buffer = NULL;
delete b;
}
while(_rx_buffer != NULL){
cbuf *b = _rx_buffer;
_rx_buffer = _rx_buffer->next;
delete b;
}
}
int SyncClient::ref(){
if(_ref == NULL){
_ref = new (std::nothrow) int;
if(_ref != NULL)
*_ref = 0;
else
return -1;
}
return (++*_ref);
}
int SyncClient::unref(){
int count = -1;
if (_ref != NULL) {
count = --*_ref;
if (0 == count) {
delete _ref;
_ref = NULL;
}
}
return count;
}
#if ASYNC_TCP_SSL_ENABLED
int SyncClient::_connect(const IPAddress& ip, uint16_t port, bool secure){
#else
int SyncClient::_connect(const IPAddress& ip, uint16_t port){
#endif
if(connected())
return 0;
if(_client != NULL)
delete _client;
_client = new (std::nothrow) AsyncClient();
if (_client == NULL)
return 0;
_client->onConnect([](void *obj, AsyncClient *c){ ((SyncClient*)(obj))->_onConnect(c); }, this);
_attachCallbacks_Disconnect();
#if ASYNC_TCP_SSL_ENABLED
if(_client->connect(ip, port, secure)){
#else
if(_client->connect(ip, port)){
#endif
while(_client != NULL && !_client->connected() && !_client->disconnecting())
delay(1);
return connected();
}
return 0;
}
#if ASYNC_TCP_SSL_ENABLED
int SyncClient::connect(const char *host, uint16_t port, bool secure){
#else
int SyncClient::connect(const char *host, uint16_t port){
#endif
if(connected())
return 0;
if(_client != NULL)
delete _client;
_client = new (std::nothrow) AsyncClient();
if (_client == NULL)
return 0;
_client->onConnect([](void *obj, AsyncClient *c){ ((SyncClient*)(obj))->_onConnect(c); }, this);
_attachCallbacks_Disconnect();
#if ASYNC_TCP_SSL_ENABLED
if(_client->connect(host, port, secure)){
#else
if(_client->connect(host, port)){
#endif
while(_client != NULL && !_client->connected() && !_client->disconnecting())
delay(1);
return connected();
}
return 0;
}
//#define SYNCCLIENT_NEW_OPERATOR_EQUAL
#ifdef SYNCCLIENT_NEW_OPERATOR_EQUAL
/*
New behavior for operator=
Allow for the object to be placed on a queue and transfered to a new container
with buffers still in tact. Avoiding receive data drops. Transfers rx and tx
buffers. Supports return by value.
Note, this is optional, the old behavior is the default.
*/
SyncClient & SyncClient::operator=(const SyncClient &other){
int *rhsref = other._ref;
++*rhsref; // Just in case the left and right side are the same object with different containers
if (0 == unref())
_release();
_ref = other._ref;
ref();
--*rhsref;
// Why do I not test _tx_buffer for != NULL and free?
// I allow for the lh target container, to be a copy of an active
// connection. Thus we are just reusing the container.
// The above unref() handles releaseing the previous client of the container.
_tx_buffer_size = other._tx_buffer_size;
_tx_buffer = other._tx_buffer;
_client = other._client;
if (_client != NULL && _tx_buffer == NULL)
_tx_buffer = new (std::nothrow) cbuf(_tx_buffer_size);
_rx_buffer = other._rx_buffer;
if(_client)
_attachCallbacks();
return *this;
}
#else // ! SYNCCLIENT_NEW_OPERATOR_EQUAL
// This is the origianl logic with null checks
SyncClient & SyncClient::operator=(const SyncClient &other){
if(_client != NULL){
_client->abort();
_client->free();
_client = NULL;
}
_tx_buffer_size = other._tx_buffer_size;
if(_tx_buffer != NULL){
cbuf *b = _tx_buffer;
_tx_buffer = NULL;
delete b;
}
while(_rx_buffer != NULL){
cbuf *b = _rx_buffer;
_rx_buffer = b->next;
delete b;
}
if(other._client != NULL)
_tx_buffer = new (std::nothrow) cbuf(other._tx_buffer_size);
_client = other._client;
if(_client)
_attachCallbacks();
return *this;
}
#endif
void SyncClient::setTimeout(uint32_t seconds){
if(_client != NULL)
_client->setRxTimeout(seconds);
}
uint8_t SyncClient::status(){
if(_client == NULL)
return 0;
return _client->state();
}
uint8_t SyncClient::connected(){
return (_client != NULL && _client->connected());
}
bool SyncClient::stop(unsigned int maxWaitMs){
(void)maxWaitMs;
if(_client != NULL)
_client->close(true);
return true;
}
size_t SyncClient::_sendBuffer(){
if(_client == NULL || _tx_buffer == NULL)
return 0;
size_t available = _tx_buffer->available();
if(!connected() || !_client->canSend() || available == 0)
return 0;
size_t sendable = _client->space();
if(sendable < available)
available= sendable;
char *out = new (std::nothrow) char[available];
if(out == NULL)
return 0;
_tx_buffer->read(out, available);
size_t sent = _client->write(out, available);
delete[] out;
return sent;
}
void SyncClient::_onData(void *data, size_t len){
_client->ackLater();
cbuf *b = new (std::nothrow) cbuf(len+1);
if(b != NULL){
b->write((const char *)data, len);
if(_rx_buffer == NULL)
_rx_buffer = b;
else {
cbuf *p = _rx_buffer;
while(p->next != NULL)
p = p->next;
p->next = b;
}
} else {
// We ran out of memory. This fail causes lost receive data.
// The connection should be closed in a manner that conveys something
// bad/abnormal has happened to the connection. Hence, we abort the
// connection to avoid possible data corruption.
// Note, callbacks maybe called.
_client->abort();
}
}
void SyncClient::_onDisconnect(){
if(_client != NULL){
_client = NULL;
}
if(_tx_buffer != NULL){
cbuf *b = _tx_buffer;
_tx_buffer = NULL;
delete b;
}
}
void SyncClient::_onConnect(AsyncClient *c){
_client = c;
if(_tx_buffer != NULL){
cbuf *b = _tx_buffer;
_tx_buffer = NULL;
delete b;
}
_tx_buffer = new (std::nothrow) cbuf(_tx_buffer_size);
_attachCallbacks_AfterConnected();
}
void SyncClient::_attachCallbacks(){
_attachCallbacks_Disconnect();
_attachCallbacks_AfterConnected();
}
void SyncClient::_attachCallbacks_AfterConnected(){
_client->onAck([](void *obj, AsyncClient* c, size_t len, uint32_t time){ (void)c; (void)len; (void)time; ((SyncClient*)(obj))->_sendBuffer(); }, this);
_client->onData([](void *obj, AsyncClient* c, void *data, size_t len){ (void)c; ((SyncClient*)(obj))->_onData(data, len); }, this);
_client->onTimeout([](void *obj, AsyncClient* c, uint32_t time){ (void)obj; (void)time; c->close(); }, this);
}
void SyncClient::_attachCallbacks_Disconnect(){
_client->onDisconnect([](void *obj, AsyncClient* c){ ((SyncClient*)(obj))->_onDisconnect(); delete c; }, this);
}
size_t SyncClient::write(uint8_t data){
return write(&data, 1);
}
size_t SyncClient::write(const uint8_t *data, size_t len){
if(_tx_buffer == NULL || !connected()){
return 0;
}
size_t toWrite = 0;
size_t toSend = len;
while(_tx_buffer->room() < toSend){
toWrite = _tx_buffer->room();
_tx_buffer->write((const char*)data, toWrite);
while(connected() && !_client->canSend())
delay(0);
if(!connected())
return 0;
_sendBuffer();
toSend -= toWrite;
}
_tx_buffer->write((const char*)(data+(len - toSend)), toSend);
if(connected() && _client->canSend())
_sendBuffer();
return len;
}
int SyncClient::available(){
if(_rx_buffer == NULL) return 0;
size_t a = 0;
cbuf *b = _rx_buffer;
while(b != NULL){
a += b->available();
b = b->next;
}
return a;
}
int SyncClient::peek(){
if(_rx_buffer == NULL) return -1;
return _rx_buffer->peek();
}
int SyncClient::read(uint8_t *data, size_t len){
if(_rx_buffer == NULL) return -1;
size_t readSoFar = 0;
while(_rx_buffer != NULL && (len - readSoFar) >= _rx_buffer->available()){
cbuf *b = _rx_buffer;
_rx_buffer = _rx_buffer->next;
size_t toRead = b->available();
readSoFar += b->read((char*)(data+readSoFar), toRead);
if(connected()){
_client->ack(b->size() - 1);
}
delete b;
}
if(_rx_buffer != NULL && readSoFar < len){
readSoFar += _rx_buffer->read((char*)(data+readSoFar), (len - readSoFar));
}
return readSoFar;
}
int SyncClient::read(){
uint8_t res = 0;
if(read(&res, 1) != 1)
return -1;
return res;
}
bool SyncClient::flush(unsigned int maxWaitMs){
(void)maxWaitMs;
if(_tx_buffer == NULL || !connected())
return false;
if(_tx_buffer->available()){
while(connected() && !_client->canSend())
delay(0);
if(_client == NULL || _tx_buffer == NULL)
return false;
_sendBuffer();
}
return true;
}