12 static const size_t size = 1024 * 1024; // size of data to transfer
13 static const size_t nchannels = 4; // number of simultaneous channels
19 struct sync_flag flag;
24 struct aio_info aio_infos[2];
27 static void aio_fd_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, int fd, size_t len, void *priv) {
33 struct aio_info *info = priv;
34 gettimeofday(&info->tv, NULL);
37 set_sync_flag(&info->flag, true);
40 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
50 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
51 assert(port && port <= nchannels);
55 struct channel_info *infos = mesh->priv;
56 struct channel_info *info = &infos[port - 1];
58 assert(meshlink_channel_aio_fd_receive(mesh, channel, fileno(info->file), size / 4, aio_fd_cb, &info->aio_infos[0]));
59 assert(meshlink_channel_aio_fd_receive(mesh, channel, fileno(info->file), size - size / 4, aio_fd_cb, &info->aio_infos[1]));
64 int main(int argc, char *argv[]) {
70 char *outdata = malloc(size);
74 for(size_t i = 0; i < size; i++) {
75 // Human readable output
76 outdata[i] = i % 96 ? i % 96 + 32 : '\n';
79 FILE *file = fopen("channels_aio_fd.in", "w");
81 assert(fwrite(outdata, size, 1, file) == 1);
82 assert(fclose(file) == 0);
84 struct channel_info in_infos[nchannels];
85 struct channel_info out_infos[nchannels];
87 memset(in_infos, 0, sizeof(in_infos));
88 memset(out_infos, 0, sizeof(out_infos));
90 for(size_t i = 0; i < nchannels; i++) {
91 char filename[PATH_MAX];
92 snprintf(filename, sizeof(filename), "channels_aio_fd.out%d", (int)i);
93 in_infos[i].file = fopen(filename, "w");
94 assert(in_infos[i].file);
95 out_infos[i].file = fopen("channels_aio_fd.in", "r");
96 assert(out_infos[i].file);
99 // Open two new meshlink instance.
101 meshlink_destroy("channels_aio_fd_conf.1");
102 meshlink_destroy("channels_aio_fd_conf.2");
104 meshlink_handle_t *mesh1 = meshlink_open("channels_aio_fd_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
107 meshlink_handle_t *mesh2 = meshlink_open("channels_aio_fd_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
110 mesh2->priv = in_infos;
112 meshlink_enable_discovery(mesh1, false);
113 meshlink_enable_discovery(mesh2, false);
115 // Import and export both side's data
117 meshlink_add_address(mesh1, "localhost");
119 char *data = meshlink_export(mesh1);
121 assert(meshlink_import(mesh2, data));
124 data = meshlink_export(mesh2);
126 assert(meshlink_import(mesh1, data));
129 // Set the callbacks.
131 meshlink_set_channel_accept_cb(mesh1, reject_cb);
132 meshlink_set_channel_accept_cb(mesh2, accept_cb);
134 // Start both instances
136 assert(meshlink_start(mesh1));
137 assert(meshlink_start(mesh2));
139 // Open channels from foo to bar.
141 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
144 meshlink_channel_t *channels[nchannels];
146 for(size_t i = 0; i < nchannels; i++) {
147 channels[i] = meshlink_channel_open(mesh1, bar, i + 1, NULL, NULL, 0);
151 // Send a large buffer of data on each channel.
153 for(size_t i = 0; i < nchannels; i++) {
154 assert(meshlink_channel_aio_fd_send(mesh1, channels[i], fileno(out_infos[i].file), size / 3, aio_fd_cb, &out_infos[i].aio_infos[0]));
155 assert(meshlink_channel_aio_fd_send(mesh1, channels[i], fileno(out_infos[i].file), size - size / 3, aio_fd_cb, &out_infos[i].aio_infos[1]));
158 // Wait for everyone to finish.
160 for(size_t i = 0; i < nchannels; i++) {
161 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
162 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
163 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
164 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
167 // Check that everything is correct.
169 for(size_t i = 0; i < nchannels; i++) {
170 assert(fclose(in_infos[i].file) == 0);
171 assert(fclose(out_infos[i].file) == 0);
173 // One callback for each AIO buffer.
174 assert(out_infos[i].aio_infos[0].callbacks == 1);
175 assert(out_infos[i].aio_infos[1].callbacks == 1);
176 assert(in_infos[i].aio_infos[0].callbacks == 1);
177 assert(in_infos[i].aio_infos[1].callbacks == 1);
179 // Correct size sent and received.
180 assert(out_infos[i].aio_infos[0].size == size / 3);
181 assert(out_infos[i].aio_infos[1].size == size - size / 3);
182 assert(in_infos[i].aio_infos[0].size == size / 4);
183 assert(in_infos[i].aio_infos[1].size == size - size / 4);
185 // First batch of data should all be sent and received before the second batch
186 for(size_t j = 0; j < nchannels; j++) {
187 assert(timercmp(&out_infos[i].aio_infos[0].tv, &out_infos[j].aio_infos[1].tv, <=));
188 assert(timercmp(&in_infos[i].aio_infos[0].tv, &in_infos[j].aio_infos[1].tv, <=));
191 // Files should be identical
192 char command[PATH_MAX];
193 snprintf(command, sizeof(command), "cmp channels_aio_fd.in channels_aio_fd.out%d", (int)i);
194 assert(system(command) == 0);
200 meshlink_close(mesh2);
201 meshlink_close(mesh1);