11 #include "../src/meshlink.h"
14 volatile bool b_responded = false;
15 volatile bool b_closed = false;
16 volatile bool a_nonzero_poll_cb = false;
18 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
19 static struct timeval tv0;
23 gettimeofday(&tv0, NULL);
26 gettimeofday(&tv, NULL);
27 fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
30 fprintf(stderr, "(%s) ", mesh->name);
33 fprintf(stderr, "[%d] %s\n", level, text);
36 void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
40 if(len == 5 && !memcmp(data, "Hello", 5)) {
47 void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
48 // Send one message back, then close the channel.
50 meshlink_channel_send(mesh, channel, data, len);
53 meshlink_channel_close(mesh, channel);
56 bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
66 bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
69 meshlink_set_channel_accept_cb(mesh, NULL);
70 meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
73 b_receive_cb(mesh, channel, data, len);
79 void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
82 meshlink_set_channel_poll_cb(mesh, channel, NULL);
83 set_sync_flag(channel->priv, true);
86 void poll_cb2(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
91 a_nonzero_poll_cb = true;
96 meshlink_handle_t *a, *b;
97 open_meshlink_pair(&a, &b, "channels-cornercases");
98 //meshlink_set_log_cb(a, MESHLINK_DEBUG, log_cb);
99 //meshlink_set_log_cb(b, MESHLINK_DEBUG, log_cb);
101 // Set the callbacks.
103 meshlink_set_channel_accept_cb(a, reject_cb);
104 meshlink_set_channel_accept_cb(b, accept_cb);
106 // Open a channel from a to b before starting the mesh.
108 meshlink_node_t *nb = meshlink_get_node(a, "b");
111 struct sync_flag channel_opened = {.flag = false};
113 meshlink_channel_t *channel = meshlink_channel_open(a, nb, 7, a_receive_cb, NULL, 0);
116 channel->priv = &channel_opened;
117 meshlink_set_channel_poll_cb(a, channel, poll_cb);
119 // Start MeshLink and wait for the channel to become connected.
120 start_meshlink_pair(a, b);
122 assert(wait_sync_flag(&channel_opened, 20));
124 // Re-initialize everything
125 close_meshlink_pair(a, b, "channels-cornercases");
128 channel_opened.flag = false;
129 open_meshlink_pair(&a, &b, "channels-cornercases");
131 meshlink_set_channel_accept_cb(a, reject_cb);
132 meshlink_set_channel_accept_cb(b, accept_cb);
134 start_meshlink_pair(a, b);
136 // Create a channel to b
137 nb = meshlink_get_node(a, "b");
140 channel = meshlink_channel_open(a, nb, 7, a_receive_cb, NULL, 0);
142 channel->priv = &channel_opened;
143 meshlink_set_channel_poll_cb(a, channel, poll_cb);
145 assert(wait_sync_flag(&channel_opened, 20));
147 assert(!b_responded);
150 // Send a message to b
152 meshlink_channel_send(a, channel, "Hello", 5);
159 // Try to create a second channel
161 meshlink_channel_t *channel2 = meshlink_channel_open(a, nb, 7, a_receive_cb, NULL, 0);
163 meshlink_set_channel_poll_cb(a, channel2, poll_cb2);
167 assert(!a_nonzero_poll_cb);