Add option to handle pbufs instead of buffers

This commit is contained in:
me-no-dev
2018-01-27 12:30:24 +01:00
parent 4dbbf10609
commit 75e2b15377
2 changed files with 35 additions and 10 deletions

View File

@@ -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;

View File

@@ -25,7 +25,8 @@
#include "IPAddress.h"
#include <functional>
extern "C" {
#include "freertos/semphr.h"
#include "freertos/semphr.h"
#include "lwip/pbuf.h"
}
class AsyncClient;
@@ -38,10 +39,10 @@ typedef std::function<void(void*, AsyncClient*)> AcConnectHandler;
typedef std::function<void(void*, AsyncClient*, size_t len, uint32_t time)> AcAckHandler;
typedef std::function<void(void*, AsyncClient*, int8_t error)> AcErrorHandler;
typedef std::function<void(void*, AsyncClient*, void *data, size_t len)> AcDataHandler;
typedef std::function<void(void*, AsyncClient*, struct pbuf *pb)> AcPacketHandler;
typedef std::function<void(void*, AsyncClient*, uint32_t time)> 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();