ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
handlers.c File Reference

ACIP protocol packet handlers implementation. More...

Go to the source code of this file.

Data Structures

struct  handler_hash_entry_t
 Hash table entry for packet type to handler mapping. More...
 

Macros

#define HANDLER_HASH_SIZE   32
 
#define CLIENT_HANDLER_COUNT   20
 
#define SERVER_HANDLER_COUNT   20
 
#define HANDLER_HASH(type)   ((type) % HANDLER_HASH_SIZE)
 

Typedefs

typedef asciichat_error_t(* acip_client_handler_func_t) (const void *payload, size_t payload_len, const acip_client_callbacks_t *callbacks)
 
typedef asciichat_error_t(* acip_server_handler_func_t) (const void *payload, size_t payload_len, void *client_ctx, const acip_server_callbacks_t *callbacks)
 

Functions

asciichat_error_t acip_handle_client_packet (acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, const acip_client_callbacks_t *callbacks)
 Handle incoming packet on client side.
 
asciichat_error_t acip_handle_server_packet (acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, void *client_ctx, const acip_server_callbacks_t *callbacks)
 Handle incoming packet on server side.
 

Detailed Description

ACIP protocol packet handlers implementation.

Implements packet dispatching for both client and server sides. Uses O(1) array-based dispatch instead of O(n) switch statements. Parses packet payloads and dispatches to registered callbacks.

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

Definition in file network/acip/handlers.c.

Macro Definition Documentation

◆ CLIENT_HANDLER_COUNT

#define CLIENT_HANDLER_COUNT   20

Definition at line 43 of file network/acip/handlers.c.

◆ HANDLER_HASH

#define HANDLER_HASH (   type)    ((type) % HANDLER_HASH_SIZE)

Definition at line 55 of file network/acip/handlers.c.

◆ HANDLER_HASH_SIZE

#define HANDLER_HASH_SIZE   32

Definition at line 42 of file network/acip/handlers.c.

◆ SERVER_HANDLER_COUNT

#define SERVER_HANDLER_COUNT   20

Definition at line 44 of file network/acip/handlers.c.

Typedef Documentation

◆ acip_client_handler_func_t

typedef asciichat_error_t(* acip_client_handler_func_t) (const void *payload, size_t payload_len, const acip_client_callbacks_t *callbacks)

Definition at line 30 of file network/acip/handlers.c.

◆ acip_server_handler_func_t

typedef asciichat_error_t(* acip_server_handler_func_t) (const void *payload, size_t payload_len, void *client_ctx, const acip_server_callbacks_t *callbacks)

Definition at line 33 of file network/acip/handlers.c.

Function Documentation

◆ acip_handle_client_packet()

asciichat_error_t acip_handle_client_packet ( acip_transport_t *  transport,
packet_type_t  type,
const void *  payload,
size_t  payload_len,
const acip_client_callbacks_t *  callbacks 
)

Handle incoming packet on client side.

Dispatches packet to appropriate callback based on type. Transport-agnostic - works with TCP, WebSocket, etc.

Parameters
transportTransport instance
typePacket type
payloadPacket payload
payload_lenPayload length
callbacksApplication callbacks
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 201 of file network/acip/handlers.c.

202 {
203 if (!transport || !callbacks) {
204 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or callbacks");
205 }
206 (void)transport;
207
208 // ACDS broadcasts participant lifecycle notifications to peers. The generic
209 // client callback set does not expose them, so ignore them while keeping the
210 // signaling receive loop alive for subsequent SDP and ICE packets.
212 return ASCIICHAT_OK;
213 }
214
215 // O(1) dispatch via hash table lookup
216 int idx = handler_hash_lookup(g_client_handler_hash, type);
217 if (idx < 0) {
218 return SET_ERRNO(ERROR_INVALID_PARAM, "Unhandled client packet type: %d", type);
219 }
220
221 return g_client_handlers[idx](payload, payload_len, callbacks);
222}
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM
@ PACKET_TYPE_ACIP_PARTICIPANT_JOINED
Participant joined notification (ACDS -> Existing Participants)
Definition packet.h:469
@ PACKET_TYPE_ACIP_PARTICIPANT_LEFT
Participant left notification (ACDS -> Remaining Participants)
Definition packet.h:471

References ASCIICHAT_OK, ERROR_INVALID_PARAM, PACKET_TYPE_ACIP_PARTICIPANT_JOINED, PACKET_TYPE_ACIP_PARTICIPANT_LEFT, and SET_ERRNO.

Referenced by acip_client_receive_and_dispatch().

◆ acip_handle_server_packet()

asciichat_error_t acip_handle_server_packet ( acip_transport_t *  transport,
packet_type_t  type,
const void *  payload,
size_t  payload_len,
void *  client_ctx,
const acip_server_callbacks_t *  callbacks 
)

Handle incoming packet on server side.

Dispatches packet to appropriate callback based on type. Transport-agnostic - works with TCP, WebSocket, etc.

Parameters
transportTransport instance
typePacket type
payloadPacket payload
payload_lenPayload length
client_ctxPer-client context (e.g., client_info_t*)
callbacksApplication callbacks
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 710 of file network/acip/handlers.c.

712 {
713 if (!transport || !callbacks) {
714 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or callbacks");
715 }
716 (void)transport;
717
718 // DEBUG: Log all packet types received
719 log_dev("ACIP_HANDLE: type=%d payload_len=%zu", type, payload_len);
720
721 // O(1) dispatch via hash table lookup
722 int idx = handler_hash_lookup(g_server_handler_hash, type);
723 if (idx < 0) {
724 log_error("ACIP_HANDLER_NOT_FOUND: No handler for packet type=%d (0x%04x, name=%s)", type, type,
725 g_packet_type_name(type));
726 return SET_ERRNO(ERROR_INVALID_PARAM, "Unhandled server packet type: %d", type);
727 }
728
729 log_dev("HANDLER_DISPATCH: type=%d (%s), handler_idx=%d, payload=%zu bytes",
730 type, g_packet_type_name(type), idx, payload_len);
731 asciichat_error_t result = g_server_handlers[idx](payload, payload_len, client_ctx, callbacks);
732
733 if (result != ASCIICHAT_OK) {
734 log_error("❌ [HANDLER_ERROR] Handler failed: type=%d (%s), result=%d", type, g_packet_type_name(type), result);
735 } else {
736 log_dev("HANDLER_COMPLETE: type=%d (%s), payload=%zu bytes", type,
737 g_packet_type_name(type), payload_len);
738 }
739
740 return result;
741}
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
#define log_dev(...)
Log a DEV message (most verbose, development only)
Definition log/log.h:534
#define log_error(...)
Log an ERROR message.
Definition log/log.h:587

References ASCIICHAT_OK, ERROR_INVALID_PARAM, log_dev, log_error, and SET_ERRNO.

Referenced by acip_server_receive_and_dispatch(), and client_dispatch_thread().