13 #include "../src/meshlink.h"
16 static struct sync_flag accept_flag;
17 static struct sync_flag close_flag;
20 meshlink_handle_t *mesh;
21 meshlink_channel_t *channel;
23 bool got_large_packet;
26 static void server_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
29 // We expect no data from clients, apart from disconnections.
32 meshlink_channel_t **c = mesh->priv;
35 for(int i = 0; i < 3; i++) {
38 meshlink_channel_close(mesh, channel);
47 set_sync_flag(&close_flag, true);
51 static void client_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
55 // We expect always the same amount of data from the server.
57 struct client *client = mesh->priv;
58 assert(len == 512 || len == 65536);
59 client->received += len;
62 client->got_large_packet = true;
66 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
68 struct client *client = mesh->priv;
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);
77 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
82 assert(meshlink_channel_get_flags(mesh, channel) == MESHLINK_CHANNEL_UDP);
83 meshlink_set_channel_receive_cb(mesh, channel, server_receive_cb);
86 meshlink_channel_t **c = mesh->priv;
88 for(int i = 0; i < 3; i++) {
93 set_sync_flag(&accept_flag, true);
104 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
106 // Open four new meshlink instance, the server and three peers.
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));
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);
116 meshlink_enable_discovery(server, false);
117 server->priv = channels;
118 meshlink_set_channel_accept_cb(server, accept_cb);
119 assert(meshlink_start(server));
121 for(int i = 0; i < 3; i++) {
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));
134 // Wait for all three channels to connect
136 assert(wait_sync_flag(&accept_flag, 10));
138 for(int i = 0; i < 3; i++) {
140 assert(clients[i].channel);
143 // Check that we can send up to 65535 bytes without errors
145 char large_data[65536] = "";
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));
152 // Assert that packets larger than 64 kiB are not allowed
154 assert(meshlink_channel_send(server, channels[0], large_data, 65537) == -1);
156 // Stream packets from server to clients for 5 seconds at 40 Mbps (1 kB * 500 Hz)
159 memset(data, 'U', sizeof(data));
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));
166 const struct timespec req = {0, 2000000};
167 clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
170 // Let the clients close the channels
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);
177 assert(wait_sync_flag(&close_flag, 10));
179 // Check that the clients have received (most of) the data
181 for(int i = 0; i < 3; i++) {
182 fprintf(stderr, "%s received %zu\n", clients[i].mesh->name, clients[i].received);
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);
193 for(int i = 0; i < 3; i++) {
194 meshlink_close(clients[i].mesh);
197 meshlink_close(server);