diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index ba1929b..7a98f8e 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -352,6 +352,8 @@ AsyncClient::AsyncClient(tcp_pcb* pcb) , _error_cb_arg(0) , _recv_cb(0) , _recv_cb_arg(0) +, _pb_cb(0) +, _pb_cb_arg(0) , _timeout_cb(0) , _timeout_cb_arg(0) , _pcb_busy(false) @@ -504,15 +506,19 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) { //Serial.write((const uint8_t *)pb->payload, pb->len); _ack_pcb = true; pbuf *b = pb; - if(_recv_cb) - _recv_cb(_recv_cb_arg, this, b->payload, b->len); - if(!_ack_pcb) - _rx_ack_len += b->len; - else - _tcp_recved(pcb, b->len); pb = b->next; b->next = NULL; - pbuf_free(b); + if(_pb_cb){ + _pb_cb(_pb_cb_arg, this, b); + } else { + if(_recv_cb) + _recv_cb(_recv_cb_arg, this, b->payload, b->len); + if(!_ack_pcb) + _rx_ack_len += b->len; + else + _tcp_recved(pcb, b->len); + pbuf_free(b); + } } return ERR_OK; } @@ -814,6 +820,14 @@ bool AsyncClient::canSend(){ return space() > 0; } +void AsyncClient::ackPacket(struct pbuf * pb){ + if(!pb){ + return; + } + _tcp_recved(_pcb, pb->len); + pbuf_free(pb); +} + // Callback Setters @@ -842,6 +856,11 @@ void AsyncClient::onData(AcDataHandler cb, void* arg){ _recv_cb_arg = arg; } +void AsyncClient::onPacket(AcPacketHandler cb, void* arg){ + _pb_cb = cb; + _pb_cb_arg = arg; +} + void AsyncClient::onTimeout(AcTimeoutHandler cb, void* arg){ _timeout_cb = cb; _timeout_cb_arg = arg; diff --git a/src/AsyncTCP.h b/src/AsyncTCP.h index 1c24ac9..79664bf 100644 --- a/src/AsyncTCP.h +++ b/src/AsyncTCP.h @@ -25,7 +25,8 @@ #include "IPAddress.h" #include extern "C" { -#include "freertos/semphr.h" + #include "freertos/semphr.h" + #include "lwip/pbuf.h" } class AsyncClient; @@ -38,10 +39,10 @@ typedef std::function AcConnectHandler; typedef std::function AcAckHandler; typedef std::function AcErrorHandler; typedef std::function AcDataHandler; +typedef std::function AcPacketHandler; typedef std::function AcTimeoutHandler; struct tcp_pcb; -struct pbuf; struct _ip_addr; class AsyncClient { @@ -58,6 +59,8 @@ class AsyncClient { void* _error_cb_arg; AcDataHandler _recv_cb; void* _recv_cb_arg; + AcPacketHandler _pb_cb; + void* _pb_cb_arg; AcTimeoutHandler _timeout_cb; void* _timeout_cb_arg; AcConnectHandler _poll_cb; @@ -141,10 +144,13 @@ class AsyncClient { void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected void onAck(AcAckHandler cb, void* arg = 0); //ack received void onError(AcErrorHandler cb, void* arg = 0); //unsuccessful connect or error - void onData(AcDataHandler cb, void* arg = 0); //data received + void onData(AcDataHandler cb, void* arg = 0); //data received (called if onPacket is not used) + void onPacket(AcPacketHandler cb, void* arg = 0); //data received void onTimeout(AcTimeoutHandler cb, void* arg = 0); //ack timeout void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected + void ackPacket(struct pbuf * pb); + const char * errorToString(int8_t error); const char * stateToString();