ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
src/client/connection.c
Go to the documentation of this file.
1#include "../main.h"
2#include "audio.h"
4#include "crypto.h"
5#include "protocol_adapter.h"
6#include "server.h"
11#include <ascii-chat/otel.h>
14#include <libplatform/system.h>
15#include <options/options.h>
16#include <string.h>
17
18static void client_sync_entries(client_connection_t *ctx, bool add) {
19 if (add)
21 "client.display_first_frame", NULL);
22 else
24 if (add)
25 NAMED_REGISTER_ATOMIC(&ctx->lifecycle.state, "client.lifecycle.state",
26 NULL);
27 else
29 if (add)
30 NAMED_REGISTER_ATOMIC(&ctx->stop_requested, "client.stop_requested", NULL);
31 else
33 if (add)
34 NAMED_REGISTER_ATOMIC(&ctx->connection_active, "client.connection_active",
35 NULL);
36 else
38 if (add)
39 NAMED_REGISTER_ATOMIC(&ctx->remote_error, "client.remote_error", NULL);
40 else
42 if (add)
43 NAMED_REGISTER_ATOMIC(&ctx->connection_lost, "client.connection_lost",
44 NULL);
45 else
47 if (add)
48 NAMED_REGISTER_ATOMIC(&ctx->data_thread_exited, "client.data_thread_exited",
49 NULL);
50 else
52 if (add)
53 NAMED_REGISTER_ATOMIC(&ctx->frames_rendered, "client.frames_rendered",
54 NULL);
55 else
57 if (add)
59 "client.snapshot_first_frame_time_ns", NULL);
60 else
62 if (add)
64 "client.capture_thread_exited", NULL);
65 else
67 if (add)
69 "client.audio_capture_thread_exited", NULL);
70 else
72 if (add)
74 "client.audio_sender_should_exit", NULL);
75 else
77 if (add)
79 "client.audio_sender_exited", NULL);
80 else
82 if (add)
83 NAMED_REGISTER_ATOMIC(&ctx->ping_thread_exited, "client.ping_thread_exited",
84 NULL);
85 else
87}
88
89static void client_interrupt(void *context);
90static asciichat_error_t client_join(void *context);
91static void client_release(void *context);
92
94#ifndef NDEBUG
95 const char *failure = SAFE_GETENV("ASCII_CHAT_TEST_CLIENT_FAIL_STAGE");
96 if (failure && strcmp(failure, stage) == 0) {
97 log_debug("Injected client startup failure after %s", stage);
98 return SET_ERRNO(ERROR_THREAD, "Injected client startup failure after %s",
99 stage);
100 }
101#else
102 (void)stage;
103#endif
104 return ASCIICHAT_OK;
105}
106
108 if (!out) {
109 return SET_ERRNO(ERROR_INVALID_STATE, "Missing client connection output");
110 }
112 if (!*out)
113 return SET_ERRNO(ERROR_MEMORY, "Could not allocate client connection");
114 client_connection_t *connection = *out;
115 client_sync_entries(connection, true);
117 &connection->lifecycle,
118 (session_connection_ops_t){client_interrupt, client_join, client_release},
119 connection);
120 connection->crypto_mode = ACIP_CRYPTO_ENCRYPT;
122 if (result == ASCIICHAT_OK && server_connection_init(connection) != 0)
123 result = ERROR_THREAD;
124 if (result == ASCIICHAT_OK)
125 result = client_connection_checkpoint("send-mutex");
126 if (result == ASCIICHAT_OK) {
127 connection->client_worker_pool = thread_pool_create("client_connection");
128 if (!connection->client_worker_pool)
129 result =
130 SET_ERRNO(ERROR_THREAD, "Could not create connection worker pool");
131 }
132 if (result == ASCIICHAT_OK)
133 result = client_connection_checkpoint("worker-pool");
134 if (result != ASCIICHAT_OK) {
136 if (cleanup != ASCIICHAT_OK)
137 return cleanup;
138 }
139 if (result == ASCIICHAT_OK)
140 log_info("CLIENT_CONNECTION_CREATED: workers=0 frames=0");
141 return result;
142}
143
144static void client_detach(void *context) {
145 client_connection_t *connection = context;
146 atomic_ptr_store(&connection->transport, NULL);
147 connection->crypto_ctx = NULL;
148}
149
150static asciichat_error_t client_prepare(network_connection_t *network,
151 const connection_endpoint_t *endpoint,
152 const char *password, void *context) {
153 client_connection_t *connection = context;
154 atomic_ptr_store(&connection->transport,
156 server_connection_set_ip(connection, endpoint->host);
157 server_connection_set_port(connection, endpoint->port);
159 asciichat_error_t result = client_crypto_init(connection, password);
160 return result;
161}
162
164 const char *address, uint16_t port,
165 char *password,
166 size_t password_capacity) {
167 ASCII_OTEL_SCOPE(CONNECTION_ATTEMPT);
168 if (!password || password_capacity < PASSWORD_BUFFER_SIZE)
169 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing session password storage");
170 if (!connection || atomic_load_int(&connection->lifecycle.state) !=
173 "Connection is not ready for startup");
174 }
175 network_connect_config_t quic_config = {
176 .timeout_ms = 10000,
177 .tls_ca_file =
178 GET_OPTION(tls_ca_file)[0] ? GET_OPTION(tls_ca_file) : NULL,
179 .quic_audio_datagrams = GET_OPTION(quic_audio_datagrams),
180 .quic_visual_datagrams = GET_OPTION(quic_visual_datagrams)};
181 const network_connect_config_t *connect_config =
182 address && strncmp(address, "quic://", 7) == 0 ? &quic_config : NULL;
185 "client", address, port, connect_config, &connection->network, password,
186 password_capacity, client_prepare, client_detach, connection);
187 atomic_ptr_store(&connection->transport,
189 connection->crypto_ctx =
190 connection->network ? network_connection_handshake(connection->network)
191 : NULL;
192 if (result != ASCIICHAT_OK)
193 return result;
195 if (should_exit())
196 return SET_ERRNO(ERROR_NETWORK, "Connection startup interrupted");
198 atomic_ptr_load(&connection->transport));
199 result = client_connection_checkpoint("transport");
200 if (result != ASCIICHAT_OK)
201 return result;
202 result = client_protocol_start(connection);
203 if (result != ASCIICHAT_OK)
204 return result;
205 if (atomic_load_bool(&connection->stop_requested))
206 return SET_ERRNO(ERROR_NETWORK, "Connection stopped during startup");
207 result = session_connection_start(&connection->lifecycle);
208 if (result != ASCIICHAT_OK)
209 return result;
211 log_info("CLIENT_CONNECTION_RUNNING");
212 return ASCIICHAT_OK;
213}
214
216 if (connection)
218}
219
220static void client_interrupt(void *context) {
221 client_connection_t *connection = context;
222 atomic_store_bool(&connection->stop_requested, true);
223 server_connection_shutdown(connection);
224}
225
227 return connection ? session_connection_stop(&connection->lifecycle)
228 : ASCIICHAT_OK;
229}
230
231static asciichat_error_t client_join(void *context) {
232 client_connection_t *connection = context;
233 log_debug("CLIENT_CONNECTION_IO_INTERRUPTED");
234 audio_sender_cleanup(connection);
235 audio_stop_thread(connection);
236 audio_context_t *audio = connection->audio_context;
237 if (audio && audio->initialized)
238 audio_stop_duplex(audio);
239
240 /* Pool entries are retained on join failure. Nothing below may free a
241 * dependency until every worker has actually joined. */
242 if (connection->client_worker_pool) {
243 asciichat_error_t result =
245 if (result != ASCIICHAT_OK)
246 return result;
247 }
248 log_info("CLIENT_CONNECTION_STOPPED: workers=0 frames=%llu",
249 (unsigned long long)atomic_load_u64(&connection->frames_rendered));
250 return ASCIICHAT_OK;
251}
252
253static void client_release(void *context) {
254 client_connection_t *connection = context;
255 audio_context_t *audio = connection->audio_context;
256 audio_sender_finalize(connection);
258 if (audio && audio->initialized) {
259 audio_set_pipeline(audio, NULL);
262 audio->capture_media_source = NULL;
263 }
265 connection->audio_pipeline = NULL;
267 connection->video_encoder = NULL;
270 /* The shared owner drains callbacks before releasing transport and security.
271 */
272 log_debug("CLIENT_CONNECTION_TRANSPORT_CLEANUP");
274 log_debug("CLIENT_CONNECTION_TRANSPORT_RELEASED");
275 atomic_ptr_store(&connection->transport, NULL);
276 connection->crypto_ctx = NULL;
277}
278
280 if (!owner || !*owner)
281 return ASCIICHAT_OK;
282 client_connection_t *connection = *owner;
283 asciichat_error_t result = client_connection_stop(connection);
284 if (result != ASCIICHAT_OK)
285 return result;
286 if (connection->client_worker_pool)
288 if (connection->send_mutex_initialized)
289 mutex_destroy(&connection->send_mutex);
290 log_info("CLIENT_CONNECTION_RELEASED");
291 client_sync_entries(connection, false);
292 SAFE_FREE(*owner);
293 *owner = NULL;
294 return ASCIICHAT_OK;
295}
#define SET_ERRNO(code,...)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
void atomic_ptr_store(atomic_ptr_t *a, void *value)
Atomically store a pointer.
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
void * atomic_ptr_load(atomic_ptr_t *a)
Atomically load a pointer.
uint64_t atomic_load_u64(const atomic_t *a)
Atomically load a uint64_t value.
int atomic_load_int(atomic_t *a)
Atomically load an int value.
void buffer_workspace_destroy(buffer_workspace_t *workspace)
Definition buffer.c:398
void client_audio_pipeline_destroy(client_audio_pipeline_t *pipeline)
Destroy a client audio pipeline.
void session_capture_set_audio_context(session_capture_ctx_t *ctx, void *audio_ctx)
Set the audio context for playback control.
Named object registry for debugging — log identifiable resource names.
void h265_encoder_destroy(h265_encoder_t *encoder)
Definition encoder.c:178
asciichat_error_t audio_stop_duplex(audio_context_t *ctx)
Stop full-duplex audio.
void audio_flush_playback_buffers(audio_context_t *ctx)
Flush all audio buffers to synchronize with seek operations.
void audio_set_pipeline(audio_context_t *ctx, void *pipeline)
Set audio pipeline for echo cancellation.
void audio_ring_buffer_clear(audio_ring_buffer_t *rb)
Clear all audio samples from ring buffer.
unsigned short uint16_t
#define SAFE_FREE(ptr)
#define SAFE_GETENV(name)
#define SAFE_CALLOC(count, size, cast)
#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr)
Register an atomic_t with automatic format specifier.
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_NETWORK
Definition error_codes.h:77
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ ERROR_THREAD
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define ACIP_CRYPTO_ENCRYPT
Encryption only (no identity verification).
Definition packet.h:1162
#define GET_OPTION(field)
Definition options.h:1339
🔊 Audio Capture and Playback Interface for ascii-chat
Transport abstraction layer for ACIP protocol.
struct network_connection network_connection_t
void network_connection_destroy(network_connection_t **owner)
crypto_handshake_context_t * network_connection_handshake(network_connection_t *c)
acip_transport_t * network_connection_transport(network_connection_t *c)
📝 Logging API with multiple log levels and terminal output control
bool should_exit(void)
Definition main.c:260
Global application exit and signal handling API.
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:102
Password prompting utilities with secure input and formatting.
asciichat_error_t client_protocol_start(client_connection_t *connection)
static bool size_t gotel_query_t * out
Definition queries.h:10
#define PASSWORD_BUFFER_SIZE
Password input buffer size (256 bytes).
Definition sizes.h:168
void audio_sender_cleanup(client_connection_t *connection)
Cleanup async audio sender.
void audio_stop_thread(client_connection_t *connection)
Stop audio capture thread.
void audio_sender_finalize(client_connection_t *connection)
ascii-chat Client Audio Processing Management Interface
asciichat_error_t client_connection_stop(client_connection_t *connection)
asciichat_error_t client_connection_destroy(client_connection_t **owner)
void client_connection_request_stop(client_connection_t *connection)
asciichat_error_t client_connection_start(client_connection_t *connection, const char *address, uint16_t port, char *password, size_t password_capacity)
asciichat_error_t client_connection_checkpoint(const char *stage)
asciichat_error_t client_connection_create(client_connection_t **out)
struct client_connection client_connection_t
asciichat_error_t client_crypto_init(client_connection_t *connection, const char *password)
void server_connection_set_transport(client_connection_t *connection, acip_transport_t *transport)
Set ACIP transport instance from connection fallback.
void server_connection_shutdown(client_connection_t *connection)
Interrupt connection I/O from normal thread context.
int server_connection_init(client_connection_t *connection)
Initialize the server connection management subsystem.
void server_connection_set_port(client_connection_t *connection, int port)
Set the server port.
void server_connection_set_ip(client_connection_t *connection, const char *ip)
Set the server IP address.
ascii-chat Client Server Connection Management Interface
asciichat_error_t session_connection_stop(session_connection_t *connection)
void session_connection_request_stop(session_connection_t *connection)
void session_connection_init(session_connection_t *connection, session_connection_ops_t ops, void *context)
asciichat_error_t session_connection_start(session_connection_t *connection)
asciichat_error_t session_connection_establish(const char *name, const char *address, uint16_t port, const network_connect_config_t *config, network_connection_t **owner, char *password, size_t capacity, network_connection_prepare_fn prepare, void(*detach)(void *), void *context)
@ SESSION_CONNECTION_STARTING
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with audio device info, it keeps index, max_input_channels,...
bool initialized
True if context has been initialized.
audio_ring_buffer_t * capture_buffer
network_connection_t * network
client_audio_pipeline_t * audio_pipeline
crypto_handshake_context_t * crypto_ctx
thread_pool_t * client_worker_pool
atomic_t snapshot_first_frame_time_ns
buffer_workspace_t encoded_video_workspace
session_capture_ctx_t * capture_context
atomic_t audio_capture_thread_exited
buffer_workspace_t decoded_ascii_workspace
h265_encoder_t *volatile video_encoder
audio_context_t * audio_context
session_connection_t lifecycle
For network connect config, this is the live record that ties timeout_ms, callback_data,...
void thread_pool_destroy(thread_pool_t *pool)
Destroy a thread pool.
thread_pool_t * thread_pool_create(const char *pool_name)
Create a new thread pool (long-lived threads mode).
Definition thread_pool.c:96
asciichat_error_t thread_pool_stop_all(thread_pool_t *pool)
Stop all threads in the pool in stop_id order.
void ui_controller_title_activity(ui_title_activity_t activity)
@ UI_TITLE_CONNECTING
@ UI_TITLE_NEGOTIATING
@ UI_TITLE_HANDSHAKE
@ UI_TITLE_RECEIVING