ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
send.c
Go to the documentation of this file.
1
11
12/* Existing buffer budgets in bytes, including NUL for text storage. */
13#define ACIP_OPUS_INLINE_FRAME_CAPACITY 8
14
16#include <ascii-chat/otel.h>
23#include <ascii-chat/buffer.h>
28#include <recast/defer.h>
29#include <ascii-chat/log/log.h>
31#include <string.h>
32#include <math.h>
34#include <time.h>
35// =============================================================================
36// Packet Helper (wraps payload with header and sends via transport)
37// =============================================================================
38
52static asciichat_error_t backend_sendv_impl(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count, const acip_send_options_t *options) {
53 if (!transport || !transport->methods || !buffers || !count || count > SOCKET_IOV_MAX)
54 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport vectors");
55 size_t total = 0;
56 for (size_t i = 0; i < count; ++i) {
57 if ((buffers[i].len && !buffers[i].data) || buffers[i].len > SOCKET_SEND_BUFFER_MAX - total)
58 return SET_ERRNO(ERROR_NETWORK_SIZE, "Invalid transport vector size");
59 total += buffers[i].len;
60 }
61 if (transport->methods->sendv_ex)
62 return transport->methods->sendv_ex(transport, buffers, count, options);
63 if (transport->methods->sendv)
64 return transport->methods->sendv(transport, buffers, count);
65 // Message transports retain their existing queue and encryption contracts.
66 buffer_domain_t *domain = NULL;
67 if (buffers[0].len >= sizeof(packet_header_t)) {
68 packet_header_t header;
69 memcpy(&header, buffers[0].data, sizeof(header));
70 packet_type_t type = NET_TO_HOST_U16(header.type);
71 if (packet_is_handshake_type(type) || type == PACKET_TYPE_ERROR_MESSAGE)
72 domain = buffer_control_domain();
73 }
74 uint8_t *packet = buffer_alloc(domain, total);
75 if (!packet)
76 return SET_ERRNO(ERROR_MEMORY, "Cannot assemble transport packet");
77 defer(buffer_release(packet));
78 size_t offset = 0;
79 for (size_t i = 0; i < count; ++i) {
80 if (buffers[i].len)
81 memcpy(packet + offset, buffers[i].data, buffers[i].len);
82 offset += buffers[i].len;
83 }
84 if (!transport->methods->send) {
85 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Transport has no send method");
86 }
87 asciichat_error_t result = transport->methods->send(transport, packet, total);
88 return result;
89}
90
91static asciichat_error_t transport_sendv_impl(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count, const acip_send_options_t *options) {
92 if (transport && transport->crypto_ctx && transport->crypto_ctx->handshake_complete && transport->crypto_ctx->encrypt_data && !atomic_load_bool(&transport->packet_security_active)) {
93 asciichat_error_t result = acip_security_activate(transport);
94 if (result != ASCIICHAT_OK)
95 return result;
96 }
97 asciichat_error_t result = acip_security_flush(transport);
98 return result == ASCIICHAT_OK ? backend_sendv_impl(transport, buffers, count, options) : result;
99}
100
102 if (!out)
103 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing transport capabilities output");
105 if (!transport || !transport->methods)
106 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport capabilities query");
107 return transport->methods->get_capabilities ? transport->methods->get_capabilities(transport, out) : ASCIICHAT_OK;
108}
109
110asciichat_error_t acip_transport_sendv_ex(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count, const acip_send_options_t *options) {
111 if (!transport || !transport->methods || !buffers || !count || count > SOCKET_IOV_MAX || !options || (unsigned)options->traffic_class > ACIP_TRAFFIC_VISUAL ||
113 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport send options");
114 if (options->delivery != ACIP_DELIVERY_RELIABLE || options->deadline_ns) {
115 if (!transport->methods->sendv_ex || !transport->methods->validate_send_options)
116 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Datagrams and deadlines are not supported by this transport");
117 asciichat_error_t result = transport->methods->validate_send_options(transport, options);
118 if (result != ASCIICHAT_OK)
119 return result;
120 }
121#ifndef __EMSCRIPTEN__
122 if (transport && transport->outbound) {
123 acip_admission_t admission;
124 asciichat_error_t result = acip_outbound_submitv_ex(transport, buffers, count, options, &admission);
125 if (result != ASCIICHAT_OK)
126 return result;
127 if (admission != ACIP_ADMISSION_ACCEPTED)
128 return SET_ERRNO(ERROR_BUFFER_FULL, "Outbound media not admitted (%d)", admission);
129 return ASCIICHAT_OK;
130 }
131#endif
132 return transport_sendv_impl(transport, buffers, count, options);
133}
134
135asciichat_error_t acip_transport_sendv(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count) {
136 acip_send_options_t options = {.traffic_class = ACIP_TRAFFIC_CONTROL, .delivery = ACIP_DELIVERY_RELIABLE};
137 if (buffers && count && buffers[0].data && buffers[0].len >= sizeof(packet_header_t)) {
138 packet_header_t header;
139 memcpy(&header, buffers[0].data, sizeof(header));
141 }
142 return acip_transport_sendv_ex(transport, buffers, count, &options);
143}
144
145asciichat_error_t acip_transport_send(acip_transport_t *transport, const void *data, size_t len) {
146 socket_buffer_t buffer = {data, len};
147 return acip_transport_sendv(transport, &buffer, 1);
148}
149
150asciichat_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) {
151 ASCII_OTEL_SCOPE(PACKET_SEND_VIA_TRANSPORTV);
152 if (!transport || !payload || !count || count >= SOCKET_IOV_MAX)
153 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid packet vectors");
154 packet_header_t header;
155 asciichat_error_t result = packet_header_build(&header, type, payload, count, client_id);
156 if (result != ASCIICHAT_OK)
157 return result;
158 size_t len = NET_TO_HOST_U32(header.length);
159 socket_buffer_t slices[SOCKET_IOV_MAX] = {{&header, sizeof(header)}};
160 memcpy(slices + 1, payload, count * sizeof(*payload));
161 acip_send_options_t options = {.traffic_class = acip_packet_traffic_class(type), .delivery = ACIP_DELIVERY_RELIABLE};
162 bool encoded = type == PACKET_TYPE_IMAGE_FRAME_H264 || type == PACKET_TYPE_IMAGE_FRAME_H265;
163 bool prefer_datagram = (transport->prefer_audio_datagrams && options.traffic_class == ACIP_TRAFFIC_AUDIO) ||
164 (transport->prefer_visual_datagrams && options.traffic_class == ACIP_TRAFFIC_VISUAL && (!encoded || client_id == 0));
165 if (prefer_datagram) {
167 asciichat_error_t result = acip_transport_get_capabilities(transport, &caps);
168 if (result != ASCIICHAT_OK)
169 return result;
170 if (caps.datagrams) {
172 options.deadline_ns = time_get_ns() + 100 * NS_PER_MS_INT;
173 }
174 }
175 result = acip_transport_sendv_ex(transport, slices, count + 1, &options);
176 stats_runtime_packet(transport->stats_peer, type, len, true, result == ASCIICHAT_OK);
177#ifndef __EMSCRIPTEN__
178 ASCII_OTEL_PACKET(type, len, "send", acip_transport_get_type(transport));
179#endif
180 return result;
181}
182
183asciichat_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) {
184 socket_buffer_t slice = {payload, payload_len};
185 return packet_send_via_transportv(transport, type, &slice, 1, client_id);
186}
187
188// =============================================================================
189// Packet Receive Helper (receives via transport, parses header)
190// =============================================================================
191
192asciichat_error_t packet_receive_via_transport(acip_transport_t *transport, packet_type_t *type, void **payload, size_t *payload_len, void **alloc_buffer) {
193 ASCII_OTEL_SCOPE(PACKET_RECEIVE_VIA_TRANSPORT);
194 if (!transport || !type || !payload || !payload_len || !alloc_buffer) {
195 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
196 }
197
198 *type = 0;
199 *payload = NULL;
200 *payload_len = 0;
201 *alloc_buffer = NULL;
202
203receive_again:;
204 // Receive raw bytes via transport
205 void *recv_buffer = NULL;
206 size_t recv_len = 0;
207 asciichat_error_t result = acip_transport_recv(transport, &recv_buffer, &recv_len, alloc_buffer);
208 if (result == ERROR_NETWORK_INCOMPLETE)
209 goto receive_again;
210 if (result != ASCIICHAT_OK) {
211 if (result != ERROR_NETWORK_TIMEOUT)
212 stats_runtime_packet(transport->stats_peer, 0, 0, false, false);
213 return result;
214 }
215
216 // Parse packet header
217 if (recv_len < sizeof(packet_header_t)) {
218 log_warn("packet too small (%zu bytes, need %zu)", recv_len, sizeof(packet_header_t));
219 if (*alloc_buffer) {
220 buffer_release(*alloc_buffer);
221 *alloc_buffer = NULL;
222 }
223 stats_runtime_packet(transport->stats_peer, 0, 0, false, false);
224 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Received packet smaller than header");
225 }
226
227 packet_header_t wire_header;
228 result = packet_validate(recv_buffer, recv_len, 0, &wire_header);
229 if (result != ASCIICHAT_OK) {
230 buffer_release(*alloc_buffer);
231 *alloc_buffer = NULL;
232 return result;
233 }
234 if (NET_TO_HOST_U16(wire_header.type) == PACKET_TYPE_ENCRYPTED) {
235 packet_envelope_t envelope = {.type = PACKET_TYPE_ENCRYPTED,
236 .data = (uint8_t *)recv_buffer + sizeof(wire_header),
237 .len = recv_len - sizeof(wire_header),
238 .allocated_buffer = *alloc_buffer,
239 .allocated_size = *alloc_buffer ? buffer_capacity(*alloc_buffer) : 0};
240 result = packet_decrypt_envelope(&envelope, transport->crypto_ctx);
241 *alloc_buffer = envelope.allocated_buffer;
242 if (result != ASCIICHAT_OK || envelope.consumed) {
243 buffer_release(*alloc_buffer);
244 *alloc_buffer = NULL;
245 if (result == ASCIICHAT_OK)
246 goto receive_again;
247 return result;
248 }
249 recv_buffer = envelope.allocated_buffer;
250 recv_len = sizeof(packet_header_t) + envelope.len;
251 }
252 const packet_header_t *header = (const packet_header_t *)recv_buffer;
253 *type = (packet_type_t)NET_TO_HOST_U16(header->type);
254 size_t plen = NET_TO_HOST_U32(header->length);
255
256 if (recv_len < sizeof(packet_header_t) + plen) {
257 log_warn("truncated packet (header says %zu payload, got %zu total)", plen, recv_len);
258 if (*alloc_buffer) {
259 buffer_release(*alloc_buffer);
260 *alloc_buffer = NULL;
261 }
262 stats_runtime_packet(transport->stats_peer, 0, 0, false, false);
263 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Truncated packet payload");
264 }
265
266 if (plen > 0) {
267 *payload = (uint8_t *)recv_buffer + sizeof(packet_header_t);
268 }
269 *payload_len = plen;
270#ifndef __EMSCRIPTEN__
271 ASCII_OTEL_PACKET(*type, plen, "receive", acip_transport_get_type(transport));
272#endif
273 stats_runtime_packet(transport->stats_peer, *type, plen, false, true);
274
275 return ASCIICHAT_OK;
276}
277
278// ASCII/Video frame functions moved to:
279// - acip_send_ascii_frame → lib/network/acip/server.c (server → client)
280// - acip_send_image_frame → lib/network/acip/client.c (client → server)
281
282// =============================================================================
283// Audio Sending
284// =============================================================================
285
286asciichat_error_t acip_send_audio_batch(acip_transport_t *transport, const float *samples, uint32_t num_samples, uint32_t batch_count) {
287 ASCII_OTEL_SCOPE(ACIP_SEND_AUDIO_BATCH);
288 if (!transport || !samples || num_samples == 0) {
289 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
290 }
291
292 if (num_samples > (MAX_PACKET_SIZE - sizeof(audio_batch_packet_t)) / sizeof(uint32_t))
293 return SET_ERRNO(ERROR_NETWORK_SIZE, "PCM batch exceeds the packet size limit");
294
295 // The receiver consumes network-order signed PCM32, rather than host floats.
297 header.batch_count = HOST_TO_NET_U32(batch_count);
298 header.total_samples = HOST_TO_NET_U32(num_samples);
299 header.sample_rate = HOST_TO_NET_U32(AUDIO_SAMPLE_RATE);
300 header.channels = HOST_TO_NET_U32(1);
301 uint32_t inline_samples[OPUS_FRAME_SAMPLES];
302 uint32_t *wire = num_samples <= OPUS_FRAME_SAMPLES ? inline_samples : buffer_alloc(NULL, (size_t)num_samples * sizeof(uint32_t));
303 if (!wire)
304 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate PCM batch samples");
305 defer({
306 if (wire != inline_samples)
307 buffer_release(wire);
308 });
309 _Static_assert(sizeof(float) == sizeof(uint32_t), "ACIP audio requires 32-bit floats");
310 for (uint32_t i = 0; i < num_samples; ++i) {
311 // Inspect the exponent before arithmetic, including builds that assume finite floats.
312 uint32_t sample_bits;
313 memcpy(&sample_bits, &samples[i], sizeof(sample_bits));
314 if ((sample_bits & UINT32_C(0x7f800000)) == UINT32_C(0x7f800000)) {
315 return SET_ERRNO(ERROR_INVALID_PARAM, "PCM samples must be finite");
316 }
317 double sample = fmax(-1.0, fmin(1.0, samples[i]));
318 int32_t scaled = (int32_t)(sample * INT32_MAX);
319 wire[i] = HOST_TO_NET_U32((uint32_t)scaled);
320 }
321 socket_buffer_t slices[] = {{&header, sizeof(header)}, {wire, (size_t)num_samples * sizeof(uint32_t)}};
323 return result;
324}
325
326asciichat_error_t acip_send_audio_opus(acip_transport_t *transport, const void *opus_data, size_t opus_len) {
327 ASCII_OTEL_SCOPE(ACIP_SEND_AUDIO_OPUS);
328 if (!transport || !opus_data || opus_len == 0) {
329 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
330 }
331
332 // Convert single opus frame to batch format (frame_count=1) with standard parameters
333 // Standard: 48kHz sample rate, 20ms frame duration
334 uint16_t frame_sizes[1] = {(uint16_t)opus_len};
335 return acip_send_audio_opus_batch(transport, opus_data, opus_len, frame_sizes, 1, AUDIO_SAMPLE_RATE, CLIENT_AUDIO_PIPELINE_FRAME_MS);
336}
337
338asciichat_error_t acip_send_audio_opus_batch(acip_transport_t *transport, const void *opus_data, size_t opus_len, const uint16_t *frame_sizes, uint32_t frame_count, uint32_t sample_rate,
339 uint32_t frame_duration) {
340 ASCII_OTEL_SCOPE(ACIP_SEND_AUDIO_OPUS_BATCH);
341 if (!transport || !opus_data || !frame_sizes || frame_count == 0) {
342 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
343 }
344
345 // Build batch header (16 bytes)
347 *(uint32_t *)(header + 0) = HOST_TO_NET_U32(sample_rate);
348 *(uint32_t *)(header + 4) = HOST_TO_NET_U32(frame_duration);
349 *(uint32_t *)(header + 8) = HOST_TO_NET_U32(frame_count);
350 *(uint32_t *)(header + 12) = 0; // reserved
351
352 if (frame_count > MAX_PACKET_SIZE / sizeof(uint16_t) || opus_len > MAX_PACKET_SIZE)
353 return SET_ERRNO(ERROR_NETWORK_SIZE, "Opus batch too large");
354 size_t sizes_len = (size_t)frame_count * sizeof(uint16_t);
356 uint16_t *sizes = frame_count <= ACIP_OPUS_INLINE_FRAME_CAPACITY ? inline_sizes : buffer_alloc(NULL, sizes_len);
357 if (!sizes)
358 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate Opus lengths");
359 for (uint32_t i = 0; i < frame_count; ++i)
360 sizes[i] = HOST_TO_NET_U16(frame_sizes[i]);
361 socket_buffer_t slices[] = {{header, sizeof(header)}, {sizes, sizes_len}, {opus_data, opus_len}};
363 if (sizes != inline_sizes)
364 buffer_release(sizes);
365 return result;
366}
367
368// =============================================================================
369// Control/Signaling
370// =============================================================================
371
373 ASCII_OTEL_SCOPE(ACIP_SEND_PING);
374 if (!transport) {
375 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport");
376 }
377
378 // Ping has no payload
379 return packet_send_via_transport(transport, PACKET_TYPE_PING, NULL, 0, 0);
380}
381
383 if (!transport) {
384 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport");
385 }
386
387 // Pong has no payload
388 return packet_send_via_transport(transport, PACKET_TYPE_PONG, NULL, 0, 0);
389}
390
391// Client control functions moved to lib/network/acip/client.c:
392// - acip_send_stream_start, acip_send_stream_stop
393// - acip_send_capabilities, acip_send_protocol_version
394//
395// Server control functions moved to lib/network/acip/server.c:
396// - acip_send_clear_console, acip_send_server_state
397
398// =============================================================================
399// Messages/Errors
400// =============================================================================
401
402asciichat_error_t acip_send_error(acip_transport_t *transport, uint32_t error_code, const char *message) {
403 ASCII_OTEL_SCOPE(ACIP_SEND_ERROR);
404 if (!transport) {
405 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport");
406 }
407
408 error_code = network_error_public_code((asciichat_error_t)error_code);
409 // Error packets can be sent before authentication. Never let a caller's
410 // context or source locations cross the network boundary.
411 (void)message;
412 message = asciichat_error_string((asciichat_error_t)error_code);
413
414 // Calculate message length (max MAX_ERROR_MESSAGE_LENGTH)
415 size_t msg_len = 0;
416 if (message) {
417 msg_len = strlen(message);
418 if (msg_len > MAX_ERROR_MESSAGE_LENGTH) {
419 msg_len = MAX_ERROR_MESSAGE_LENGTH;
420 }
421 }
422
423 error_packet_t header;
424 header.error_code = HOST_TO_NET_U32(error_code);
425 header.message_length = HOST_TO_NET_U32((uint32_t)msg_len);
426
427 // Allocate buffer for header + message
428 size_t total_size = sizeof(header) + msg_len;
429 uint8_t *buffer = buffer_alloc(buffer_control_domain(), total_size);
430 if (!buffer) {
431 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate buffer");
432 }
433
434 // Build packet
435 memcpy(buffer, &header, sizeof(header));
436 if (msg_len > 0) {
437 memcpy(buffer + sizeof(header), message, msg_len);
438 }
439
440 asciichat_error_t result = packet_send_via_transport(transport, PACKET_TYPE_ERROR_MESSAGE, buffer, total_size, 0);
441
442 buffer_release(buffer);
443 return result;
444}
445
446asciichat_error_t acip_send_remote_log(acip_transport_t *transport, uint8_t log_level, uint8_t direction, const char *message) {
447 if (!transport || !message) {
448 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or message");
449 }
450
451 size_t msg_len = strlen(message);
452
453 remote_log_packet_t header;
454 header.log_level = log_level;
455 header.direction = direction;
456 header.flags = 0;
457 header.message_length = HOST_TO_NET_U32((uint32_t)msg_len);
458
459 size_t total_size = sizeof(header) + msg_len;
460
461 uint8_t *buffer = buffer_alloc(NULL, total_size);
462 if (!buffer) {
463 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate buffer");
464 }
465
466 // Build packet
467 memcpy(buffer, &header, sizeof(header));
468 // NOLINTNEXTLINE(bugprone-not-null-terminated-result) - binary packet data
469 memcpy(buffer + sizeof(header), message, msg_len);
470
471 asciichat_error_t result = packet_send_via_transport(transport, PACKET_TYPE_REMOTE_LOG, buffer, total_size, 0);
472
473 buffer_release(buffer);
474 return result;
475}
476
477// =============================================================================
478// ACDS (Discovery Server) Response Sending
479// =============================================================================
480
482 if (!transport || !response) {
483 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or response");
484 }
485
486 return packet_send_via_transport(transport, PACKET_TYPE_ACIP_SESSION_CREATED, response, sizeof(*response), 0);
487}
488
490 if (!transport || !info) {
491 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or info");
492 }
493
494 return packet_send_via_transport(transport, PACKET_TYPE_ACIP_SESSION_INFO, info, sizeof(*info), 0);
495}
496
498 if (!transport || !response) {
499 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid transport or response");
500 }
501
502 if (!response->success)
504 return packet_send_via_transport(transport, PACKET_TYPE_ACIP_SESSION_JOINED, response, sizeof(*response), 0);
505}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
#define OPUS_FRAME_SAMPLES
#define CLIENT_AUDIO_PIPELINE_FRAME_MS
#define AUDIO_SAMPLE_RATE
Definition audio/limits.h:8
asciichat_error_t acip_outbound_submitv_ex(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count, const acip_send_options_t *options, acip_admission_t *admission)
acip_admission_t
@ ACIP_ADMISSION_ACCEPTED
size_t buffer_capacity(const void *data)
Definition buffer.c:278
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:272
buffer_domain_t * buffer_control_domain(void)
Definition buffer.c:432
struct buffer_domain buffer_domain_t
Definition buffer.h:28
#define buffer_release(data)
Definition buffer.h:99
Hardware-Accelerated CRC32 Checksum Computation.
🔄 Network byte order conversion helpers
#define HOST_TO_NET_U16(val)
Definition endian.h:100
#define HOST_TO_NET_U32(val)
Definition endian.h:70
#define NET_TO_HOST_U16(val)
Definition endian.h:115
#define NET_TO_HOST_U32(val)
Definition endian.h:85
unsigned short uint16_t
unsigned int uint32_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NETWORK_INCOMPLETE
Definition error_codes.h:94
@ ERROR_BUFFER_FULL
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:80
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_NETWORK_SIZE
Definition error_codes.h:81
@ ERROR_INVALID_PARAM
@ ERROR_NETWORK_TIMEOUT
Definition error_codes.h:79
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
acip_session_joined_t
Definition acds.h:479
acip_session_created_t
Definition acds.h:274
acip_session_info_t
Definition acds.h:360
#define NS_PER_MS_INT
Definition time.h:172
asciichat_error_t packet_validate(const void *packet, size_t length, size_t limit, packet_header_t *output)
Definition packet.c:81
asciichat_error_t packet_decrypt_envelope(packet_envelope_t *envelope, void *crypto_ctx)
Send a clear console packet.
Definition packet.c:639
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
#define MAX_PACKET_SIZE
Maximum packet size (5MB).
Definition packet.h:144
asciichat_error_t packet_header_build(packet_header_t *header, packet_type_t type, const socket_buffer_t *payload, size_t count, uint32_t client_id)
Definition packet.c:47
#define MAX_ERROR_MESSAGE_LENGTH
Maximum error message length (512 bytes).
Definition packet.h:153
#define ACIP_OPUS_BATCH_HEADER_SIZE
Definition packet.h:81
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
Definition packet.h:412
@ PACKET_TYPE_ACIP_SESSION_INFO
Session info response (Discovery Server -> Client).
Definition packet.h:451
@ PACKET_TYPE_PONG
Keepalive pong response.
Definition packet.h:423
@ PACKET_TYPE_ENCRYPTED
Encrypted packet (after handshake completion).
Definition packet.h:369
@ PACKET_TYPE_REMOTE_LOG
Bidirectional remote logging packet.
Definition packet.h:390
@ PACKET_TYPE_IMAGE_FRAME_H265
H.265-encoded frame from client (HEVC/H.265 codec).
Definition packet.h:401
@ PACKET_TYPE_ACIP_SESSION_CREATED
Session created response (Discovery Server -> Client).
Definition packet.h:447
@ PACKET_TYPE_IMAGE_FRAME_H264
Definition packet.h:403
@ PACKET_TYPE_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
Definition packet.h:388
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:421
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
Definition packet.h:410
@ PACKET_TYPE_ACIP_SESSION_JOINED
Session joined response (Discovery Server -> Client).
Definition packet.h:455
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
🔊 Audio Capture and Playback Interface for ascii-chat
Transport abstraction layer for ACIP protocol.
Network error handling utilities.
asciichat_error_t network_error_public_code(asciichat_error_t code)
void stats_counter_add(stats_scope_t *scope, stats_counter_id_t id, uint64_t amount)
📝 Logging API with multiple log levels and terminal output control
🔢 Mathematical Utility Functions
asciichat_error_t acip_security_activate(acip_transport_t *transport)
asciichat_error_t acip_security_flush(acip_transport_t *transport)
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
#define ASCII_OTEL_PACKET(type, size, action, transport)
Definition otel.h:73
✅ Safe Integer Arithmetic and Overflow Detection
static bool size_t gotel_query_t * out
Definition queries.h:8
void stats_runtime_packet(stats_peer_t *peer, unsigned type, size_t payload_bytes, bool sent, bool success)
Definition runtime.c:192
stats_scope_t * stats_runtime_scope(void)
Definition runtime.c:78
asciichat_error_t acip_transport_sendv_ex(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count, const acip_send_options_t *options)
Definition send.c:110
asciichat_error_t acip_send_audio_opus(acip_transport_t *transport, const void *opus_data, size_t opus_len)
Send Opus-encoded audio packet.
Definition send.c:326
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
#define ACIP_OPUS_INLINE_FRAME_CAPACITY
Definition send.c:13
asciichat_error_t acip_send_ping(acip_transport_t *transport)
Send ping packet.
Definition send.c:372
asciichat_error_t acip_send_session_joined(acip_transport_t *transport, const acip_session_joined_t *response)
Send SESSION_JOINED response packet.
Definition send.c:497
asciichat_error_t acip_send_remote_log(acip_transport_t *transport, uint8_t log_level, uint8_t direction, const char *message)
Send remote log packet.
Definition send.c:446
asciichat_error_t acip_transport_get_capabilities(acip_transport_t *transport, acip_transport_capabilities_t *out)
Definition send.c:101
asciichat_error_t acip_send_error(acip_transport_t *transport, uint32_t error_code, const char *message)
Send error message packet.
Definition send.c:402
asciichat_error_t acip_send_session_created(acip_transport_t *transport, const acip_session_created_t *response)
Send SESSION_CREATED response packet.
Definition send.c:481
asciichat_error_t acip_send_pong(acip_transport_t *transport)
Send pong packet.
Definition send.c:382
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
asciichat_error_t acip_send_audio_opus_batch(acip_transport_t *transport, const void *opus_data, size_t opus_len, const uint16_t *frame_sizes, uint32_t frame_count, uint32_t sample_rate, uint32_t frame_duration)
Send batched Opus-encoded audio frames.
Definition send.c:338
asciichat_error_t acip_transport_sendv(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count)
Send data through transport.
Definition send.c:135
asciichat_error_t acip_send_audio_batch(acip_transport_t *transport, const float *samples, uint32_t num_samples, uint32_t batch_count)
Send audio batch packet.
Definition send.c:286
asciichat_error_t acip_send_session_info(acip_transport_t *transport, const acip_session_info_t *info)
Send SESSION_INFO response packet.
Definition send.c:489
asciichat_error_t acip_transport_send(acip_transport_t *transport, const void *data, size_t len)
Definition send.c:145
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
ACIP shared/bidirectional packet sending functions.
🔤 String Manipulation and Shell Escaping Utilities
uint64_t deadline_ns
Definition traffic.h:23
acip_delivery_t delivery
Definition traffic.h:22
acip_traffic_class_t traffic_class
Definition traffic.h:21
The useful part of acip transport capabilities is in independent_streams, datagrams,...
asciichat_error_t(* validate_send_options)(acip_transport_t *transport, const acip_send_options_t *options)
asciichat_error_t(* send)(acip_transport_t *transport, const void *data, size_t len)
Send data through this transport.
asciichat_error_t(* sendv)(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count)
Custom destroy implementation (optional).
asciichat_error_t(* sendv_ex)(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count, const acip_send_options_t *options)
asciichat_error_t(* get_capabilities)(acip_transport_t *transport, acip_transport_capabilities_t *out)
struct acip_outbound * outbound
Per-connection plaintext admission and scheduling.
atomic_t packet_security_active
Publishes the stable post-handshake crypto owner.
bool prefer_visual_datagrams
Select negotiated visual datagrams when supported.
stats_peer_t * stats_peer
Owned statistics handle, destroyed with transport.
const acip_transport_methods_t * methods
Method table (virtual functions).
crypto_context_t * crypto_ctx
Optional encryption context.
bool prefer_audio_datagrams
Select negotiated datagrams for audio, otherwise reliable delivery.
The metadata prefix for a batch of encoded audio samples. batch_count, total_samples,...
Definition packet.h:979
uint32_t batch_count
Number of audio chunks in this batch (usually AUDIO_BATCH_COUNT = 32).
Definition packet.h:981
Error packet structure carrying error code and textual description.
Definition packet.h:778
uint32_t error_code
Error code from asciichat_error_t enumeration.
Definition packet.h:780
Packet envelope containing received packet data.
Definition packet.h:1204
Fixed prefix shared by every ACIP packet on the wire.
Definition packet.h:641
uint16_t type
Packet type (packet_type_t enumeration).
Definition packet.h:645
Remote log packet structure carrying log level and message text.
Definition packet.h:792
uint8_t log_level
Log level associated with the message (log_level_t cast to uint8_t).
Definition packet.h:794
⏱️ High-precision timing utilities using sokol_time.h and uthash
acip_traffic_class_t acip_packet_traffic_class(uint16_t type)
Definition traffic.c:4
@ ACIP_TRAFFIC_CONTROL
Definition traffic.h:10
@ ACIP_TRAFFIC_VISUAL
Definition traffic.h:12
@ ACIP_TRAFFIC_AUDIO
Definition traffic.h:11
@ ACIP_DELIVERY_RELIABLE
Definition traffic.h:16
@ ACIP_DELIVERY_DATAGRAM
Definition traffic.h:17