Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit 75c2513

Browse files
mhightower83me-no-dev
authored andcommitted
Fix for tcp_err() callbacks and other issues (#115)
* Fixes the handling tcp_err() callback and addresses build issues with different core releases. Summary: The operator "new ..." was changed to "new (std::nothrow) ...", which will return NULL when the heap is out of memory. Without the change "soft WDT" was the result, starting with Arduino ESP8266 Core 2.5.0. (Note, RE:"soft WDT" - the error reporting may improve with core 2.6.) With proir core versions the library appears to work fine. ref: esp8266/Arduino#6269 (comment) To support newer lwIP versions and buffer models. All references to 1460 were replaced with TCP_MSS. If TCP_MSS is not defined (exp. 1.4v lwIP) 1460 is assumed. The ESPAsyncTCP library should build for Arduino ESP8266 Core releases: 2.3.0, 2.4.1, 2.4.2, 2.5.1, 2.5.2. It may still build with core versions 2.4.0 and 2.5.0. I did not do any regression testing with these, since they had too many issues and were quickly superseded. lwIP tcp_err() callback often resulted in crashes. The problem was a tcp_err() would come in, while processing a send or receive in the forground. The tcp_err() callback would be passed down to a client's registered disconnect CB. A common problem with SyncClient and other modules as well as some client code was: the freeing of ESPAsyncTCP AsyncClient objects via disconnect CB handlers while the library was waiting for an operstion to finished. Attempts to access bad pointers followed. For SyncClient this commonly occured during a call to delay(). On return to SyncClient _client was invalid. Also the problem described by issue #94 also surfaced Use of tcp_abort() required some very special handling and was very challenging to make work without changing client API. ERR_ABRT can only be used once on a return to lwIP for a given connection and since the AsyncClient structure was sometimes deleted before returning to lwIP, the state tracking became tricky. While ugly, a global variable for this seemed to work; however, I abanded it when I saw a possible reentrancy/concurrency issue. After several approaches I settled the problem by creating "class ACErrorTracker" to manage the issue. Additional Async Client considerations: The client sketch must always test if the connection is still up at loop() entry and after the return of any function call, that may have done a delay() or yield() or any ESPAsyncTCP library family call. For example, the connection could be lost during a call to _client->write(...). Client sketches that delete _client as part of their onDisconnect() handler must be very careful as _client will become invalid after calls to delay(), yield(), etc. * moved DebugPrintMacros.h to the correct loccation.
1 parent 545260d commit 75c2513

14 files changed

+1517
-840
lines changed

library.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
"type": "git",
1313
"url": "https://github.com/me-no-dev/ESPAsyncTCP.git"
1414
},
15-
"version": "1.2.0",
15+
"version": "1.2.1",
1616
"license": "LGPL-3.0",
1717
"frameworks": "arduino",
1818
"platforms": "espressif8266",
1919
"build": {
2020
"libCompatMode": 2
21-
}
21+
}
2222
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP AsyncTCP
2-
version=1.2.0
2+
version=1.2.1
33
author=Me-No-Dev
44
maintainer=Me-No-Dev
55
sentence=Async TCP Library for ESP8266 and ESP31B

src/AsyncPrinter.cpp

+41-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ AsyncPrinter::AsyncPrinter()
2828
, _close_cb(NULL)
2929
, _close_arg(NULL)
3030
, _tx_buffer(NULL)
31-
, _tx_buffer_size(1460)
31+
, _tx_buffer_size(TCP_MSS)
3232
, next(NULL)
3333
{}
3434

@@ -43,7 +43,10 @@ AsyncPrinter::AsyncPrinter(AsyncClient *client, size_t txBufLen)
4343
, next(NULL)
4444
{
4545
_attachCallbacks();
46-
_tx_buffer = new cbuf(_tx_buffer_size);
46+
_tx_buffer = new (std::nothrow) cbuf(_tx_buffer_size);
47+
if(_tx_buffer == NULL) {
48+
panic(); //What should we do?
49+
}
4750
}
4851

4952
AsyncPrinter::~AsyncPrinter(){
@@ -63,10 +66,14 @@ void AsyncPrinter::onClose(ApCloseHandler cb, void *arg){
6366
int AsyncPrinter::connect(IPAddress ip, uint16_t port){
6467
if(_client != NULL && connected())
6568
return 0;
66-
_client = new AsyncClient();
69+
_client = new (std::nothrow) AsyncClient();
70+
if (_client == NULL) {
71+
panic();
72+
}
73+
6774
_client->onConnect([](void *obj, AsyncClient *c){ ((AsyncPrinter*)(obj))->_onConnect(c); }, this);
6875
if(_client->connect(ip, port)){
69-
while(_client->state() < 4)
76+
while(_client && _client->state() < 4)
7077
delay(1);
7178
return connected();
7279
}
@@ -76,23 +83,32 @@ int AsyncPrinter::connect(IPAddress ip, uint16_t port){
7683
int AsyncPrinter::connect(const char *host, uint16_t port){
7784
if(_client != NULL && connected())
7885
return 0;
79-
_client = new AsyncClient();
86+
_client = new (std::nothrow) AsyncClient();
87+
if (_client == NULL) {
88+
panic();
89+
}
90+
8091
_client->onConnect([](void *obj, AsyncClient *c){ ((AsyncPrinter*)(obj))->_onConnect(c); }, this);
8192
if(_client->connect(host, port)){
82-
while(_client->state() < 4)
93+
while(_client && _client->state() < 4)
8394
delay(1);
8495
return connected();
8596
}
8697
return 0;
8798
}
8899

89100
void AsyncPrinter::_onConnect(AsyncClient *c){
101+
(void)c;
90102
if(_tx_buffer != NULL){
91103
cbuf *b = _tx_buffer;
92104
_tx_buffer = NULL;
93105
delete b;
94106
}
95-
_tx_buffer = new cbuf(_tx_buffer_size);
107+
_tx_buffer = new (std::nothrow) cbuf(_tx_buffer_size);
108+
if(_tx_buffer) {
109+
panic();
110+
}
111+
96112
_attachCallbacks();
97113
}
98114

@@ -109,7 +125,11 @@ AsyncPrinter & AsyncPrinter::operator=(const AsyncPrinter &other){
109125
_tx_buffer = NULL;
110126
delete b;
111127
}
112-
_tx_buffer = new cbuf(other._tx_buffer_size);
128+
_tx_buffer = new (std::nothrow) cbuf(other._tx_buffer_size);
129+
if(_tx_buffer == NULL) {
130+
panic();
131+
}
132+
113133
_client = other._client;
114134
_attachCallbacks();
115135
return *this;
@@ -127,13 +147,16 @@ size_t AsyncPrinter::write(const uint8_t *data, size_t len){
127147
while(_tx_buffer->room() < toSend){
128148
toWrite = _tx_buffer->room();
129149
_tx_buffer->write((const char*)data, toWrite);
130-
while(!_client->canSend())
150+
while(connected() && !_client->canSend())
131151
delay(0);
152+
if(!connected())
153+
return 0; // or len - toSend;
132154
_sendBuffer();
133155
toSend -= toWrite;
134156
}
135157
_tx_buffer->write((const char*)(data+(len - toSend)), toSend);
136-
while(!_client->canSend()) delay(0);
158+
while(connected() && !_client->canSend()) delay(0);
159+
if(!connected()) return 0; // or len - toSend;
137160
_sendBuffer();
138161
return len;
139162
}
@@ -154,7 +177,11 @@ size_t AsyncPrinter::_sendBuffer(){
154177
size_t sendable = _client->space();
155178
if(sendable < available)
156179
available= sendable;
157-
char *out = new char[available];
180+
char *out = new (std::nothrow) char[available];
181+
if (out == NULL) {
182+
panic(); // Connection should be aborted instead
183+
}
184+
158185
_tx_buffer->read(out, available);
159186
size_t sent = _client->write(out, available);
160187
delete out;
@@ -180,8 +207,8 @@ void AsyncPrinter::_on_close(){
180207
}
181208

182209
void AsyncPrinter::_attachCallbacks(){
183-
_client->onPoll([](void *obj, AsyncClient* c){ ((AsyncPrinter*)(obj))->_sendBuffer(); }, this);
184-
_client->onAck([](void *obj, AsyncClient* c, size_t len, uint32_t time){ ((AsyncPrinter*)(obj))->_sendBuffer(); }, this);
210+
_client->onPoll([](void *obj, AsyncClient* c){ (void)c; ((AsyncPrinter*)(obj))->_sendBuffer(); }, this);
211+
_client->onAck([](void *obj, AsyncClient* c, size_t len, uint32_t time){ (void)c; (void)len; (void)time; ((AsyncPrinter*)(obj))->_sendBuffer(); }, this);
185212
_client->onDisconnect([](void *obj, AsyncClient* c){ ((AsyncPrinter*)(obj))->_on_close(); delete c; }, this);
186-
_client->onData([](void *obj, AsyncClient* c, void *data, size_t len){ ((AsyncPrinter*)(obj))->_onData(data, len); }, this);
213+
_client->onData([](void *obj, AsyncClient* c, void *data, size_t len){ (void)c; ((AsyncPrinter*)(obj))->_onData(data, len); }, this);
187214
}

src/AsyncPrinter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AsyncPrinter: public Print {
4646
AsyncPrinter *next;
4747

4848
AsyncPrinter();
49-
AsyncPrinter(AsyncClient *client, size_t txBufLen = 1460);
49+
AsyncPrinter(AsyncClient *client, size_t txBufLen = TCP_MSS);
5050
virtual ~AsyncPrinter();
5151

5252
int connect(IPAddress ip, uint16_t port);

src/DebugPrintMacros.h

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#ifndef _DEBUG_PRINT_MACROS_H
2+
#define _DEBUG_PRINT_MACROS_H
3+
// Some customizable print macros to suite the debug needs de jour.
4+
5+
// Debug macros
6+
// #include <pgmspace.h>
7+
// https://stackoverflow.com/questions/8487986/file-macro-shows-full-path
8+
// This value is resolved at compile time.
9+
#define _FILENAME_ strrchr("/" __FILE__, '/')
10+
11+
// #define DEBUG_ESP_ASYNC_TCP 1
12+
// #define DEBUG_ESP_TCP_SSL 1
13+
// #define DEBUG_ESP_PORT Serial
14+
15+
#if defined(DEBUG_ESP_PORT) && !defined(DEBUG_TIME_STAMP_FMT)
16+
#define DEBUG_TIME_STAMP_FMT "%06u.%03u "
17+
struct _DEBUG_TIME_STAMP {
18+
unsigned dec;
19+
unsigned whole;
20+
};
21+
inline struct _DEBUG_TIME_STAMP debugTimeStamp(void) {
22+
struct _DEBUG_TIME_STAMP st;
23+
unsigned now = millis() % 1000000000;
24+
st.dec = now % 1000;
25+
st.whole = now / 1000;
26+
return st;
27+
}
28+
#endif
29+
30+
#if defined(DEBUG_ESP_PORT) && !defined(DEBUG_GENERIC)
31+
#define DEBUG_GENERIC( module, format, ... ) \
32+
do { \
33+
struct _DEBUG_TIME_STAMP st = debugTimeStamp(); \
34+
DEBUG_ESP_PORT.printf( DEBUG_TIME_STAMP_FMT module " " format, st.whole, st.dec, ##__VA_ARGS__ ); \
35+
} while(false)
36+
#endif
37+
#if defined(DEBUG_ESP_PORT) && !defined(DEBUG_GENERIC_P)
38+
#define DEBUG_GENERIC_P( module, format, ... ) \
39+
do { \
40+
struct _DEBUG_TIME_STAMP st = debugTimeStamp(); \
41+
DEBUG_ESP_PORT.printf_P(PSTR( DEBUG_TIME_STAMP_FMT module " " format ), st.whole, st.dec, ##__VA_ARGS__ ); \
42+
} while(false)
43+
#endif
44+
45+
#if defined(DEBUG_GENERIC) && !defined(ASSERT_GENERIC)
46+
#define ASSERT_GENERIC( a, module ) \
47+
do { \
48+
if ( !(a) ) { \
49+
DEBUG_GENERIC( module, "%s:%s:%u: ASSERT("#a") failed!\n", __FILE__, __func__, __LINE__); \
50+
DEBUG_ESP_PORT.flush(); \
51+
} \
52+
} while(false)
53+
#endif
54+
#if defined(DEBUG_GENERIC_P) && !defined(ASSERT_GENERIC_P)
55+
#define ASSERT_GENERIC_P( a, module ) \
56+
do { \
57+
if ( !(a) ) { \
58+
DEBUG_GENERIC_P( module, "%s:%s:%u: ASSERT("#a") failed!\n", __FILE__, __func__, __LINE__); \
59+
DEBUG_ESP_PORT.flush(); \
60+
} \
61+
} while(false)
62+
#endif
63+
64+
#ifndef DEBUG_GENERIC
65+
#define DEBUG_GENERIC(...) do { (void)0;} while(false)
66+
#endif
67+
68+
#ifndef DEBUG_GENERIC_P
69+
#define DEBUG_GENERIC_P(...) do { (void)0;} while(false)
70+
#endif
71+
72+
#ifndef ASSERT_GENERIC
73+
#define ASSERT_GENERIC(...) do { (void)0;} while(false)
74+
#endif
75+
76+
#ifndef ASSERT_GENERIC_P
77+
#define ASSERT_GENERIC_P(...) do { (void)0;} while(false)
78+
#endif
79+
80+
#ifndef DEBUG_ESP_PRINTF
81+
#define DEBUG_ESP_PRINTF( format, ...) DEBUG_GENERIC_P("[%s]", format, &_FILENAME_[1], ##__VA_ARGS__)
82+
#endif
83+
84+
#if defined(DEBUG_ESP_ASYNC_TCP) && !defined(ASYNC_TCP_DEBUG)
85+
#define ASYNC_TCP_DEBUG( format, ...) DEBUG_GENERIC_P("[ASYNC_TCP]", format, ##__VA_ARGS__)
86+
#endif
87+
88+
#ifndef ASYNC_TCP_ASSERT
89+
#define ASYNC_TCP_ASSERT( a ) ASSERT_GENERIC_P( (a), "[ASYNC_TCP]")
90+
#endif
91+
92+
#if defined(DEBUG_ESP_TCP_SSL) && !defined(TCP_SSL_DEBUG)
93+
#define TCP_SSL_DEBUG( format, ...) DEBUG_GENERIC_P("[TCP_SSL]", format, ##__VA_ARGS__)
94+
#endif
95+
96+
#endif //_DEBUG_PRINT_MACROS_H

0 commit comments

Comments
 (0)