13 #include "../src/meshlink.h"
16 static struct sync_flag accept_flag;
19 meshlink_handle_t *mesh;
20 meshlink_channel_t *channel;
22 bool got_large_packet;
23 struct sync_flag close_flag;
26 static void client_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
28 struct client *client = mesh->priv;
31 set_sync_flag(&client->close_flag, true);
32 meshlink_channel_close(mesh, channel);
36 // We expect always the same amount of data from the server.
37 assert(len == 512 || len == 65536);
38 client->received += len;
41 client->got_large_packet = true;
45 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
47 struct client *client = mesh->priv;
49 if(reachable && !strcmp(node->name, "server")) {
50 assert(!client->channel);
51 client->channel = meshlink_channel_open_ex(mesh, node, 1, client_receive_cb, NULL, 0, MESHLINK_CHANNEL_UDP);
52 assert(client->channel);
56 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
61 assert(meshlink_channel_get_flags(mesh, channel) == MESHLINK_CHANNEL_UDP);
64 meshlink_channel_t **c = mesh->priv;
66 for(int i = 0; i < 3; i++) {
71 set_sync_flag(&accept_flag, true);
82 init_sync_flag(&accept_flag);
84 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
86 // Open four new meshlink instance, the server and three peers.
88 const char *names[3] = {"foo", "bar", "baz"};
89 struct client clients[3];
90 meshlink_channel_t *channels[3] = {NULL, NULL, NULL};
91 memset(clients, 0, sizeof(clients));
93 assert(meshlink_destroy("channels_udp_conf.0"));
94 meshlink_handle_t *server = meshlink_open("channels_udp_conf.0", "server", "channels-udp", DEV_CLASS_BACKBONE);
96 meshlink_enable_discovery(server, false);
97 server->priv = channels;
98 meshlink_set_channel_accept_cb(server, accept_cb);
99 assert(meshlink_start(server));
101 for(int i = 0; i < 3; i++) {
103 snprintf(dir, sizeof(dir), "channels_udp_conf.%d", i + 1);
104 assert(meshlink_destroy(dir));
105 init_sync_flag(&clients[i].close_flag);
106 clients[i].mesh = meshlink_open(dir, names[i], "channels-udp", DEV_CLASS_STATIONARY);
107 assert(clients[i].mesh);
108 clients[i].mesh->priv = &clients[i];
109 meshlink_enable_discovery(clients[i].mesh, false);
110 link_meshlink_pair(server, clients[i].mesh);
111 meshlink_set_node_status_cb(clients[i].mesh, status_cb);
112 assert(meshlink_start(clients[i].mesh));
115 // Wait for all three channels to connect
117 assert(wait_sync_flag(&accept_flag, 10));
119 for(int i = 0; i < 3; i++) {
121 assert(clients[i].channel);
124 // Check that we can send up to 65535 bytes without errors
126 char large_data[65536] = "";
128 for(int i = 0; i < 3; i++) {
129 assert(meshlink_channel_send(server, channels[i], large_data, sizeof(large_data) + 1) == -1);
130 assert(meshlink_channel_send(server, channels[i], large_data, sizeof(large_data)) == sizeof(large_data));
133 // Assert that packets larger than 64 kiB are not allowed
135 assert(meshlink_channel_send(server, channels[0], large_data, 65537) == -1);
137 // Stream packets from server to clients for 5 seconds at 40 Mbps (1 kB * 500 Hz)
140 memset(data, 'U', sizeof(data));
142 for(int j = 0; j < 2500; j++) {
143 for(int i = 0; i < 3; i++) {
144 assert(meshlink_channel_send(server, channels[i], data, sizeof(data)) == sizeof(data));
147 const struct timespec req = {0, 2000000};
148 nanosleep(&req, NULL);
151 // Shutdown the write side of the server's channels
153 for(int i = 0; i < 3; i++) {
154 meshlink_channel_shutdown(server, channels[i], SHUT_WR);
157 // Wait for the clients to finish reading all the data
159 for(int i = 0; i < 3; i++) {
160 assert(wait_sync_flag(&clients[i].close_flag, 10));
163 // Check that the clients have received (most of) the data
165 for(int i = 0; i < 3; i++) {
166 fprintf(stderr, "%s received %zu\n", clients[i].mesh->name, clients[i].received);
169 for(int i = 0; i < 3; i++) {
170 assert(clients[i].received >= 1000000);
171 assert(clients[i].received <= 1345536);
172 assert(clients[i].got_large_packet);
177 for(int i = 0; i < 3; i++) {
178 meshlink_close(clients[i].mesh);
181 meshlink_close(server);