]> git.meshlink.io Git - meshlink/blob - test/channels-aio-fd.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / channels-aio-fd.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 #include <limits.h>
12
13 #include "meshlink.h"
14 #include "utils.h"
15
16 static const size_t size = 1024 * 1024; // size of data to transfer
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         FILE *file;
28         struct aio_info aio_infos[2];
29 };
30
31 static void aio_fd_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, int fd, size_t len, void *priv) {
32         (void)mesh;
33         (void)channel;
34         (void)fd;
35         (void)len;
36
37         struct aio_info *info = priv;
38         gettimeofday(&info->tv, NULL);
39         info->callbacks++;
40         info->size += len;
41         set_sync_flag(&info->flag, true);
42 }
43
44 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
45         (void)mesh;
46         (void)channel;
47         (void)port;
48         (void)data;
49         (void)len;
50
51         return false;
52 }
53
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);
56         assert(!data);
57         assert(!len);
58
59         struct channel_info *infos = mesh->priv;
60         struct channel_info *info = &infos[port - 1];
61
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]));
64
65         return true;
66 }
67
68 int main(int argc, char *argv[]) {
69         (void)argc;
70         (void)argv;
71
72         meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
73
74         // Prepare file
75
76         char *outdata = malloc(size);
77         assert(outdata);
78
79         for(size_t i = 0; i < size; i++) {
80                 // Human readable output
81                 outdata[i] = i % 96 ? i % 96 + 32 : '\n';
82         }
83
84         FILE *file = fopen("channels_aio_fd.in", "w");
85         assert(file);
86         assert(fwrite(outdata, size, 1, file) == 1);
87         assert(fclose(file) == 0);
88
89         struct channel_info in_infos[nchannels];
90         struct channel_info out_infos[nchannels];
91
92         memset(in_infos, 0, sizeof(in_infos));
93         memset(out_infos, 0, sizeof(out_infos));
94
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);
102         }
103
104         // Open two new meshlink instance.
105
106         meshlink_handle_t *mesh_a, *mesh_b;
107         open_meshlink_pair(&mesh_a, &mesh_b, "channels_aio_fd");
108
109         mesh_b->priv = in_infos;
110
111         meshlink_enable_discovery(mesh_a, false);
112         meshlink_enable_discovery(mesh_b, false);
113
114         // Set the callbacks.
115
116         meshlink_set_channel_accept_cb(mesh_a, reject_cb);
117         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
118
119         // Start both instances
120
121         start_meshlink_pair(mesh_a, mesh_b);
122
123         // Open channels from a to b.
124
125         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
126         assert(b);
127
128         meshlink_channel_t *channels[nchannels];
129
130         for(size_t i = 0; i < nchannels; i++) {
131                 channels[i] = meshlink_channel_open(mesh_a, b, i + 1, NULL, NULL, 0);
132                 assert(channels[i]);
133         }
134
135         // Send a large buffer of data on each channel.
136
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]));
140         }
141
142         // Wait for everyone to finish.
143
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));
149         }
150
151         // Check that everything is correct.
152
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);
156
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);
162
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);
168
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, <=));
173                 }
174
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);
179
180         }
181
182         // Clean up.
183
184         close_meshlink_pair(mesh_a, mesh_b);
185 }