5 #define _POSIX_C_SOURCE 200809L
14 #include "meshlink-tiny.h"
17 static const size_t size = 2000000; // size of data to transfer
24 struct sync_flag flag;
27 static void aio_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, void *priv) {
28 fprintf(stderr, "%s aio_cb %s %p %zu\n", mesh->name, channel->node->name, data, len);
34 struct aio_info *info = priv;
35 clock_gettime(CLOCK_MONOTONIC, &info->ts);
38 set_sync_flag(&info->flag, true);
39 meshlink_channel_abort(mesh, channel);
43 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
44 fprintf(stderr, "%s accept %s\n", mesh->name, channel->node->name);
49 struct aio_info *info = mesh->priv;
51 assert(meshlink_channel_aio_receive(mesh, channel, info->data, size / 2, aio_cb, info));
57 meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
59 struct aio_info in_info;
60 struct aio_info out_info;
62 memset(&in_info, 0, sizeof(in_info));
63 memset(&out_info, 0, sizeof(out_info));
65 init_sync_flag(&in_info.flag);
66 init_sync_flag(&out_info.flag);
68 in_info.data = calloc(1, size / 2);
70 out_info.data = calloc(1, size);
71 assert(out_info.data);
73 // Open two new meshlink instance.
75 meshlink_handle_t *mesh_a, *mesh_b;
76 open_meshlink_pair(&mesh_a, &mesh_b, "channels_aio_abort");
80 mesh_b->priv = &in_info;
81 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
83 // Start both instances
85 start_meshlink_pair(mesh_a, mesh_b);
87 // Open channel from a to b.
89 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
91 meshlink_channel_t *channel = meshlink_channel_open(mesh_a, b, 1, NULL, NULL, 0);
94 // Send data, receiver aborts halfway
96 assert(meshlink_channel_aio_send(mesh_a, channel, out_info.data, size, aio_cb, &out_info));
98 // Wait for everyone to finish.
100 assert(wait_sync_flag(&out_info.flag, 10));
101 assert(wait_sync_flag(&in_info.flag, 10));
103 // Open a new data, now sender aborts halfway
105 init_sync_flag(&in_info.flag);
106 init_sync_flag(&out_info.flag);
108 in_info.data = calloc(1, size / 2);
109 assert(in_info.data);
110 out_info.data = calloc(1, size / 4);
111 assert(out_info.data);
113 channel = meshlink_channel_open(mesh_a, b, 1, NULL, NULL, 0);
115 assert(meshlink_channel_aio_send(mesh_a, channel, out_info.data, size / 4, aio_cb, &out_info));
117 // Wait for everyone to finish.
119 assert(wait_sync_flag(&out_info.flag, 10));
120 assert(wait_sync_flag(&in_info.flag, 10));
124 close_meshlink_pair(mesh_a, mesh_b);