ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
backpressure.c File Reference

Go to the source code of this file.

Data Structures

struct  outbound_packet
struct  outbound_stream_t
struct  acip_outbound

Macros

#define OUTBOUND_MAX_PACKETS   512
#define OUTBOUND_MAX_STREAMS   64
#define WIRE_OVERHEAD   (sizeof(packet_header_t) + 32 + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE)

Typedefs

typedef struct outbound_packet outbound_packet_t

Functions

asciichat_error_t acip_outbound_create (acip_transport_t *transport)
void acip_outbound_stop (acip_transport_t *transport)
void acip_outbound_destroy (acip_transport_t *transport)
asciichat_error_t acip_outbound_submitv (acip_transport_t *transport, const socket_buffer_t *buffers, size_t count, acip_admission_t *admission)
asciichat_error_t acip_outbound_submitv_ex (acip_transport_t *transport, const socket_buffer_t *buffers, size_t count, const acip_send_options_t *options, acip_admission_t *admission)
asciichat_error_t acip_outbound_snapshot (acip_transport_t *transport, acip_outbound_stats_t *stats)
bool acip_outbound_needs_keyframe (acip_transport_t *transport, uint16_t type, uint32_t id)

Macro Definition Documentation

◆ OUTBOUND_MAX_PACKETS

#define OUTBOUND_MAX_PACKETS   512

Definition at line 14 of file backpressure.c.

◆ OUTBOUND_MAX_STREAMS

#define OUTBOUND_MAX_STREAMS   64

Definition at line 15 of file backpressure.c.

◆ WIRE_OVERHEAD

#define WIRE_OVERHEAD   (sizeof(packet_header_t) + 32 + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE)

Definition at line 16 of file backpressure.c.

Typedef Documentation

◆ outbound_packet_t

Function Documentation

◆ acip_outbound_create()

asciichat_error_t acip_outbound_create ( acip_transport_t * transport)

Definition at line 429 of file backpressure.c.

429 {
430 ASCII_OTEL_SCOPE(ACIP_OUTBOUND_CREATE);
431 if (!transport || !transport->methods || (!transport->methods->send && !transport->methods->sendv_ex))
432 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing outbound backend");
433 if (transport->outbound)
434 return ASCIICHAT_OK;
436 if (mutex_init(&out->mutex, "connection_outgoing_packet_queue") != ASCIICHAT_OK) {
437 SAFE_FREE(out);
438 return GET_ERRNO();
439 }
440 if (cond_init(&out->changed, "outgoing_packet_ready_or_done") != ASCIICHAT_OK) {
441 mutex_destroy(&out->mutex);
442 SAFE_FREE(out);
443 return GET_ERRNO();
444 }
445 out->transport = transport;
446 asciichat_error_t result = platform_thread_create(&out->worker, "outbound", outbound_worker, out);
447 if (result != ASCIICHAT_OK) {
448 cond_destroy(&out->changed);
449 mutex_destroy(&out->mutex);
450 SAFE_FREE(out);
451 return result;
452 }
453 transport->outbound = out;
454 return ASCIICHAT_OK;
455}
#define SET_ERRNO(code,...)
#define GET_ERRNO()
struct acip_outbound acip_outbound_t
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:102
static bool size_t gotel_query_t * out
Definition queries.h:10
asciichat_error_t(* send)(acip_transport_t *transport, const void *data, size_t len)
Send data through this transport.
asciichat_error_t(* sendv_ex)(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count, const acip_send_options_t *options)
struct acip_outbound * outbound
Per-connection plaintext admission and scheduling.
const acip_transport_methods_t * methods
Method table (virtual functions).

References ASCII_OTEL_SCOPE, ASCIICHAT_OK, ERROR_INVALID_PARAM, GET_ERRNO, acip_transport::methods, out, acip_transport::outbound, SAFE_CALLOC, SAFE_FREE, acip_transport_methods_t::send, acip_transport_methods_t::sendv_ex, and SET_ERRNO.

Referenced by acip_async_tcp_client_create(), acip_async_tcp_transport_create(), acip_tcp_transport_create(), acip_websocket_client_transport_create(), acip_websocket_server_transport_create(), and quic_transport_begin_security().

◆ acip_outbound_destroy()

void acip_outbound_destroy ( acip_transport_t * transport)

Stop admission, interrupt writes, join the worker, and release every queued owner.

Definition at line 468 of file backpressure.c.

468 {
469 ASCII_OTEL_SCOPE(ACIP_OUTBOUND_DESTROY);
470 acip_outbound_t *out = transport->outbound;
471 if (!out)
472 return;
473 mutex_lock(&out->mutex);
474 out->stopping = true;
475 cond_broadcast(&out->changed);
476 mutex_unlock(&out->mutex);
477 /* Wake a blocked send while every backend resource is still alive. The
478 * failed-send worker also closes its backend, so wait for that worker before
479 * closing here: two concurrent closes can join the same service thread. */
480 if (transport->methods->interrupt)
481 transport->methods->interrupt(transport);
482 else
483 transport->methods->close(transport);
484 thread_join(&out->worker, NULL);
485 if (transport->methods->interrupt)
486 transport->methods->close(transport);
487 mutex_lock(&out->mutex);
488 outbound_record_failure_and_fail_pending(out, ERROR_NETWORK, NULL, NULL);
489 while (out->recycled) {
490 outbound_packet_t *p = out->recycled;
491 out->recycled = p->next;
492 packet_free(out, p);
493 }
494 mutex_unlock(&out->mutex);
495 cond_destroy(&out->changed);
496 mutex_destroy(&out->mutex);
497 SAFE_FREE(transport->outbound);
498}
struct outbound_packet outbound_packet_t
@ ERROR_NETWORK
Definition error_codes.h:77
void(* interrupt)(acip_transport_t *transport)
asciichat_error_t(* close)(acip_transport_t *transport)
Close this transport.
struct outbound_packet * next

References ASCII_OTEL_SCOPE, acip_transport_methods_t::close, ERROR_NETWORK, acip_transport_methods_t::interrupt, acip_transport::methods, outbound_packet::next, out, acip_transport::outbound, and SAFE_FREE.

Referenced by acip_transport_destroy().

◆ acip_outbound_needs_keyframe()

bool acip_outbound_needs_keyframe ( acip_transport_t * transport,
uint16_t type,
uint32_t id )

Definition at line 687 of file backpressure.c.

687 {
688 if (!transport || !transport->outbound)
689 return false;
690 acip_outbound_t *out = transport->outbound;
691 if (transport->methods->consume_keyframe_request && transport->methods->consume_keyframe_request(transport, type, id)) {
692 outbound_packet_t stream = {.type = type, .stream = id, .video = true, .encoded = true};
693 mutex_lock(&out->mutex);
694 discard_stream(out, &stream);
695 mutex_unlock(&out->mutex);
696 return true;
697 }
698 mutex_lock(&out->mutex);
699 size_t count = (size_t)atomic_load_u64(&out->stream_count);
700 for (size_t i = 0; i < count; ++i)
701 if (out->streams[i].type == type && out->streams[i].stream == id) {
702 bool needs_keyframe = atomic_load_bool(&out->streams[i].needs_keyframe);
703 mutex_unlock(&out->mutex);
704 return needs_keyframe;
705 }
706 mutex_unlock(&out->mutex);
707 return true;
708}
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
uint64_t atomic_load_u64(const atomic_t *a)
Atomically load a uint64_t value.
bool(* consume_keyframe_request)(acip_transport_t *transport, uint16_t type, uint32_t stream_id)

References atomic_load_bool(), atomic_load_u64(), acip_transport_methods_t::consume_keyframe_request, acip_transport::methods, out, and acip_transport::outbound.

Referenced by acip_send_image_frame_h265(), and threaded_send_image_frame_encoded().

◆ acip_outbound_snapshot()

asciichat_error_t acip_outbound_snapshot ( acip_transport_t * transport,
acip_outbound_stats_t * stats )

Definition at line 665 of file backpressure.c.

665 {
666 if (!transport || !transport->outbound || !stats)
667 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing outbound statistics");
668 acip_outbound_t *out = transport->outbound;
669 mutex_lock(&out->mutex);
670 stats->accepted = atomic_load_u64(&out->accepted);
671 stats->submitted = atomic_load_u64(&out->submitted);
672 stats->dropped = atomic_load_u64(&out->dropped);
673 stats->congestion = atomic_load_u64(&out->congestion);
674 stats->recovery_frames = atomic_load_u64(&out->recovery_frames);
675 stats->peak_bytes = (size_t)atomic_load_u64(&out->peak_bytes);
676 stats->application_bytes = out->bytes + out->active_bytes;
677 stats->backend_bytes = backend_bytes(out);
678 stats->backend_reserved_bytes = transport->methods->reserved_bytes;
679 uint64_t now = time_get_ns();
680 for (outbound_packet_t *p = out->head; p; p = p->next)
681 if (p->video && now - p->created > stats->oldest_frame_age_ns)
682 stats->oldest_frame_age_ns = now - p->created;
683 mutex_unlock(&out->mutex);
684 return ASCIICHAT_OK;
685}
unsigned long long uint64_t
size_t reserved_bytes
Bound for backend-owned pending data not exposed by queued_bytes.

References acip_outbound_stats_t::accepted, acip_outbound_stats_t::application_bytes, ASCIICHAT_OK, atomic_load_u64(), acip_outbound_stats_t::backend_bytes, acip_outbound_stats_t::backend_reserved_bytes, acip_outbound_stats_t::congestion, outbound_packet::created, acip_outbound_stats_t::dropped, ERROR_INVALID_PARAM, acip_transport::methods, outbound_packet::next, acip_outbound_stats_t::oldest_frame_age_ns, out, acip_transport::outbound, acip_outbound_stats_t::peak_bytes, acip_outbound_stats_t::recovery_frames, acip_transport_methods_t::reserved_bytes, SET_ERRNO, acip_outbound_stats_t::submitted, and outbound_packet::video.

◆ acip_outbound_stop()

void acip_outbound_stop ( acip_transport_t * transport)

Stop admission and wake queued producers without joining the worker or releasing backend resources.

Definition at line 457 of file backpressure.c.

457 {
458 acip_outbound_t *out = transport ? transport->outbound : NULL;
459 if (!out)
460 return;
461 mutex_lock(&out->mutex);
462 out->stopping = true;
463 out->stopped_by_caller = true;
464 outbound_record_failure_and_fail_pending(out, ERROR_NETWORK, NULL, NULL);
465 mutex_unlock(&out->mutex);
466}

References ERROR_NETWORK, out, and acip_transport::outbound.

◆ acip_outbound_submitv()

asciichat_error_t acip_outbound_submitv ( acip_transport_t * transport,
const socket_buffer_t * buffers,
size_t count,
acip_admission_t * admission )

Copy plaintext before returning ACCEPTED; congestion never changes encryption state. Handshake/control submissions except ping/pong wait for backend submission so crypto/setup barriers hold. Success describes local admission/submission, never receiver delivery. A terminal backend failure retains its status and errno context for waiting and subsequent producers. Congestion remains a nonfatal admission outcome.

Definition at line 655 of file backpressure.c.

655 {
656 return outbound_submitv(transport, buffers, count, NULL, admission);
657}

◆ acip_outbound_submitv_ex()

asciichat_error_t acip_outbound_submitv_ex ( acip_transport_t * transport,
const socket_buffer_t * buffers,
size_t count,
const acip_send_options_t * options,
acip_admission_t * admission )

Definition at line 659 of file backpressure.c.

659 {
660 if (!options)
661 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing outbound send options");
662 return outbound_submitv(transport, buffers, count, options, admission);
663}

References ERROR_INVALID_PARAM, and SET_ERRNO.

Referenced by acip_transport_sendv_ex().