ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
handlers.h File Reference

ACIP protocol packet handlers (transport-agnostic) More...

Go to the source code of this file.

Data Structures

struct  acip_client_callbacks_t
 Client-side packet handler callbacks. More...
 
struct  acip_server_callbacks_t
 Server-side packet handler callbacks. More...
 

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 (transport-agnostic)

Provides handler functions for all ACIP packet types (1-199). Handlers are transport-agnostic - they work with any acip_transport_t.

DESIGN PATTERN:

Handlers use callback pattern to decouple protocol logic from application logic. Applications register callbacks for events they care about.

Example usage:

// Define application callbacks
void my_on_ascii_frame(const ascii_frame_packet_t *frame, const void *data, size_t len, void *ctx) {
// Render frame to terminal
}
// Register callbacks
.on_ascii_frame = my_on_ascii_frame,
.app_ctx = my_app_state
};
// Process incoming packet
acip_handle_client_packet(transport, type, payload, payload_len, &callbacks);
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.
Definition handlers.c:119
Client-side packet handler callbacks.
Definition handlers.h:51
void(* on_ascii_frame)(const ascii_frame_packet_t *header, const void *frame_data, size_t data_len, void *ctx)
Called when ASCII frame received from server.
Definition handlers.h:53
ASCII frame packet structure (Packet Type 2)
Definition packet.h:740
Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026

Definition in file handlers.h.

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