ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
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   19
 
#define SERVER_HANDLER_COUNT   19
 
#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)
 
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)
 

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   19

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

◆ HANDLER_HASH

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

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

◆ HANDLER_HASH_SIZE

#define HANDLER_HASH_SIZE   32

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

◆ SERVER_HANDLER_COUNT

#define SERVER_HANDLER_COUNT   19

Definition at line 43 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 29 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 32 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 
)

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

196 {
197 if (!transport || !callbacks) {
198 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or callbacks");
199 }
200 (void)transport;
201
202 // O(1) dispatch via hash table lookup
203 int idx = handler_hash_lookup(g_client_handler_hash, type);
204 if (idx < 0) {
205 return SET_ERRNO(ERROR_INVALID_PARAM, "Unhandled client packet type: %d", type);
206 }
207
208 return g_client_handlers[idx](payload, payload_len, callbacks);
209}

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 
)

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

637 {
638 if (!transport || !callbacks) {
639 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or callbacks");
640 }
641 (void)transport;
642
643 // DEBUG: Log all packet types received
644 log_info("ACIP_HANDLE: Received packet type=%d (0x%04x), payload_len=%zu", type, type, payload_len);
645
646 // O(1) dispatch via hash table lookup
647 int idx = handler_hash_lookup(g_server_handler_hash, type);
648 if (idx < 0) {
649 log_error("ACIP_HANDLER_NOT_FOUND: No handler for packet type=%d (0x%04x)", type, type);
650 return SET_ERRNO(ERROR_INVALID_PARAM, "Unhandled server packet type: %d", type);
651 }
652
653 log_info("ACIP_DISPATCH_HANDLER: type=%d, handler_idx=%d, calling handler...", type, idx);
654 asciichat_error_t result = g_server_handlers[idx](payload, payload_len, client_ctx, callbacks);
655 log_info("ACIP_HANDLER_RESULT: type=%d, result=%d", type, result);
656 return result;
657}

Referenced by acip_server_receive_and_dispatch(), and client_dispatch_thread().