16 static const size_t size = 1024 * 1024; // size of data to transfer
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 void aio_fd_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, int fd, size_t len, void *priv) {
37 struct aio_info *info = priv;
38 clock_gettime(CLOCK_MONOTONIC, &info->ts);
41 set_sync_flag(&info->flag, true);
44 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
45 assert(port && port <= nchannels);
49 struct channel_info *infos = mesh->priv;
50 struct channel_info *info = &infos[port - 1];
52 assert(meshlink_channel_aio_fd_receive(mesh, channel, fileno(info->file), size / 4, aio_fd_cb, &info->aio_infos[0]));
53 assert(meshlink_channel_aio_fd_receive(mesh, channel, fileno(info->file), size - size / 4, aio_fd_cb, &info->aio_infos[1]));
59 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
63 char *outdata = malloc(size);
66 for(size_t i = 0; i < size; i++) {
67 // Human readable output
68 outdata[i] = i % 96 ? i % 96 + 32 : '\n';
71 FILE *file = fopen("channels_aio_fd.in", "w");
73 assert(fwrite(outdata, size, 1, file) == 1);
74 assert(fclose(file) == 0);
76 struct channel_info in_infos[nchannels];
77 struct channel_info out_infos[nchannels];
79 memset(in_infos, 0, sizeof(in_infos));
80 memset(out_infos, 0, sizeof(out_infos));
82 for(size_t i = 0; i < nchannels; i++) {
83 init_sync_flag(&in_infos[i].aio_infos[0].flag);
84 init_sync_flag(&in_infos[i].aio_infos[1].flag);
85 init_sync_flag(&out_infos[i].aio_infos[0].flag);
86 init_sync_flag(&out_infos[i].aio_infos[1].flag);
88 char filename[PATH_MAX];
89 snprintf(filename, sizeof(filename), "channels_aio_fd.out%d", (int)i);
90 in_infos[i].file = fopen(filename, "w");
91 assert(in_infos[i].file);
92 out_infos[i].file = fopen("channels_aio_fd.in", "r");
93 assert(out_infos[i].file);
96 // Open two new meshlink instance.
98 meshlink_handle_t *mesh_a, *mesh_b;
99 open_meshlink_pair(&mesh_a, &mesh_b, "channels_aio_fd");
101 mesh_b->priv = in_infos;
103 meshlink_enable_discovery(mesh_a, false);
104 meshlink_enable_discovery(mesh_b, false);
106 // Set the callbacks.
108 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
110 // Start both instances
112 start_meshlink_pair(mesh_a, mesh_b);
114 // Open channels from a to b.
116 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
119 meshlink_channel_t *channels[nchannels];
121 for(size_t i = 0; i < nchannels; i++) {
122 channels[i] = meshlink_channel_open(mesh_a, b, i + 1, NULL, NULL, 0);
126 // Send a large buffer of data on each channel.
128 for(size_t i = 0; i < nchannels; i++) {
129 assert(meshlink_channel_aio_fd_send(mesh_a, channels[i], fileno(out_infos[i].file), size / 3, aio_fd_cb, &out_infos[i].aio_infos[0]));
130 assert(meshlink_channel_aio_fd_send(mesh_a, channels[i], fileno(out_infos[i].file), size - size / 3, aio_fd_cb, &out_infos[i].aio_infos[1]));
133 // Wait for everyone to finish.
135 for(size_t i = 0; i < nchannels; i++) {
136 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
137 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
138 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
139 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
142 // Check that everything is correct.
144 for(size_t i = 0; i < nchannels; i++) {
145 assert(fclose(in_infos[i].file) == 0);
146 assert(fclose(out_infos[i].file) == 0);
148 // One callback for each AIO buffer.
149 assert(out_infos[i].aio_infos[0].callbacks == 1);
150 assert(out_infos[i].aio_infos[1].callbacks == 1);
151 assert(in_infos[i].aio_infos[0].callbacks == 1);
152 assert(in_infos[i].aio_infos[1].callbacks == 1);
154 // Correct size sent and received.
155 assert(out_infos[i].aio_infos[0].size == size / 3);
156 assert(out_infos[i].aio_infos[1].size == size - size / 3);
157 assert(in_infos[i].aio_infos[0].size == size / 4);
158 assert(in_infos[i].aio_infos[1].size == size - size / 4);
160 // First batch of data should all be sent and received before the second batch
161 for(size_t j = 0; j < nchannels; j++) {
162 assert(timespec_lt(&out_infos[i].aio_infos[0].ts, &out_infos[j].aio_infos[1].ts));
163 assert(timespec_lt(&in_infos[i].aio_infos[0].ts, &in_infos[j].aio_infos[1].ts));
166 // Files should be identical
167 char command[PATH_MAX];
168 snprintf(command, sizeof(command), "cmp channels_aio_fd.in channels_aio_fd.out%d", (int)i);
169 assert(system(command) == 0);
175 close_meshlink_pair(mesh_a, mesh_b);