]> git.meshlink.io Git - meshlink/blob - test/channels-udp-framed.c
6f3c3cc3377c5be19d6ad45fe3dac383c9c24bb7
[meshlink] / test / channels-udp-framed.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/time.h>
11 #include <time.h>
12
13 #include "../src/meshlink.h"
14 #include "utils.h"
15
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;
22
23 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
24         (void)mesh;
25
26         if(!data && !len) {
27                 meshlink_channel_close(mesh, channel);
28                 set_sync_flag(&close_flag, true);
29         }
30
31         if(len >= 2) {
32                 uint16_t checklen;
33                 memcpy(&checklen, data, sizeof(checklen));
34                 assert(len == checklen);
35         }
36
37         if(len == 65535) {
38                 set_sync_flag(&large_flag, true);
39         }
40
41         if(len == 0) {
42                 set_sync_flag(&small_flag, true);
43         }
44
45         received += len;
46 }
47
48 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
49         assert(port == 1);
50         assert(!data);
51         assert(!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);
55
56         return true;
57 }
58
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);
62 }
63
64 int main(void) {
65         meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
66
67         // Open two meshlink instances
68
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);
72
73         // Create a channel from a to b
74
75         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
76
77         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
78         assert(b);
79
80         meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_a, b, 1, NULL, NULL, 0, MESHLINK_CHANNEL_UDP | MESHLINK_CHANNEL_FRAMED);
81         assert(channel);
82
83         // Wait for the channel to be fully established
84
85         meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
86         assert(wait_sync_flag(&poll_flag, 10));
87
88         // Check that we can send zero bytes, and that it is received
89
90         assert(meshlink_channel_send(mesh_a, channel, "", 0) == 0);
91         assert(wait_sync_flag(&small_flag, 1));
92
93         // Check that we cannot send more than 65535 bytes without errors
94
95         char data[65535] = "";
96         assert(meshlink_channel_send(mesh_a, channel, data, 65536) == -1);
97
98         // Check that we can send 65535 bytes
99
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));
104
105         // Stream packets from a to b for 5 seconds at 40 Mbps (~1 kB * 500 Hz)
106
107         size_t total_len = framelen;
108
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);
113
114                 total_len += framelen;
115
116                 long msec = j % 100 ? 2 : 100;
117                 const struct timespec req = {0, msec * 1000000};
118                 clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
119         }
120
121         // Closes the channel and wait for the other end to closes it as well
122
123         meshlink_channel_close(mesh_a, channel);
124         assert(wait_sync_flag(&close_flag, 10));
125
126         // Check that the clients have received (most of) the data
127
128         assert(received <= total_len);
129         assert(received >= total_len / 4 * 3);
130
131         close_meshlink_pair(mesh_a, mesh_b);
132
133         return 0;
134 }