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 gettimeofday(&info->tv, NULL);
41 set_sync_flag(&info->flag, true);
44 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
54 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
55 assert(port && port <= nchannels);
59 struct channel_info *infos = mesh->priv;
60 struct channel_info *info = &infos[port - 1];
62 assert(meshlink_channel_aio_fd_receive(mesh, channel, fileno(info->file), size / 4, aio_fd_cb, &info->aio_infos[0]));
63 assert(meshlink_channel_aio_fd_receive(mesh, channel, fileno(info->file), size - size / 4, aio_fd_cb, &info->aio_infos[1]));
68 int main(int argc, char *argv[]) {
72 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
76 char *outdata = malloc(size);
79 for(size_t i = 0; i < size; i++) {
80 // Human readable output
81 outdata[i] = i % 96 ? i % 96 + 32 : '\n';
84 FILE *file = fopen("channels_aio_fd.in", "w");
86 assert(fwrite(outdata, size, 1, file) == 1);
87 assert(fclose(file) == 0);
89 struct channel_info in_infos[nchannels];
90 struct channel_info out_infos[nchannels];
92 memset(in_infos, 0, sizeof(in_infos));
93 memset(out_infos, 0, sizeof(out_infos));
95 for(size_t i = 0; i < nchannels; i++) {
96 char filename[PATH_MAX];
97 snprintf(filename, sizeof(filename), "channels_aio_fd.out%d", (int)i);
98 in_infos[i].file = fopen(filename, "w");
99 assert(in_infos[i].file);
100 out_infos[i].file = fopen("channels_aio_fd.in", "r");
101 assert(out_infos[i].file);
104 // Open two new meshlink instance.
106 meshlink_handle_t *mesh_a, *mesh_b;
107 open_meshlink_pair(&mesh_a, &mesh_b, "channels_aio_fd");
109 mesh_b->priv = in_infos;
111 meshlink_enable_discovery(mesh_a, false);
112 meshlink_enable_discovery(mesh_b, false);
114 // Set the callbacks.
116 meshlink_set_channel_accept_cb(mesh_a, reject_cb);
117 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
119 // Start both instances
121 start_meshlink_pair(mesh_a, mesh_b);
123 // Open channels from a to b.
125 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
128 meshlink_channel_t *channels[nchannels];
130 for(size_t i = 0; i < nchannels; i++) {
131 channels[i] = meshlink_channel_open(mesh_a, b, i + 1, NULL, NULL, 0);
135 // Send a large buffer of data on each channel.
137 for(size_t i = 0; i < nchannels; i++) {
138 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]));
139 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]));
142 // Wait for everyone to finish.
144 for(size_t i = 0; i < nchannels; i++) {
145 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
146 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
147 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
148 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
151 // Check that everything is correct.
153 for(size_t i = 0; i < nchannels; i++) {
154 assert(fclose(in_infos[i].file) == 0);
155 assert(fclose(out_infos[i].file) == 0);
157 // One callback for each AIO buffer.
158 assert(out_infos[i].aio_infos[0].callbacks == 1);
159 assert(out_infos[i].aio_infos[1].callbacks == 1);
160 assert(in_infos[i].aio_infos[0].callbacks == 1);
161 assert(in_infos[i].aio_infos[1].callbacks == 1);
163 // Correct size sent and received.
164 assert(out_infos[i].aio_infos[0].size == size / 3);
165 assert(out_infos[i].aio_infos[1].size == size - size / 3);
166 assert(in_infos[i].aio_infos[0].size == size / 4);
167 assert(in_infos[i].aio_infos[1].size == size - size / 4);
169 // First batch of data should all be sent and received before the second batch
170 for(size_t j = 0; j < nchannels; j++) {
171 assert(timercmp(&out_infos[i].aio_infos[0].tv, &out_infos[j].aio_infos[1].tv, <=));
172 assert(timercmp(&in_infos[i].aio_infos[0].tv, &in_infos[j].aio_infos[1].tv, <=));
175 // Files should be identical
176 char command[PATH_MAX];
177 snprintf(command, sizeof(command), "cmp channels_aio_fd.in channels_aio_fd.out%d", (int)i);
178 assert(system(command) == 0);
184 close_meshlink_pair(mesh_a, mesh_b);