13 #include "../src/meshlink-tiny.h"
16 #define SMALL_SIZE 512
17 #define SMALL_COUNT 2500
18 #define LARGE_SIZE 131072
20 static struct sync_flag accept_flag;
23 meshlink_handle_t *mesh;
24 meshlink_channel_t *channel;
26 bool got_large_packet;
27 struct sync_flag close_flag;
30 static void client_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
32 struct client *client = mesh->priv;
35 set_sync_flag(&client->close_flag, true);
36 meshlink_channel_close(mesh, channel);
40 // We expect always the same amount of data from the server.
41 assert(len == 512 || len == LARGE_SIZE);
42 client->received += len;
44 if(len == LARGE_SIZE) {
45 client->got_large_packet = true;
49 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
51 struct client *client = mesh->priv;
53 if(reachable && !strcmp(node->name, "server")) {
54 assert(!client->channel);
55 client->channel = meshlink_channel_open_ex(mesh, node, 1, client_receive_cb, NULL, 0, MESHLINK_CHANNEL_UDP);
56 assert(client->channel);
60 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
65 assert(meshlink_channel_get_flags(mesh, channel) == MESHLINK_CHANNEL_UDP);
68 meshlink_channel_t **c = mesh->priv;
70 for(int i = 0; i < 3; i++) {
75 set_sync_flag(&accept_flag, true);
86 init_sync_flag(&accept_flag);
88 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
90 // Open four new meshlink instance, the server and three peers.
92 const char *names[3] = {"foo", "bar", "baz"};
93 struct client clients[3];
94 meshlink_channel_t *channels[3] = {NULL, NULL, NULL};
95 memset(clients, 0, sizeof(clients));
97 assert(meshlink_destroy("channels_udp_conf.0"));
98 meshlink_handle_t *server = meshlink_open("channels_udp_conf.0", "server", "channels-udp", DEV_CLASS_BACKBONE);
100 server->priv = channels;
101 meshlink_set_channel_accept_cb(server, accept_cb);
102 assert(meshlink_start(server));
104 for(int i = 0; i < 3; i++) {
106 snprintf(dir, sizeof(dir), "channels_udp_conf.%d", i + 1);
107 assert(meshlink_destroy(dir));
108 init_sync_flag(&clients[i].close_flag);
109 clients[i].mesh = meshlink_open(dir, names[i], "channels-udp", DEV_CLASS_STATIONARY);
110 assert(clients[i].mesh);
111 clients[i].mesh->priv = &clients[i];
112 link_meshlink_pair(server, clients[i].mesh);
113 meshlink_set_node_status_cb(clients[i].mesh, status_cb);
114 assert(meshlink_start(clients[i].mesh));
117 // Wait for all three channels to connect
119 assert(wait_sync_flag(&accept_flag, 10));
121 for(int i = 0; i < 3; i++) {
123 assert(clients[i].channel);
126 // Check that we can send up to 65535 bytes without errors
128 char large_data[LARGE_SIZE] = "";
130 for(int i = 0; i < 3; i++) {
131 assert(meshlink_channel_send(server, channels[i], large_data, sizeof(large_data)) == sizeof(large_data));
134 // Assert that packets larger than 16 MiB are not allowed
136 assert(meshlink_channel_send(server, channels[0], large_data, 16777216) == -1);
138 // Stream packets from server to clients for 5 seconds at 40 Mbps (1 kB * 500 Hz)
140 char data[SMALL_SIZE];
141 memset(data, 'U', sizeof(data));
143 for(int j = 0; j < SMALL_COUNT; j++) {
144 const struct timespec req = {0, 2000000};
145 nanosleep(&req, NULL);
147 for(int i = 0; i < 3; i++) {
148 assert(meshlink_channel_send(server, channels[i], data, sizeof(data)) == sizeof(data));
152 // Shutdown the write side of the server's channels
154 for(int i = 0; i < 3; i++) {
155 meshlink_channel_shutdown(server, channels[i], SHUT_WR);
158 // Wait for the clients to finish reading all the data
160 for(int i = 0; i < 3; i++) {
161 assert(wait_sync_flag(&clients[i].close_flag, 10));
164 // Check that the clients have received (most of) the data
166 for(int i = 0; i < 3; i++) {
167 fprintf(stderr, "%s received %zu\n", clients[i].mesh->name, clients[i].received);
170 for(int i = 0; i < 3; i++) {
171 size_t max_received = SMALL_SIZE * SMALL_COUNT + LARGE_SIZE;
172 assert(clients[i].received >= max_received / 2);
173 assert(clients[i].received <= max_received);
174 assert(clients[i].got_large_packet);
179 for(int i = 0; i < 3; i++) {
180 meshlink_close(clients[i].mesh);
183 meshlink_close(server);