From: Guus Sliepen Date: Sun, 14 Sep 2014 15:49:09 +0000 (+0200) Subject: Ensure receive_cb and utcp structures have been properly initialized after setting... X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=ac24ceb9a5976628dd4ac31b2b502f1caac2b2ec;p=meshlink Ensure receive_cb and utcp structures have been properly initialized after setting channel_accept_cb. This allows the channels example to initiate a chat from one side only. --- diff --git a/src/graph.c b/src/graph.c index 2453d6f1..b18d0610 100644 --- a/src/graph.c +++ b/src/graph.c @@ -223,8 +223,7 @@ static void check_reachability(meshlink_handle_t *mesh) { timeout_del(&mesh->loop, &n->mtutimeout); - if(mesh->node_status_cb) - mesh->node_status_cb(mesh, (meshlink_node_t *)n, n->status.reachable); + update_node_status(mesh, n); if(!n->status.reachable) { update_node_udp(mesh, n, NULL); diff --git a/src/meshlink.c b/src/meshlink.c index 9a462c27..bd74b1d8 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1947,10 +1947,6 @@ static ssize_t channel_send(struct utcp *utcp, const void *data, size_t len) { return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? len : -1; } -void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) { - mesh->channel_accept_cb = cb; -} - void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb) { channel->receive_cb = cb; } @@ -1965,6 +1961,19 @@ static void channel_receive(meshlink_handle_t *mesh, meshlink_node_t *source, co utcp_recv(n->utcp, data, len); } +void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) { + pthread_mutex_lock(&mesh->mesh_mutex); + mesh->channel_accept_cb = cb; + mesh->receive_cb = channel_receive; + for splay_each(node_t, n, mesh->nodes) { + if(!n->utcp && n != mesh->self) { + logger(mesh, MESHLINK_WARNING, "utcp_init on node %s", n->name); + n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); + } + } + pthread_mutex_unlock(&mesh->mesh_mutex); +} + void meshlink_channel_init(meshlink_handle_t *mesh) { } @@ -2008,6 +2017,13 @@ ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *chann return utcp_send(channel->c, data, len); } +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); + if(mesh->node_status_cb) + mesh->node_status_cb(mesh, (meshlink_node_t *)n, n->status.reachable); +} + static void __attribute__((constructor)) meshlink_init(void) { crypto_init(); } @@ -2016,7 +2032,6 @@ static void __attribute__((destructor)) meshlink_exit(void) { crypto_exit(); } - /// Device class traits dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX +1] = { { .min_connects = 3, .max_connects = 10000, .edge_weight = 1 }, // DEV_CLASS_BACKBONE diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 6c7ac7bd..782cc76e 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -161,6 +161,7 @@ typedef struct meshlink_packethdr { } __attribute__ ((__packed__)) meshlink_packethdr_t; extern void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh); +extern void update_node_status(meshlink_handle_t *mesh, struct node_t *n); extern meshlink_log_level_t global_log_level; extern meshlink_log_cb_t global_log_cb;