From 72a9c7ede43a275eba374a3de1ee880b93c2f328 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 13 Oct 2019 14:05:07 +0200 Subject: [PATCH] Add meshlink_set_node_channel_timeout(). This function allows setting the user timeout for UTCP connections. --- src/meshlink++.h | 12 ++++++++++++ src/meshlink.c | 19 +++++++++++++++++++ src/meshlink.h | 12 ++++++++++++ src/meshlink.sym | 1 + 4 files changed, 44 insertions(+) diff --git a/src/meshlink++.h b/src/meshlink++.h index e39ef81c..f5921363 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -708,6 +708,18 @@ public: meshlink_set_channel_rcvbuf(handle, channel, size); } + /// Set the connection timeout used for channels to the given node. + /** This sets the timeout after which unresponsive channels will be reported as closed. + * The timeout is set for all current and future channels to the given node. + * + * @param channel A handle for the channel. + * @param timeout The timeout in seconds after which unresponsive channels will be reported as closed. + * The default is 60 seconds. + */ + void set_node_channel_timeout(node *node, int timeout) { + meshlink_set_node_channel_timeout(handle, node, timeout); + } + /// Open a reliable stream channel to another node. /** This function is called whenever a remote node wants to open a channel to the local node. * The application then has to decide whether to accept or reject this channel. diff --git a/src/meshlink.c b/src/meshlink.c index 7a01bd0f..2db62ea1 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -3446,6 +3446,25 @@ size_t meshlink_channel_get_recvq(meshlink_handle_t *mesh, meshlink_channel_t *c return utcp_get_recvq(channel->c); } +void meshlink_set_node_channel_timeout(meshlink_handle_t *mesh, meshlink_node_t *node, int timeout) { + if(!mesh || !node) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + node_t *n = (node_t *)node; + + pthread_mutex_lock(&mesh->mesh_mutex); + + if(!n->utcp) { + n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); + } + + utcp_set_user_timeout(n->utcp, timeout); + + pthread_mutex_unlock(&mesh->mesh_mutex); +} + void update_node_status(meshlink_handle_t *mesh, node_t *n) { if(n->status.reachable && mesh->channel_accept_cb && !n->utcp) { n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); diff --git a/src/meshlink.h b/src/meshlink.h index 00aa19b2..11395f04 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -1380,6 +1380,18 @@ extern size_t meshlink_channel_get_sendq(struct meshlink_handle *mesh, struct me */ extern size_t meshlink_channel_get_recvq(struct meshlink_handle *mesh, struct meshlink_channel *channel); +/// Set the connection timeout used for channels to the given node. +/** This sets the timeout after which unresponsive channels will be reported as closed. + * The timeout is set for all current and future channels to the given node. + * + * \memberof meshlink_node + * @param mesh A handle which represents an instance of MeshLink. + * @param channel A handle for the channel. + * @param timeout The timeout in seconds after which unresponsive channels will be reported as closed. + * The default is 60 seconds. + */ +extern void meshlink_set_node_channel_timeout(struct meshlink_handle *mesh, struct meshlink_node *node, int timeout); + /// Hint that a hostname may be found at an address /** This function indicates to meshlink that the given hostname is likely found * at the given IP address and port. diff --git a/src/meshlink.sym b/src/meshlink.sym index d94947c0..cf38e94a 100644 --- a/src/meshlink.sym +++ b/src/meshlink.sym @@ -66,6 +66,7 @@ meshlink_set_dev_class_timeouts meshlink_set_error_cb meshlink_set_invitation_timeout meshlink_set_log_cb +meshlink_set_node_channel_timeout meshlink_set_node_duplicate_cb meshlink_set_node_pmtu_cb meshlink_set_node_status_cb -- 2.39.2