13 #include "../src/meshlink.h"
16 static size_t received;
17 static struct sync_flag accept_flag;
18 static struct sync_flag poll_flag;
19 static struct sync_flag small_flag;
20 static struct sync_flag large_flag;
21 static struct sync_flag close_flag;
23 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
27 meshlink_channel_close(mesh, channel);
28 set_sync_flag(&close_flag, true);
33 memcpy(&checklen, data, sizeof(checklen));
34 assert(len == checklen);
38 set_sync_flag(&large_flag, true);
42 set_sync_flag(&small_flag, true);
48 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
52 assert(meshlink_channel_get_flags(mesh, channel) == (MESHLINK_CHANNEL_UDP | MESHLINK_CHANNEL_FRAMED));
53 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
54 set_sync_flag(&accept_flag, true);
59 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
60 meshlink_set_channel_poll_cb(mesh, channel, NULL);
61 set_sync_flag(&poll_flag, len > 0);
65 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
67 // Open two meshlink instances
69 meshlink_handle_t *mesh_a, *mesh_b;
70 open_meshlink_pair(&mesh_a, &mesh_b, "channels_udp_framed");
71 start_meshlink_pair(mesh_a, mesh_b);
73 // Create a channel from a to b
75 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
77 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
80 meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_a, b, 1, NULL, NULL, 0, MESHLINK_CHANNEL_UDP | MESHLINK_CHANNEL_FRAMED);
83 // Wait for the channel to be fully established
85 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
86 assert(wait_sync_flag(&poll_flag, 10));
88 // Check that we can send zero bytes, and that it is received
90 assert(meshlink_channel_send(mesh_a, channel, "", 0) == 0);
91 assert(wait_sync_flag(&small_flag, 1));
93 // Check that we cannot send more than 65535 bytes without errors
95 char data[65535] = "";
96 assert(meshlink_channel_send(mesh_a, channel, data, 65536) == -1);
98 // Check that we can send 65535 bytes
100 uint16_t framelen = 65535;
101 memcpy(data, &framelen, sizeof(framelen));
102 assert(meshlink_channel_send(mesh_a, channel, data, framelen) == framelen);
103 assert(wait_sync_flag(&large_flag, 1));
105 // Stream packets from a to b for 5 seconds at 40 Mbps (~1 kB * 500 Hz)
107 size_t total_len = framelen;
109 for(int j = 0; j < 2500; j++) {
110 framelen = rand() % 2048;
111 memcpy(data, &framelen, sizeof(framelen));
112 assert(meshlink_channel_send(mesh_a, channel, data, framelen) == framelen);
114 total_len += framelen;
116 long msec = j % 100 ? 2 : 100;
117 const struct timespec req = {0, msec * 1000000};
118 clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
121 // Closes the channel and wait for the other end to closes it as well
123 meshlink_channel_close(mesh_a, channel);
124 assert(wait_sync_flag(&close_flag, 10));
126 // Check that the clients have received (most of) the data
128 assert(received <= total_len);
129 assert(received >= total_len / 4 * 3);
131 close_meshlink_pair(mesh_a, mesh_b);