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};
112 pthread_mutex_lock(&channel_opened.mutex);
114 meshlink_channel_t *channel = meshlink_channel_open(a, nb, 7, a_receive_cb, NULL, 0);
117 channel->priv = &channel_opened;
118 meshlink_set_channel_poll_cb(a, channel, poll_cb);
120 // Start MeshLink and wait for the channel to become connected.
121 start_meshlink_pair(a, b);
123 assert(wait_sync_flag(&channel_opened, 20));
125 // Re-initialize everything
126 close_meshlink_pair(a, b, "channels-cornercases");
129 channel_opened.flag = false;
130 open_meshlink_pair(&a, &b, "channels-cornercases");
132 meshlink_set_channel_accept_cb(a, reject_cb);
133 meshlink_set_channel_accept_cb(b, accept_cb);
135 start_meshlink_pair(a, b);
137 // Create a channel to b
138 nb = meshlink_get_node(a, "b");
141 channel = meshlink_channel_open(a, nb, 7, a_receive_cb, NULL, 0);
143 channel->priv = &channel_opened;
144 meshlink_set_channel_poll_cb(a, channel, poll_cb);
146 assert(wait_sync_flag(&channel_opened, 20));
148 assert(!b_responded);
151 // Send a message to b
153 meshlink_channel_send(a, channel, "Hello", 5);
160 // Try to create a second channel
162 meshlink_channel_t *channel2 = meshlink_channel_open(a, nb, 7, a_receive_cb, NULL, 0);
164 meshlink_set_channel_poll_cb(a, channel2, poll_cb2);
168 assert(!a_nonzero_poll_cb);