|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
📡 Server packet processor: client communication handling, protocol state management, and packet dispatch More...
Go to the source code of this file.
Macros | |
| #define | OPUS_DECODE_STATIC_MAX_SAMPLES (32 * 960) |
Functions | |
| void | disconnect_client_for_bad_data (client_info_t *client, const char *format,...) |
| void | handle_client_join_packet (client_info_t *client, const void *data, size_t len) |
| Process CLIENT_JOIN packet - client announces identity and capabilities. | |
| void | handle_protocol_version_packet (client_info_t *client, const void *data, size_t len) |
| Process PROTOCOL_VERSION packet - validate protocol compatibility. | |
| void | handle_client_leave_packet (client_info_t *client, const void *data, size_t len) |
| Process CLIENT_LEAVE packet - handle clean client disconnect. | |
| void | handle_stream_start_packet (client_info_t *client, const void *data, size_t len) |
| Process STREAM_START packet - client requests to begin media transmission. | |
| void | handle_stream_stop_packet (client_info_t *client, const void *data, size_t len) |
| Process STREAM_STOP packet - client requests to halt media transmission. | |
| void | handle_image_frame_packet (client_info_t *client, void *data, size_t len) |
| Process IMAGE_FRAME packet - store client's video data for rendering. | |
| void | handle_audio_packet (client_info_t *client, const void *data, size_t len) |
| Process AUDIO packet - store single audio sample batch (legacy format) | |
| void | handle_remote_log_packet_from_client (client_info_t *client, const void *data, size_t len) |
| void | handle_audio_batch_packet (client_info_t *client, const void *data, size_t len) |
| Process AUDIO_BATCH packet - store efficiently batched audio samples. | |
| void | handle_audio_opus_batch_packet (client_info_t *client, const void *data, size_t len) |
| Process AUDIO_OPUS_BATCH packet - efficient Opus-encoded audio batch from client. | |
| void | handle_audio_opus_packet (client_info_t *client, const void *data, size_t len) |
| Process AUDIO_OPUS packet - decode single Opus frame from client. | |
| void | handle_client_capabilities_packet (client_info_t *client, const void *data, size_t len) |
| Process CLIENT_CAPABILITIES packet - configure client-specific rendering. | |
| void | handle_size_packet (client_info_t *client, const void *data, size_t len) |
| Process terminal size update packet - handle client window resize. | |
| void | handle_ping_packet (client_info_t *client) |
| Process PING packet - respond with PONG for keepalive. | |
| int | send_server_state_to_client (client_info_t *client) |
| Send current server state to a specific client. | |
| void | broadcast_clear_console_to_all_clients (void) |
| Signal all active clients to clear their displays before next video frame. | |
Variables | |
| atomic_bool | g_server_should_exit |
| Global shutdown flag from main.c - used to avoid error spam during shutdown. | |
📡 Server packet processor: client communication handling, protocol state management, and packet dispatch
The protocol processing follows a clear pattern:
CLIENT LIFECYCLE:
MEDIA STREAMING:
CONTROL PROTOCOL:
CLIENT STATE SYNCHRONIZATION: All client state modifications use the snapshot pattern:
MEDIA BUFFER COORDINATION: Video frames: Stored in client->incoming_video_buffer (thread-safe) Audio samples: Stored in client->incoming_audio_buffer (lock-free) Both buffers are processed by render threads in render.c
PACKET VALIDATION STRATEGY: All handlers validate:
The original server.c mixed protocol handling with connection management and rendering logic, making it difficult to:
This separation provides:
Definition in file server/protocol.c.
| #define OPUS_DECODE_STATIC_MAX_SAMPLES (32 * 960) |
| void broadcast_clear_console_to_all_clients | ( | void | ) |
Signal all active clients to clear their displays before next video frame.
Sets the needs_display_clear flag for all currently connected and active clients. This is used when the grid layout changes (clients join/leave) to ensure all clients clear their displays before receiving frames with the new layout.
ARCHITECTURE:
SYNCHRONIZATION:
USAGE SCENARIO:
Definition at line 1774 of file server/protocol.c.
References ERROR_INVALID_STATE, log_warn, and SET_ERRNO.
| void handle_audio_batch_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Process AUDIO_BATCH packet - store efficiently batched audio samples.
Handles the optimized audio packet format that bundles multiple sample chunks into a single packet. This reduces packet overhead and improves network efficiency for audio streaming.
PACKET STRUCTURE EXPECTED:
PERFORMANCE ADVANTAGES:
VALIDATION PERFORMED:
BUFFER MANAGEMENT:
ERROR HANDLING:
| client | Source client providing batched audio data |
| data | Packet payload containing batch header and samples |
| len | Total size of packet payload in bytes |
Definition at line 1029 of file server/protocol.c.
References ASCIICHAT_OK, AUDIO_BATCH_SAMPLES, audio_dequantize_samples(), audio_parse_batch_header(), audio_ring_buffer_write(), audio_batch_info_t::batch_count, client_info::client_id, disconnect_client_for_bad_data(), ERROR_MEMORY, client_info::incoming_audio_buffer, client_info::is_sending_audio, log_debug_every, log_error, log_info, LOG_RATE_DEFAULT, NET_TO_HOST_U32, SAFE_FREE, SAFE_MALLOC, safe_size_mul, audio_batch_info_t::sample_rate, SET_ERRNO, audio_batch_info_t::total_samples, VALIDATE_AUDIO_STREAM_ENABLED, VALIDATE_MIN_SIZE, VALIDATE_NONZERO, and VALIDATE_NOTNULL_DATA.
Referenced by process_decrypted_packet().
| void handle_audio_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Process AUDIO packet - store single audio sample batch (legacy format)
Handles the original audio packet format that sends one batch of float samples per packet. This format is less efficient than AUDIO_BATCH but still supported for backward compatibility.
PACKET STRUCTURE:
PERFORMANCE CHARACTERISTICS:
BUFFER MANAGEMENT:
STATE VALIDATION:
ERROR HANDLING:
| client | Source client providing audio data |
| data | Packet payload containing float audio samples |
| len | Size of packet payload in bytes |
Definition at line 932 of file server/protocol.c.
References ASCIICHAT_OK, audio_ring_buffer_write(), AUDIO_SAMPLES_PER_PACKET, client_info::incoming_audio_buffer, log_error, VALIDATE_AUDIO_ALIGNMENT, VALIDATE_AUDIO_SAMPLE_COUNT, VALIDATE_AUDIO_STREAM_ENABLED, VALIDATE_NOTNULL_DATA, and VALIDATE_RESOURCE_INITIALIZED.
Referenced by process_decrypted_packet().
| void handle_client_capabilities_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Process CLIENT_CAPABILITIES packet - configure client-specific rendering.
This packet contains detailed information about the client's terminal capabilities and preferences. The server uses this data to generate appropriately formatted ASCII art and ANSI escape sequences.
PACKET STRUCTURE EXPECTED:
STATE CHANGES PERFORMED:
PALETTE INITIALIZATION: The function performs critical palette setup:
THREAD SAFETY:
VALIDATION PERFORMED:
ERROR HANDLING:
INTEGRATION IMPACT:
| client | Target client whose capabilities are being configured |
| data | Packet payload containing terminal_capabilities_packet_t |
| len | Size of packet payload in bytes |
Definition at line 1464 of file server/protocol.c.
References terminal_capabilities_packet_t::capabilities, terminal_capabilities_t::capabilities, client_info::client_id, client_info::client_luminance_palette, client_info::client_palette_chars, client_info::client_palette_initialized, client_info::client_palette_len, client_info::client_palette_type, client_info::client_state_mutex, terminal_capabilities_packet_t::color_count, terminal_capabilities_t::color_count, terminal_capabilities_packet_t::color_level, terminal_capabilities_t::color_level, terminal_capabilities_packet_t::colorterm, terminal_capabilities_t::colorterm, terminal_capabilities_packet_t::desired_fps, terminal_capabilities_t::desired_fps, terminal_capabilities_packet_t::detection_reliable, terminal_capabilities_t::detection_reliable, ERROR_INVALID_STATE, client_info::has_terminal_caps, client_info::height, terminal_capabilities_packet_t::height, initialize_client_palette(), log_debug, log_info, log_info_client, mutex_lock, mutex_unlock, NET_TO_HOST_U16, NET_TO_HOST_U32, terminal_capabilities_packet_t::palette_custom, terminal_capabilities_t::palette_custom, PALETTE_CUSTOM, terminal_capabilities_packet_t::palette_type, terminal_capabilities_t::palette_type, terminal_capabilities_packet_t::render_mode, terminal_capabilities_t::render_mode, RENDER_MODE_BACKGROUND, RENDER_MODE_HALF_BLOCK, SAFE_STRNCPY, SET_ERRNO, terminal_capabilities_packet_t::term_type, terminal_capabilities_t::term_type, client_info::terminal_caps, terminal_color_level_name(), terminal_capabilities_packet_t::utf8_support, terminal_capabilities_t::utf8_support, VALIDATE_NONZERO, VALIDATE_PACKET_SIZE, VALIDATE_RANGE, terminal_capabilities_t::wants_background, client_info::width, and terminal_capabilities_packet_t::width.
Referenced by process_decrypted_packet().
| void handle_client_join_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Process CLIENT_JOIN packet - client announces identity and capabilities.
This is the first substantive packet clients send after establishing a TCP connection. It provides the server with essential information for managing the client throughout its session.
PACKET STRUCTURE EXPECTED:
STATE CHANGES PERFORMED:
PROTOCOL BEHAVIOR:
ERROR HANDLING:
| client | Target client whose state will be updated |
| data | Packet payload (should be client_info_packet_t) |
| len | Size of packet payload in bytes |
Definition at line 282 of file server/protocol.c.
References client_info::can_send_audio, client_info::can_send_video, client_info_packet_t::capabilities, CLIENT_CAP_AUDIO, CLIENT_CAP_COLOR, CLIENT_CAP_STRETCH, CLIENT_CAP_VIDEO, client_info::client_id, disconnect_client_for_bad_data(), client_info::display_name, client_info_packet_t::display_name, log_info, log_info_client, MAX_DISPLAY_NAME_LEN, NET_TO_HOST_U32, SAFE_STRNCPY, VALIDATE_CAPABILITY_FLAGS, VALIDATE_FLAGS_MASK, VALIDATE_PACKET_SIZE, and client_info::wants_stretch.
Referenced by process_decrypted_packet().
| void handle_client_leave_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Process CLIENT_LEAVE packet - handle clean client disconnect.
Clients may send this packet before disconnecting to allow the server to log the disconnect reason and perform clean state management. This is optional but preferred over abrupt disconnects.
PACKET STRUCTURE EXPECTED:
PROTOCOL BEHAVIOR:
ERROR HANDLING:
| client | Client that sent the leave packet |
| data | Packet payload (optional reason string) |
| len | Size of packet payload in bytes (0-256) |
Definition at line 424 of file server/protocol.c.
References client_info::active, client_info::client_id, ERROR_INVALID_STATE, log_info, log_warn, and SET_ERRNO.
Referenced by process_decrypted_packet().
| void handle_image_frame_packet | ( | client_info_t * | client, |
| void * | data, | ||
| size_t | len | ||
| ) |
Process IMAGE_FRAME packet - store client's video data for rendering.
This is the most performance-critical packet handler, processing real-time video data from clients. It validates, stores, and tracks video frames for subsequent ASCII conversion and grid layout.
PACKET STRUCTURE EXPECTED:
PERFORMANCE CHARACTERISTICS:
STATE CHANGES PERFORMED:
BUFFER MANAGEMENT:
VALIDATION PERFORMED:
ERROR HANDLING:
PERFORMANCE OPTIMIZATIONS:
| client | Source client providing video data |
| data | Packet payload containing image dimensions and RGB data |
| len | Total size of packet payload in bytes |
Definition at line 683 of file server/protocol.c.
References ASCIICHAT_OK, video_frame_t::capture_timestamp_us, client_info::client_id, client_info::client_state_mutex, video_frame_t::data, decompress_data(), disconnect_client_for_bad_data(), ERROR_INVALID_STATE, ERROR_MEMORY, format_bytes_pretty(), client_info::frames_received, client_info::frames_received_logged, g_server_should_exit, video_frame_t::height, HOST_TO_NET_U32, image_calc_rgb_size(), IMAGE_MAX_PIXELS_SIZE, image_validate_buffer_size(), image_validate_dimensions(), client_info::incoming_video_buffer, client_info::is_sending_video, log_debug, log_error, log_info, log_info_client, log_warn, mutex_lock, mutex_unlock, NET_TO_HOST_U32, SAFE_FREE, SAFE_MALLOC, video_frame_t::sequence_number, SET_ERRNO, video_frame_t::size, video_frame_begin_write(), video_frame_commit(), and video_frame_t::width.
Referenced by process_decrypted_packet().
| void handle_ping_packet | ( | client_info_t * | client | ) |
Process PING packet - respond with PONG for keepalive.
Clients send periodic PING packets to verify the connection is still active. The server responds with a PONG packet to confirm bi-directional connectivity. This prevents network equipment from timing out idle connections.
PACKET STRUCTURE:
PROTOCOL BEHAVIOR:
ERROR HANDLING:
PERFORMANCE CHARACTERISTICS:
| client | Source client requesting keepalive confirmation |
Definition at line 1651 of file server/protocol.c.
| void handle_protocol_version_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Process PROTOCOL_VERSION packet - validate protocol compatibility.
Clients send this packet to announce their protocol version and capabilities. The server validates that the major version matches and logs any version mismatches for debugging purposes.
PACKET STRUCTURE EXPECTED:
VALIDATION PERFORMED:
ERROR HANDLING:
| client | Client that sent the packet |
| data | Packet payload (protocol_version_packet_t) |
| len | Size of packet payload in bytes |
Definition at line 347 of file server/protocol.c.
References client_info::client_id, protocol_version_packet_t::compression_algorithms, disconnect_client_for_bad_data(), protocol_version_packet_t::feature_flags, log_debug, log_info, log_warn, NET_TO_HOST_U16, protocol_version_packet_t::protocol_revision, protocol_version_packet_t::protocol_version, PROTOCOL_VERSION_MAJOR, PROTOCOL_VERSION_MINOR, protocol_version_packet_t::reserved, and protocol_version_packet_t::supports_encryption.
Referenced by process_decrypted_packet().
| void handle_remote_log_packet_from_client | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Definition at line 948 of file server/protocol.c.
References ASCIICHAT_OK, client_info::client_id, disconnect_client_for_bad_data(), client_info::display_name, LOG_INFO, log_msg(), MAX_REMOTE_LOG_MESSAGE_LENGTH, packet_parse_remote_log(), REMOTE_LOG_DIRECTION_CLIENT_TO_SERVER, REMOTE_LOG_DIRECTION_UNKNOWN, and REMOTE_LOG_FLAG_TRUNCATED.
Referenced by process_decrypted_packet().
| void handle_size_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Process terminal size update packet - handle client window resize.
Clients send this packet when their terminal window is resized, allowing the server to adjust ASCII frame dimensions accordingly. This ensures optimal use of the client's display area.
PACKET STRUCTURE EXPECTED:
STATE CHANGES PERFORMED:
RENDERING IMPACT:
ERROR HANDLING:
| client | Target client whose terminal was resized |
| data | Packet payload containing new dimensions |
| len | Size of packet payload (should be sizeof(size_packet_t)) |
Definition at line 1592 of file server/protocol.c.
References client_info::client_id, client_info::client_state_mutex, client_info::height, size_packet_t::height, log_info, mutex_lock, mutex_unlock, NET_TO_HOST_U16, VALIDATE_NONZERO, VALIDATE_PACKET_SIZE, VALIDATE_RANGE, client_info::width, and size_packet_t::width.
| void handle_stream_start_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Process STREAM_START packet - client requests to begin media transmission.
Clients send this packet to indicate they're ready to start sending video and/or audio data. The server updates its internal state to expect and process media packets from this client.
PACKET STRUCTURE EXPECTED:
STATE CHANGES PERFORMED:
PROTOCOL BEHAVIOR:
ERROR HANDLING:
| client | Target client starting media transmission |
| data | Packet payload containing stream type flags |
| len | Size of packet payload (should be sizeof(uint32_t)) |
Definition at line 509 of file server/protocol.c.
References client_info::client_id, client_info::is_sending_audio, log_error, log_info, log_info_client, NET_TO_HOST_U32, opus_codec_create_decoder(), client_info::opus_decoder, STREAM_TYPE_AUDIO, STREAM_TYPE_VIDEO, VALIDATE_CAPABILITY_FLAGS, VALIDATE_FLAGS_MASK, and VALIDATE_PACKET_SIZE.
Referenced by process_decrypted_packet().
| void handle_stream_stop_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Process STREAM_STOP packet - client requests to halt media transmission.
Clients send this packet to gracefully stop sending video and/or audio data. The server updates its state to exclude this client from active media processing and grid layout calculations.
PACKET STRUCTURE EXPECTED:
STATE CHANGES PERFORMED:
PROTOCOL BEHAVIOR:
ERROR HANDLING:
| client | Target client stopping media transmission |
| data | Packet payload containing stream type flags |
| len | Size of packet payload (should be sizeof(uint32_t)) |
Definition at line 589 of file server/protocol.c.
References client_info::client_id, client_info::is_sending_audio, client_info::is_sending_video, log_info, log_info_client, NET_TO_HOST_U32, STREAM_TYPE_AUDIO, STREAM_TYPE_VIDEO, VALIDATE_CAPABILITY_FLAGS, VALIDATE_FLAGS_MASK, and VALIDATE_PACKET_SIZE.
Referenced by process_decrypted_packet().
| int send_server_state_to_client | ( | client_info_t * | client | ) |
Send current server state to a specific client.
Generates and queues a SERVER_STATE packet containing information about the current number of connected and active clients. This helps clients understand the multi-user environment and adjust their behavior accordingly.
PACKET CONTENT GENERATED:
USAGE SCENARIOS:
IMPLEMENTATION DETAILS:
THREAD SAFETY:
ERROR HANDLING:
| client | Target client to receive server state information |
Definition at line 1704 of file server/protocol.c.
References acip_send_server_state(), client_info::active, server_state_packet_t::active_client_count, ASCIICHAT_OK, client_info::client_id, client_manager_t::clients, server_state_packet_t::connected_client_count, ERROR_NETWORK, g_client_manager, HOST_TO_NET_U32, log_debug, MAX_CLIENTS, mutex_lock, mutex_unlock, server_state_packet_t::reserved, client_info::send_mutex, SET_ERRNO, and client_info::transport.
|
extern |
Global shutdown flag from main.c - used to avoid error spam during shutdown.
When the server is shutting down, certain packet processing errors become expected (e.g., buffer allocation failures, queue shutdowns). This flag helps handlers distinguish between genuine errors and shutdown conditions.
Global shutdown flag from main.c - used to avoid error spam during shutdown.
Global shutdown flag from main.c.
This flag is the primary coordination mechanism for clean server shutdown. It's atomic to ensure thread-safe access without mutexes, as it's checked frequently in tight loops across all worker threads.
USAGE PATTERN:
Definition at line 135 of file server/main.c.
Referenced by handle_image_frame_packet().