ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
lib/network/client.h
Go to the documentation of this file.
1
8#pragma once
9
10// C11 stdatomic.h conflicts with MSVC's C++ <atomic> header on Windows.
11#if defined(__cplusplus) && defined(_WIN32)
12#include <atomic>
13using std::atomic_bool;
14using std::atomic_int;
15using std::atomic_uint;
16#else
17#include <stdatomic.h>
18#endif
19#include <stdbool.h>
20#include <stdint.h>
21#include <time.h>
22
23#include "network/network.h"
24#include "network/packet.h"
26#include "network/logging.h"
27#include "network/acip/transport.h" // For acip_transport_t
28#include "crypto/handshake/common.h" // For crypto_handshake_context_t (complete type needed for field)
29#include "ringbuffer.h"
30#include "video/video_frame.h"
31#include "platform/terminal.h"
32#include "video/palette.h"
33#include "util/uthash.h"
34
77typedef struct client_info {
79 acip_transport_t *transport; // ACIP transport for protocol-agnostic packet sending
80 asciichat_thread_t receive_thread; // Thread for receiving client data
81 atomic_uint client_id; // Thread-safe client ID
83 char client_ip[INET_ADDRSTRLEN];
84 int port;
85
86 // Media capabilities
89 bool wants_stretch; // Client wants stretched output (ignore aspect ratio)
90 atomic_bool is_sending_video; // Thread-safe video stream state
91 atomic_bool is_sending_audio; // Thread-safe audio stream state
92
93 // Opus codec for audio compression/decompression
94 void *opus_decoder; // opus_codec_t* - Opus decoder for this client's audio
95
96 // Terminal capabilities (for rendering appropriate ASCII frames)
98 bool has_terminal_caps; // Whether we've received terminal capabilities from this client
99
100 // Per-client palette cache
101 char client_palette_chars[256]; // Client's palette characters
102 size_t client_palette_len; // Length of client's palette
103 char client_luminance_palette[256]; // Client's luminance-to-character mapping
104 palette_type_t client_palette_type; // Client's palette type
105 bool client_palette_initialized; // Whether client's palette is set up
106
107 // Stream dimensions
108 atomic_ushort width, height;
109
110 // Statistics
111 atomic_bool active;
112 atomic_bool shutting_down; // Set when client is being removed
113 atomic_bool protocol_disconnect_requested; // Set when protocol violation requires disconnect
116 uint64_t frames_received; // Track incoming frames from this client
117 uint32_t frames_received_logged; // Track for periodic logging (thread-safe via client_state_mutex)
118
119 // Buffers for incoming media (individual per client)
120 video_frame_buffer_t *incoming_video_buffer; // Modern double-buffered video frame
121 audio_ring_buffer_t *incoming_audio_buffer; // Buffer for this client's audio
122
123 // Professional double-buffer system for outgoing frames
124 video_frame_buffer_t *outgoing_video_buffer; // Double buffer for ASCII frames to send
125
126 // Packet queue for audio only (video uses double buffer now)
127 packet_queue_t *audio_queue; // Queue for audio packets to send to this client
128
129 // Dedicated send thread for this client
132
133 // Per-client grid tracking for CLEAR_CONSOLE logic
134 atomic_int last_rendered_grid_sources; // Render thread: source count in buffered frame
135 atomic_int last_sent_grid_sources; // Send thread: source count in last sent frame
136
137 // Pre-allocated buffers to avoid malloc/free in send thread (prevents buffer pool contention)
140 void *crypto_plaintext_buffer; // For encryption plaintext (frame + header)
142 void *crypto_ciphertext_buffer; // For encryption ciphertext (encrypted result)
144
145 // Per-client rendering threads
150
151 // Per-client processing state
152 struct timespec last_video_render_time;
153 struct timespec last_audio_render_time;
154
155 // Per-client synchronization
157 mutex_t send_mutex; // Protects socket writes (prevents concurrent send race)
158
159 // Per-client crypto context for secure communication
162
163 // Pending packet storage for --no-encrypt mode
164 // When client uses --no-encrypt, the first packet (e.g., CLIENT_JOIN) arrives
165 // during crypto handshake attempt. We store it here so the caller can process it.
169
170 // uthash handle for hash table operations
171 UT_hash_handle hh;
Common declarations and data structures for cryptographic handshake.
unsigned int uint32_t
Definition common.h:58
unsigned long long uint64_t
Definition common.h:59
#define MAX_DISPLAY_NAME_LEN
Maximum display name length in characters.
Definition limits.h:20
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:281
palette_type_t
Built-in palette type enumeration.
Definition palette.h:84
int socket_t
Socket handle type (POSIX: int)
Definition socket.h:50
pthread_mutex_t mutex_t
Mutex type (POSIX: pthread_mutex_t)
Definition mutex.h:38
pthread_t asciichat_thread_t
Thread handle type (POSIX: pthread_t)
struct client_info client_info_t
Per-client state structure for server-side client management.
Network logging macros and remote log direction enumeration.
🌐 Core network I/O operations with timeout support
Packet protocol implementation with encryption and compression support.
📬 Thread-safe packet queue system for per-client send threads
ASCII Palette Management for Video-to-ASCII Conversion.
Lock-Free Ring Buffer and Frame Buffer Management.
Transport instance structure.
Definition transport.h:169
Audio ring buffer for real-time audio streaming.
Definition ringbuffer.h:208
Per-client state structure for server-side client management.
atomic_bool video_render_thread_running
terminal_capabilities_t terminal_caps
atomic_uint client_id
void * crypto_plaintext_buffer
acip_transport_t * transport
size_t crypto_ciphertext_size
char display_name[MAX_DISPLAY_NAME_LEN]
atomic_int last_sent_grid_sources
asciichat_thread_t audio_render_thread
size_t crypto_plaintext_size
uint32_t frames_received_logged
video_frame_buffer_t * outgoing_video_buffer
asciichat_thread_t send_thread
uint64_t frames_received
crypto_handshake_context_t crypto_handshake_ctx
struct timespec last_video_render_time
char client_palette_chars[256]
atomic_ushort height
video_frame_buffer_t * incoming_video_buffer
atomic_ushort width
bool client_palette_initialized
audio_ring_buffer_t * incoming_audio_buffer
atomic_bool protocol_disconnect_requested
mutex_t client_state_mutex
atomic_bool is_sending_audio
atomic_bool audio_render_thread_running
atomic_int last_rendered_grid_sources
asciichat_thread_t receive_thread
packet_queue_t * audio_queue
char client_ip[INET_ADDRSTRLEN]
atomic_bool shutting_down
packet_type_t pending_packet_type
asciichat_thread_t video_render_thread
char client_luminance_palette[256]
atomic_bool is_sending_video
struct timespec last_audio_render_time
void * pending_packet_payload
void * crypto_ciphertext_buffer
palette_type_t client_palette_type
atomic_bool active
UT_hash_handle hh
size_t pending_packet_length
atomic_bool send_thread_running
Cryptographic handshake context structure.
Thread-safe packet queue for producer-consumer communication.
Complete terminal capabilities structure.
Definition terminal.h:485
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.
#️⃣ Wrapper for uthash.h that ensures common.h is included first