forked from me-no-dev/ESPAsyncTCP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESPAsyncTCPbuffer.cpp
555 lines (467 loc) · 14.5 KB
/
ESPAsyncTCPbuffer.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
* @file ESPAsyncTCPbuffer.cpp
* @date 22.01.2016
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the Asynv TCP for ESP.
*
* 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 <debug.h>
#include "ESPAsyncTCPbuffer.h"
AsyncTCPbuffer::AsyncTCPbuffer(AsyncClient* client) {
if(client == NULL) {
DEBUG_ASYNC_TCP("[A-TCP] client is null!!!\n");
panic();
}
_client = client;
_TXbufferWrite = new (std::nothrow) cbuf(TCP_MSS);
_TXbufferRead = _TXbufferWrite;
_RXbuffer = new (std::nothrow) cbuf(100);
_RXmode = ATB_RX_MODE_FREE;
_rxSize = 0;
_rxTerminator = 0x00;
_rxReadBytesPtr = NULL;
_rxReadStringPtr = NULL;
_cbDisconnect = NULL;
_cbRX = NULL;
_cbDone = NULL;
_attachCallbacks();
}
AsyncTCPbuffer::~AsyncTCPbuffer() {
if(_client) {
_client->close();
}
if(_RXbuffer) {
delete _RXbuffer;
_RXbuffer = NULL;
}
if(_TXbufferWrite) {
// will be deleted in _TXbufferRead chain
_TXbufferWrite = NULL;
}
if(_TXbufferRead) {
cbuf * next = _TXbufferRead->next;
delete _TXbufferRead;
while(next != NULL) {
_TXbufferRead = next;
next = _TXbufferRead->next;
delete _TXbufferRead;
}
_TXbufferRead = NULL;
}
}
size_t AsyncTCPbuffer::write(String & data) {
return write(data.c_str(), data.length());
}
size_t AsyncTCPbuffer::write(uint8_t data) {
return write(&data, 1);
}
size_t AsyncTCPbuffer::write(const char* data) {
return write((const uint8_t *) data, strlen(data));
}
size_t AsyncTCPbuffer::write(const char *data, size_t len) {
return write((const uint8_t *) data, len);
}
/**
* write data in to buffer and try to send the data
* @param data
* @param len
* @return
*/
size_t AsyncTCPbuffer::write(const uint8_t *data, size_t len) {
if(_TXbufferWrite == NULL || _client == NULL || !_client->connected() || data == NULL || len == 0) {
return 0;
}
size_t bytesLeft = len;
while(bytesLeft) {
size_t w = _TXbufferWrite->write((const char*) data, bytesLeft);
bytesLeft -= w;
data += w;
_sendBuffer();
// add new buffer since we have more data
if(_TXbufferWrite->full() && bytesLeft > 0) {
// to less ram!!!
if(ESP.getFreeHeap() < 4096) {
DEBUG_ASYNC_TCP("[A-TCP] run out of Heap can not send all Data!\n");
return (len - bytesLeft);
}
cbuf * next = new (std::nothrow) cbuf(TCP_MSS);
if(next == NULL) {
DEBUG_ASYNC_TCP("[A-TCP] run out of Heap!\n");
panic();
} else {
DEBUG_ASYNC_TCP("[A-TCP] new cbuf\n");
}
// add new buffer to chain (current cbuf)
_TXbufferWrite->next = next;
// move ptr for next data
_TXbufferWrite = next;
}
}
return len;
}
/**
* wait until all data has send out
*/
void AsyncTCPbuffer::flush() {
while(!_TXbufferWrite->empty()) {
while(connected() && !_client->canSend()) {
delay(0);
}
if(!connected())
return;
_sendBuffer();
}
}
void AsyncTCPbuffer::noCallback() {
_RXmode = ATB_RX_MODE_NONE;
}
void AsyncTCPbuffer::readStringUntil(char terminator, String * str, AsyncTCPbufferDoneCb done) {
if(_client == NULL) {
return;
}
DEBUG_ASYNC_TCP("[A-TCP] readStringUntil terminator: %02X\n", terminator);
_RXmode = ATB_RX_MODE_NONE;
_cbDone = done;
_rxReadStringPtr = str;
_rxTerminator = terminator;
_rxSize = 0;
_RXmode = ATB_RX_MODE_TERMINATOR_STRING;
}
/*
void AsyncTCPbuffer::readBytesUntil(char terminator, char *buffer, size_t length, AsyncTCPbufferDoneCb done) {
_RXmode = ATB_RX_MODE_NONE;
_cbDone = done;
_rxReadBytesPtr = (uint8_t *) buffer;
_rxTerminator = terminator;
_rxSize = length;
_RXmode = ATB_RX_MODE_TERMINATOR;
_handleRxBuffer(NULL, 0);
}
void AsyncTCPbuffer::readBytesUntil(char terminator, uint8_t *buffer, size_t length, AsyncTCPbufferDoneCb done) {
readBytesUntil(terminator, (char *) buffer, length, done);
}
*/
void AsyncTCPbuffer::readBytes(char *buffer, size_t length, AsyncTCPbufferDoneCb done) {
if(_client == NULL) {
return;
}
DEBUG_ASYNC_TCP("[A-TCP] readBytes length: %d\n", length);
_RXmode = ATB_RX_MODE_NONE;
_cbDone = done;
_rxReadBytesPtr = (uint8_t *) buffer;
_rxSize = length;
_RXmode = ATB_RX_MODE_READ_BYTES;
}
void AsyncTCPbuffer::readBytes(uint8_t *buffer, size_t length, AsyncTCPbufferDoneCb done) {
readBytes((char *) buffer, length, done);
}
void AsyncTCPbuffer::onData(AsyncTCPbufferDataCb cb) {
if(_client == NULL) {
return;
}
DEBUG_ASYNC_TCP("[A-TCP] onData\n");
_RXmode = ATB_RX_MODE_NONE;
_cbDone = NULL;
_cbRX = cb;
_RXmode = ATB_RX_MODE_FREE;
}
void AsyncTCPbuffer::onDisconnect(AsyncTCPbufferDisconnectCb cb) {
_cbDisconnect = cb;
}
IPAddress AsyncTCPbuffer::remoteIP() {
if(!_client) {
return IPAddress(0U);
}
return _client->remoteIP();
}
uint16_t AsyncTCPbuffer::remotePort() {
if(!_client) {
return 0;
}
return _client->remotePort();
}
bool AsyncTCPbuffer::connected() {
if(!_client) {
return false;
}
return _client->connected();
}
void AsyncTCPbuffer::stop() {
if(!_client) {
return;
}
_client->stop();
_client = NULL;
if(_cbDone) {
switch(_RXmode) {
case ATB_RX_MODE_READ_BYTES:
case ATB_RX_MODE_TERMINATOR:
case ATB_RX_MODE_TERMINATOR_STRING:
_RXmode = ATB_RX_MODE_NONE;
_cbDone(false, NULL);
break;
default:
break;
}
}
_RXmode = ATB_RX_MODE_NONE;
}
void AsyncTCPbuffer::close() {
stop();
}
///--------------------------------
/**
* attachCallbacks to AsyncClient class
*/
void AsyncTCPbuffer::_attachCallbacks() {
if(!_client) {
return;
}
DEBUG_ASYNC_TCP("[A-TCP] attachCallbacks\n");
_client->onPoll([](void *obj, AsyncClient* c) {
(void)c;
AsyncTCPbuffer* b = ((AsyncTCPbuffer*)(obj));
if((b->_TXbufferRead != NULL) && !b->_TXbufferRead->empty()) {
b->_sendBuffer();
}
// if(!b->_RXbuffer->empty()) {
// b->_handleRxBuffer(NULL, 0);
// }
}, this);
_client->onAck([](void *obj, AsyncClient* c, size_t len, uint32_t time) {
(void)c;
(void)len;
(void)time;
DEBUG_ASYNC_TCP("[A-TCP] onAck\n");
((AsyncTCPbuffer*)(obj))->_sendBuffer();
}, this);
_client->onDisconnect([](void *obj, AsyncClient* c) {
DEBUG_ASYNC_TCP("[A-TCP] onDisconnect\n");
AsyncTCPbuffer* b = ((AsyncTCPbuffer*)(obj));
b->_client = NULL;
bool del = true;
if(b->_cbDisconnect) {
del = b->_cbDisconnect(b);
}
delete c;
if(del) {
delete b;
}
}, this);
_client->onData([](void *obj, AsyncClient* c, void *buf, size_t len) {
(void)c;
AsyncTCPbuffer* b = ((AsyncTCPbuffer*)(obj));
b->_rxData((uint8_t *)buf, len);
}, this);
_client->onTimeout([](void *obj, AsyncClient* c, uint32_t time){
(void)obj;
(void)time;
DEBUG_ASYNC_TCP("[A-TCP] onTimeout\n");
c->close();
}, this);
DEBUG_ASYNC_TCP("[A-TCP] attachCallbacks Done.\n");
}
/**
* send TX buffer if possible
*/
void AsyncTCPbuffer::_sendBuffer() {
//DEBUG_ASYNC_TCP("[A-TCP] _sendBuffer...\n");
size_t available = _TXbufferRead->available();
if(available == 0 || _client == NULL || !_client->connected() || !_client->canSend()) {
return;
}
while(connected() && (_client->space() > 0) && (_TXbufferRead->available() > 0) && _client->canSend()) {
available = _TXbufferRead->available();
if(available > _client->space()) {
available = _client->space();
}
char *out = new (std::nothrow) char[available];
if(out == NULL) {
DEBUG_ASYNC_TCP("[A-TCP] to less heap, try later.\n");
return;
}
// read data from buffer
_TXbufferRead->peek(out, available);
// send data
size_t send = _client->write((const char*) out, available);
if(send != available) {
DEBUG_ASYNC_TCP("[A-TCP] write failed send: %d available: %d \n", send, available);
if(!connected()) {
DEBUG_ASYNC_TCP("[A-TCP] incomplete transfer, connection lost.\n");
}
}
// remove really send data from buffer
_TXbufferRead->remove(send);
// if buffer is empty and there is a other buffer in chain delete the empty one
if(_TXbufferRead->available() == 0 && _TXbufferRead->next != NULL) {
cbuf * old = _TXbufferRead;
_TXbufferRead = _TXbufferRead->next;
delete old;
DEBUG_ASYNC_TCP("[A-TCP] delete cbuf\n");
}
delete out;
}
}
/**
* called on incoming data
* @param buf
* @param len
*/
void AsyncTCPbuffer::_rxData(uint8_t *buf, size_t len) {
if(!_client || !_client->connected()) {
DEBUG_ASYNC_TCP("[A-TCP] not connected!\n");
return;
}
if(!_RXbuffer) {
DEBUG_ASYNC_TCP("[A-TCP] _rxData no _RXbuffer!\n");
return;
}
DEBUG_ASYNC_TCP("[A-TCP] _rxData len: %d RXmode: %d\n", len, _RXmode);
size_t handled = 0;
if(_RXmode != ATB_RX_MODE_NONE) {
handled = _handleRxBuffer((uint8_t *) buf, len);
buf += handled;
len -= handled;
// handle as much as possible before using the buffer
if(_RXbuffer->empty()) {
while(_RXmode != ATB_RX_MODE_NONE && handled != 0 && len > 0) {
handled = _handleRxBuffer(buf, len);
buf += handled;
len -= handled;
}
}
}
if(len > 0) {
if(_RXbuffer->room() < len) {
// to less space
DEBUG_ASYNC_TCP("[A-TCP] _rxData buffer full try resize\n");
_RXbuffer->resizeAdd((len + _RXbuffer->room()));
if(_RXbuffer->room() < len) {
DEBUG_ASYNC_TCP("[A-TCP] _rxData buffer to full can only handle %d!!!\n", _RXbuffer->room());
}
}
_RXbuffer->write((const char *) (buf), len);
}
if(!_RXbuffer->empty() && _RXmode != ATB_RX_MODE_NONE) {
// handle as much as possible data in buffer
handled = _handleRxBuffer(NULL, 0);
while(_RXmode != ATB_RX_MODE_NONE && handled != 0) {
handled = _handleRxBuffer(NULL, 0);
}
}
// clean up ram
if(_RXbuffer->empty() && _RXbuffer->room() != 100) {
_RXbuffer->resize(100);
}
}
/**
*
*/
size_t AsyncTCPbuffer::_handleRxBuffer(uint8_t *buf, size_t len) {
if(!_client || !_client->connected() || _RXbuffer == NULL) {
return 0;
}
DEBUG_ASYNC_TCP("[A-TCP] _handleRxBuffer len: %d RXmode: %d\n", len, _RXmode);
size_t BufferAvailable = _RXbuffer->available();
size_t r = 0;
if(_RXmode == ATB_RX_MODE_NONE) {
return 0;
} else if(_RXmode == ATB_RX_MODE_FREE) {
if(_cbRX == NULL) {
return 0;
}
if(BufferAvailable > 0) {
uint8_t * b = new (std::nothrow) uint8_t[BufferAvailable];
if(b == NULL){
panic(); //TODO: What action should this be ?
}
_RXbuffer->peek((char *) b, BufferAvailable);
r = _cbRX(b, BufferAvailable);
_RXbuffer->remove(r);
}
if(r == BufferAvailable && buf && (len > 0)) {
return _cbRX(buf, len);
} else {
return 0;
}
} else if(_RXmode == ATB_RX_MODE_READ_BYTES) {
if(_rxReadBytesPtr == NULL || _cbDone == NULL) {
return 0;
}
size_t newReadCount = 0;
if(BufferAvailable) {
r = _RXbuffer->read((char *) _rxReadBytesPtr, _rxSize);
_rxSize -= r;
_rxReadBytesPtr += r;
}
if(_RXbuffer->empty() && (len > 0) && buf) {
r = len;
if(r > _rxSize) {
r = _rxSize;
}
memcpy(_rxReadBytesPtr, buf, r);
_rxReadBytesPtr += r;
_rxSize -= r;
newReadCount += r;
}
if(_rxSize == 0) {
_RXmode = ATB_RX_MODE_NONE;
_cbDone(true, NULL);
}
// add left over bytes to Buffer
return newReadCount;
} else if(_RXmode == ATB_RX_MODE_TERMINATOR) {
// TODO implement read terminator non string
} else if(_RXmode == ATB_RX_MODE_TERMINATOR_STRING) {
if(_rxReadStringPtr == NULL || _cbDone == NULL) {
return 0;
}
// handle Buffer
if(BufferAvailable > 0) {
while(!_RXbuffer->empty()) {
char c = _RXbuffer->read();
if(c == _rxTerminator || c == 0x00) {
_RXmode = ATB_RX_MODE_NONE;
_cbDone(true, _rxReadStringPtr);
return 0;
} else {
(*_rxReadStringPtr) += c;
}
}
}
if(_RXbuffer->empty() && (len > 0) && buf) {
size_t newReadCount = 0;
while(newReadCount < len) {
char c = (char) *buf;
buf++;
newReadCount++;
if(c == _rxTerminator || c == 0x00) {
_RXmode = ATB_RX_MODE_NONE;
_cbDone(true, _rxReadStringPtr);
return newReadCount;
} else {
(*_rxReadStringPtr) += c;
}
}
return newReadCount;
}
}
return 0;
}