Skip to content

Commit 55cd520

Browse files
committed
Add posibility to handle packet buffers directly
1 parent 084eb0b commit 55cd520

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/ESPAsyncTCP.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ AsyncClient::AsyncClient(tcp_pcb* pcb):
5050
, _error_cb_arg(0)
5151
, _recv_cb(0)
5252
, _recv_cb_arg(0)
53+
, _pb_cb(0)
54+
, _pb_cb_arg(0)
5355
, _timeout_cb(0)
5456
, _timeout_cb_arg(0)
5557
, _pcb_busy(false)
@@ -402,16 +404,20 @@ err_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, err_t err) {
402404
//we should not ack before we assimilate the data
403405
_ack_pcb = true;
404406
pbuf *b = pb;
405-
ASYNC_TCP_DEBUG("_recv: %d\n", b->len);
406-
if(_recv_cb)
407-
_recv_cb(_recv_cb_arg, this, b->payload, b->len);
408-
if(!_ack_pcb)
409-
_rx_ack_len += b->len;
410-
else
411-
tcp_recved(pcb, b->len);
412407
pb = b->next;
413408
b->next = NULL;
414-
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+
}
415421
}
416422
return ERR_OK;
417423
}
@@ -690,6 +696,11 @@ void AsyncClient::onData(AcDataHandler cb, void* arg){
690696
_recv_cb_arg = arg;
691697
}
692698

699+
void AsyncClient::onPacket(AcPacketHandler cb, void* arg){
700+
_pb_cb = cb;
701+
_pb_cb_arg = arg;
702+
}
703+
693704
void AsyncClient::onTimeout(AcTimeoutHandler cb, void* arg){
694705
_timeout_cb = cb;
695706
_timeout_cb_arg = arg;
@@ -724,6 +735,14 @@ size_t AsyncClient::space(){
724735
return 0;
725736
}
726737

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+
727746
const char * AsyncClient::errorToString(int8_t error){
728747
switch(error){
729748
case 0: return "OK";

src/ESPAsyncTCP.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
extern "C" {
3030
#include "lwip/init.h"
3131
#include "lwip/err.h"
32+
#include "lwip/pbuf.h"
3233
};
3334

3435
class AsyncClient;
@@ -37,14 +38,7 @@ class AsyncClient;
3738
#define ASYNC_WRITE_FLAG_COPY 0x01 //will allocate new buffer to hold the data while sending (else will hold reference to the data given)
3839
#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.
3940

40-
typedef std::function<void(void*, AsyncClient*)> AcConnectHandler;
41-
typedef std::function<void(void*, AsyncClient*, size_t len, uint32_t time)> AcAckHandler;
42-
typedef std::function<void(void*, AsyncClient*, int8_t error)> AcErrorHandler;
43-
typedef std::function<void(void*, AsyncClient*, void *data, size_t len)> AcDataHandler;
44-
typedef std::function<void(void*, AsyncClient*, uint32_t time)> AcTimeoutHandler;
45-
4641
struct tcp_pcb;
47-
struct pbuf;
4842
struct ip_addr;
4943
#if ASYNC_TCP_SSL_ENABLED
5044
struct SSL_;
@@ -53,6 +47,13 @@ struct SSL_CTX_;
5347
typedef struct SSL_CTX_ SSL_CTX;
5448
#endif
5549

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+
5657
class AsyncClient {
5758
protected:
5859
friend class AsyncTCPbuffer;
@@ -67,6 +68,8 @@ class AsyncClient {
6768
void* _error_cb_arg;
6869
AcDataHandler _recv_cb;
6970
void* _recv_cb_arg;
71+
AcPacketHandler _pb_cb;
72+
void* _pb_cb_arg;
7073
AcTimeoutHandler _timeout_cb;
7174
void* _timeout_cb_arg;
7275
AcConnectHandler _poll_cb;
@@ -190,10 +193,13 @@ class AsyncClient {
190193
void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected
191194
void onAck(AcAckHandler cb, void* arg = 0); //ack received
192195
void onError(AcErrorHandler cb, void* arg = 0); //unsuccessful connect or error
193-
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
194198
void onTimeout(AcTimeoutHandler cb, void* arg = 0); //ack timeout
195199
void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected
196200

201+
void ackPacket(struct pbuf * pb);
202+
197203
const char * errorToString(int8_t error);
198204
const char * stateToString();
199205

0 commit comments

Comments
 (0)