15 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
25 int main(int argc, char *argv[]) {
29 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
31 // Start two new meshlink instance.
33 meshlink_handle_t *mesh_a;
34 meshlink_handle_t *mesh_b;
36 open_meshlink_pair(&mesh_a, &mesh_b, "channels_no_partial");
37 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
38 start_meshlink_pair(mesh_a, mesh_b);
42 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
45 meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_a, b, 1, NULL, NULL, 0, MESHLINK_CHANNEL_TCP | MESHLINK_CHANNEL_NO_PARTIAL);
48 // Stop a to ensure we get deterministic behaviour for the channel send queue.
50 meshlink_stop(mesh_a);
52 // Verify that no partial sends succeed.
53 // If rejected sends would fit an empty send buffer, 0 should be returned, otherwise -1.
55 char buf[512] = "data";
57 meshlink_set_channel_sndbuf(mesh_a, channel, 256);
58 assert(meshlink_channel_send(mesh_a, channel, buf, 257) == -1);
59 assert(meshlink_channel_send(mesh_a, channel, buf, 256) == 256);
61 meshlink_set_channel_sndbuf(mesh_a, channel, 512);
62 assert(meshlink_channel_send(mesh_a, channel, buf, 257) == 0);
63 assert(meshlink_channel_send(mesh_a, channel, buf, 128) == 128);
64 assert(meshlink_channel_send(mesh_a, channel, buf, 129) == 0);
65 assert(meshlink_channel_send(mesh_a, channel, buf, 100) == 100);
66 assert(meshlink_channel_send(mesh_a, channel, buf, 29) == 0);
67 assert(meshlink_channel_send(mesh_a, channel, buf, 513) == -1);
69 // Restart a to ensure it gets to flush the channel send queue.
71 assert(meshlink_start(mesh_a));
73 assert_after(!meshlink_channel_get_sendq(mesh_a, channel), 30);
74 assert(meshlink_channel_send(mesh_a, channel, buf, 512) == 512);
76 // Check that we can change the NO_PARTIAL flag
78 assert_after(!meshlink_channel_get_sendq(mesh_a, channel), 30);
79 meshlink_set_channel_sndbuf(mesh_a, channel, 256);
80 assert(meshlink_channel_send(mesh_a, channel, buf, 257) == -1);
81 meshlink_set_channel_flags(mesh_a, channel, 0);
82 assert(meshlink_channel_send(mesh_a, channel, buf, 257) == 256);
84 assert_after(!meshlink_channel_get_sendq(mesh_a, channel), 30);
85 meshlink_set_channel_flags(mesh_a, channel, MESHLINK_CHANNEL_NO_PARTIAL);
86 assert(meshlink_channel_send(mesh_a, channel, buf, 257) == -1);
87 assert(meshlink_channel_send(mesh_a, channel, buf, 256) == 256);
91 close_meshlink_pair(mesh_a, mesh_b);