ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
include/ascii-chat/network/client.h
Go to the documentation of this file.
1
9#pragma once
10
11#include <stdbool.h>
12#include <stdint.h>
13#include <time.h>
14
15#include <ascii-chat/atomic.h>
18#include <ascii-chat/network/acip/transport.h> // For acip_transport_t
20#include <ascii-chat/crypto/handshake/common.h> // For crypto_handshake_context_t (complete type needed for field)
24#include <ascii-chat/platform/thread.h> // For thread_id_t
28
36typedef enum {
37 PARTICIPANT_TYPE_NETWORK, // Remote participant via socket/transport
38 PARTICIPANT_TYPE_MEMORY // Local host participant (direct memory access)
40
83typedef struct client_info {
84 participant_type_t participant_type; // Network (socket) or memory (direct injection)
86 bool is_tcp_client; // True for TCP clients, false for WebRTC (for cleanup logic)
87 acip_transport_t *transport; // ACIP transport for protocol-agnostic packet sending
88 asciichat_thread_t receive_thread; // Thread for receiving client data
89 asciichat_thread_t send_thread; // Thread for sending data to client
90 thread_id_t send_thread_id; // Thread ID of send thread (for self-join detection)
91 thread_id_t receive_thread_id; // Thread ID of receive thread (for self-join detection)
92 void *server_ctx; // Pointer to server_context_t (avoid circular includes)
93 char client_id[MAX_CLIENT_ID_LEN]; // String-based client ID: "noun.N (transport:port)"
94 uint32_t session_client_id; // Session host client ID (0 if not registered)
96 char client_ip[INET_ADDRSTRLEN];
97 int port;
98
99 // Media capabilities
102 bool wants_stretch; // Client wants stretched output (ignore aspect ratio)
103 atomic_t is_sending_video; // Thread-safe video stream state
104 atomic_t is_sending_audio; // Thread-safe audio stream state
105
106 // Codec capabilities (from CLIENT_CAPABILITIES packet)
107 uint32_t codec_capabilities_video; // VIDEO_CODEC_CAP_* bitmask
108 uint32_t codec_capabilities_audio; // AUDIO_CODEC_CAP_* bitmask
109
110 // Opus codec for audio compression/decompression
111 void *opus_decoder; // opus_codec_t* - Opus decoder for this client's audio
112
113 // H.265 decoder (persistent across frames for proper video decoding)
114 void *h265_decoder_ctx; // AVCodecContext* - H.265 HEVC decoder for this client
115 void *h265_decode_frame; // AVFrame* - Persistent frame for H.265 decoding
116 void *h265_rgb_frame; // AVFrame* - RGB frame for color space conversion
117 void *h265_sws_ctx; // SwsContext* - Color space converter for YUV->RGB
118
119 // Terminal capabilities (for rendering appropriate ASCII frames)
121 bool has_terminal_caps; // Whether we've received terminal capabilities from this client
122
123 // Per-client palette cache
124 char client_palette_chars[256]; // Client's palette characters
125 size_t client_palette_len; // Length of client's palette
126 char client_luminance_palette[256]; // Client's luminance-to-character mapping
127 palette_type_t client_palette_type; // Client's palette type
128 bool client_palette_initialized; // Whether client's palette is set up
129
130 // Per-client state for Matrix Rain post-processing of generated ASCII.
133
134 // Stream dimensions
136
137 // Statistics
139 atomic_t shutting_down; // Set when client is being removed
140 atomic_t protocol_disconnect_requested; // Set when protocol violation requires disconnect
143 uint64_t frames_received; // Track incoming frames from this client
144 uint32_t frames_received_logged; // Track for periodic logging (thread-safe via client_state_mutex)
147 uint32_t last_received_frame_hash; // Hash of last frame received from this client
148
149 // Buffers for incoming media (individual per client)
150 video_frame_buffer_t *incoming_video_buffer; // Modern double-buffered video frame
151 audio_ring_buffer_t *incoming_audio_buffer; // Buffer for this client's audio
152
153 // Professional double-buffer system for outgoing frames
154 video_frame_buffer_t *outgoing_video_buffer; // Double buffer for ASCII frames to send
155
156 // Packet queue for audio only (video uses double buffer now)
157 packet_queue_t *audio_queue; // Queue for audio packets to send to this client
158
159 // Async dispatch: queue for received packets (async processing)
160 packet_queue_t *received_packet_queue; // Queue of complete received packets waiting for dispatch
161 asciichat_thread_t dispatch_thread; // Async dispatch thread processes queued packets
162 atomic_t dispatch_thread_running; // Flag to signal dispatch thread to exit
163 cond_t dispatch_queue_cond; // Condition variable to wake dispatch thread when packets arrive
164
165 // Dedicated send thread for this client
167
168 // Per-client grid tracking for CLEAR_CONSOLE logic
169 atomic_t last_rendered_grid_sources; // Render thread: source count in buffered frame
170 atomic_t last_sent_grid_sources; // Send thread: source count in last sent frame
171
172 // Frame statistics for debugging
173 atomic_t frames_sent_count; // Total ASCII frames sent to this client
174
175 // Pre-allocated buffers to avoid malloc/free in send thread (prevents buffer pool contention)
178 void *crypto_plaintext_buffer; // For encryption plaintext (frame + header)
180 void *crypto_ciphertext_buffer; // For encryption ciphertext (encrypted result)
182
183 // Per-client rendering threads
188
189 // Per-client synchronization
191 mutex_t send_mutex; // Protects socket writes (prevents concurrent send race)
192
193 // Per-client crypto context for secure communication
197 bool encrypt_data; // Whether payload encryption is enabled (false = auth-only mode)
198
199 // Pending packet storage for --no-encrypt mode
200 // When client uses --no-encrypt, the first packet (e.g., CLIENT_JOIN) arrives
201 // during crypto handshake attempt. We store it here so the caller can process it.
205
206 // uthash handle for hash table operations
207 UT_hash_handle hh;
209
210/* ============================================================================
211 * Client-Side Application State (replaces tcp_client_t mixed concerns)
212 * ============================================================================ */
213
217typedef struct {
218 uint8_t data[4096];
219 size_t size;
220 uint16_t frame_sizes[48];
223
224#define APP_CLIENT_AUDIO_QUEUE_SIZE 256
225
233typedef struct app_client {
234 /* ========================================================================
235 * Active Transport & Network Client
236 * ======================================================================== */
237
239
240 /* ========================================================================
241 * Audio State
242 * ======================================================================== */
243
257
258 /* ========================================================================
259 * Protocol State
260 * ======================================================================== */
261
270
271 /* ========================================================================
272 * Capture State
273 * ======================================================================== */
274
278
279 /* ========================================================================
280 * Keepalive State
281 * ======================================================================== */
282
286
287 /* ========================================================================
288 * Display State
289 * ======================================================================== */
290
294
295 /* ========================================================================
296 * Crypto State
297 * ======================================================================== */
298
301
303
309
314void app_client_destroy(app_client_t **client_ptr);
⚛️ Atomic operations abstraction layer with debug tracking
Shared connection ownership handle for TCP and WebSocket clients.
Common declarations and data structures for cryptographic handshake.
Matrix-style digital rain effect for ASCII frames.
unsigned short uint16_t
Definition common.h:57
unsigned int uint32_t
Definition common.h:58
unsigned long long uint64_t
Definition common.h:59
unsigned char uint8_t
Definition common.h:56
#define MAX_DISPLAY_NAME_LEN
Maximum display name length in characters.
Definition limits.h:20
#define MAX_CLIENT_ID_LEN
Maximum client ID length - format: "noun.N (transport:port)" e.g., "mountain.0 (tcp:48036)".
Definition limits.h:23
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:286
palette_type_t
Built-in palette type enumeration.
Definition palette.h:84
int socket_t
🔊 Audio Capture and Playback Interface for ascii-chat
struct app_client app_client_t
Client-side application state.
struct client_info client_info_t
Per-client state structure for server-side client management.
void app_client_destroy(app_client_t **client_ptr)
Destroy client application context and free all resources.
participant_type_t
Participant type for distinguishing network vs memory participants.
#define APP_CLIENT_AUDIO_QUEUE_SIZE
app_client_t * app_client_create(void)
Create and initialize client application context.
🧵 Cross-platform thread interface for ascii-chat
void * asciichat_thread_t
ASCII Palette Management for Video-to-ASCII Conversion.
Lock-Free Ring Buffer and Frame Buffer Management.
ClangTool/LibTooling compatibility shim for stdbool.h.
Transport instance structure.
Definition transport.h:214
Audio packet for async transmission.
int frame_count
Number of frames in packet.
Client-side application state.
crypto_handshake_context_t crypto_ctx
asciichat_thread_t audio_sender_thread
app_client_audio_packet_t audio_send_queue[256]
asciichat_thread_t data_reception_thread
connection_handle_t connection
asciichat_thread_t capture_thread
asciichat_thread_t audio_capture_thread
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76
Audio context for full-duplex capture and playback.
Audio ring buffer for real-time audio streaming.
Definition ringbuffer.h:205
Per-client state structure for server-side client management.
bool transport_encrypted
TLS or DTLS already encrypts this client's transport.
terminal_capabilities_t terminal_caps
char display_name[MAX_DISPLAY_NAME_LEN]
asciichat_thread_t audio_render_thread
packet_queue_t * received_packet_queue
video_frame_buffer_t * outgoing_video_buffer
asciichat_thread_t dispatch_thread
crypto_handshake_context_t crypto_handshake_ctx
video_frame_buffer_t * incoming_video_buffer
participant_type_t participant_type
audio_ring_buffer_t * incoming_audio_buffer
asciichat_thread_t receive_thread
char client_ip[INET_ADDRSTRLEN]
asciichat_thread_t video_render_thread
char client_id[MAX_CLIENT_ID_LEN]
Condition variable type (POSIX: pthread_cond_t with debug tracking)
Definition cond.h:63
Cryptographic handshake context structure.
Digital rain effect context.
Mutex type (POSIX: pthread_mutex_t with debug tracking)
Thread-safe packet queue for producer-consumer communication.
Definition queue.h:211
Complete terminal capabilities structure.
Definition terminal.h:709
TTY detection and management structure.
Definition terminal.h:753
Video frame buffer manager.
🖥️ Cross-platform terminal interface for ascii-chat
⏱️ High-precision timing utilities using sokol_time.h and uthash
Transport abstraction layer for ACIP protocol.