ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
async.c
Go to the documentation of this file.
7#include <ascii-chat/common.h>
9#include <ascii-chat/buffer.h>
12
13#include <libplatform/cond.h>
15
18typedef struct async_packet {
20 void *data;
21 size_t size;
22 bool pooled;
24
25#define ASYNC_RECEIVE_POOL_CAPACITY (256u * 1024u)
26#define ASYNC_RECEIVE_POOL_SIZE 64u
27
62
63static void recycle_packet(async_tcp_t *tcp, async_packet_t *packet) {
64 packet->data = NULL;
65 packet->size = 0;
66 packet->pooled = false;
67 packet->next = tcp->recycled;
68 tcp->recycled = packet;
69}
70
71static asciichat_error_t async_sendv(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count) {
72 async_tcp_t *tcp = transport->impl_data;
73 /* The outbound worker is the sole writer, but the close callback can clear
74 * tcp->stream and destroy its event stream concurrently. Keep the lifetime
75 * guard through the write so the stream cannot disappear between the state
76 * check and platform_event_stream_write(). */
77 mutex_lock(&tcp->mutex);
78 platform_event_stream_t *stream = tcp->stream;
79 if (!stream || atomic_load_bool(&tcp->closed) || atomic_load_bool(&tcp->interrupted)) {
80 mutex_unlock(&tcp->mutex);
81 return SET_ERRNO(ERROR_NETWORK, "Async transport closed");
82 }
84 asciichat_error_t result = acip_packet_plan(transport->crypto_ctx, buffers, count, &plan);
85 if (result != ASCIICHAT_OK) {
86 mutex_unlock(&tcp->mutex);
87 return result;
88 }
89 if (!plan.encrypt) {
90 result = platform_event_stream_write(stream, buffers, count, NULL, NULL);
91 } else {
93 mutex_unlock(&tcp->mutex);
94 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate async ciphertext");
95 }
96 uint8_t *ciphertext = tcp->ciphertext.data;
97 size_t length = 0;
98 packet_header_t outer;
99 result = acip_packet_encrypt(transport->crypto_ctx, &plan, plan.domain, ciphertext, plan.ciphertext_capacity, &outer, &length);
100 if (result == ERROR_MEMORY) {
101 mutex_unlock(&tcp->mutex);
102 return result;
103 }
104 if (result == ASCIICHAT_OK) {
105 socket_buffer_t wire[] = {{&outer, sizeof(outer)}, {ciphertext, length}};
106 // The event stream copies the vectors before returning.
107 result = platform_event_stream_write(stream, wire, 2, NULL, NULL);
108 }
109 }
110 if (result != ASCIICHAT_OK) {
111 // Encryption sequence may already have advanced. Never continue this stream.
112 atomic_store_bool(&tcp->closed, true);
114 if (result != ERROR_CRYPTO)
115 result = SET_ERRNO(ERROR_NETWORK, "Async packet transmission failed");
116 }
117 mutex_unlock(&tcp->mutex);
118 return result;
119}
120
121static asciichat_error_t async_send(acip_transport_t *transport, const void *data, size_t length) {
122 socket_buffer_t buffer = {data, length};
123 return async_sendv(transport, &buffer, 1);
124}
125
126static asciichat_error_t async_recv(acip_transport_t *transport, void **data, size_t *length, void **allocation) {
127 async_tcp_t *tcp = transport->impl_data;
128 if (!tcp->queued_receive || !data || !length || !allocation)
129 return SET_ERRNO(ERROR_INVALID_STATE, "Async server receives through packet callbacks");
130 *data = *allocation = NULL;
131 *length = 0;
132 /* A pooled buffer retains one transport-owned reference while the caller
133 * holds the shared reference returned below. Recycle it only after the
134 * caller has released that reference, without taking the buffer-domain lock.
135 */
136 async_packet_t **held_link = &tcp->held;
137 while (*held_link) {
138 async_packet_t *held = *held_link;
139 if (!buffer_is_unique(held->data)) {
140 held_link = &held->next;
141 continue;
142 }
143 *held_link = held->next;
144 void *reusable = held->data;
145 recycle_packet(tcp, held);
146 if (!ringbuffer_write(tcp->receive_free, &reusable)) {
147 buffer_release(reusable);
148 continue;
149 }
150 }
151 uint64_t start = time_get_ns();
152 uint64_t timeout = transport->receive_timeout_ns ? transport->receive_timeout_ns : NS_PER_SEC_INT;
153 if (!tcp->receive_lock_held) {
154 mutex_lock(&tcp->receive_mutex);
155 tcp->receive_lock_held = true;
156 }
157 while (atomic_load_u64(&tcp->received_count) == 0 && !atomic_load_bool(&tcp->closed)) {
158 if (atomic_load_bool(&tcp->interrupted)) {
160 mutex_unlock(&tcp->receive_mutex);
161 tcp->receive_lock_held = false;
162 return SET_ERRNO(ERROR_NETWORK, "Async receive interrupted");
163 }
164 uint64_t elapsed = time_get_ns() - start;
165 if (elapsed >= timeout) {
166 mutex_unlock(&tcp->receive_mutex);
167 tcp->receive_lock_held = false;
168 // Short receive deadlines drive polling; the enclosing operation owns its final timeout.
169 bool previous = errno_notifications_suppress(true);
170 asciichat_error_t result = SET_ERRNO(ERROR_NETWORK_TIMEOUT, "Async receive deadline expired");
172 return result;
173 }
174 uint64_t wait = timeout - elapsed;
175 if (wait > 50 * NS_PER_MS_INT)
176 wait = 50 * NS_PER_MS_INT;
177 cond_timedwait(&tcp->received, &tcp->receive_mutex, wait);
178 }
179 async_packet_t *packet = NULL;
180 if (!ringbuffer_read(tcp->receive_queue, &packet) || !packet) {
181 mutex_unlock(&tcp->receive_mutex);
182 tcp->receive_lock_held = false;
183 return SET_ERRNO(ERROR_NETWORK, "Async peer disconnected");
184 }
187 packet_header_t header;
188 memcpy(&header, packet->data, sizeof(header));
189 packet_envelope_t envelope = {
190 .type = NET_TO_HOST_U16(header.type), .data = (uint8_t *)packet->data + sizeof(header), .len = packet->size - sizeof(header), .allocated_buffer = packet->data, .allocated_size = packet->size};
192 if (envelope.type == PACKET_TYPE_ENCRYPTED) {
193 result = packet_decrypt_envelope(&envelope, transport->crypto_ctx);
194 if (result == ASCIICHAT_OK) {
195 memcpy(&header, envelope.allocated_buffer, sizeof(header));
196 if (NET_TO_HOST_U64(header.magic) != PACKET_MAGIC || (envelope.len && asciichat_crc32(envelope.data, envelope.len) != NET_TO_HOST_U32(header.crc32)))
197 result = SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid decrypted async packet");
198 }
199 } else if (transport->crypto_ctx && transport->crypto_ctx->encrypt_data && !packet_is_initial_handshake_type(envelope.type)) {
200 result = SET_ERRNO(ERROR_CRYPTO, "Unexpected plaintext packet");
201 }
202 if (result == ASCIICHAT_OK && envelope.consumed) {
203 if (packet->pooled) {
204 if (envelope.allocated_buffer != packet->data)
205 buffer_release(envelope.allocated_buffer);
206 ringbuffer_write(tcp->receive_free, &packet->data);
207 } else {
208 buffer_release(envelope.allocated_buffer);
209 }
210 recycle_packet(tcp, packet);
212 }
213 if (result != ASCIICHAT_OK) {
214 if (packet->pooled) {
215 if (envelope.allocated_buffer != packet->data)
216 buffer_release(envelope.allocated_buffer);
217 ringbuffer_write(tcp->receive_free, &packet->data);
218 } else {
219 buffer_release(envelope.allocated_buffer);
220 }
221 recycle_packet(tcp, packet);
222 mutex_unlock(&tcp->receive_mutex);
223 tcp->receive_lock_held = false;
224 mutex_lock(&tcp->mutex);
226 mutex_unlock(&tcp->mutex);
227 return result;
228 }
229 if (packet->pooled) {
230 if (!buffer_share(envelope.allocated_buffer)) {
231 ringbuffer_write(tcp->receive_free, &packet->data);
232 recycle_packet(tcp, packet);
233 mutex_unlock(&tcp->receive_mutex);
234 tcp->receive_lock_held = false;
235 return SET_ERRNO(ERROR_MEMORY, "Cannot retain async receive buffer");
236 }
237 packet->next = tcp->held;
238 tcp->held = packet;
239 } else {
240 recycle_packet(tcp, packet);
241 }
242 *data = *allocation = envelope.allocated_buffer;
243 *length = envelope.len + sizeof(header);
244 return ASCIICHAT_OK;
245}
246
247static asciichat_error_t async_close(acip_transport_t *transport) {
248 async_tcp_t *tcp = transport->impl_data;
249 mutex_lock(&tcp->mutex);
250 atomic_store_bool(&tcp->closed, true);
252 mutex_unlock(&tcp->mutex);
253 return ASCIICHAT_OK;
254}
255
257 async_tcp_t *tcp = transport->impl_data;
258 mutex_lock(&tcp->mutex);
259 atomic_store_bool(&tcp->closed, true);
261 tcp->stream = NULL;
262 mutex_unlock(&tcp->mutex);
263}
264
265static bool async_connected(acip_transport_t *transport) {
266 async_tcp_t *tcp = transport->impl_data;
267 bool connected = !atomic_load_bool(&tcp->closed) && !atomic_load_bool(&tcp->interrupted);
268 return connected;
269}
270static acip_transport_type_t async_type(acip_transport_t *transport) {
271 (void)transport;
272 return ACIP_TRANSPORT_TCP;
273}
274static socket_t async_socket(acip_transport_t *transport) {
275 (void)transport;
276 return INVALID_SOCKET_VALUE;
277}
278static bool async_pending(acip_transport_t *transport) {
279 async_tcp_t *tcp = transport->impl_data;
280 bool pending = atomic_load_u64(&tcp->received_count) != 0 || atomic_load_bool(&tcp->closed) || atomic_load_bool(&tcp->interrupted);
281 return pending;
282}
283static void async_destroy(acip_transport_t *transport) {
284 async_tcp_t *tcp = transport->impl_data;
285 mutex_lock(&tcp->mutex);
287 mutex_unlock(&tcp->mutex);
288 if (tcp->queued_receive && !tcp->loop) {
289 mutex_lock(&tcp->mutex);
290 if (tcp->stream)
292 mutex_unlock(&tcp->mutex);
293 }
294 if (tcp->loop) {
296 if (tcp->thread_started)
297 thread_join(&tcp->thread, NULL);
298 else
301 }
302 if (tcp->queued_receive) {
304 async_packet_t *packet = NULL;
305 while (tcp->receive_queue && ringbuffer_read(tcp->receive_queue, &packet)) {
306 if (packet->pooled) {
307 if (!ringbuffer_write(tcp->receive_free, &packet->data))
308 buffer_release(packet->data);
309 } else {
310 buffer_release(packet->data);
311 }
312 SAFE_FREE(packet);
313 }
315 tcp->receive_queue = NULL;
316 while (tcp->held) {
317 packet = tcp->held;
318 tcp->held = packet->next;
319 buffer_release(packet->data);
320 SAFE_FREE(packet);
321 }
322 if (tcp->receive_free) {
323 void *reusable = NULL;
324 while (ringbuffer_read(tcp->receive_free, &reusable))
325 buffer_release(reusable);
327 tcp->receive_free = NULL;
328 }
329 while (tcp->recycled) {
330 async_packet_t *packet = tcp->recycled;
331 tcp->recycled = packet->next;
332 buffer_release(packet->data);
333 SAFE_FREE(packet);
334 }
335 cond_destroy(&tcp->received);
336 }
337 cond_destroy(&tcp->writable);
339 mutex_destroy(&tcp->receive_mutex);
340 mutex_destroy(&tcp->mutex);
341}
342static void async_interrupt(acip_transport_t *transport) {
343 async_tcp_t *tcp = transport->impl_data;
344 atomic_store_bool(&tcp->interrupted, true);
345}
346
347static size_t async_queued_bytes(acip_transport_t *transport) {
348 return acip_async_tcp_transport_queued(transport);
349}
350
351static asciichat_error_t async_wait_writable(acip_transport_t *transport, uint64_t timeout_ns) {
352 async_tcp_t *tcp = transport->impl_data;
353 mutex_lock(&tcp->mutex);
354 while (atomic_load_u64(&tcp->queued_wire_bytes) != 0 && !atomic_load_bool(&tcp->closed) && !atomic_load_bool(&tcp->interrupted)) {
355 if (cond_timedwait(&tcp->writable, &tcp->mutex, timeout_ns) == ERROR_NETWORK_TIMEOUT)
356 break;
357 }
358 bool writable = atomic_load_u64(&tcp->queued_wire_bytes) == 0;
359 bool closed = atomic_load_bool(&tcp->closed) || atomic_load_bool(&tcp->interrupted);
360 mutex_unlock(&tcp->mutex);
361 if (closed)
362 return SET_ERRNO(ERROR_NETWORK, "Async transport closed while waiting for writable output");
363 return writable ? ASCIICHAT_OK : ERROR_NETWORK_TIMEOUT;
364}
365
366static const acip_transport_methods_t async_methods = {.send = async_send,
367 .reserved_bytes = SOCKET_STREAM_SEND_RESERVE,
368 .queued_bytes = async_queued_bytes,
369 .wait_writable = async_wait_writable,
370 .sendv = async_sendv,
371 .recv = async_recv,
372 .close = async_close,
373 .interrupt = async_interrupt,
374 .get_type = async_type,
375 .get_socket = async_socket,
376 .is_connected = async_connected,
377 .has_pending_data = async_pending,
378 .destroy_impl = async_destroy};
379
381 return transport && transport->methods == &async_methods;
382}
383
385 async_tcp_t *tcp = transport->impl_data;
386 return (size_t)atomic_load_u64(&tcp->queued_wire_bytes);
387}
388
389static void queue_changed(void *context, size_t bytes) {
390 async_tcp_t *tcp = context;
392 /* The event backend may invoke this callback synchronously from
393 * platform_event_stream_write(), while async_sendv() holds the lifetime
394 * guard. Condition-variable notifications do not require owning the
395 * associated mutex; taking it here would recurse into tcp->mutex. */
396 cond_broadcast(&tcp->writable);
397}
398
400 acip_transport_t *transport = SAFE_CALLOC(1, sizeof(*transport), acip_transport_t *);
401 async_tcp_t *tcp = SAFE_CALLOC(1, sizeof(*tcp), async_tcp_t *);
402 if (mutex_init(&tcp->mutex, "tcp_packet_io_lifetime_guard") != ASCIICHAT_OK) {
403 SAFE_FREE(tcp);
404 SAFE_FREE(transport);
405 return NULL;
406 }
407 if (mutex_init(&tcp->receive_mutex, "tcp_receive_queue") != ASCIICHAT_OK) {
408 mutex_destroy(&tcp->mutex);
409 SAFE_FREE(tcp);
410 SAFE_FREE(transport);
411 return NULL;
412 }
413 if (cond_init(&tcp->writable, "tcp_output_writable") != ASCIICHAT_OK) {
414 mutex_destroy(&tcp->receive_mutex);
415 mutex_destroy(&tcp->mutex);
416 SAFE_FREE(tcp);
417 SAFE_FREE(transport);
418 return NULL;
419 }
420 tcp->receive_queue = ringbuffer_create(sizeof(async_packet_t *), 4096);
421 if (!tcp->receive_queue) {
423 cond_destroy(&tcp->writable);
424 mutex_destroy(&tcp->receive_mutex);
425 mutex_destroy(&tcp->mutex);
426 SAFE_FREE(tcp);
427 SAFE_FREE(transport);
428 return NULL;
429 }
430 tcp->stream = stream;
431 transport->methods = &async_methods;
432 transport->impl_data = tcp;
433 transport->crypto_ctx = crypto;
434 transport->stats_peer = stats_runtime_peer_open("TCP-async");
435 if (acip_outbound_create(transport) != ASCIICHAT_OK) {
436 acip_transport_destroy(transport);
437 return NULL;
438 }
439 platform_event_stream_observe_queue(stream, queue_changed, tcp);
440 return transport;
441}
442
443static asciichat_error_t client_packet(const void *data, size_t length, void *arg) {
444 async_tcp_t *tcp = arg;
445 uint64_t queued_bytes = atomic_load_u64(&tcp->received_bytes);
446 if (atomic_load_u64(&tcp->received_count) >= 4096 || length > 16 * 1024 * 1024 - queued_bytes) {
447 return SET_ERRNO(ERROR_NETWORK_SIZE, "Async client receive queue full");
448 }
449 async_packet_t *packet = tcp->recycled;
450 if (packet)
451 tcp->recycled = packet->next;
452 else
453 packet = SAFE_CALLOC(1, sizeof(*packet), async_packet_t *);
454 if (length <= ASYNC_RECEIVE_POOL_CAPACITY && tcp->receive_free && ringbuffer_read(tcp->receive_free, &packet->data))
455 packet->pooled = true;
456 else
457 packet->data = buffer_alloc(NULL, length);
458 packet->size = length;
459 if (!packet->data) {
460 recycle_packet(tcp, packet);
461 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate async receive packet");
462 }
463 memcpy(packet->data, data, length);
464 if (!ringbuffer_write(tcp->receive_queue, &packet)) {
465 if (!packet->pooled)
466 buffer_release(packet->data);
467 else
468 ringbuffer_write(tcp->receive_free, &packet->data);
469 recycle_packet(tcp, packet);
470 return SET_ERRNO(ERROR_NETWORK_SIZE, "Async client receive queue full");
471 }
472 bool was_empty = atomic_load_u64(&tcp->received_count) == 0;
475 tcp->packet_received = true;
476 if (was_empty)
477 cond_signal(&tcp->received);
478 return ASCIICHAT_OK;
479}
480static void client_read(platform_event_stream_t *stream, const void *data, size_t length, void *arg) {
481 async_tcp_t *tcp = arg;
482 bool partial = packet_decoder_pending(&tcp->decoder);
483 tcp->packet_received = false;
484 asciichat_error_t result = packet_decoder_feed(&tcp->decoder, data, length, client_packet, tcp);
485 if (result != ASCIICHAT_OK)
486 platform_event_stream_close(stream, result);
487 else if (!packet_decoder_pending(&tcp->decoder))
489 else if (!partial || tcp->packet_received)
490 platform_event_stream_deadline(stream, 30000);
491}
492static void client_closed(void *arg, asciichat_error_t reason) {
493 (void)reason;
494 async_tcp_t *tcp = arg;
495 mutex_lock(&tcp->mutex);
496 atomic_store_bool(&tcp->closed, true);
497 tcp->stream = NULL;
498 platform_event_loop_t *loop = tcp->loop;
499 acip_transport_t *destroy_on_close = tcp->destroy_on_close;
500 mutex_unlock(&tcp->mutex);
501 cond_broadcast(&tcp->received);
502 if (loop)
504 if (destroy_on_close)
505 acip_transport_destroy(destroy_on_close);
506}
507static void client_tick(void *arg) {
508 async_tcp_t *tcp = arg;
509 if (atomic_load_bool(&tcp->interrupted))
511}
512static void *client_loop(void *arg) {
513 async_tcp_t *tcp = arg;
515 return NULL;
516}
518 acip_transport_t *transport = acip_async_tcp_transport_create(NULL, crypto);
519 if (!transport)
520 return NULL;
521 async_tcp_t *tcp = transport->impl_data;
522 if (cond_init(&tcp->received, "async_tcp_receive") != ASCIICHAT_OK) {
523 acip_transport_destroy(transport);
524 return NULL;
525 }
526 tcp->queued_receive = true;
528 if (!tcp->receive_free)
529 goto client_pool_failure;
530 for (size_t i = 0; i < ASYNC_RECEIVE_POOL_SIZE; i++) {
531 void *pool_buffer = buffer_alloc(NULL, ASYNC_RECEIVE_POOL_CAPACITY);
532 if (!pool_buffer)
533 goto client_pool_failure;
534 if (!ringbuffer_write(tcp->receive_free, &pool_buffer)) {
535 buffer_release(pool_buffer);
536 goto client_pool_failure;
537 }
538 }
540 acip_transport_destroy(transport);
541 return NULL;
542 }
543 asciichat_error_t result = platform_event_stream_adopt(tcp->loop, socket, &tcp->stream);
544 if (result == ASCIICHAT_OK) {
545 platform_event_stream_observe_queue(tcp->stream, queue_changed, tcp);
546 result = platform_event_stream_start(tcp->stream, client_read, client_closed, tcp);
547 }
548 if (result == ASCIICHAT_OK)
549 result = platform_event_loop_tick(tcp->loop, 50, client_tick, tcp);
550 if (result == ASCIICHAT_OK)
551 result = platform_thread_create(&tcp->thread, "tcp_io", client_loop, tcp);
552 if (result != ASCIICHAT_OK) {
553 acip_transport_destroy(transport);
554 return NULL;
555 }
556 tcp->thread_started = true;
557 if (acip_outbound_create(transport) != ASCIICHAT_OK) {
558 acip_transport_destroy(transport);
559 return NULL;
560 }
561 return transport;
562
563client_pool_failure:
564 acip_transport_destroy(transport);
565 return NULL;
566}
567
569 acip_transport_t *transport = acip_async_tcp_transport_create(stream, NULL);
570 if (!transport)
571 return NULL;
572 async_tcp_t *tcp = transport->impl_data;
573 if (cond_init(&tcp->received, "accepted_receive") != ASCIICHAT_OK) {
575 tcp->stream = NULL;
576 atomic_store_bool(&tcp->closed, true);
577 acip_transport_destroy(transport);
578 return NULL;
579 }
580 tcp->queued_receive = true;
581 asciichat_error_t result = platform_event_stream_start(stream, client_read, client_closed, tcp);
582 if (result != ASCIICHAT_OK) {
583 // The stream retains its callbacks on startup failure; release their state after close completes.
584 tcp->destroy_on_close = transport;
585 platform_event_stream_close(stream, result);
586 return NULL;
587 }
588 return transport;
589}
bool errno_notifications_suppress(bool suppress)
#define SET_ERRNO(code,...)
acip_transport_t * acip_async_tcp_transport_create(platform_event_stream_t *stream, crypto_context_t *crypto)
Definition async.c:399
#define ASYNC_RECEIVE_POOL_SIZE
Definition async.c:26
#define ASYNC_RECEIVE_POOL_CAPACITY
Definition async.c:25
size_t acip_async_tcp_transport_queued(acip_transport_t *transport)
Definition async.c:384
struct async_packet async_packet_t
void acip_async_tcp_transport_detach(acip_transport_t *transport)
Definition async.c:256
acip_transport_t * acip_async_tcp_client_create(socket_t *socket, crypto_context_t *crypto)
Definition async.c:517
bool acip_async_tcp_transport_is(const acip_transport_t *transport)
Definition async.c:380
acip_transport_t * acip_async_tcp_accepted_create(platform_event_stream_t *stream)
Definition async.c:568
uint64_t atomic_fetch_add_u64(atomic_t *a, uint64_t delta)
Atomically add to a uint64_t and return the previous value.
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
uint64_t atomic_fetch_sub_u64(atomic_t *a, uint64_t delta)
Atomically subtract from a uint64_t and return the previous value.
void atomic_store_u64(atomic_t *a, uint64_t value)
Atomically store a uint64_t value.
uint64_t atomic_load_u64(const atomic_t *a)
Atomically load a uint64_t value.
asciichat_error_t acip_outbound_create(acip_transport_t *transport)
void acip_transport_destroy(acip_transport_t *transport)
Destroy transport and free all resources.
bool buffer_is_unique(const void *data)
Definition buffer.c:335
asciichat_error_t buffer_workspace_reserve(buffer_workspace_t *workspace, buffer_domain_t *domain, size_t capacity)
Definition buffer.c:376
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:294
void buffer_workspace_destroy(buffer_workspace_t *workspace)
Definition buffer.c:398
const void * buffer_share(const void *data)
Definition buffer.c:322
#define buffer_release(data)
Definition buffer.h:108
Hardware-Accelerated CRC32 Checksum Computation.
🔄 Network byte order conversion helpers
#define NET_TO_HOST_U64(val)
Definition endian.h:224
#define NET_TO_HOST_U16(val)
Definition endian.h:115
#define NET_TO_HOST_U32(val)
Definition endian.h:85
asciichat_error_t platform_event_loop_destroy(platform_event_loop_t *loop)
Definition event.c:277
void platform_event_stream_close(platform_event_stream_t *stream, asciichat_error_t reason)
Definition event.c:123
void platform_event_stream_request_close(platform_event_stream_t *stream, asciichat_error_t reason)
Definition event.c:662
asciichat_error_t platform_event_stream_write(platform_event_stream_t *stream, const socket_buffer_t *buffers, size_t count, platform_event_write_fn done, void *arg)
Definition event.c:675
asciichat_error_t platform_event_stream_adopt(platform_event_loop_t *loop, socket_t *socket, platform_event_stream_t **out)
Definition event.c:434
asciichat_error_t platform_event_stream_start(platform_event_stream_t *stream, platform_event_read_fn read, platform_event_close_fn close, void *arg)
Definition event.c:341
void platform_event_loop_stop(platform_event_loop_t *loop)
Definition event.c:266
asciichat_error_t platform_event_loop_run(platform_event_loop_t *loop)
Definition event.c:240
asciichat_error_t platform_event_loop_create(platform_event_loop_t **out)
Definition event.c:210
void platform_event_stream_observe_queue(platform_event_stream_t *stream, platform_event_queue_fn observer, void *context)
Definition event.c:755
asciichat_error_t platform_event_loop_tick(platform_event_loop_t *loop, uint64_t interval, platform_event_task_fn fn, void *arg)
Definition event.c:306
asciichat_error_t platform_event_stream_deadline(platform_event_stream_t *stream, uint64_t timeout_ms)
Definition event.c:377
struct platform_event_loop platform_event_loop_t
Definition event.h:15
struct platform_event_stream platform_event_stream_t
Definition event.h:16
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_NETWORK_INCOMPLETE
Definition error_codes.h:97
@ ERROR_NETWORK
Definition error_codes.h:77
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
@ ERROR_CRYPTO
@ ERROR_NETWORK_TIMEOUT
Definition error_codes.h:80
#define NS_PER_SEC_INT
Definition time.h:173
#define NS_PER_MS_INT
Definition time.h:172
asciichat_error_t packet_decrypt_envelope(packet_envelope_t *envelope, void *crypto_ctx)
Send a clear console packet.
Definition packet.c:639
#define PACKET_MAGIC
Packet magic number (alias for MAGIC_PACKET_VALID).
Definition packet.h:284
@ PACKET_TYPE_ENCRYPTED
Encrypted packet (after handshake completion).
Definition packet.h:369
ringbuffer_t * ringbuffer_create(size_t element_size, size_t capacity)
Create a new ring buffer.
Definition ringbuffer.c:35
void ringbuffer_destroy(ringbuffer_t *rb)
Destroy a ring buffer and free its memory.
Definition ringbuffer.c:63
bool ringbuffer_read(ringbuffer_t *rb, void *data)
Try to read an element from the ring buffer (non-blocking).
Definition ringbuffer.c:96
bool ringbuffer_write(ringbuffer_t *rb, const void *data)
Try to write an element to the ring buffer (non-blocking).
Definition ringbuffer.c:73
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
#define asciichat_crc32(data, len)
Main CRC32 dispatcher macro - use this in application code.
Definition crc32.h:153
int socket_t
⚙️ Common definitions, error codes, macros, and types shared throughout the application
acip_transport_type_t
Transport method table (virtual function table).
@ ACIP_TRANSPORT_TCP
Raw TCP socket.
bool packet_decoder_pending(const packet_decoder_t *decoder)
void packet_decoder_reset(packet_decoder_t *decoder)
asciichat_error_t packet_decoder_feed(packet_decoder_t *decoder, const void *data, size_t length, packet_decoder_fn emit, void *arg)
✅ Safe Integer Arithmetic and Overflow Detection
asciichat_error_t acip_packet_plan(crypto_context_t *crypto, const socket_buffer_t *buffers, size_t count, acip_packet_plan_t *plan)
Definition prepare.c:9
asciichat_error_t acip_packet_encrypt(crypto_context_t *crypto, const acip_packet_plan_t *plan, buffer_domain_t *domain, void *ciphertext, size_t capacity, packet_header_t *header, size_t *length)
The useful part of acip packet plan is in buffers, count, plaintext_size, ciphertext_capacity: togeth...
Definition prepare.c:53
Lock-Free Ring Buffer and Frame Buffer Management.
stats_peer_t * stats_runtime_peer_open(const char *transport)
Definition runtime.c:161
size_t ciphertext_capacity
Definition prepare.h:24
buffer_domain_t * domain
Borrowed control reserve for handshake/error traffic; NULL otherwise.
Definition prepare.h:29
Transport method table (virtual function table).
void * impl_data
Transport-specific state.
stats_peer_t * stats_peer
Owned statistics handle, destroyed with transport.
const acip_transport_methods_t * methods
Method table (virtual functions).
crypto_context_t * crypto_ctx
Optional encryption context.
uint64_t receive_timeout_ns
Optional TCP receive timeout override; zero uses the normal polling timeout.
void * data
Definition async.c:20
bool pooled
Definition async.c:22
size_t size
Definition async.c:21
struct async_packet * next
Definition async.c:19
bool queued_receive
Definition async.c:36
bool thread_started
Definition async.c:35
atomic_t closed
Definition async.c:32
ringbuffer_t * receive_queue
Definition async.c:42
packet_decoder_t decoder
Definition async.c:41
async_packet_t * recycled
Definition async.c:45
atomic_t received_count
Definition async.c:49
async_packet_t * head
Definition async.c:44
buffer_workspace_t ciphertext
Definition async.c:50
platform_event_loop_t * loop
Definition async.c:33
size_t recycled_count
Definition async.c:47
acip_transport_t * destroy_on_close
Definition async.c:37
cond_t received
Definition async.c:38
ringbuffer_t * receive_free
Definition async.c:43
mutex_t mutex
Definition async.c:30
atomic_t interrupted
Definition async.c:53
bool receive_lock_held
Definition async.c:39
async_packet_t * tail
Definition async.c:44
async_packet_t * held
Definition async.c:46
platform_event_stream_t * stream
Definition async.c:29
bool packet_received
Definition async.c:52
cond_t writable
Definition async.c:40
mutex_t receive_mutex
Definition async.c:31
atomic_t queued_wire_bytes
Definition async.c:51
atomic_t received_bytes
Definition async.c:48
platform_thread_t thread
Definition async.c:34
Atomic value wrapper for integral/boolean types.
Definition atomic.h:80
Owns the temporary storage used by buffer workspace. The data pointers and size or cursor fields desc...
Definition buffer.h:122
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.
When code needs to work with packet decoder, it keeps header_used, packet, used, size here instead of...
Packet envelope containing received packet data.
Definition packet.h:1208
Fixed prefix shared by every ACIP packet on the wire.
Definition packet.h:641
Audio ring buffer for real-time audio streaming.
Definition ringbuffer.h:94
⏱️ High-precision timing utilities using sokol_time.h and uthash