ascii-chat 0.6.0
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.

Typedefs

typedef asciichat_error_t(* acip_client_handler_func_t) (const void *payload, size_t payload_len, const acip_client_callbacks_t *callbacks)
 Client-side packet handler function pointer type.
 
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)
 Server-side packet handler function pointer type.
 

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 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)

Client-side packet handler function pointer type.

All client packet handlers follow this signature for O(1) array dispatch.

Parameters
payloadPacket payload data
payload_lenPayload length in bytes
callbacksApplication callbacks structure
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 39 of file 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)

Server-side packet handler function pointer type.

All server packet handlers follow this signature for O(1) array dispatch.

Parameters
payloadPacket payload data
payload_lenPayload length in bytes
client_ctxPer-client context (e.g., client_info_t*)
callbacksApplication callbacks structure
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 53 of file 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 119 of file handlers.c.

120 {
121 if (!transport || !callbacks) {
122 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or callbacks");
123 }
124
125 // TODO: Use transport for sending responses or out-of-band messages in future versions
126 (void)transport;
127
128 // O(1) array-based dispatch - bounds check packet type
129 if (type >= 200) {
130 log_warn("Invalid client packet type: %d (out of range)", type);
131 return ASCIICHAT_OK;
132 }
133
134 // Lookup handler in dispatch table
135 acip_client_handler_func_t handler = g_client_packet_handlers[type];
136 if (!handler) {
137 log_warn("Unhandled client packet type: %d", type);
138 return ASCIICHAT_OK;
139 }
140
141 // Dispatch to handler
142 return handler(payload, payload_len, callbacks);
143}
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
@ ASCIICHAT_OK
Definition error_codes.h:48
@ ERROR_INVALID_PARAM
#define log_warn(...)
Log a WARN message.
asciichat_error_t(* acip_client_handler_func_t)(const void *payload, size_t payload_len, const acip_client_callbacks_t *callbacks)
Client-side packet handler function pointer type.
Definition handlers.c:39

References ASCIICHAT_OK, ERROR_INVALID_PARAM, log_warn, 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 534 of file handlers.c.

536 {
537 if (!transport || !callbacks) {
538 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or callbacks");
539 }
540
541 // TODO: Use transport for sending responses or out-of-band messages in future versions
542 (void)transport;
543
544 // O(1) array-based dispatch - bounds check packet type
545 if (type >= 200) {
546 log_warn("Invalid server packet type: %d (out of range)", type);
547 return ASCIICHAT_OK;
548 }
549
550 // Lookup handler in dispatch table
551 acip_server_handler_func_t handler = g_server_packet_handlers[type];
552 if (!handler) {
553 log_warn("Unhandled server packet type: %d", type);
554 return ASCIICHAT_OK;
555 }
556
557 // Dispatch to handler
558 return handler(payload, payload_len, client_ctx, callbacks);
559}
asciichat_error_t(* acip_server_handler_func_t)(const void *payload, size_t payload_len, void *client_ctx, const acip_server_callbacks_t *callbacks)
Server-side packet handler function pointer type.
Definition handlers.c:53

References ASCIICHAT_OK, ERROR_INVALID_PARAM, log_warn, and SET_ERRNO.

Referenced by acip_server_receive_and_dispatch().