]> git.meshlink.io Git - meshlink/blob - test/channels-udp.c
Allow meshlink_open() to be called with a NULL name.
[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 == 2000);
59         client->received += len;
60
61         if(len == 2000) {
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         // Send a packet larger than the MTU
144
145         char large_data[2000] = "";
146
147         for(int i = 0; i < 3; i++) {
148                 assert(meshlink_channel_send(server, channels[i], large_data, sizeof(large_data)) == sizeof(large_data));
149         }
150
151         // Assert that packets larger than 64 kiB are not allowed
152
153         assert(meshlink_channel_send(server, channels[0], large_data, 65537) == -1);
154
155         // Stream packets from server to clients for 5 seconds at 40 Mbps (1 kB * 500 Hz)
156
157         char data[512];
158         memset(data, 'U', sizeof(data));
159
160         for(int j = 0; j < 2500; j++) {
161                 for(int i = 0; i < 3; i++) {
162                         assert(meshlink_channel_send(server, channels[i], data, sizeof(data)) == sizeof(data));
163                 }
164
165                 const struct timespec req = {0, 2000000};
166                 clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
167         }
168
169         // Let the clients close the channels
170
171         for(int i = 0; i < 3; i++) {
172                 meshlink_channel_close(clients[i].mesh, clients[i].channel);
173                 meshlink_set_node_status_cb(clients[i].mesh, NULL);
174         }
175
176         assert(wait_sync_flag(&close_flag, 10));
177
178         // Check that the clients have received (most of) the data
179
180         for(int i = 0; i < 3; i++) {
181                 fprintf(stderr, "%s received %zu\n", clients[i].mesh->name, clients[i].received);
182         }
183
184         for(int i = 0; i < 3; i++) {
185                 assert(clients[i].received >= 1000000);
186                 assert(clients[i].received <= 1282000);
187                 assert(clients[i].got_large_packet);
188         }
189
190         // Clean up.
191
192         for(int i = 0; i < 3; i++) {
193                 meshlink_close(clients[i].mesh);
194         }
195
196         meshlink_close(server);
197
198         return 0;
199 }