ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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 80 of file src/client/connection.c.

80 {
81#ifndef NDEBUG
82 const char *failure = SAFE_GETENV("ASCII_CHAT_TEST_CLIENT_FAIL_STAGE");
83 if (failure && strcmp(failure, stage) == 0) {
84 log_debug("Injected client startup failure after %s", stage);
85 return SET_ERRNO(ERROR_THREAD, "Injected client startup failure after %s", stage);
86 }
87#else
88 (void)stage;
89#endif
90 return ASCIICHAT_OK;
91}
#define SET_ERRNO(code,...)
#define SAFE_GETENV(name)
Definition common.h:449
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_THREAD
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:595

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 93 of file src/client/connection.c.

93 {
94 if (!out) {
95 return SET_ERRNO(ERROR_INVALID_STATE, "Missing client connection output");
96 }
98 if (!*out)
99 return SET_ERRNO(ERROR_MEMORY, "Could not allocate client connection");
100 client_connection_t *connection = *out;
101 client_sync_entries(connection, true);
103 (session_connection_ops_t){client_interrupt, client_join, client_release}, connection);
104 connection->crypto_mode = ACIP_CRYPTO_ENCRYPT;
106 if (result == ASCIICHAT_OK && server_connection_init(connection) != 0)
107 result = ERROR_THREAD;
108 if (result == ASCIICHAT_OK)
109 result = client_connection_checkpoint("send-mutex");
110 if (result == ASCIICHAT_OK) {
111 connection->client_worker_pool = thread_pool_create("client_connection");
112 if (!connection->client_worker_pool)
113 result = SET_ERRNO(ERROR_THREAD, "Could not create connection worker pool");
114 }
115 if (result == ASCIICHAT_OK)
116 result = client_connection_checkpoint("worker-pool");
117 if (result != ASCIICHAT_OK) {
119 if (cleanup != ASCIICHAT_OK)
120 return cleanup;
121 }
122 if (result == ASCIICHAT_OK)
123 log_info("CLIENT_CONNECTION_CREATED: workers=0 frames=0");
124 return result;
125}
#define SAFE_CALLOC(count, size, cast)
Definition common.h:275
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:51
#define log_info(...)
Log an INFO message.
Definition log/log.h:608
#define ACIP_CRYPTO_ENCRYPT
Encryption only (no identity verification).
Definition packet.h:1139
static bool size_t gotel_query_t * out
Definition queries.h:8
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:92

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 242 of file src/client/connection.c.

242 {
243 if (!owner || !*owner)
244 return ASCIICHAT_OK;
245 client_connection_t *connection = *owner;
246 asciichat_error_t result = client_connection_stop(connection);
247 if (result != ASCIICHAT_OK)
248 return result;
249 if (connection->client_worker_pool)
251 if (connection->send_mutex_initialized)
252 mutex_destroy(&connection->send_mutex);
253 log_info("CLIENT_CONNECTION_RELEASED");
254 client_sync_entries(connection, false);
255 SAFE_FREE(*owner);
256 *owner = NULL;
257 return ASCIICHAT_OK;
258}
#define SAFE_FREE(ptr)
Definition common.h:377
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 181 of file src/client/connection.c.

181 {
182 if (connection)
184}
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 143 of file src/client/connection.c.

144 {
145 ASCII_OTEL_SCOPE(CONNECTION_ATTEMPT);
146 if (!password || password_capacity < PASSWORD_BUFFER_SIZE)
147 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing session password storage");
148 if (!connection || atomic_load_int(&connection->lifecycle.state) != SESSION_CONNECTION_STARTING) {
149 return SET_ERRNO(ERROR_INVALID_STATE, "Connection is not ready for startup");
150 }
151 network_connect_config_t quic_config = {.timeout_ms = 10000,
152 .tls_ca_file = GET_OPTION(tls_ca_file)[0] ? GET_OPTION(tls_ca_file) : NULL,
153 .quic_audio_datagrams = GET_OPTION(quic_audio_datagrams),
154 .quic_visual_datagrams = GET_OPTION(quic_visual_datagrams)};
155 const network_connect_config_t *connect_config = address && strncmp(address, "quic://", 7) == 0 ? &quic_config : NULL;
156 asciichat_error_t result =
157 session_connection_establish("client", address, port, connect_config, &connection->network, password,
158 password_capacity, client_prepare, client_detach, connection);
159 atomic_ptr_store(&connection->transport, network_connection_transport(connection->network));
160 connection->crypto_ctx = connection->network ? network_connection_handshake(connection->network) : NULL;
161 if (result != ASCIICHAT_OK)
162 return result;
163 if (should_exit())
164 return SET_ERRNO(ERROR_NETWORK, "Connection startup interrupted");
165 server_connection_set_transport(connection, atomic_ptr_load(&connection->transport));
166 result = client_connection_checkpoint("transport");
167 if (result != ASCIICHAT_OK)
168 return result;
169 result = client_protocol_start(connection);
170 if (result != ASCIICHAT_OK)
171 return result;
172 if (atomic_load_bool(&connection->stop_requested))
173 return SET_ERRNO(ERROR_NETWORK, "Connection stopped during startup");
174 result = session_connection_start(&connection->lifecycle);
175 if (result != ASCIICHAT_OK)
176 return result;
177 log_info("CLIENT_CONNECTION_RUNNING");
178 return ASCIICHAT_OK;
179}
@ ERROR_NETWORK
Definition error_codes.h:72
@ ERROR_INVALID_PARAM
#define GET_OPTION(field)
Safely get a specific option field (lock-free read).
Definition options.h:1336
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:249
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:72
asciichat_error_t client_protocol_start(client_connection_t *connection)
#define PASSWORD_BUFFER_SIZE
Password input buffer size (256 bytes).
Definition sizes.h:158
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,...

References ASCII_OTEL_SCOPE, ASCIICHAT_OK, 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, and client_connection::transport.

◆ client_connection_stop()

asciichat_error_t client_connection_stop ( client_connection_t * connection)

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

192 {
193 return connection ? session_connection_stop(&connection->lifecycle) : ASCIICHAT_OK;
194}
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().