Skip to content

Commit 8ba895c

Browse files
authored
Merge pull request #1 from me-no-dev/master
Update fork with master
2 parents 9b0cc37 + 55cd520 commit 8ba895c

File tree

3 files changed

+103
-54
lines changed

3 files changed

+103
-54
lines changed

library.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
"type": "git",
1313
"url": "https://github.com/me-no-dev/ESPAsyncTCP.git"
1414
},
15-
"version": "1.1.0",
15+
"version": "1.1.3",
1616
"license": "LGPL-3.0",
1717
"frameworks": "arduino",
18-
"platforms":"espressif8266"
18+
"platforms": "espressif8266",
19+
"build": {
20+
"libCompatMode": 2
21+
}
1922
}

src/ESPAsyncTCP.cpp

+55-28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern "C"{
2727
#include "lwip/tcp.h"
2828
#include "lwip/inet.h"
2929
#include "lwip/dns.h"
30+
#include "lwip/init.h"
3031
}
3132
#include <tcp_axtls.h>
3233

@@ -49,6 +50,8 @@ AsyncClient::AsyncClient(tcp_pcb* pcb):
4950
, _error_cb_arg(0)
5051
, _recv_cb(0)
5152
, _recv_cb_arg(0)
53+
, _pb_cb(0)
54+
, _pb_cb_arg(0)
5255
, _timeout_cb(0)
5356
, _timeout_cb_arg(0)
5457
, _pcb_busy(false)
@@ -110,11 +113,12 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
110113
return false;
111114
ip_addr_t addr;
112115
addr.addr = ip;
116+
#if LWIP_VERSION_MAJOR == 1
113117
netif* interface = ip_route(&addr);
114118
if (!interface){ //no route to host
115119
return false;
116120
}
117-
121+
#endif
118122
tcp_pcb* pcb = tcp_new();
119123
if (!pcb){ //could not allocate pcb
120124
return false;
@@ -281,7 +285,7 @@ size_t AsyncClient::ack(size_t len){
281285

282286
// Private Callbacks
283287

284-
int8_t AsyncClient::_connected(void* pcb, int8_t err){
288+
err_t AsyncClient::_connected(void* pcb, err_t err){
285289
_pcb = reinterpret_cast<tcp_pcb*>(pcb);
286290
if(_pcb){
287291
_pcb_busy = false;
@@ -334,7 +338,7 @@ int8_t AsyncClient::_close(){
334338
return err;
335339
}
336340

337-
void AsyncClient::_error(int8_t err) {
341+
void AsyncClient::_error(err_t err) {
338342
if(_pcb){
339343
#if ASYNC_TCP_SSL_ENABLED
340344
if(_pcb_secure){
@@ -361,7 +365,7 @@ void AsyncClient::_ssl_error(int8_t err){
361365
}
362366
#endif
363367

364-
int8_t AsyncClient::_sent(tcp_pcb* pcb, uint16_t len) {
368+
err_t AsyncClient::_sent(tcp_pcb* pcb, uint16_t len) {
365369
_rx_last_packet = millis();
366370
ASYNC_TCP_DEBUG("_sent: %u\n", len);
367371
_tx_unacked_len -= len;
@@ -375,7 +379,7 @@ int8_t AsyncClient::_sent(tcp_pcb* pcb, uint16_t len) {
375379
return ERR_OK;
376380
}
377381

378-
int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
382+
err_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, err_t err) {
379383
if(pb == NULL){
380384
ASYNC_TCP_DEBUG("_recv: pb == NULL! Closing... %d\n", err);
381385
return _close();
@@ -400,21 +404,25 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
400404
//we should not ack before we assimilate the data
401405
_ack_pcb = true;
402406
pbuf *b = pb;
403-
ASYNC_TCP_DEBUG("_recv: %d\n", b->len);
404-
if(_recv_cb)
405-
_recv_cb(_recv_cb_arg, this, b->payload, b->len);
406-
if(!_ack_pcb)
407-
_rx_ack_len += b->len;
408-
else
409-
tcp_recved(pcb, b->len);
410407
pb = b->next;
411408
b->next = NULL;
412-
pbuf_free(b);
409+
ASYNC_TCP_DEBUG("_recv: %d\n", b->len);
410+
if(_pb_cb){
411+
_pb_cb(_pb_cb_arg, this, b);
412+
} else {
413+
if(_recv_cb)
414+
_recv_cb(_recv_cb_arg, this, b->payload, b->len);
415+
if(!_ack_pcb)
416+
_rx_ack_len += b->len;
417+
else
418+
tcp_recved(pcb, b->len);
419+
pbuf_free(b);
420+
}
413421
}
414422
return ERR_OK;
415423
}
416424

417-
int8_t AsyncClient::_poll(tcp_pcb* pcb){
425+
err_t AsyncClient::_poll(tcp_pcb* pcb){
418426
// Close requested
419427
if(_close_pcb){
420428
_close_pcb = false;
@@ -448,7 +456,11 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
448456
return ERR_OK;
449457
}
450458

451-
void AsyncClient::_dns_found(ip_addr_t *ipaddr){
459+
#if LWIP_VERSION_MAJOR == 1
460+
void AsyncClient::_dns_found(struct ip_addr *ipaddr){
461+
#else
462+
void AsyncClient::_dns_found(const ip_addr *ipaddr){
463+
#endif
452464
if(ipaddr){
453465
#if ASYNC_TCP_SSL_ENABLED
454466
connect(IPAddress(ipaddr->addr), _connect_port, _pcb_secure);
@@ -464,28 +476,31 @@ void AsyncClient::_dns_found(ip_addr_t *ipaddr){
464476
}
465477

466478
// lWIP Callbacks
467-
479+
#if LWIP_VERSION_MAJOR == 1
468480
void AsyncClient::_s_dns_found(const char *name, ip_addr_t *ipaddr, void *arg){
481+
#else
482+
void AsyncClient::_s_dns_found(const char *name, const ip_addr *ipaddr, void *arg){
483+
#endif
469484
reinterpret_cast<AsyncClient*>(arg)->_dns_found(ipaddr);
470485
}
471486

472-
int8_t AsyncClient::_s_poll(void *arg, struct tcp_pcb *tpcb) {
487+
err_t AsyncClient::_s_poll(void *arg, struct tcp_pcb *tpcb) {
473488
return reinterpret_cast<AsyncClient*>(arg)->_poll(tpcb);
474489
}
475490

476-
int8_t AsyncClient::_s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, int8_t err) {
491+
err_t AsyncClient::_s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) {
477492
return reinterpret_cast<AsyncClient*>(arg)->_recv(tpcb, pb, err);
478493
}
479494

480-
void AsyncClient::_s_error(void *arg, int8_t err) {
495+
void AsyncClient::_s_error(void *arg, err_t err) {
481496
reinterpret_cast<AsyncClient*>(arg)->_error(err);
482497
}
483498

484-
int8_t AsyncClient::_s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len) {
499+
err_t AsyncClient::_s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len) {
485500
return reinterpret_cast<AsyncClient*>(arg)->_sent(tpcb, len);
486501
}
487502

488-
int8_t AsyncClient::_s_connected(void* arg, void* tpcb, int8_t err){
503+
err_t AsyncClient::_s_connected(void* arg, void* tpcb, err_t err){
489504
return reinterpret_cast<AsyncClient*>(arg)->_connected(tpcb, err);
490505
}
491506

@@ -681,6 +696,11 @@ void AsyncClient::onData(AcDataHandler cb, void* arg){
681696
_recv_cb_arg = arg;
682697
}
683698

699+
void AsyncClient::onPacket(AcPacketHandler cb, void* arg){
700+
_pb_cb = cb;
701+
_pb_cb_arg = arg;
702+
}
703+
684704
void AsyncClient::onTimeout(AcTimeoutHandler cb, void* arg){
685705
_timeout_cb = cb;
686706
_timeout_cb_arg = arg;
@@ -715,6 +735,14 @@ size_t AsyncClient::space(){
715735
return 0;
716736
}
717737

738+
void AsyncClient::ackPacket(struct pbuf * pb){
739+
if(!pb){
740+
return;
741+
}
742+
tcp_recved(_pcb, pb->len);
743+
pbuf_free(pb);
744+
}
745+
718746
const char * AsyncClient::errorToString(int8_t error){
719747
switch(error){
720748
case 0: return "OK";
@@ -855,7 +883,6 @@ void AsyncServer::beginSecure(const char *cert, const char *key, const char *pas
855883
void AsyncServer::end(){
856884
if(_pcb){
857885
//cleanup all connections?
858-
tcp_abort(_pcb);
859886
tcp_arg(_pcb, NULL);
860887
tcp_accept(_pcb, NULL);
861888
if(tcp_close(_pcb) != ERR_OK){
@@ -896,7 +923,7 @@ uint8_t AsyncServer::status(){
896923
return _pcb->state;
897924
}
898925

899-
int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
926+
err_t AsyncServer::_accept(tcp_pcb* pcb, err_t err){
900927
if(_connect_cb){
901928
#if ASYNC_TCP_SSL_ENABLED
902929
if (_noDelay || _ssl_ctx)
@@ -964,12 +991,12 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
964991
return ERR_OK;
965992
}
966993

967-
int8_t AsyncServer::_s_accept(void *arg, tcp_pcb* pcb, int8_t err){
994+
err_t AsyncServer::_s_accept(void *arg, tcp_pcb* pcb, err_t err){
968995
return reinterpret_cast<AsyncServer*>(arg)->_accept(pcb, err);
969996
}
970997

971998
#if ASYNC_TCP_SSL_ENABLED
972-
int8_t AsyncServer::_poll(tcp_pcb* pcb){
999+
err_t AsyncServer::_poll(tcp_pcb* pcb){
9731000
if(!tcp_ssl_has_client() && _pending){
9741001
struct pending_pcb * p = _pending;
9751002
if(p->pcb == pcb){
@@ -995,7 +1022,7 @@ int8_t AsyncServer::_poll(tcp_pcb* pcb){
9951022
return ERR_OK;
9961023
}
9971024

998-
int8_t AsyncServer::_recv(struct tcp_pcb *pcb, struct pbuf *pb, int8_t err){
1025+
err_t AsyncServer::_recv(struct tcp_pcb *pcb, struct pbuf *pb, err_t err){
9991026
if(!_pending)
10001027
return ERR_OK;
10011028

@@ -1047,11 +1074,11 @@ int AsyncServer::_s_cert(void *arg, const char *filename, uint8_t **buf){
10471074
return reinterpret_cast<AsyncServer*>(arg)->_cert(filename, buf);
10481075
}
10491076

1050-
int8_t AsyncServer::_s_poll(void *arg, struct tcp_pcb *pcb){
1077+
err_t AsyncServer::_s_poll(void *arg, struct tcp_pcb *pcb){
10511078
return reinterpret_cast<AsyncServer*>(arg)->_poll(pcb);
10521079
}
10531080

1054-
int8_t AsyncServer::_s_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *pb, int8_t err){
1081+
err_t AsyncServer::_s_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *pb, err_t err){
10551082
return reinterpret_cast<AsyncServer*>(arg)->_recv(pcb, pb, err);
10561083
}
10571084
#endif

src/ESPAsyncTCP.h

+43-24
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,19 @@
2626
#include "IPAddress.h"
2727
#include <functional>
2828

29+
extern "C" {
30+
#include "lwip/init.h"
31+
#include "lwip/err.h"
32+
#include "lwip/pbuf.h"
33+
};
34+
2935
class AsyncClient;
3036

3137
#define ASYNC_MAX_ACK_TIME 5000
3238
#define ASYNC_WRITE_FLAG_COPY 0x01 //will allocate new buffer to hold the data while sending (else will hold reference to the data given)
3339
#define ASYNC_WRITE_FLAG_MORE 0x02 //will not send PSH flag, meaning that there should be more data to be sent before the application should react.
3440

35-
typedef std::function<void(void*, AsyncClient*)> AcConnectHandler;
36-
typedef std::function<void(void*, AsyncClient*, size_t len, uint32_t time)> AcAckHandler;
37-
typedef std::function<void(void*, AsyncClient*, int8_t error)> AcErrorHandler;
38-
typedef std::function<void(void*, AsyncClient*, void *data, size_t len)> AcDataHandler;
39-
typedef std::function<void(void*, AsyncClient*, uint32_t time)> AcTimeoutHandler;
40-
4141
struct tcp_pcb;
42-
struct pbuf;
4342
struct ip_addr;
4443
#if ASYNC_TCP_SSL_ENABLED
4544
struct SSL_;
@@ -48,6 +47,13 @@ struct SSL_CTX_;
4847
typedef struct SSL_CTX_ SSL_CTX;
4948
#endif
5049

50+
typedef std::function<void(void*, AsyncClient*)> AcConnectHandler;
51+
typedef std::function<void(void*, AsyncClient*, size_t len, uint32_t time)> AcAckHandler;
52+
typedef std::function<void(void*, AsyncClient*, int8_t error)> AcErrorHandler;
53+
typedef std::function<void(void*, AsyncClient*, void *data, size_t len)> AcDataHandler;
54+
typedef std::function<void(void*, AsyncClient*, struct pbuf *pb)> AcPacketHandler;
55+
typedef std::function<void(void*, AsyncClient*, uint32_t time)> AcTimeoutHandler;
56+
5157
class AsyncClient {
5258
protected:
5359
friend class AsyncTCPbuffer;
@@ -62,6 +68,8 @@ class AsyncClient {
6268
void* _error_cb_arg;
6369
AcDataHandler _recv_cb;
6470
void* _recv_cb_arg;
71+
AcPacketHandler _pb_cb;
72+
void* _pb_cb_arg;
6573
AcTimeoutHandler _timeout_cb;
6674
void* _timeout_cb_arg;
6775
AcConnectHandler _poll_cb;
@@ -84,20 +92,28 @@ class AsyncClient {
8492
uint16_t _connect_port;
8593

8694
int8_t _close();
87-
int8_t _connected(void* pcb, int8_t err);
88-
void _error(int8_t err);
95+
err_t _connected(void* pcb, err_t err);
96+
void _error(err_t err);
8997
#if ASYNC_TCP_SSL_ENABLED
9098
void _ssl_error(int8_t err);
9199
#endif
92-
int8_t _poll(tcp_pcb* pcb);
93-
int8_t _sent(tcp_pcb* pcb, uint16_t len);
100+
err_t _poll(tcp_pcb* pcb);
101+
err_t _sent(tcp_pcb* pcb, uint16_t len);
102+
#if LWIP_VERSION_MAJOR == 1
94103
void _dns_found(struct ip_addr *ipaddr);
95-
static int8_t _s_poll(void *arg, struct tcp_pcb *tpcb);
96-
static int8_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, int8_t err);
97-
static void _s_error(void *arg, int8_t err);
98-
static int8_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len);
99-
static int8_t _s_connected(void* arg, void* tpcb, int8_t err);
104+
#else
105+
void _dns_found(const ip_addr *ipaddr);
106+
#endif
107+
static err_t _s_poll(void *arg, struct tcp_pcb *tpcb);
108+
static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err);
109+
static void _s_error(void *arg, err_t err);
110+
static err_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len);
111+
static err_t _s_connected(void* arg, void* tpcb, err_t err);
112+
#if LWIP_VERSION_MAJOR == 1
100113
static void _s_dns_found(const char *name, struct ip_addr *ipaddr, void *arg);
114+
#else
115+
static void _s_dns_found(const char *name, const ip_addr *ipaddr, void *arg);
116+
#endif
101117
#if ASYNC_TCP_SSL_ENABLED
102118
static void _s_data(void *arg, struct tcp_pcb *tcp, uint8_t * data, size_t len);
103119
static void _s_handshake(void *arg, struct tcp_pcb *tcp, SSL *ssl);
@@ -177,14 +193,17 @@ class AsyncClient {
177193
void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected
178194
void onAck(AcAckHandler cb, void* arg = 0); //ack received
179195
void onError(AcErrorHandler cb, void* arg = 0); //unsuccessful connect or error
180-
void onData(AcDataHandler cb, void* arg = 0); //data received
196+
void onData(AcDataHandler cb, void* arg = 0); //data received (called if onPacket is not used)
197+
void onPacket(AcPacketHandler cb, void* arg = 0); //data received
181198
void onTimeout(AcTimeoutHandler cb, void* arg = 0); //ack timeout
182199
void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected
183200

201+
void ackPacket(struct pbuf * pb);
202+
184203
const char * errorToString(int8_t error);
185204
const char * stateToString();
186205

187-
int8_t _recv(tcp_pcb* pcb, pbuf* pb, int8_t err);
206+
err_t _recv(tcp_pcb* pcb, pbuf* pb, err_t err);
188207
};
189208

190209
#if ASYNC_TCP_SSL_ENABLED
@@ -224,15 +243,15 @@ class AsyncServer {
224243
uint8_t status();
225244

226245
protected:
227-
int8_t _accept(tcp_pcb* newpcb, int8_t err);
228-
static int8_t _s_accept(void *arg, tcp_pcb* newpcb, int8_t err);
246+
err_t _accept(tcp_pcb* newpcb, err_t err);
247+
static err_t _s_accept(void *arg, tcp_pcb* newpcb, err_t err);
229248
#if ASYNC_TCP_SSL_ENABLED
230249
int _cert(const char *filename, uint8_t **buf);
231-
int8_t _poll(tcp_pcb* pcb);
232-
int8_t _recv(tcp_pcb *pcb, struct pbuf *pb, int8_t err);
250+
err_t _poll(tcp_pcb* pcb);
251+
err_t _recv(tcp_pcb *pcb, struct pbuf *pb, err_t err);
233252
static int _s_cert(void *arg, const char *filename, uint8_t **buf);
234-
static int8_t _s_poll(void *arg, struct tcp_pcb *tpcb);
235-
static int8_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, int8_t err);
253+
static err_t _s_poll(void *arg, struct tcp_pcb *tpcb);
254+
static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err);
236255
#endif
237256
};
238257

0 commit comments

Comments
 (0)