15 static const size_t size = 25000000; // size of data to transfer
16 static const size_t smallsize = 100000; // size of the data to transfer without AIO
17 static const size_t nchannels = 4; // number of simultaneous channels
23 struct sync_flag flag;
28 struct aio_info aio_infos[2];
31 static size_t b_received_len;
32 static struct timeval b_received_tv;
33 static struct sync_flag b_received_flag;
35 static void aio_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, void *priv) {
41 struct aio_info *info = priv;
42 gettimeofday(&info->tv, NULL);
45 set_sync_flag(&info->flag, true);
48 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
58 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
63 b_received_len += len;
65 if(b_received_len >= smallsize) {
66 gettimeofday(&b_received_tv, NULL);
67 set_sync_flag(&b_received_flag, true);
71 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
72 assert(port && port <= nchannels + 1);
76 if(port <= nchannels) {
77 struct channel_info *infos = mesh->priv;
78 struct channel_info *info = &infos[port - 1];
80 assert(meshlink_channel_aio_receive(mesh, channel, info->data, size / 4, aio_cb, &info->aio_infos[0]));
81 assert(meshlink_channel_aio_receive(mesh, channel, info->data + size / 4, size - size / 4, aio_cb, &info->aio_infos[1]));
83 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
89 int main(int argc, char *argv[]) {
93 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
95 // Prepare data buffers
97 char *outdata = malloc(size);
100 for(size_t i = 0; i < size; i++) {
104 struct channel_info in_infos[nchannels];
106 struct channel_info out_infos[nchannels];
108 memset(in_infos, 0, sizeof(in_infos));
110 memset(out_infos, 0, sizeof(out_infos));
112 for(size_t i = 0; i < nchannels; i++) {
113 in_infos[i].data = malloc(size);
114 assert(in_infos[i].data);
115 out_infos[i].data = outdata;
118 // Open two new meshlink instance.
120 meshlink_handle_t *mesh_a, *mesh_b;
121 open_meshlink_pair(&mesh_a, &mesh_b, "channels_aio");
123 // Set the callbacks.
125 mesh_b->priv = in_infos;
127 meshlink_set_channel_accept_cb(mesh_a, reject_cb);
128 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
130 // Start both instances
132 start_meshlink_pair(mesh_a, mesh_b);
134 // Open channels from a to b.
136 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
139 meshlink_channel_t *channels[nchannels + 1];
141 for(size_t i = 0; i < nchannels + 1; i++) {
142 channels[i] = meshlink_channel_open(mesh_a, b, i + 1, NULL, NULL, 0);
146 // Send a large buffer of data on each channel.
148 for(size_t i = 0; i < nchannels; i++) {
149 assert(meshlink_channel_aio_send(mesh_a, channels[i], outdata, size / 3, aio_cb, &out_infos[i].aio_infos[0]));
150 assert(meshlink_channel_aio_send(mesh_a, channels[i], outdata + size / 3, size - size / 3, aio_cb, &out_infos[i].aio_infos[1]));
153 // Send a little bit on the last channel using a regular send
155 assert(meshlink_channel_send(mesh_a, channels[nchannels], outdata, smallsize) == (ssize_t)smallsize);
157 // Wait for everyone to finish.
159 assert(wait_sync_flag(&b_received_flag, 10));
161 for(size_t i = 0; i < nchannels; i++) {
162 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
163 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
164 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
165 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
168 // Check that everything is correct.
170 assert(b_received_len == smallsize);
172 for(size_t i = 0; i < nchannels; i++) {
173 // Data should be transferred intact.
174 assert(!memcmp(in_infos[i].data, out_infos[i].data, size));
176 // One callback for each AIO buffer.
177 assert(out_infos[i].aio_infos[0].callbacks == 1);
178 assert(out_infos[i].aio_infos[1].callbacks == 1);
179 assert(in_infos[i].aio_infos[0].callbacks == 1);
180 assert(in_infos[i].aio_infos[1].callbacks == 1);
182 // Correct size sent and received.
183 assert(out_infos[i].aio_infos[0].size == size / 3);
184 assert(out_infos[i].aio_infos[1].size == size - size / 3);
185 assert(in_infos[i].aio_infos[0].size == size / 4);
186 assert(in_infos[i].aio_infos[1].size == size - size / 4);
188 // First batch of data should all be sent and received before the second batch
189 for(size_t j = 0; j < nchannels; j++) {
190 assert(timercmp(&out_infos[i].aio_infos[0].tv, &out_infos[j].aio_infos[1].tv, <=));
191 assert(timercmp(&in_infos[i].aio_infos[0].tv, &in_infos[j].aio_infos[1].tv, <=));
194 // The non-AIO transfer should have completed before everything else
195 assert(timercmp(&out_infos[i].aio_infos[0].tv, &b_received_tv, >=));
196 assert(timercmp(&in_infos[i].aio_infos[0].tv, &b_received_tv, >=));
201 close_meshlink_pair(mesh_a, mesh_b);