ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
client.c File Reference

ACIP client-side protocol implementation. More...

Go to the source code of this file.

Functions

asciichat_error_t acip_client_receive_packet (acip_transport_t *transport, packet_envelope_t *out)
asciichat_error_t acip_client_receive_and_dispatch (acip_transport_t *transport, const acip_client_callbacks_t *callbacks)
 Receive packet from server and dispatch to callbacks.
asciichat_error_t acip_client_dispatch_packet (acip_transport_t *transport, packet_type_t type, const void *payload, size_t length, const acip_client_callbacks_t *callbacks)
void acip_client_session_init (acip_client_session_t *session, acip_transport_t *transport, const acip_client_callbacks_t *callbacks)
asciichat_error_t acip_client_session_receive (acip_client_session_t *session)
asciichat_error_t acip_client_session_dispatch (acip_client_session_t *session, packet_type_t type, const void *payload, size_t length)
asciichat_error_t acip_client_session_process_wire (acip_client_session_t *session, const void *wire, size_t length)
asciichat_error_t acip_send_image_frame (acip_transport_t *transport, const void *pixel_data, uint32_t width, uint32_t height, uint32_t pixel_format)
 Send image frame to server (client → server).
asciichat_error_t acip_send_image_frame_h265 (acip_transport_t *transport, h265_encoder_t *encoder, const void *pixel_data, uint32_t width, uint32_t height)
 Send H.265-encoded video frame to server.
asciichat_error_t acip_send_stream_start (acip_transport_t *transport, uint8_t stream_types)
 Request to start media streaming (client → server).
asciichat_error_t acip_send_stream_stop (acip_transport_t *transport, uint8_t stream_types)
 Request to stop media streaming (client → server).
asciichat_error_t acip_send_capabilities (acip_transport_t *transport, const void *cap_data, size_t cap_len)
 Send terminal capabilities to server (client → server).
asciichat_error_t acip_send_protocol_version (acip_transport_t *transport, const protocol_version_packet_t *version)
 Send protocol version to server (client → server).

Detailed Description

ACIP client-side protocol implementation.

Provides client-side ACIP protocol implementation for:

  • Session discovery and management
  • WebRTC signaling relay
  • String reservation (future)

ACIP (ascii-chat IP Protocol) is the wire protocol for session discovery and WebRTC signaling. ACDS is the reference server implementation.

Client-side packet sending and receiving for ascii-chat protocol. Handles client→server communication (image frames, join/leave, streaming control).

Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026

Definition in file lib/network/acip/client.c.

Function Documentation

◆ acip_client_dispatch_packet()

asciichat_error_t acip_client_dispatch_packet ( acip_transport_t * transport,
packet_type_t type,
const void * payload,
size_t length,
const acip_client_callbacks_t * callbacks )

Dispatch one already-decoded client packet through the shared ACIP client path.

Definition at line 60 of file lib/network/acip/client.c.

60 {
61 if (!transport || !callbacks || (!payload && length))
62 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client packet dispatch input");
63 return acip_handle_client_packet(transport, type, payload, length, callbacks);
64}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_PARAM
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

References acip_handle_client_packet(), ERROR_INVALID_PARAM, and SET_ERRNO.

Referenced by acip_client_receive_and_dispatch(), and acip_client_session_dispatch().

◆ acip_client_receive_and_dispatch()

asciichat_error_t acip_client_receive_and_dispatch ( acip_transport_t * transport,
const acip_client_callbacks_t * callbacks )

Receive packet from server and dispatch to callbacks.

High-level receive function for client side. Receives a single packet from the transport, handles decryption, and dispatches to appropriate callback handler.

Parameters
transportTransport instance
callbacksCallback structure for handling received packets
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 48 of file lib/network/acip/client.c.

48 {
49 ASCII_OTEL_SCOPE(ACIP_CLIENT_RECEIVE_AND_DISPATCH);
50 if (!callbacks)
51 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing client packet callbacks");
52 packet_envelope_t envelope = {0};
53 asciichat_error_t result = acip_client_receive_packet(transport, &envelope);
54 if (result == ASCIICHAT_OK)
55 result = acip_client_dispatch_packet(transport, envelope.type, envelope.data, envelope.len, callbacks);
56 packet_envelope_release(&envelope);
57 return result;
58}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
asciichat_error_t acip_client_dispatch_packet(acip_transport_t *transport, packet_type_t type, const void *payload, size_t length, const acip_client_callbacks_t *callbacks)
asciichat_error_t acip_client_receive_packet(acip_transport_t *transport, packet_envelope_t *out)
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
void packet_envelope_release(packet_envelope_t *envelope)
Definition packet.c:711
Packet envelope containing received packet data.
Definition packet.h:1204

References acip_client_dispatch_packet(), acip_client_receive_packet(), ASCII_OTEL_SCOPE, ASCIICHAT_OK, ERROR_INVALID_PARAM, packet_envelope_release(), and SET_ERRNO.

Referenced by acip_client_session_receive().

◆ acip_client_receive_packet()

asciichat_error_t acip_client_receive_packet ( acip_transport_t * transport,
packet_envelope_t * out )

Definition at line 33 of file lib/network/acip/client.c.

33 {
34 if (!transport || !out) {
35 log_error("[ACIP_RECV] ❌ INVALID_PARAMS: transport=%p, output=%p", (void *)transport, (void *)out);
36 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or packet output");
37 }
38
39 *out = (packet_envelope_t){0};
40 if (!acip_transport_is_connected(transport) && !acip_transport_has_pending_data(transport))
41 return SET_ERRNO(ERROR_NETWORK, "Transport not connected");
42 asciichat_error_t result = packet_receive_via_transport(transport, &out->type, &out->data, &out->len, &out->allocated_buffer);
43 if (result == ASCIICHAT_OK && out->allocated_buffer)
44 out->allocated_size = buffer_capacity(out->allocated_buffer);
45 return result;
46}
size_t buffer_capacity(const void *data)
Definition buffer.c:278
@ ERROR_NETWORK
Definition error_codes.h:76
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
static bool size_t gotel_query_t * out
Definition queries.h:8
asciichat_error_t packet_receive_via_transport(acip_transport_t *transport, packet_type_t *type, void **payload, size_t *payload_len, void **alloc_buffer)
Receive a packet via transport with header parsing and validation.
Definition send.c:192

References ASCIICHAT_OK, buffer_capacity(), ERROR_INVALID_PARAM, ERROR_NETWORK, log_error, out, packet_receive_via_transport(), and SET_ERRNO.

Referenced by acip_client_receive_and_dispatch().

◆ acip_client_session_dispatch()

asciichat_error_t acip_client_session_dispatch ( acip_client_session_t * session,
packet_type_t type,
const void * payload,
size_t length )

Definition at line 80 of file lib/network/acip/client.c.

80 {
81 if (!session)
82 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing ACIP client session");
83 return acip_client_dispatch_packet(session->transport, type, payload, length, &session->callbacks);
84}

References acip_client_dispatch_packet(), acip_client_session_t::callbacks, ERROR_INVALID_PARAM, SET_ERRNO, and acip_client_session_t::transport.

Referenced by acip_client_session_process_wire(), and session_protocol_dispatch().

◆ acip_client_session_init()

void acip_client_session_init ( acip_client_session_t * session,
acip_transport_t * transport,
const acip_client_callbacks_t * callbacks )

Definition at line 66 of file lib/network/acip/client.c.

66 {
67 if (!session)
68 return;
69 *session = (acip_client_session_t){.transport = transport};
70 if (callbacks)
71 session->callbacks = *callbacks;
72}

References acip_client_session_t::callbacks.

Referenced by session_protocol_init().

◆ acip_client_session_process_wire()

asciichat_error_t acip_client_session_process_wire ( acip_client_session_t * session,
const void * wire,
size_t length )

Definition at line 86 of file lib/network/acip/client.c.

86 {
87 if (!session || !wire)
88 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid ACIP client wire packet");
89 packet_envelope_t envelope = {0};
90 asciichat_error_t result = packet_parse_envelope(wire, length, session->transport ? session->transport->crypto_ctx : NULL, &envelope);
91 if (result == ASCIICHAT_OK && !envelope.consumed)
92 result = acip_client_session_dispatch(session, envelope.type, envelope.data, envelope.len);
93 packet_envelope_release(&envelope);
94 return result;
95}
asciichat_error_t acip_client_session_dispatch(acip_client_session_t *session, packet_type_t type, const void *payload, size_t length)
asciichat_error_t packet_parse_envelope(const void *wire, size_t length, void *crypto_ctx, packet_envelope_t *out)
Parse one complete wire packet using the shared client receive path.
Definition packet.c:686
crypto_context_t * crypto_ctx
Optional encryption context.

References acip_client_session_dispatch(), ASCIICHAT_OK, acip_transport::crypto_ctx, ERROR_INVALID_PARAM, packet_envelope_release(), packet_parse_envelope(), SET_ERRNO, and acip_client_session_t::transport.

◆ acip_client_session_receive()

asciichat_error_t acip_client_session_receive ( acip_client_session_t * session)

Definition at line 74 of file lib/network/acip/client.c.

74 {
75 if (!session)
76 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing ACIP client session");
77 return acip_client_receive_and_dispatch(session->transport, &session->callbacks);
78}
asciichat_error_t acip_client_receive_and_dispatch(acip_transport_t *transport, const acip_client_callbacks_t *callbacks)
Receive packet from server and dispatch to callbacks.

References acip_client_receive_and_dispatch(), acip_client_session_t::callbacks, ERROR_INVALID_PARAM, SET_ERRNO, and acip_client_session_t::transport.

◆ acip_send_capabilities()

asciichat_error_t acip_send_capabilities ( acip_transport_t * transport,
const void * cap_data,
size_t cap_len )

Send terminal capabilities to server (client → server).

Reports client's terminal capabilities (color support, dimensions, etc.)

Parameters
transportTransport instance
cap_dataCapability data buffer
cap_lenCapability data length
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 213 of file lib/network/acip/client.c.

213 {
214 if (!transport || !cap_data) {
215 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or cap_data");
216 }
217
218 return packet_send_via_transport(transport, PACKET_TYPE_CLIENT_CAPABILITIES, cap_data, cap_len, 0);
219}
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
Definition packet.h:419
asciichat_error_t packet_send_via_transport(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, uint32_t client_id)
Definition send.c:183

References ERROR_INVALID_PARAM, packet_send_via_transport(), PACKET_TYPE_CLIENT_CAPABILITIES, and SET_ERRNO.

◆ acip_send_image_frame()

asciichat_error_t acip_send_image_frame ( acip_transport_t * transport,
const void * pixel_data,
uint32_t width,
uint32_t height,
uint32_t pixel_format )

Send image frame to server (client → server).

Sends webcam video frame as RGB24 pixel data.

Parameters
transportTransport instance
pixel_dataRGB24 pixel data (width * height * 3 bytes)
widthFrame width in pixels
heightFrame height in pixels
pixel_formatPixel format (e.g., RGB24)
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 101 of file lib/network/acip/client.c.

101 {
102 ASCII_OTEL_SCOPE(ACIP_SEND_IMAGE_FRAME);
103 log_dev("★ ACIP_SEND_IMAGE_FRAME: Called with %ux%u, transport=%p, pixel_data=%p", width, height, (void *)transport, pixel_data);
104
105 if (!transport || !pixel_data) {
106 log_dev("★ ACIP_SEND_IMAGE_FRAME: Invalid params");
107 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or pixel_data");
108 }
109
110 // Calculate pixel data size (3 bytes per pixel for RGB24)
111 size_t pixel_size = (size_t)width * height * 3;
112 log_dev("★ ACIP_SEND_IMAGE_FRAME: pixel_size=%zu", pixel_size);
113
114 // Create image frame packet header
116 header.width = HOST_TO_NET_U32(width);
117 header.height = HOST_TO_NET_U32(height);
118 header.pixel_format = HOST_TO_NET_U32(pixel_format);
119 header.compressed_size = 0;
120 header.checksum = 0;
121 header.timestamp = 0;
122
123 socket_buffer_t slices[] = {{&header, sizeof(header)}, {pixel_data, pixel_size}};
124 return packet_send_via_transportv(transport, PACKET_TYPE_IMAGE_FRAME, slices, 2, 0);
125}
#define HOST_TO_NET_U32(val)
Definition endian.h:70
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:575
@ PACKET_TYPE_IMAGE_FRAME
Complete RGB image with dimensions.
Definition packet.h:399
asciichat_error_t packet_send_via_transportv(acip_transport_t *transport, packet_type_t type, const socket_buffer_t *payload, size_t count, uint32_t client_id)
Send arbitrary packet via transport (generic packet sender).
Definition send.c:150
Describes one compressed image sent through ACIP. width, height, and pixel_format tell the decoder ho...
Definition packet.h:948
uint32_t width
Image width in pixels.
Definition packet.h:950

References ASCII_OTEL_SCOPE, ERROR_INVALID_PARAM, height, HOST_TO_NET_U32, log_dev, packet_send_via_transportv(), PACKET_TYPE_IMAGE_FRAME, SET_ERRNO, image_frame_packet_t::width, and width.

Referenced by threaded_send_image_frame().

◆ acip_send_image_frame_h265()

asciichat_error_t acip_send_image_frame_h265 ( acip_transport_t * transport,
h265_encoder_t * encoder,
const void * pixel_data,
uint32_t width,
uint32_t height )

Send H.265-encoded video frame to server.

Send H.265-encoded video frame to server (client → server).

Encodes frame using H.265 codec and sends as PACKET_TYPE_IMAGE_FRAME_H265. Packet format: [flags:u8][width:u16][height:u16][h265_encoded_data...]

Parameters
transportACIP transport
encoderH.265 encoder instance
pixel_dataRGB pixel data from capture
widthFrame width in pixels
heightFrame height in pixels
Returns
Error code

Definition at line 140 of file lib/network/acip/client.c.

140 {
141 ASCII_OTEL_SCOPE(ACIP_SEND_IMAGE_FRAME_H265);
142 if (!transport || !encoder || !pixel_data) {
143 log_debug("★ ACIP_SEND_IMAGE_FRAME_H265: Invalid params");
144 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport, encoder, or pixel_data");
145 }
146
147 // Allocate output buffer for H.265 encoding
148 // H.265 can produce output larger than input due to codec overhead
149 // Use 2x multiplier to account for worst-case frame content
150 size_t output_buffer_size = 5 + (width * height * 2);
151 log_dev("[ACIP_SEND_H265_1] About to allocate output buffer (%zu bytes)", output_buffer_size);
152 uint8_t *output_buffer = buffer_alloc(NULL, output_buffer_size);
153 log_dev("[ACIP_SEND_H265_2] ✅ Allocated output buffer");
154 if (!output_buffer) {
155 log_debug("★ ACIP_SEND_IMAGE_FRAME_H265: Failed to allocate output buffer");
156 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate H.265 output buffer: %zu bytes", output_buffer_size);
157 }
158 defer(buffer_release(output_buffer));
159
160 // Admission expiry can invalidate a previously accepted reference chain.
161#ifndef __EMSCRIPTEN__
164#endif
165 size_t encoded_size = output_buffer_size;
166 log_dev("[ACIP_SEND_H265_3] Calling h265_encode");
167 asciichat_error_t encode_result = h265_encode(encoder, (uint16_t)width, (uint16_t)height, (const uint8_t *)pixel_data, output_buffer, &encoded_size);
168 log_dev("[ACIP_SEND_H265_4] h265_encode returned: %d, encoded_size=%zu", encode_result, encoded_size);
169
170 if (encode_result != ASCIICHAT_OK) {
171 log_error("H.265 encoding failed: %s", asciichat_error_format(NULL, encode_result));
172 return encode_result;
173 }
174
175 // h265_encode includes flags, dimensions, and the encoded picture already.
176 size_t total_size = encoded_size;
177 if (!total_size) {
178 return ASCIICHAT_OK;
179 }
180
181 log_debug("★ ACIP_SEND_IMAGE_FRAME_H265: Encoded %ux%u → %zu bytes", width, height, total_size);
182
183 // Send via transport
184 log_dev("[ACIP_SEND_H265_5] Calling packet_send_via_transport with %zu bytes", total_size);
185 asciichat_error_t result = packet_send_via_transport(transport, PACKET_TYPE_IMAGE_FRAME_H265, output_buffer, total_size, 0);
186 log_dev("[ACIP_SEND_H265_6] packet_send_via_transport returned: %d", result);
187
188 return result;
189}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
bool acip_outbound_needs_keyframe(acip_transport_t *transport, uint16_t type, uint32_t id)
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:272
#define buffer_release(data)
Definition buffer.h:99
asciichat_error_t h265_encode(h265_encoder_t *encoder, uint16_t width, uint16_t height, const uint8_t *ascii_data, uint8_t *output_buf, size_t *output_size)
Definition encoder.c:316
void h265_encoder_request_keyframe(h265_encoder_t *encoder)
Definition encoder.c:436
unsigned short uint16_t
unsigned char uint8_t
@ ERROR_MEMORY
Definition error_codes.h:55
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
@ PACKET_TYPE_IMAGE_FRAME_H265
H.265-encoded frame from client (HEVC/H.265 codec).
Definition packet.h:401

References acip_outbound_needs_keyframe(), ASCII_OTEL_SCOPE, asciichat_error_format(), ASCIICHAT_OK, buffer_alloc(), buffer_release, ERROR_INVALID_PARAM, ERROR_MEMORY, h265_encode(), h265_encoder_request_keyframe(), height, log_debug, log_dev, log_error, packet_send_via_transport(), PACKET_TYPE_IMAGE_FRAME_H265, SET_ERRNO, and width.

◆ acip_send_protocol_version()

asciichat_error_t acip_send_protocol_version ( acip_transport_t * transport,
const protocol_version_packet_t * version )

Send protocol version to server (client → server).

Negotiates protocol version with server.

Parameters
transportTransport instance
versionProtocol version structure
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 221 of file lib/network/acip/client.c.

221 {
222 if (!transport || !version) {
223 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or version");
224 }
225
226 return packet_send_via_transport(transport, PACKET_TYPE_PROTOCOL_VERSION, version, sizeof(*version), 0);
227}
@ PACKET_TYPE_PROTOCOL_VERSION
Protocol version and capabilities negotiation.
Definition packet.h:317

References ERROR_INVALID_PARAM, packet_send_via_transport(), PACKET_TYPE_PROTOCOL_VERSION, and SET_ERRNO.

◆ acip_send_stream_start()

asciichat_error_t acip_send_stream_start ( acip_transport_t * transport,
uint8_t stream_types )

Request to start media streaming (client → server).

Notifies server that client wants to start sending video/audio.

Parameters
transportTransport instance
stream_typesStream type bitmask (video, audio, etc.)
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 191 of file lib/network/acip/client.c.

191 {
192 ASCII_OTEL_SCOPE(ACIP_SEND_STREAM_START);
193 if (!transport) {
194 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport");
195 }
196
197 // Server expects uint32_t (4 bytes), not uint8_t
198 uint32_t stream_types_net = HOST_TO_NET_U32((uint32_t)stream_types);
199 return packet_send_via_transport(transport, PACKET_TYPE_STREAM_START, &stream_types_net, sizeof(stream_types_net), 0);
200}
unsigned int uint32_t
@ PACKET_TYPE_STREAM_START
Client requests to start sending video/audio.
Definition packet.h:429

References ASCII_OTEL_SCOPE, ERROR_INVALID_PARAM, HOST_TO_NET_U32, packet_send_via_transport(), PACKET_TYPE_STREAM_START, and SET_ERRNO.

◆ acip_send_stream_stop()

asciichat_error_t acip_send_stream_stop ( acip_transport_t * transport,
uint8_t stream_types )

Request to stop media streaming (client → server).

Notifies server that client wants to stop sending video/audio.

Parameters
transportTransport instance
stream_typesStream type bitmask (video, audio, etc.)
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 202 of file lib/network/acip/client.c.

202 {
203 ASCII_OTEL_SCOPE(ACIP_SEND_STREAM_STOP);
204 if (!transport) {
205 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport");
206 }
207
208 // Server expects uint32_t (4 bytes), not uint8_t
209 uint32_t stream_types_net = HOST_TO_NET_U32((uint32_t)stream_types);
210 return packet_send_via_transport(transport, PACKET_TYPE_STREAM_STOP, &stream_types_net, sizeof(stream_types_net), 0);
211}
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
Definition packet.h:431

References ASCII_OTEL_SCOPE, ERROR_INVALID_PARAM, HOST_TO_NET_U32, packet_send_via_transport(), PACKET_TYPE_STREAM_STOP, and SET_ERRNO.