ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
client.c File Reference

Client application state implementation. More...

Go to the source code of this file.

Functions

app_client_t * app_client_create (void)
 Create and initialize client application context.
 
void app_client_destroy (app_client_t **client_ptr)
 Destroy client application context and free all resources.
 

Detailed Description

Client application state implementation.

Implements app_client_create() and app_client_destroy() for managing the application-layer state of ASCII chat clients.

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

Definition in file lib/network/client.c.

Function Documentation

◆ app_client_create()

app_client_t * app_client_create ( void  )

Create and initialize client application context.

Definition at line 23 of file lib/network/client.c.

23 {
24 app_client_t *client = SAFE_MALLOC(sizeof(app_client_t), app_client_t *);
25 if (!client) {
26 log_error("Failed to allocate app_client_t");
27 return NULL;
28 }
29
30 // Zero-initialize all fields
31 memset(client, 0, sizeof(*client));
32
33 /* Transport */
34 client->active_transport = NULL;
35 client->transport_type = ACIP_TRANSPORT_TCP;
36 client->tcp_client = NULL;
37 client->ws_client = NULL;
38
39 /* Audio State */
40 memset(&client->audio_ctx, 0, sizeof(client->audio_ctx));
41 memset(client->audio_send_queue, 0, sizeof(client->audio_send_queue));
42 client->audio_send_queue_head = 0;
43 client->audio_send_queue_tail = 0;
44 client->audio_send_queue_initialized = false;
45 atomic_store(&client->audio_sender_should_exit, false);
46 client->audio_capture_thread_created = false;
47 client->audio_sender_thread_created = false;
48 atomic_store(&client->audio_capture_thread_exited, false);
49
50 // Initialize audio queue mutex and condition variable
51 if (mutex_init(&client->audio_send_queue_mutex) != 0) {
52 log_error("Failed to initialize audio queue mutex");
53 SAFE_FREE(client);
54 return NULL;
55 }
56
57 if (cond_init(&client->audio_send_queue_cond) != 0) {
58 log_error("Failed to initialize audio queue cond");
59 mutex_destroy(&client->audio_send_queue_mutex);
60 SAFE_FREE(client);
61 return NULL;
62 }
63
64 /* Protocol State */
65 client->data_thread_created = false;
66 atomic_store(&client->data_thread_exited, false);
67 client->last_active_count = 0;
68 client->server_state_initialized = false;
69 client->should_clear_before_next_frame = false;
70 client->my_client_id = 0;
71 client->encryption_enabled = false;
72
73 /* Capture State */
74 client->capture_thread_created = false;
75 atomic_store(&client->capture_thread_exited, false);
76
77 /* Keepalive State */
78 client->ping_thread_created = false;
79 atomic_store(&client->ping_thread_exited, false);
80
81 /* Display State */
82 client->has_tty = false;
83 atomic_store(&client->is_first_frame_of_connection, false);
84 memset(&client->tty_info, 0, sizeof(client->tty_info));
85
86 /* Crypto State */
87 memset(&client->crypto_ctx, 0, sizeof(client->crypto_ctx));
88 client->crypto_initialized = false;
89
90 log_debug("App client created");
91
92 return client;
93}
int mutex_init(mutex_t *mutex)
Definition threading.c:16
int mutex_destroy(mutex_t *mutex)
Definition threading.c:21

References mutex_destroy(), and mutex_init().

◆ app_client_destroy()

void app_client_destroy ( app_client_t **  client_ptr)

Destroy client application context and free all resources.

Definition at line 98 of file lib/network/client.c.

98 {
99 if (!client_ptr || !*client_ptr) {
100 return; // No-op if NULL
101 }
102
103 app_client_t *client = *client_ptr;
104
105 log_debug("Destroying app client");
106
107 // Destroy mutexes and condition variables
108 mutex_destroy(&client->audio_send_queue_mutex);
109 cond_destroy(&client->audio_send_queue_cond);
110
111 // Note: Network clients (tcp_client, ws_client) are not destroyed here.
112 // They should be destroyed separately in connection_context_cleanup()
113 // to maintain proper lifecycle management.
114
115 SAFE_FREE(*client_ptr);
116}

References mutex_destroy().