ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
connection.h File Reference

Go to the source code of this file.

Typedefs

typedef struct client_connection client_connection_t

Functions

asciichat_error_t client_connection_create (client_connection_t **out)
asciichat_error_t client_connection_start (client_connection_t *connection, const char *address, uint16_t port, char *password, size_t password_capacity)
void client_connection_request_stop (client_connection_t *connection)
asciichat_error_t client_connection_stop (client_connection_t *connection)
asciichat_error_t client_connection_destroy (client_connection_t **connection)
asciichat_error_t client_connection_checkpoint (const char *stage)

Typedef Documentation

◆ client_connection_t

Definition at line 8 of file src/client/connection.h.

Function Documentation

◆ client_connection_checkpoint()

asciichat_error_t client_connection_checkpoint ( const char * stage)

Definition at line 93 of file src/client/connection.c.

93 {
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}
#define SET_ERRNO(code,...)
#define SAFE_GETENV(name)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_THREAD
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565

References ASCIICHAT_OK, ERROR_THREAD, log_debug, SAFE_GETENV, and SET_ERRNO.

Referenced by audio_sender_init(), client_connection_create(), client_connection_start(), client_crypto_init(), and client_protocol_start().

◆ client_connection_create()

asciichat_error_t client_connection_create ( client_connection_t ** out)

Definition at line 107 of file src/client/connection.c.

107 {
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}
#define SAFE_CALLOC(count, size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define ACIP_CRYPTO_ENCRYPT
Encryption only (no identity verification).
Definition packet.h:1162
static bool size_t gotel_query_t * out
Definition queries.h:10
asciichat_error_t client_connection_destroy(client_connection_t **owner)
asciichat_error_t client_connection_checkpoint(const char *stage)
struct client_connection client_connection_t
int server_connection_init(client_connection_t *connection)
Initialize the server connection management subsystem.
void session_connection_init(session_connection_t *connection, session_connection_ops_t ops, void *context)
thread_pool_t * client_worker_pool
session_connection_t lifecycle
thread_pool_t * thread_pool_create(const char *pool_name)
Create a new thread pool (long-lived threads mode).
Definition thread_pool.c:96

References ACIP_CRYPTO_ENCRYPT, ASCIICHAT_OK, client_connection_checkpoint(), client_connection_destroy(), client_connection::client_worker_pool, client_connection::crypto_mode, ERROR_INVALID_STATE, ERROR_MEMORY, ERROR_THREAD, client_connection::lifecycle, log_info, out, SAFE_CALLOC, server_connection_init(), session_connection_init(), SET_ERRNO, and thread_pool_create().

◆ client_connection_destroy()

asciichat_error_t client_connection_destroy ( client_connection_t ** connection)

Definition at line 279 of file src/client/connection.c.

279 {
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 SAFE_FREE(ptr)
asciichat_error_t client_connection_stop(client_connection_t *connection)
void thread_pool_destroy(thread_pool_t *pool)
Destroy a thread pool.

References ASCIICHAT_OK, client_connection_stop(), client_connection::client_worker_pool, log_info, SAFE_FREE, client_connection::send_mutex, client_connection::send_mutex_initialized, and thread_pool_destroy().

Referenced by client_connection_create().

◆ client_connection_request_stop()

void client_connection_request_stop ( client_connection_t * connection)

Definition at line 215 of file src/client/connection.c.

215 {
216 if (connection)
218}
void session_connection_request_stop(session_connection_t *connection)

References client_connection::lifecycle, and session_connection_request_stop().

◆ client_connection_start()

asciichat_error_t client_connection_start ( client_connection_t * connection,
const char * address,
uint16_t port,
char * password,
size_t password_capacity )

Reuse caller-owned session credentials across attempts; the owner wipes them at session shutdown.

Definition at line 163 of file src/client/connection.c.

166 {
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}
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.
int atomic_load_int(atomic_t *a)
Atomically load an int value.
@ ERROR_NETWORK
Definition error_codes.h:77
@ ERROR_INVALID_PARAM
#define GET_OPTION(field)
Definition options.h:1339
crypto_handshake_context_t * network_connection_handshake(network_connection_t *c)
acip_transport_t * network_connection_transport(network_connection_t *c)
bool should_exit(void)
Definition main.c:260
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:102
asciichat_error_t client_protocol_start(client_connection_t *connection)
#define PASSWORD_BUFFER_SIZE
Password input buffer size (256 bytes).
Definition sizes.h:168
void server_connection_set_transport(client_connection_t *connection, acip_transport_t *transport)
Set ACIP transport instance from connection fallback.
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
network_connection_t * network
crypto_handshake_context_t * crypto_ctx
For network connect config, this is the live record that ties timeout_ms, callback_data,...
void ui_controller_title_activity(ui_title_activity_t activity)
@ UI_TITLE_CONNECTING
@ UI_TITLE_NEGOTIATING
@ UI_TITLE_RECEIVING

References ASCII_OTEL_SCOPE, ASCIICHAT_OK, atomic_load_bool(), atomic_load_int(), atomic_ptr_load(), atomic_ptr_store(), client_connection_checkpoint(), client_protocol_start(), client_connection::crypto_ctx, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_NETWORK, GET_OPTION, client_connection::lifecycle, log_info, client_connection::network, network_connection_handshake(), network_connection_transport(), PASSWORD_BUFFER_SIZE, server_connection_set_transport(), session_connection_establish(), session_connection_start(), SESSION_CONNECTION_STARTING, SET_ERRNO, should_exit(), session_connection_t::state, client_connection::stop_requested, client_connection::transport, ui_controller_title_activity(), UI_TITLE_CONNECTING, UI_TITLE_NEGOTIATING, and UI_TITLE_RECEIVING.

◆ client_connection_stop()

asciichat_error_t client_connection_stop ( client_connection_t * connection)

Definition at line 226 of file src/client/connection.c.

226 {
227 return connection ? session_connection_stop(&connection->lifecycle)
228 : ASCIICHAT_OK;
229}
asciichat_error_t session_connection_stop(session_connection_t *connection)

References ASCIICHAT_OK, client_connection::lifecycle, and session_connection_stop().

Referenced by client_connection_destroy().