11 static const size_t size = 25000000; // size of data to transfer
12 static const size_t smallsize = 100000; // size of the data to transfer without AIO
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 size_t bar_received_len;
28 static struct timeval bar_received_tv;
29 static struct sync_flag bar_received_flag;
31 static void aio_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, 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 void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
59 bar_received_len += len;
61 if(bar_received_len >= smallsize) {
62 gettimeofday(&bar_received_tv, NULL);
63 set_sync_flag(&bar_received_flag, true);
67 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
68 assert(port && port <= nchannels + 1);
72 if(port <= nchannels) {
73 struct channel_info *infos = mesh->priv;
74 struct channel_info *info = &infos[port - 1];
76 assert(meshlink_channel_aio_receive(mesh, channel, info->data, size / 4, aio_cb, &info->aio_infos[0]));
77 assert(meshlink_channel_aio_receive(mesh, channel, info->data + size / 4, size - size / 4, aio_cb, &info->aio_infos[1]));
79 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
85 int main(int argc, char *argv[]) {
89 // Prepare data buffers
91 char *outdata = malloc(size);
95 for(size_t i = 0; i < size; i++) {
99 struct channel_info in_infos[nchannels];
101 struct channel_info out_infos[nchannels];
103 memset(in_infos, 0, sizeof(in_infos));
105 memset(out_infos, 0, sizeof(out_infos));
107 for(size_t i = 0; i < nchannels; i++) {
108 in_infos[i].data = malloc(size);
109 assert(in_infos[i].data);
110 out_infos[i].data = outdata;
113 // Open two new meshlink instance.
115 meshlink_destroy("channels_aio_conf.1");
116 meshlink_destroy("channels_aio_conf.2");
118 meshlink_handle_t *mesh1 = meshlink_open("channels_aio_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
121 meshlink_handle_t *mesh2 = meshlink_open("channels_aio_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
124 mesh2->priv = in_infos;
126 meshlink_enable_discovery(mesh1, false);
127 meshlink_enable_discovery(mesh2, false);
129 // Import and export both side's data
131 meshlink_add_address(mesh1, "localhost");
133 char *data = meshlink_export(mesh1);
135 assert(meshlink_import(mesh2, data));
138 data = meshlink_export(mesh2);
140 assert(meshlink_import(mesh1, data));
143 // Set the callbacks.
145 meshlink_set_channel_accept_cb(mesh1, reject_cb);
146 meshlink_set_channel_accept_cb(mesh2, accept_cb);
148 // Start both instances
150 assert(meshlink_start(mesh1));
151 assert(meshlink_start(mesh2));
153 // Open channels from foo to bar.
155 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
158 meshlink_channel_t *channels[nchannels + 1];
160 for(size_t i = 0; i < nchannels + 1; i++) {
161 channels[i] = meshlink_channel_open(mesh1, bar, i + 1, NULL, NULL, 0);
165 // Send a large buffer of data on each channel.
167 for(size_t i = 0; i < nchannels; i++) {
168 assert(meshlink_channel_aio_send(mesh1, channels[i], outdata, size / 3, aio_cb, &out_infos[i].aio_infos[0]));
169 assert(meshlink_channel_aio_send(mesh1, channels[i], outdata + size / 3, size - size / 3, aio_cb, &out_infos[i].aio_infos[1]));
172 // Send a little bit on the last channel using a regular send
174 assert(meshlink_channel_send(mesh1, channels[nchannels], outdata, smallsize) == smallsize);
176 // Wait for everyone to finish.
178 assert(wait_sync_flag(&bar_received_flag, 10));
180 for(size_t i = 0; i < nchannels; i++) {
181 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
182 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
183 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
184 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
187 // Check that everything is correct.
189 assert(bar_received_len == smallsize);
191 for(size_t i = 0; i < nchannels; i++) {
192 // Data should be transferred intact.
193 assert(!memcmp(in_infos[i].data, out_infos[i].data, size));
195 // One callback for each AIO buffer.
196 assert(out_infos[i].aio_infos[0].callbacks == 1);
197 assert(out_infos[i].aio_infos[1].callbacks == 1);
198 assert(in_infos[i].aio_infos[0].callbacks == 1);
199 assert(in_infos[i].aio_infos[1].callbacks == 1);
201 // Correct size sent and received.
202 assert(out_infos[i].aio_infos[0].size == size / 3);
203 assert(out_infos[i].aio_infos[1].size == size - size / 3);
204 assert(in_infos[i].aio_infos[0].size == size / 4);
205 assert(in_infos[i].aio_infos[1].size == size - size / 4);
207 // First batch of data should all be sent and received before the second batch
208 for(size_t j = 0; j < nchannels; j++) {
209 assert(timercmp(&out_infos[i].aio_infos[0].tv, &out_infos[j].aio_infos[1].tv, <=));
210 assert(timercmp(&in_infos[i].aio_infos[0].tv, &in_infos[j].aio_infos[1].tv, <=));
213 // The non-AIO transfer should have completed before everything else
214 assert(timercmp(&out_infos[i].aio_infos[0].tv, &bar_received_tv, >=));
215 assert(timercmp(&in_infos[i].aio_infos[0].tv, &bar_received_tv, >=));
220 meshlink_close(mesh2);
221 meshlink_close(mesh1);