13 #include "../src/meshlink.h"
15 static struct sync_flag b_responded;
17 static void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
21 printf("a_receive_cb %zu: ", len);
22 fwrite(data, 1, len, stdout);
25 if(len == 5 && !memcmp(data, "Hello", 5)) {
26 set_sync_flag(&b_responded, true);
30 static void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
31 printf("b_receive_cb %zu: ", len);
32 fwrite(data, 1, len, stdout);
34 // Echo the data back.
35 assert(meshlink_channel_send(mesh, channel, data, len) == (ssize_t)len);
38 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
39 printf("accept_cb: (from %s on port %u) ", channel->node->name, (unsigned int)port);
42 fwrite(data, 1, len, stdout);
51 meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
54 b_receive_cb(mesh, channel, data, len);
60 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
63 meshlink_set_channel_poll_cb(mesh, channel, NULL);
65 assert(meshlink_channel_send(mesh, channel, "Hello", 5) == 5);
69 init_sync_flag(&b_responded);
71 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
73 // Open two new meshlink instance.
75 meshlink_handle_t *mesh_a, *mesh_b;
76 open_meshlink_pair(&mesh_a, &mesh_b, "channels");
80 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
82 // Start both instances
84 start_meshlink_pair(mesh_a, mesh_b);
86 // Open a channel from a to b.
88 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
91 meshlink_channel_t *channel = meshlink_channel_open(mesh_a, b, 7, a_receive_cb, NULL, 0);
94 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
95 assert(wait_sync_flag(&b_responded, 20));
97 meshlink_channel_close(mesh_a, channel);
101 close_meshlink_pair(mesh_a, mesh_b);