ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
acds_handlers.h File Reference

ACIP Discovery Server (ACDS) packet handlers. More...

Go to the source code of this file.

Data Structures

struct  acip_acds_callbacks_t
 ACDS packet handler callbacks. More...

Functions

asciichat_error_t acip_handle_acds_packet (acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, const char *client_ip, const acip_acds_callbacks_t *callbacks)
 ACDS packet handler callbacks.

Detailed Description

ACIP Discovery Server (ACDS) packet handlers.

Provides O(1) array-based packet dispatching for ascii-chat Discovery Server. Handles session management, WebRTC signaling, and discovery protocol packets.

Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026

Definition in file acds_handlers.h.

Function Documentation

◆ acip_handle_acds_packet()

asciichat_error_t acip_handle_acds_packet ( acip_transport_t * transport,
packet_type_t type,
const void * payload,
size_t payload_len,
const char * client_ip,
const acip_acds_callbacks_t * callbacks )

ACDS packet handler callbacks.

Handle incoming ACDS packet with O(1) dispatch

Dispatches packet to appropriate callback based on type. Uses array-based lookup for constant-time handler selection.

Parameters
transportTransport instance for sending responses and accessing per-client state
typePacket type
payloadPacket payload
payload_lenPayload length
client_ipClient IP address string
callbacksApplication callbacks
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 128 of file acds_handlers.c.

130 {
131 stats_runtime_packet(transport ? transport->stats_peer : NULL, type, payload_len, false, true);
132 if (!callbacks) {
133 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid callbacks");
134 }
135
138 "★ ACIP_HANDLE_ACDS_PACKET: Dispatching type=%u (NETWORK_QUALITY) from %s, payload_len=%zu", type,
139 client_ip, payload_len);
140 }
141
142 if ((unsigned)type == CONSENSUS_HANDOFF_PACKET) {
143 if (!callbacks->on_handoff)
144 return SET_ERRNO(ERROR_INVALID_STATE, "Handoff unsupported");
145 return callbacks->on_handoff(payload, payload_len, transport, callbacks->app_ctx);
146 }
147
148 // O(1) dispatch via hash table lookup
149 int idx = acds_handler_hash_lookup(g_acds_handler_hash, type);
150 if (idx < 0) {
151 return SET_ERRNO(ERROR_INVALID_PARAM, "Unhandled ACDS packet type: %d from %s", type, client_ip);
152 }
153
155 log_debug_every(US_PER_SEC_INT, "★ ACIP_HANDLE_ACDS_PACKET: Found handler at index=%d", idx);
156 }
157
158 uint64_t stats_start = time_get_ns();
159 asciichat_error_t result = g_acds_handlers[idx](payload, payload_len, transport, client_ip, callbacks);
161 if (result != ASCIICHAT_OK)
163 return result;
164}
#define SET_ERRNO(code,...)
unsigned long long uint64_t
Definition common.h:63
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_PARAM
#define US_PER_SEC_INT
Definition time.h:173
@ PACKET_TYPE_ACIP_NETWORK_QUALITY
NETWORK_QUALITY exchange (unified packet for all quality metrics).
Definition packet.h:479
#define CONSENSUS_HANDOFF_PACKET
Definition handoff.h:9
void stats_counter_add(stats_scope_t *scope, stats_counter_id_t id, uint64_t amount)
void stats_duration_record(stats_scope_t *scope, stats_duration_id_t id, uint64_t elapsed_ns)
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:749
void stats_runtime_packet(stats_peer_t *peer, unsigned type, size_t payload_bytes, bool sent, bool success)
Definition runtime.c:196
stats_scope_t * stats_runtime_scope(void)
Definition runtime.c:80
void * app_ctx
Application context (passed to all callbacks).
asciichat_error_t(* on_handoff)(const void *, size_t, acip_transport_t *, void *)
stats_peer_t * stats_peer
Owned statistics handle, destroyed with transport.

References acip_acds_callbacks_t::app_ctx, ASCIICHAT_OK, CONSENSUS_HANDOFF_PACKET, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, log_debug_every, acip_acds_callbacks_t::on_handoff, PACKET_TYPE_ACIP_NETWORK_QUALITY, SET_ERRNO, stats_counter_add(), STATS_COUNTER_REQUEST_FAILURES, stats_duration_record(), STATS_DURATION_REQUEST, acip_transport::stats_peer, stats_runtime_packet(), stats_runtime_scope(), and US_PER_SEC_INT.