ascii-chat 0.11.33
Video chat in your terminal
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 (6000-6199). 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.
int frame
Definition splash.c:99
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:848
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 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().