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 reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
48 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
49 printf("accept_cb: (from %s on port %u) ", channel->node->name, (unsigned int)port);
52 fwrite(data, 1, len, stdout);
61 meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
64 b_receive_cb(mesh, channel, data, len);
70 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
73 meshlink_set_channel_poll_cb(mesh, channel, NULL);
75 assert(meshlink_channel_send(mesh, channel, "Hello", 5) == 5);
79 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
81 // Open two new meshlink instance.
83 meshlink_handle_t *mesh_a, *mesh_b;
84 open_meshlink_pair(&mesh_a, &mesh_b, "channels");
88 meshlink_set_channel_accept_cb(mesh_a, reject_cb);
89 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
91 // Start both instances
93 start_meshlink_pair(mesh_a, mesh_b);
95 // Open a channel from a to b.
97 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
100 meshlink_channel_t *channel = meshlink_channel_open(mesh_a, b, 7, a_receive_cb, NULL, 0);
103 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
104 assert(wait_sync_flag(&b_responded, 20));
106 meshlink_channel_close(mesh_a, channel);
110 close_meshlink_pair(mesh_a, mesh_b);