5 #define _POSIX_C_SOURCE 200809L
14 #include "meshlink-tiny.h"
17 static const size_t size = 25000000; // size of data to transfer
18 static const size_t smallsize = 100000; // size of the data to transfer without AIO
19 static const size_t nchannels = 4; // number of simultaneous channels
25 struct sync_flag flag;
30 struct aio_info aio_infos[2];
33 static size_t b_received_len;
34 static struct timespec b_received_ts;
35 static struct sync_flag b_received_flag;
37 static void aio_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, void *priv) {
43 struct aio_info *info = priv;
44 clock_gettime(CLOCK_MONOTONIC, &info->ts);
47 set_sync_flag(&info->flag, true);
50 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
55 b_received_len += len;
57 if(b_received_len >= smallsize) {
58 clock_gettime(CLOCK_MONOTONIC, &b_received_ts);
59 set_sync_flag(&b_received_flag, true);
63 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
64 assert(port && port <= nchannels + 1);
68 if(port <= nchannels) {
69 struct channel_info *infos = mesh->priv;
70 struct channel_info *info = &infos[port - 1];
72 assert(meshlink_channel_aio_receive(mesh, channel, info->data, size / 4, aio_cb, &info->aio_infos[0]));
73 assert(meshlink_channel_aio_receive(mesh, channel, info->data + size / 4, size - size / 4, aio_cb, &info->aio_infos[1]));
75 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
82 init_sync_flag(&b_received_flag);
84 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
86 // Prepare data buffers
88 char *outdata = malloc(size);
91 for(size_t i = 0; i < size; i++) {
95 struct channel_info in_infos[nchannels];
97 struct channel_info out_infos[nchannels];
99 memset(in_infos, 0, sizeof(in_infos));
101 memset(out_infos, 0, sizeof(out_infos));
103 for(size_t i = 0; i < nchannels; i++) {
104 init_sync_flag(&in_infos[i].aio_infos[0].flag);
105 init_sync_flag(&in_infos[i].aio_infos[1].flag);
106 init_sync_flag(&out_infos[i].aio_infos[0].flag);
107 init_sync_flag(&out_infos[i].aio_infos[1].flag);
109 in_infos[i].data = malloc(size);
110 assert(in_infos[i].data);
111 out_infos[i].data = outdata;
114 // Open two new meshlink instance.
116 meshlink_handle_t *mesh_a, *mesh_b;
117 open_meshlink_pair(&mesh_a, &mesh_b, "channels_aio");
119 // Set the callbacks.
121 mesh_b->priv = in_infos;
123 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
125 // Start both instances
127 start_meshlink_pair(mesh_a, mesh_b);
129 // Open channels from a to b.
131 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
134 meshlink_channel_t *channels[nchannels + 1];
136 for(size_t i = 0; i < nchannels + 1; i++) {
137 channels[i] = meshlink_channel_open(mesh_a, b, i + 1, NULL, NULL, 0);
141 // Send a large buffer of data on each channel.
143 for(size_t i = 0; i < nchannels; i++) {
144 assert(meshlink_channel_aio_send(mesh_a, channels[i], outdata, size / 3, aio_cb, &out_infos[i].aio_infos[0]));
145 assert(meshlink_channel_aio_send(mesh_a, channels[i], outdata + size / 3, size - size / 3, aio_cb, &out_infos[i].aio_infos[1]));
148 // Send a little bit on the last channel using a regular send
150 assert(meshlink_channel_send(mesh_a, channels[nchannels], outdata, smallsize) == (ssize_t)smallsize);
152 // Wait for everyone to finish.
154 assert(wait_sync_flag(&b_received_flag, 10));
156 for(size_t i = 0; i < nchannels; i++) {
157 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
158 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
159 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
160 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
163 // Check that everything is correct.
165 assert(b_received_len == smallsize);
167 for(size_t i = 0; i < nchannels; i++) {
168 // Data should be transferred intact.
169 assert(!memcmp(in_infos[i].data, out_infos[i].data, size));
171 // One callback for each AIO buffer.
172 assert(out_infos[i].aio_infos[0].callbacks == 1);
173 assert(out_infos[i].aio_infos[1].callbacks == 1);
174 assert(in_infos[i].aio_infos[0].callbacks == 1);
175 assert(in_infos[i].aio_infos[1].callbacks == 1);
177 // Correct size sent and received.
178 assert(out_infos[i].aio_infos[0].size == size / 3);
179 assert(out_infos[i].aio_infos[1].size == size - size / 3);
180 assert(in_infos[i].aio_infos[0].size == size / 4);
181 assert(in_infos[i].aio_infos[1].size == size - size / 4);
183 // First batch of data should all be sent and received before the second batch
184 for(size_t j = 0; j < nchannels; j++) {
185 assert(timespec_lt(&out_infos[i].aio_infos[0].ts, &out_infos[j].aio_infos[1].ts));
186 assert(timespec_lt(&in_infos[i].aio_infos[0].ts, &in_infos[j].aio_infos[1].ts));
189 // The non-AIO transfer should have completed before everything else
190 assert(!timespec_lt(&out_infos[i].aio_infos[0].ts, &b_received_ts));
191 assert(!timespec_lt(&in_infos[i].aio_infos[0].ts, &b_received_ts));
193 free(in_infos[i].data);
198 close_meshlink_pair(mesh_a, mesh_b);