]> git.meshlink.io Git - meshlink/blob - test/channels-aio.c
Allow meshlink_open() to be called with a NULL name.
[meshlink] / test / channels-aio.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/time.h>
11
12 #include "meshlink.h"
13 #include "utils.h"
14
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
18
19 struct aio_info {
20         int callbacks;
21         size_t size;
22         struct timeval tv;
23         struct sync_flag flag;
24 };
25
26 struct channel_info {
27         char *data;
28         struct aio_info aio_infos[2];
29 };
30
31 static size_t b_received_len;
32 static struct timeval b_received_tv;
33 static struct sync_flag b_received_flag;
34
35 static void aio_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, void *priv) {
36         (void)mesh;
37         (void)channel;
38         (void)data;
39         (void)len;
40
41         struct aio_info *info = priv;
42         gettimeofday(&info->tv, NULL);
43         info->callbacks++;
44         info->size += len;
45         set_sync_flag(&info->flag, true);
46 }
47
48 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
49         (void)mesh;
50         (void)channel;
51         (void)port;
52         (void)data;
53         (void)len;
54
55         return false;
56 }
57
58 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
59         (void)mesh;
60         (void)channel;
61         (void)data;
62
63         b_received_len += len;
64
65         if(b_received_len >= smallsize) {
66                 gettimeofday(&b_received_tv, NULL);
67                 set_sync_flag(&b_received_flag, true);
68         }
69 }
70
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);
73         assert(!data);
74         assert(!len);
75
76         if(port <= nchannels) {
77                 struct channel_info *infos = mesh->priv;
78                 struct channel_info *info = &infos[port - 1];
79
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]));
82         } else {
83                 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
84         }
85
86         return true;
87 }
88
89 int main(int argc, char *argv[]) {
90         (void)argc;
91         (void)argv;
92
93         meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
94
95         // Prepare data buffers
96
97         char *outdata = malloc(size);
98         assert(outdata);
99
100         for(size_t i = 0; i < size; i++) {
101                 outdata[i] = i;
102         }
103
104         struct channel_info in_infos[nchannels];
105
106         struct channel_info out_infos[nchannels];
107
108         memset(in_infos, 0, sizeof(in_infos));
109
110         memset(out_infos, 0, sizeof(out_infos));
111
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;
116         }
117
118         // Open two new meshlink instance.
119
120         meshlink_handle_t *mesh_a, *mesh_b;
121         open_meshlink_pair(&mesh_a, &mesh_b, "channels_aio");
122
123         // Set the callbacks.
124
125         mesh_b->priv = in_infos;
126
127         meshlink_set_channel_accept_cb(mesh_a, reject_cb);
128         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
129
130         // Start both instances
131
132         start_meshlink_pair(mesh_a, mesh_b);
133
134         // Open channels from a to b.
135
136         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
137         assert(b);
138
139         meshlink_channel_t *channels[nchannels + 1];
140
141         for(size_t i = 0; i < nchannels + 1; i++) {
142                 channels[i] = meshlink_channel_open(mesh_a, b, i + 1, NULL, NULL, 0);
143                 assert(channels[i]);
144         }
145
146         // Send a large buffer of data on each channel.
147
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]));
151         }
152
153         // Send a little bit on the last channel using a regular send
154
155         assert(meshlink_channel_send(mesh_a, channels[nchannels], outdata, smallsize) == (ssize_t)smallsize);
156
157         // Wait for everyone to finish.
158
159         assert(wait_sync_flag(&b_received_flag, 10));
160
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));
166         }
167
168         // Check that everything is correct.
169
170         assert(b_received_len == smallsize);
171
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));
175
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);
181
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);
187
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, <=));
192                 }
193
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, >=));
197         }
198
199         // Clean up.
200
201         close_meshlink_pair(mesh_a, mesh_b);
202 }