forked from me-no-dev/AsyncTCP
Add option to handle pbufs instead of buffers
This commit is contained in:
@@ -352,6 +352,8 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
|
|||||||
, _error_cb_arg(0)
|
, _error_cb_arg(0)
|
||||||
, _recv_cb(0)
|
, _recv_cb(0)
|
||||||
, _recv_cb_arg(0)
|
, _recv_cb_arg(0)
|
||||||
|
, _pb_cb(0)
|
||||||
|
, _pb_cb_arg(0)
|
||||||
, _timeout_cb(0)
|
, _timeout_cb(0)
|
||||||
, _timeout_cb_arg(0)
|
, _timeout_cb_arg(0)
|
||||||
, _pcb_busy(false)
|
, _pcb_busy(false)
|
||||||
@@ -504,16 +506,20 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
|
|||||||
//Serial.write((const uint8_t *)pb->payload, pb->len);
|
//Serial.write((const uint8_t *)pb->payload, pb->len);
|
||||||
_ack_pcb = true;
|
_ack_pcb = true;
|
||||||
pbuf *b = pb;
|
pbuf *b = pb;
|
||||||
|
pb = b->next;
|
||||||
|
b->next = NULL;
|
||||||
|
if(_pb_cb){
|
||||||
|
_pb_cb(_pb_cb_arg, this, b);
|
||||||
|
} else {
|
||||||
if(_recv_cb)
|
if(_recv_cb)
|
||||||
_recv_cb(_recv_cb_arg, this, b->payload, b->len);
|
_recv_cb(_recv_cb_arg, this, b->payload, b->len);
|
||||||
if(!_ack_pcb)
|
if(!_ack_pcb)
|
||||||
_rx_ack_len += b->len;
|
_rx_ack_len += b->len;
|
||||||
else
|
else
|
||||||
_tcp_recved(pcb, b->len);
|
_tcp_recved(pcb, b->len);
|
||||||
pb = b->next;
|
|
||||||
b->next = NULL;
|
|
||||||
pbuf_free(b);
|
pbuf_free(b);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -814,6 +820,14 @@ bool AsyncClient::canSend(){
|
|||||||
return space() > 0;
|
return space() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AsyncClient::ackPacket(struct pbuf * pb){
|
||||||
|
if(!pb){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_tcp_recved(_pcb, pb->len);
|
||||||
|
pbuf_free(pb);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Callback Setters
|
// Callback Setters
|
||||||
|
|
||||||
@@ -842,6 +856,11 @@ void AsyncClient::onData(AcDataHandler cb, void* arg){
|
|||||||
_recv_cb_arg = 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){
|
void AsyncClient::onTimeout(AcTimeoutHandler cb, void* arg){
|
||||||
_timeout_cb = cb;
|
_timeout_cb = cb;
|
||||||
_timeout_cb_arg = arg;
|
_timeout_cb_arg = arg;
|
||||||
|
@@ -25,7 +25,8 @@
|
|||||||
#include "IPAddress.h"
|
#include "IPAddress.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "freertos/semphr.h"
|
#include "freertos/semphr.h"
|
||||||
|
#include "lwip/pbuf.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
class AsyncClient;
|
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*, size_t len, uint32_t time)> AcAckHandler;
|
||||||
typedef std::function<void(void*, AsyncClient*, int8_t error)> AcErrorHandler;
|
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*, 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;
|
typedef std::function<void(void*, AsyncClient*, uint32_t time)> AcTimeoutHandler;
|
||||||
|
|
||||||
struct tcp_pcb;
|
struct tcp_pcb;
|
||||||
struct pbuf;
|
|
||||||
struct _ip_addr;
|
struct _ip_addr;
|
||||||
|
|
||||||
class AsyncClient {
|
class AsyncClient {
|
||||||
@@ -58,6 +59,8 @@ class AsyncClient {
|
|||||||
void* _error_cb_arg;
|
void* _error_cb_arg;
|
||||||
AcDataHandler _recv_cb;
|
AcDataHandler _recv_cb;
|
||||||
void* _recv_cb_arg;
|
void* _recv_cb_arg;
|
||||||
|
AcPacketHandler _pb_cb;
|
||||||
|
void* _pb_cb_arg;
|
||||||
AcTimeoutHandler _timeout_cb;
|
AcTimeoutHandler _timeout_cb;
|
||||||
void* _timeout_cb_arg;
|
void* _timeout_cb_arg;
|
||||||
AcConnectHandler _poll_cb;
|
AcConnectHandler _poll_cb;
|
||||||
@@ -141,10 +144,13 @@ class AsyncClient {
|
|||||||
void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected
|
void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected
|
||||||
void onAck(AcAckHandler cb, void* arg = 0); //ack received
|
void onAck(AcAckHandler cb, void* arg = 0); //ack received
|
||||||
void onError(AcErrorHandler cb, void* arg = 0); //unsuccessful connect or error
|
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 onTimeout(AcTimeoutHandler cb, void* arg = 0); //ack timeout
|
||||||
void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected
|
void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected
|
||||||
|
|
||||||
|
void ackPacket(struct pbuf * pb);
|
||||||
|
|
||||||
const char * errorToString(int8_t error);
|
const char * errorToString(int8_t error);
|
||||||
const char * stateToString();
|
const char * stateToString();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user