12 #include "../src/meshlink.h"
15 static bool listen_cb(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port) {
22 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
32 static struct sync_flag poll_flag;
33 static size_t poll_len;
35 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
36 meshlink_set_channel_poll_cb(mesh, channel, NULL);
38 set_sync_flag(&poll_flag, true);
41 static struct sync_flag receive_flag;
42 static size_t receive_len;
44 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
50 set_sync_flag(&receive_flag, true);
54 init_sync_flag(&poll_flag);
55 init_sync_flag(&receive_flag);
57 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
59 // Open two meshlink instances.
61 meshlink_handle_t *mesh_a, *mesh_b;
62 open_meshlink_pair(&mesh_a, &mesh_b, "channels_failure");
66 meshlink_set_channel_listen_cb(mesh_b, listen_cb);
67 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
69 // Open a channel from a to b
71 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
74 meshlink_channel_t *channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
77 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
79 // Start both instances
81 start_meshlink_pair(mesh_a, mesh_b);
83 // Wait for the channel to be established
85 assert(wait_sync_flag(&poll_flag, 10));
86 assert(poll_len != 0);
90 // Set a very small timeout for channels to b.
92 meshlink_set_node_channel_timeout(mesh_a, b, 1);
94 // Stop mesh_b. We should get a notification that the channel has closed after a while.
96 meshlink_stop(mesh_b);
98 assert(wait_sync_flag(&receive_flag, 5));
99 assert(receive_len == 0);
101 meshlink_channel_close(mesh_a, channel);
103 // Try setting up a new channel while b is still down.
105 reset_sync_flag(&poll_flag);
106 reset_sync_flag(&receive_flag);
108 channel = meshlink_channel_open(mesh_a, b, 7, NULL, NULL, 0);
111 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
113 assert(wait_sync_flag(&poll_flag, 5));
114 assert(poll_len == 0);
116 meshlink_channel_close(mesh_a, channel);
118 // Restart b and create a new channel to the wrong port
120 reset_sync_flag(&poll_flag);
121 reset_sync_flag(&receive_flag);
123 meshlink_set_node_channel_timeout(mesh_a, b, 60);
125 assert(meshlink_start(mesh_b));
127 channel = meshlink_channel_open(mesh_a, b, 42, receive_cb, NULL, 0);
128 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
130 assert(wait_sync_flag(&poll_flag, 10));
131 assert(poll_len == 0);
132 meshlink_channel_close(mesh_a, channel);
134 // Create a channel that will be accepted
136 reset_sync_flag(&poll_flag);
138 channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
139 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
141 assert(wait_sync_flag(&poll_flag, 10));
142 assert(poll_len != 0);
144 // Close and reopen b, we should get a fast notification that the channel has been closed.
146 meshlink_close(mesh_b);
147 mesh_b = meshlink_open("channels_failure_conf.2", "b", "channels_failure", DEV_CLASS_BACKBONE);
149 assert(meshlink_start(mesh_b));
151 assert(wait_sync_flag(&receive_flag, 10));
152 assert(receive_len == 0);
156 close_meshlink_pair(mesh_a, mesh_b);