]> git.meshlink.io Git - meshlink/blob - test/channels-aio.c
Add meshlink_set_channel_listen_cb().
[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 <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 timespec ts;
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 timespec b_received_ts;
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         clock_gettime(CLOCK_MONOTONIC, &info->ts);
43         info->callbacks++;
44         info->size += len;
45         set_sync_flag(&info->flag, true);
46 }
47
48 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
49         (void)mesh;
50         (void)channel;
51         (void)data;
52
53         b_received_len += len;
54
55         if(b_received_len >= smallsize) {
56                 clock_gettime(CLOCK_MONOTONIC, &b_received_ts);
57                 set_sync_flag(&b_received_flag, true);
58         }
59 }
60
61 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
62         assert(port && port <= nchannels + 1);
63         assert(!data);
64         assert(!len);
65
66         if(port <= nchannels) {
67                 struct channel_info *infos = mesh->priv;
68                 struct channel_info *info = &infos[port - 1];
69
70                 assert(meshlink_channel_aio_receive(mesh, channel, info->data, size / 4, aio_cb, &info->aio_infos[0]));
71                 assert(meshlink_channel_aio_receive(mesh, channel, info->data + size / 4, size - size / 4, aio_cb, &info->aio_infos[1]));
72         } else {
73                 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
74         }
75
76         return true;
77 }
78
79 int main(void) {
80         init_sync_flag(&b_received_flag);
81
82         meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
83
84         // Prepare data buffers
85
86         char *outdata = malloc(size);
87         assert(outdata);
88
89         for(size_t i = 0; i < size; i++) {
90                 outdata[i] = i;
91         }
92
93         struct channel_info in_infos[nchannels];
94
95         struct channel_info out_infos[nchannels];
96
97         memset(in_infos, 0, sizeof(in_infos));
98
99         memset(out_infos, 0, sizeof(out_infos));
100
101         for(size_t i = 0; i < nchannels; i++) {
102                 init_sync_flag(&in_infos[i].aio_infos[0].flag);
103                 init_sync_flag(&in_infos[i].aio_infos[1].flag);
104                 init_sync_flag(&out_infos[i].aio_infos[0].flag);
105                 init_sync_flag(&out_infos[i].aio_infos[1].flag);
106
107                 in_infos[i].data = malloc(size);
108                 assert(in_infos[i].data);
109                 out_infos[i].data = outdata;
110         }
111
112         // Open two new meshlink instance.
113
114         meshlink_handle_t *mesh_a, *mesh_b;
115         open_meshlink_pair(&mesh_a, &mesh_b, "channels_aio");
116
117         // Set the callbacks.
118
119         mesh_b->priv = in_infos;
120
121         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
122
123         // Start both instances
124
125         start_meshlink_pair(mesh_a, mesh_b);
126
127         // Open channels from a to b.
128
129         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
130         assert(b);
131
132         meshlink_channel_t *channels[nchannels + 1];
133
134         for(size_t i = 0; i < nchannels + 1; i++) {
135                 channels[i] = meshlink_channel_open(mesh_a, b, i + 1, NULL, NULL, 0);
136                 assert(channels[i]);
137         }
138
139         // Send a large buffer of data on each channel.
140
141         for(size_t i = 0; i < nchannels; i++) {
142                 assert(meshlink_channel_aio_send(mesh_a, channels[i], outdata, size / 3, aio_cb, &out_infos[i].aio_infos[0]));
143                 assert(meshlink_channel_aio_send(mesh_a, channels[i], outdata + size / 3, size - size / 3, aio_cb, &out_infos[i].aio_infos[1]));
144         }
145
146         // Send a little bit on the last channel using a regular send
147
148         assert(meshlink_channel_send(mesh_a, channels[nchannels], outdata, smallsize) == (ssize_t)smallsize);
149
150         // Wait for everyone to finish.
151
152         assert(wait_sync_flag(&b_received_flag, 10));
153
154         for(size_t i = 0; i < nchannels; i++) {
155                 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
156                 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
157                 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
158                 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
159         }
160
161         // Check that everything is correct.
162
163         assert(b_received_len == smallsize);
164
165         for(size_t i = 0; i < nchannels; i++) {
166                 // Data should be transferred intact.
167                 assert(!memcmp(in_infos[i].data, out_infos[i].data, size));
168
169                 // One callback for each AIO buffer.
170                 assert(out_infos[i].aio_infos[0].callbacks == 1);
171                 assert(out_infos[i].aio_infos[1].callbacks == 1);
172                 assert(in_infos[i].aio_infos[0].callbacks == 1);
173                 assert(in_infos[i].aio_infos[1].callbacks == 1);
174
175                 // Correct size sent and received.
176                 assert(out_infos[i].aio_infos[0].size == size / 3);
177                 assert(out_infos[i].aio_infos[1].size == size - size / 3);
178                 assert(in_infos[i].aio_infos[0].size == size / 4);
179                 assert(in_infos[i].aio_infos[1].size == size - size / 4);
180
181                 // First batch of data should all be sent and received before the second batch
182                 for(size_t j = 0; j < nchannels; j++) {
183                         assert(timespec_lt(&out_infos[i].aio_infos[0].ts, &out_infos[j].aio_infos[1].ts));
184                         assert(timespec_lt(&in_infos[i].aio_infos[0].ts, &in_infos[j].aio_infos[1].ts));
185                 }
186
187                 // The non-AIO transfer should have completed before everything else
188                 assert(!timespec_lt(&out_infos[i].aio_infos[0].ts, &b_received_ts));
189                 assert(!timespec_lt(&in_infos[i].aio_infos[0].ts, &b_received_ts));
190         }
191
192         // Clean up.
193
194         close_meshlink_pair(mesh_a, mesh_b);
195 }