From: Guus Sliepen Date: Wed, 1 Oct 2014 17:18:40 +0000 (+0200) Subject: Add a poll callback to UTCP connections. X-Git-Url: http://git.meshlink.io/?p=utcp;a=commitdiff_plain;h=5d4cf9d0eb85c7322298a2a0526132197e3bf633 Add a poll callback to UTCP connections. The callback is called whenever the send buffer of a connection is more than half empty when utcp_timeout() is called. An argument is passed to the callee informing him of the maximum number of bytes that will be accepted when calling utcp_send(). --- diff --git a/utcp.c b/utcp.c index 5962820..ca3c132 100644 --- a/utcp.c +++ b/utcp.c @@ -1022,6 +1022,9 @@ int utcp_timeout(struct utcp *utcp) { retransmit(c); } + if(c->poll && c->sndbufsize < c->maxsndbufsize / 2) + c->poll(c, c->maxsndbufsize - c->sndbufsize); + if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &next, <)) next = c->conn_timeout; @@ -1097,6 +1100,10 @@ size_t utcp_get_sndbuf(struct utcp_connection *c) { return c->maxsndbufsize; } +size_t utcp_get_sndbuf_free(struct utcp_connection *c) { + return c->maxsndbufsize - c->sndbufsize; +} + void utcp_set_sndbuf(struct utcp_connection *c, size_t size) { c->maxsndbufsize = size; if(c->maxsndbufsize != size) @@ -1122,3 +1129,11 @@ void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) { size_t utcp_get_outq(struct utcp_connection *c) { return seqdiff(c->snd.nxt, c->snd.una); } + +void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) { + c->recv = recv; +} + +void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) { + c->poll = poll; +} diff --git a/utcp.h b/utcp.h index 0bf8417..53a107f 100644 --- a/utcp.h +++ b/utcp.h @@ -44,6 +44,8 @@ typedef void (*utcp_accept_t)(struct utcp_connection *utcp_connection, uint16_t typedef ssize_t (*utcp_send_t)(struct utcp *utcp, const void *data, size_t len); typedef ssize_t (*utcp_recv_t)(struct utcp_connection *connection, const void *data, size_t len); +typedef void (*utcp_poll_t)(struct utcp_connection *connection, size_t len); + extern struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv); extern void utcp_exit(struct utcp *utcp); @@ -55,6 +57,8 @@ extern int utcp_close(struct utcp_connection *connection); extern int utcp_abort(struct utcp_connection *connection); extern int utcp_shutdown(struct utcp_connection *connection, int how); extern int utcp_timeout(struct utcp *utcp); +extern void utcp_set_recv_cb(struct utcp_connection *connection, utcp_recv_t recv); +extern void utcp_set_poll_cb(struct utcp_connection *connection, utcp_poll_t poll); // Global socket options @@ -69,6 +73,8 @@ extern void utcp_set_mtu(struct utcp *utcp, uint16_t mtu); extern size_t utcp_get_sndbuf(struct utcp_connection *connection); extern void utcp_set_sndbuf(struct utcp_connection *connection, size_t size); +extern size_t utcp_get_sndbuf_free(struct utcp_connection *connection); + extern bool utcp_get_nodelay(struct utcp_connection *connection); extern void utcp_set_nodelay(struct utcp_connection *connection, bool nodelay); diff --git a/utcp_priv.h b/utcp_priv.h index d8f6554..d73d664 100644 --- a/utcp_priv.h +++ b/utcp_priv.h @@ -80,6 +80,7 @@ struct utcp_connection { // Callbacks utcp_recv_t recv; + utcp_poll_t poll; // TCP State