|
ascii-chat 0.8.38
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_ping_packet (client_info_t *client, const void *data, size_t len) |
| Handle PING packet - respond with PONG. | |
| void | handle_pong_packet (client_info_t *client, const void *data, size_t len) |
| Handle PONG packet - client acknowledged our PING. | |
| 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. | |
| 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. | |
📡 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 1756 of file server/protocol.c.
| void disconnect_client_for_bad_data | ( | client_info_t * | client, |
| const char * | format, | ||
| ... | |||
| ) |
Definition at line 152 of file server/protocol.c.
References acip_send_error(), args, crypto_handshake_get_context(), log_network_message(), packet_queue_stop(), packet_send_error(), platform_sleep_ms(), and safe_vsnprintf().
Referenced by handle_audio_batch_packet(), handle_audio_opus_batch_packet(), handle_audio_opus_packet(), handle_client_join_packet(), handle_image_frame_packet(), handle_protocol_version_packet(), handle_remote_log_packet_from_client(), and process_decrypted_packet().
| 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 1040 of file server/protocol.c.
References audio_dequantize_samples(), audio_parse_batch_header(), audio_ring_buffer_write(), audio_batch_info_t::batch_count, disconnect_client_for_bad_data(), audio_batch_info_t::sample_rate, and audio_batch_info_t::total_samples.
| 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 943 of file server/protocol.c.
References audio_ring_buffer_write().
| 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 1475 of file server/protocol.c.
References initialize_client_palette().
Referenced by add_client().
| 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 288 of file server/protocol.c.
References disconnect_client_for_bad_data().
| 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 432 of file server/protocol.c.
| 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 732 of file server/protocol.c.
References disconnect_client_for_bad_data(), format_bytes_pretty(), frame_check_size_overflow(), frame_validate_legacy(), g_server_should_exit, image_calc_rgb_size(), image_validate_buffer_size(), image_validate_dimensions(), video_frame_begin_write(), and video_frame_commit().
| void handle_ping_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Handle PING packet - respond with PONG.
Definition at line 642 of file server/protocol.c.
References acip_send_pong().
| void handle_pong_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Handle PONG packet - client acknowledged our PING.
Definition at line 666 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 355 of file server/protocol.c.
References disconnect_client_for_bad_data().
| void handle_remote_log_packet_from_client | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Definition at line 959 of file server/protocol.c.
References disconnect_client_for_bad_data(), log_msg(), and packet_parse_remote_log().
| 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 1612 of file server/protocol.c.
| 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 518 of file server/protocol.c.
References opus_codec_create_decoder().
| 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 601 of file server/protocol.c.
| 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 1686 of file server/protocol.c.
References acip_send_server_state(), client_manager_t::clients, and g_client_manager.
Referenced by add_webrtc_client().