ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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)
 Client-side packet handler 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)
 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)
Client-side packet handler callbacks.
Definition handlers.c:203
Client-side packet handler callbacks.
Definition handlers.h:51
The wire metadata for one rendered ASCII frame. width and height describe the terminal grid,...
Definition packet.h:902
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 )

Client-side packet handler callbacks.

Server-side packet handler 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 203 of file handlers.c.

204 {
205 stats_runtime_packet(transport ? transport->stats_peer : NULL, type, payload_len, false, true);
206 if (!transport || !callbacks) {
207 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or callbacks");
208 }
209 (void)transport;
210
211 // ACDS broadcasts participant lifecycle notifications to peers. The generic
212 // client callback set does not expose them, so ignore them while keeping the
213 // signaling receive loop alive for subsequent SDP and ICE packets.
215 return ASCIICHAT_OK;
216 }
217
218 // O(1) dispatch via hash table lookup
219 int idx = handler_hash_lookup(g_client_handler_hash, type);
220 if (idx < 0) {
221 return SET_ERRNO(ERROR_INVALID_PARAM, "Unhandled client packet type: %d", type);
222 }
223
224 return g_client_handlers[idx](payload, payload_len, callbacks);
225}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_PARAM
@ PACKET_TYPE_ACIP_PARTICIPANT_JOINED
Participant joined notification (ACDS -> Existing Participants).
Definition packet.h:493
@ PACKET_TYPE_ACIP_PARTICIPANT_LEFT
Participant left notification (ACDS -> Remaining Participants).
Definition packet.h:495
void stats_runtime_packet(stats_peer_t *peer, unsigned type, size_t payload_bytes, bool sent, bool success)
Definition runtime.c:196
stats_peer_t * stats_peer
Owned statistics handle, destroyed with transport.

References ASCIICHAT_OK, ERROR_INVALID_PARAM, PACKET_TYPE_ACIP_PARTICIPANT_JOINED, PACKET_TYPE_ACIP_PARTICIPANT_LEFT, SET_ERRNO, acip_transport::stats_peer, and stats_runtime_packet().

Referenced by acip_client_receive_and_dispatch(), and session_protocol_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 799 of file handlers.c.

801 {
802 stats_runtime_packet(transport ? transport->stats_peer : NULL, type, payload_len, false, true);
803 if (!transport || !callbacks) {
804 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or callbacks");
805 }
806 (void)transport;
807
808 // DEBUG: Log all packet types received
809 log_dev("ACIP_HANDLE: type=%d payload_len=%zu", type, payload_len);
810
811 // O(1) dispatch via hash table lookup
812 int idx = handler_hash_lookup(g_server_handler_hash, type);
813 if (idx < 0) {
814 log_error("ACIP_HANDLER_NOT_FOUND: No handler for packet type=%d (0x%04x, name=%s)", type, type,
816 return SET_ERRNO(ERROR_INVALID_PARAM, "Unhandled server packet type: %d", type);
817 }
818
819 log_dev("HANDLER_DISPATCH: type=%d (%s), handler_idx=%d, payload=%zu bytes", type, acip_packet_type_name(type), idx,
820 payload_len);
821 asciichat_error_t result = g_server_handlers[idx](payload, payload_len, client_ctx, callbacks);
822
823 if (result != ASCIICHAT_OK) {
824 log_error("❌ [HANDLER_ERROR] Handler failed: type=%d (%s), result=%s", type, acip_packet_type_name(type),
825 asciichat_error_format(NULL, result));
826 } else {
827 log_dev("HANDLER_COMPLETE: type=%d (%s), payload=%zu bytes", type, acip_packet_type_name(type), payload_len);
828 }
829
830 return result;
831}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:581
#define log_error(...)
Log an ERROR message.
Definition log/log.h:634
const char * acip_packet_type_name(uint16_t type)
Get human-readable name for packet type.
Definition handlers.c:648

References acip_packet_type_name(), asciichat_error_format(), ASCIICHAT_OK, ERROR_INVALID_PARAM, log_dev, log_error, SET_ERRNO, acip_transport::stats_peer, and stats_runtime_packet().

Referenced by acip_server_receive_and_dispatch(), handle_image_frame_h264_packet(), and server_dispatch_packet().