]> git.meshlink.io Git - meshlink/blob - test/channels-udp-framed.c
9fb269719f7a81ef3bf5d453abf0fbc00546601e
[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 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;
21
22 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
23         (void)mesh;
24
25         if(!data && !len) {
26                 meshlink_channel_close(mesh, channel);
27                 set_sync_flag(&close_flag, true);
28         }
29
30         if(len >= 2) {
31                 uint16_t checklen;
32                 memcpy(&checklen, data, sizeof(checklen));
33                 assert(len == checklen);
34         }
35
36         if(len == 65535) {
37                 received_large = true;
38         }
39
40         if(len == 0) {
41                 received_zero = true;
42         }
43
44         received += len;
45 }
46
47 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
48         assert(port == 1);
49         assert(!data);
50         assert(!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);
54
55         return true;
56 }
57
58 int main(void) {
59         meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
60
61         // Open two meshlink instances
62
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);
66
67         // Create a channel from a to b
68
69         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
70
71         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
72         assert(b);
73
74         meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_a, b, 1, NULL, NULL, 0, MESHLINK_CHANNEL_UDP | MESHLINK_CHANNEL_FRAMED);
75         assert(channel);
76
77         // Check that we cannot send more than 65535 bytes without errors
78
79         char data[65535] = "";
80         assert(meshlink_channel_send(mesh_a, channel, data, 65536) == -1);
81
82         // Check that we can send 65535 bytes
83
84         uint16_t framelen = 65535;
85         memcpy(data, &framelen, sizeof(framelen));
86         assert(meshlink_channel_send(mesh_a, channel, data, framelen) == framelen);
87
88         // Check that we can send zero bytes
89
90         assert(meshlink_channel_send(mesh_a, channel, data, 0) == 0);
91
92         // Stream packets from a to b for 5 seconds at 40 Mbps (~1 kB * 500 Hz)
93
94         size_t total_len = framelen;
95
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);
100
101                 total_len += framelen;
102
103                 long msec = j % 100 ? 2 : 100;
104                 const struct timespec req = {0, msec * 1000000};
105                 clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
106         }
107
108         // Closes the channel and wait for the other end to closes it as well
109
110         meshlink_channel_close(mesh_a, channel);
111         assert(wait_sync_flag(&close_flag, 10));
112
113         // Check that the clients have received (most of) the data
114
115         assert(received <= total_len);
116         assert(received >= total_len / 2);
117         assert(received_zero);
118
119         close_meshlink_pair(mesh_a, mesh_b);
120
121         return 0;
122 }