13 #include "../src/meshlink.h"
16 static bool received_large;
17 static bool received_zero;
18 static size_t received;
19 static struct sync_flag accept_flag;
20 static struct sync_flag close_flag;
22 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
26 meshlink_channel_close(mesh, channel);
27 set_sync_flag(&close_flag, true);
32 memcpy(&checklen, data, sizeof(checklen));
33 assert(len == checklen);
37 received_large = true;
47 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
51 assert(meshlink_channel_get_flags(mesh, channel) == (MESHLINK_CHANNEL_UDP | MESHLINK_CHANNEL_FRAMED));
52 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
53 set_sync_flag(&accept_flag, true);
59 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
61 // Open two meshlink instances
63 meshlink_handle_t *mesh_a, *mesh_b;
64 open_meshlink_pair(&mesh_a, &mesh_b, "channels_udp_framed");
65 start_meshlink_pair(mesh_a, mesh_b);
67 // Create a channel from a to b
69 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
71 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
74 meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_a, b, 1, NULL, NULL, 0, MESHLINK_CHANNEL_UDP | MESHLINK_CHANNEL_FRAMED);
77 // Check that we cannot send more than 65535 bytes without errors
79 char data[65535] = "";
80 assert(meshlink_channel_send(mesh_a, channel, data, 65536) == -1);
82 // Check that we can send 65535 bytes
84 uint16_t framelen = 65535;
85 memcpy(data, &framelen, sizeof(framelen));
86 assert(meshlink_channel_send(mesh_a, channel, data, framelen) == framelen);
88 // Check that we can send zero bytes
90 assert(meshlink_channel_send(mesh_a, channel, data, 0) == 0);
92 // Stream packets from a to b for 5 seconds at 40 Mbps (~1 kB * 500 Hz)
94 size_t total_len = framelen;
96 for(int j = 0; j < 2500; j++) {
97 framelen = rand() % 2048;
98 memcpy(data, &framelen, sizeof(framelen));
99 assert(meshlink_channel_send(mesh_a, channel, data, framelen) == framelen);
101 total_len += framelen;
103 long msec = j % 100 ? 2 : 100;
104 const struct timespec req = {0, msec * 1000000};
105 clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
108 // Closes the channel and wait for the other end to closes it as well
110 meshlink_channel_close(mesh_a, channel);
111 assert(wait_sync_flag(&close_flag, 10));
113 // Check that the clients have received (most of) the data
115 assert(received <= total_len);
116 assert(received >= total_len / 2);
117 assert(received_zero);
119 close_meshlink_pair(mesh_a, mesh_b);