]> git.meshlink.io Git - meshlink/blob - test/channels-udp.c
8071d467f6a5ca8d3bbf82858b59a4d6eecd5b8d
[meshlink] / test / channels-udp.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 struct sync_flag accept_flag;
17 static struct sync_flag close_flag;
18
19 struct client {
20         meshlink_handle_t *mesh;
21         meshlink_channel_t *channel;
22         size_t received;
23         bool got_large_packet;
24 };
25
26 static void server_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
27         (void)data;
28
29         // We expect no data from clients, apart from disconnections.
30         assert(len == 0);
31
32         meshlink_channel_t **c = mesh->priv;
33         int count = 0;
34
35         for(int i = 0; i < 3; i++) {
36                 if(c[i] == channel) {
37                         c[i] = NULL;
38                         meshlink_channel_close(mesh, channel);
39                 }
40
41                 if(c[i]) {
42                         count++;
43                 }
44         }
45
46         if(!count) {
47                 set_sync_flag(&close_flag, true);
48         }
49 }
50
51 static void client_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
52         (void)channel;
53         (void)data;
54
55         // We expect always the same amount of data from the server.
56         assert(mesh->priv);
57         struct client *client = mesh->priv;
58         assert(len == 512 || len == 65536);
59         client->received += len;
60
61         if(len == 65536) {
62                 client->got_large_packet = true;
63         }
64 }
65
66 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
67         assert(mesh->priv);
68         struct client *client = mesh->priv;
69
70         if(reachable && !strcmp(node->name, "server")) {
71                 assert(!client->channel);
72                 client->channel = meshlink_channel_open_ex(mesh, node, 1, client_receive_cb, NULL, 0, MESHLINK_CHANNEL_UDP);
73                 assert(client->channel);
74         }
75 }
76
77 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
78         (void)data;
79         (void)len;
80
81         assert(port == 1);
82         assert(meshlink_channel_get_flags(mesh, channel) == MESHLINK_CHANNEL_UDP);
83         meshlink_set_channel_receive_cb(mesh, channel, server_receive_cb);
84
85         assert(mesh->priv);
86         meshlink_channel_t **c = mesh->priv;
87
88         for(int i = 0; i < 3; i++) {
89                 if(c[i] == NULL) {
90                         c[i] = channel;
91
92                         if(i == 2) {
93                                 set_sync_flag(&accept_flag, true);
94                         }
95
96                         return true;
97                 }
98         }
99
100         return false;
101 }
102
103 int main() {
104         meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
105
106         // Open four new meshlink instance, the server and three peers.
107
108         const char *names[3] = {"foo", "bar", "baz"};
109         struct client clients[3];
110         meshlink_channel_t *channels[3] = {NULL, NULL, NULL};
111         memset(clients, 0, sizeof(clients));
112
113         assert(meshlink_destroy("channels_udp_conf.0"));
114         meshlink_handle_t *server = meshlink_open("channels_udp_conf.0", "server", "channels-udp", DEV_CLASS_BACKBONE);
115         assert(server);
116         meshlink_enable_discovery(server, false);
117         server->priv = channels;
118         meshlink_set_channel_accept_cb(server, accept_cb);
119         assert(meshlink_start(server));
120
121         for(int i = 0; i < 3; i++) {
122                 char dir[100];
123                 snprintf(dir, sizeof(dir), "channels_udp_conf.%d", i + 1);
124                 assert(meshlink_destroy(dir));
125                 clients[i].mesh = meshlink_open(dir, names[i], "channels-udp", DEV_CLASS_STATIONARY);
126                 assert(clients[i].mesh);
127                 clients[i].mesh->priv = &clients[i];
128                 meshlink_enable_discovery(clients[i].mesh, false);
129                 link_meshlink_pair(server, clients[i].mesh);
130                 meshlink_set_node_status_cb(clients[i].mesh, status_cb);
131                 assert(meshlink_start(clients[i].mesh));
132         }
133
134         // Wait for all three channels to connect
135
136         assert(wait_sync_flag(&accept_flag, 10));
137
138         for(int i = 0; i < 3; i++) {
139                 assert(channels[i]);
140                 assert(clients[i].channel);
141         }
142
143         // Check that we can send up to 65535 bytes without errors
144
145         char large_data[65536] = "";
146
147         for(int i = 0; i < 3; i++) {
148                 assert(meshlink_channel_send(server, channels[i], large_data, sizeof(large_data) + 1) == -1);
149                 assert(meshlink_channel_send(server, channels[i], large_data, sizeof(large_data)) == sizeof(large_data));
150         }
151
152         // Assert that packets larger than 64 kiB are not allowed
153
154         assert(meshlink_channel_send(server, channels[0], large_data, 65537) == -1);
155
156         // Stream packets from server to clients for 5 seconds at 40 Mbps (1 kB * 500 Hz)
157
158         char data[512];
159         memset(data, 'U', sizeof(data));
160
161         for(int j = 0; j < 2500; j++) {
162                 for(int i = 0; i < 3; i++) {
163                         assert(meshlink_channel_send(server, channels[i], data, sizeof(data)) == sizeof(data));
164                 }
165
166                 const struct timespec req = {0, 2000000};
167                 clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
168         }
169
170         // Let the clients close the channels
171
172         for(int i = 0; i < 3; i++) {
173                 meshlink_channel_close(clients[i].mesh, clients[i].channel);
174                 meshlink_set_node_status_cb(clients[i].mesh, NULL);
175         }
176
177         assert(wait_sync_flag(&close_flag, 10));
178
179         // Check that the clients have received (most of) the data
180
181         for(int i = 0; i < 3; i++) {
182                 fprintf(stderr, "%s received %zu\n", clients[i].mesh->name, clients[i].received);
183         }
184
185         for(int i = 0; i < 3; i++) {
186                 assert(clients[i].received >= 1000000);
187                 assert(clients[i].received <= 1345536);
188                 assert(clients[i].got_large_packet);
189         }
190
191         // Clean up.
192
193         for(int i = 0; i < 3; i++) {
194                 meshlink_close(clients[i].mesh);
195         }
196
197         meshlink_close(server);
198
199         return 0;
200 }