ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
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:182
Client-side packet handler callbacks.
Definition handlers.h:55
The wire metadata for one rendered ASCII frame. width and height describe the terminal grid,...
Definition packet.h:920
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 182 of file handlers.c.

182 {
183 stats_runtime_packet(transport ? transport->stats_peer : NULL, type, payload_len, false, true);
184 if (!transport || !callbacks) {
185 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or callbacks");
186 }
187 (void)transport;
188
189 // ACDS broadcasts participant lifecycle notifications to peers. The generic
190 // client callback set does not expose them, so ignore them while keeping the
191 // signaling receive loop alive for subsequent SDP and ICE packets.
193 return ASCIICHAT_OK;
194 }
195
196 // O(1) dispatch via hash table lookup
197 int idx = handler_hash_lookup(g_client_handler_hash, type);
198 if (idx < 0) {
199 return SET_ERRNO(ERROR_INVALID_PARAM, "Unhandled client packet type: %d", type);
200 }
201
202 return g_client_handlers[idx](payload, payload_len, callbacks);
203}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ PACKET_TYPE_ACIP_PARTICIPANT_JOINED
Participant joined notification (ACDS -> Existing Participants).
Definition packet.h:507
@ PACKET_TYPE_ACIP_PARTICIPANT_LEFT
Participant left notification (ACDS -> Remaining Participants).
Definition packet.h:509
void stats_runtime_packet(stats_peer_t *peer, unsigned type, size_t payload_bytes, bool sent, bool success)
Definition runtime.c:205
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_dispatch_packet().

◆ 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 730 of file handlers.c.

730 {
731 stats_runtime_packet(transport ? transport->stats_peer : NULL, type, payload_len, false, true);
732 if (!transport || !callbacks) {
733 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or callbacks");
734 }
735 (void)transport;
736
737 // DEBUG: Log all packet types received
738 log_dev("ACIP_HANDLE: type=%d payload_len=%zu", type, payload_len);
739
740 // O(1) dispatch via hash table lookup
741 int idx = handler_hash_lookup(g_server_handler_hash, type);
742 if (idx < 0) {
743 log_error("ACIP_HANDLER_NOT_FOUND: No handler for packet type=%d (0x%04x, name=%s)", type, type, acip_packet_type_name(type));
744 return SET_ERRNO(ERROR_INVALID_PARAM, "Unhandled server packet type: %d", type);
745 }
746
747 log_dev("HANDLER_DISPATCH: type=%d (%s), handler_idx=%d, payload=%zu bytes", type, acip_packet_type_name(type), idx, payload_len);
748 asciichat_error_t result = g_server_handlers[idx](payload, payload_len, client_ctx, callbacks);
749
750 if (result != ASCIICHAT_OK) {
751 log_error("❌ [HANDLER_ERROR] Handler failed: type=%d (%s), result=%s", type, acip_packet_type_name(type), asciichat_error_format(NULL, result));
752 } else {
753 log_dev("HANDLER_COMPLETE: type=%d (%s), payload=%zu bytes", type, acip_packet_type_name(type), payload_len);
754 }
755
756 return result;
757}
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:48
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:551
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
const char * acip_packet_type_name(uint16_t type)
Get human-readable name for packet type.
Definition handlers.c:579

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