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.h File Reference

Go to the source code of this file.

Functions

acip_transport_t * acip_async_tcp_transport_create (platform_event_stream_t *, crypto_context_t *)
void acip_async_tcp_transport_detach (acip_transport_t *)
bool acip_async_tcp_transport_is (const acip_transport_t *)
size_t acip_async_tcp_transport_queued (acip_transport_t *)
acip_transport_t * acip_async_tcp_client_create (socket_t *, crypto_context_t *)
acip_transport_t * acip_async_tcp_accepted_create (platform_event_stream_t *)

Function Documentation

◆ acip_async_tcp_accepted_create()

acip_transport_t * acip_async_tcp_accepted_create ( platform_event_stream_t * stream)

Queue complete packets from an accepted stream on its listener's loop. The returned transport is owned by the accepting caller. Destroy it outside the loop thread while the listener is running, or after the listener stops.

Definition at line 568 of file async.c.

568 {
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}
acip_transport_t * acip_async_tcp_transport_create(platform_event_stream_t *stream, crypto_context_t *crypto)
Definition async.c:399
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
void acip_transport_destroy(acip_transport_t *transport)
Destroy transport and free all resources.
void platform_event_stream_close(platform_event_stream_t *stream, asciichat_error_t reason)
Definition event.c:123
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_stream_observe_queue(platform_event_stream_t *stream, platform_event_queue_fn observer, void *context)
Definition event.c:755
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
void * impl_data
Transport-specific state.
bool queued_receive
Definition async.c:36
atomic_t closed
Definition async.c:32
acip_transport_t * destroy_on_close
Definition async.c:37
cond_t received
Definition async.c:38
platform_event_stream_t * stream
Definition async.c:29

References acip_async_tcp_transport_create(), acip_transport_destroy(), ASCIICHAT_OK, atomic_store_bool(), async_tcp_t::closed, async_tcp_t::destroy_on_close, acip_transport::impl_data, platform_event_stream_close(), platform_event_stream_observe_queue(), platform_event_stream_start(), async_tcp_t::queued_receive, async_tcp_t::received, and async_tcp_t::stream.

◆ acip_async_tcp_client_create()

acip_transport_t * acip_async_tcp_client_create ( socket_t * socket,
crypto_context_t * crypto )

Native client adapter. Owns a loop thread; recv waits on a bounded packet queue. Takes socket ownership when *socket becomes INVALID_SOCKET_VALUE.

Definition at line 517 of file async.c.

517 {
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}
#define ASYNC_RECEIVE_POOL_SIZE
Definition async.c:26
#define ASYNC_RECEIVE_POOL_CAPACITY
Definition async.c:25
asciichat_error_t acip_outbound_create(acip_transport_t *transport)
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:294
#define buffer_release(data)
Definition buffer.h:108
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_loop_create(platform_event_loop_t **out)
Definition event.c:210
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
ringbuffer_t * ringbuffer_create(size_t element_size, size_t capacity)
Create a new ring buffer.
Definition ringbuffer.c:35
bool ringbuffer_write(ringbuffer_t *rb, const void *data)
Try to write an element to the ring buffer (non-blocking).
Definition ringbuffer.c:73
bool thread_started
Definition async.c:35
platform_event_loop_t * loop
Definition async.c:33
ringbuffer_t * receive_free
Definition async.c:43
platform_thread_t thread
Definition async.c:34

References acip_async_tcp_transport_create(), acip_outbound_create(), acip_transport_destroy(), ASCIICHAT_OK, ASYNC_RECEIVE_POOL_CAPACITY, ASYNC_RECEIVE_POOL_SIZE, buffer_alloc(), buffer_release, acip_transport::impl_data, async_tcp_t::loop, platform_event_loop_create(), platform_event_loop_tick(), platform_event_stream_adopt(), platform_event_stream_observe_queue(), platform_event_stream_start(), async_tcp_t::queued_receive, async_tcp_t::receive_free, async_tcp_t::received, ringbuffer_create(), ringbuffer_write(), async_tcp_t::stream, async_tcp_t::thread, and async_tcp_t::thread_started.

◆ acip_async_tcp_transport_create()

acip_transport_t * acip_async_tcp_transport_create ( platform_event_stream_t * stream,
crypto_context_t * crypto )

Outbound ACIP adapter. Incoming packets are delivered by the connection's decoder. Destroy after the stream close callback, when workers have drained.

Definition at line 399 of file async.c.

399 {
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}
struct async_packet async_packet_t
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
void ringbuffer_destroy(ringbuffer_t *rb)
Destroy a ring buffer and free its memory.
Definition ringbuffer.c:63
stats_peer_t * stats_runtime_peer_open(const char *transport)
Definition runtime.c:161
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.
ringbuffer_t * receive_queue
Definition async.c:42
mutex_t mutex
Definition async.c:30
cond_t writable
Definition async.c:40
mutex_t receive_mutex
Definition async.c:31

References acip_outbound_create(), acip_transport_destroy(), ASCIICHAT_OK, acip_transport::crypto_ctx, acip_transport::impl_data, acip_transport::methods, async_tcp_t::mutex, platform_event_stream_observe_queue(), async_tcp_t::receive_mutex, async_tcp_t::receive_queue, ringbuffer_create(), ringbuffer_destroy(), SAFE_CALLOC, SAFE_FREE, acip_transport::stats_peer, stats_runtime_peer_open(), async_tcp_t::stream, and async_tcp_t::writable.

Referenced by acip_async_tcp_accepted_create(), and acip_async_tcp_client_create().

◆ acip_async_tcp_transport_detach()

void acip_async_tcp_transport_detach ( acip_transport_t * transport)

Definition at line 256 of file async.c.

256 {
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}

References atomic_store_bool(), async_tcp_t::closed, acip_transport::impl_data, async_tcp_t::mutex, platform_event_stream_observe_queue(), and async_tcp_t::stream.

◆ acip_async_tcp_transport_is()

bool acip_async_tcp_transport_is ( const acip_transport_t * transport)

Definition at line 380 of file async.c.

380 {
381 return transport && transport->methods == &async_methods;
382}

References acip_transport::methods.

◆ acip_async_tcp_transport_queued()

size_t acip_async_tcp_transport_queued ( acip_transport_t * transport)

Definition at line 384 of file async.c.

384 {
385 async_tcp_t *tcp = transport->impl_data;
386 return (size_t)atomic_load_u64(&tcp->queued_wire_bytes);
387}
uint64_t atomic_load_u64(const atomic_t *a)
Atomically load a uint64_t value.
atomic_t queued_wire_bytes
Definition async.c:51

References atomic_load_u64(), acip_transport::impl_data, and async_tcp_t::queued_wire_bytes.