|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
📡 Network packet processing and protocol implementation More...
Files | |
| file | protocol.c |
| 📡 Server packet processor: client communication handling, protocol state management, and packet dispatch | |
| file | protocol.h |
| Server packet processing and protocol implementation. | |
Functions | |
| 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. | |
📡 Network packet processing and protocol implementation
The protocol handler module processes all incoming packets from clients, validates packet structure, updates client state, and stores media data for render threads. It serves as the communication bridge between clients and the server, translating network packets into actionable state changes and media data storage.
Implementation: src/server/protocol.c, src/server/protocol.h
Key Responsibilities:
Packet processing follows a three-stage pipeline:
Stage 1: Packet Reception (in client.c receive thread):
Stage 2: Handler Function (this module):
Stage 3: Response Generation (via packet queues):
PACKET_TYPE_CLIENT_JOIN:
PACKET_TYPE_CLIENT_LEAVE:
PACKET_TYPE_CLIENT_CAPABILITIES:
PACKET_TYPE_STREAM_START:
PACKET_TYPE_STREAM_STOP:
PACKET_TYPE_IMAGE_FRAME:
PACKET_TYPE_AUDIO_BATCH:
PACKET_TYPE_AUDIO (legacy):
PACKET_TYPE_PING:
PACKET_TYPE_PONG:
All client state modifications use the snapshot pattern:
State Fields:
Video Frames:
client->incoming_video_buffer (double-buffer system)Audio Samples:
client->incoming_audio_buffer (lock-free ring buffer)All handlers validate packets before processing:
Validation Checks:
Invalid Packets:
Buffer Allocation Failures:
Network Errors During Responses:
Shutdown Conditions:
g_server_should_exit flagCalled By:
Provides To:
Consumed By:
incoming_video_bufferincoming_audio_bufferProvides To:
Used By:
Provides To:
Processing Speed:
Memory Usage:
Concurrency:
DO:
DON'T:
| void handle_audio_opus_batch_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
#include <protocol.c>
Process AUDIO_OPUS_BATCH packet - efficient Opus-encoded audio batch from client.
Handles batched Opus-encoded audio frames sent by the client. This provides ~98% bandwidth reduction compared to raw PCM audio while maintaining excellent audio quality.
PACKET STRUCTURE EXPECTED:
PROCESSING FLOW:
PERFORMANCE:
| client | Client that sent the audio packet |
| data | Packet payload data |
| len | Packet payload length |
Definition at line 1156 of file server/protocol.c.
References ASCIICHAT_OK, audio_ring_buffer_write(), client_info::client_id, disconnect_client_for_bad_data(), ERROR_MEMORY, client_info::incoming_audio_buffer, log_debug_every, log_error, log_info, LOG_RATE_DEFAULT, LOG_RATE_SLOW, log_warn, NET_TO_HOST_U16, opus_codec_decode(), OPUS_DECODE_STATIC_MAX_SAMPLES, client_info::opus_decoder, packet_parse_opus_batch(), SAFE_FREE, SAFE_MALLOC, SET_ERRNO, VALIDATE_AUDIO_STREAM_ENABLED, VALIDATE_NONZERO, VALIDATE_NOTNULL_DATA, VALIDATE_RANGE, and VALIDATE_RESOURCE_INITIALIZED.
Referenced by process_decrypted_packet().
| void handle_audio_opus_packet | ( | client_info_t * | client, |
| const void * | data, | ||
| size_t | len | ||
| ) |
#include <protocol.c>
Process AUDIO_OPUS packet - decode single Opus frame from client.
Handles single Opus-encoded audio frames sent by clients. The packet format includes a 16-byte header followed by the Opus-encoded data.
PACKET STRUCTURE:
| client | Client info structure |
| data | Packet payload |
| len | Packet length |
Definition at line 1318 of file server/protocol.c.
References ASCIICHAT_OK, audio_ring_buffer_write(), client_info::client_id, disconnect_client_for_bad_data(), client_info::incoming_audio_buffer, client_info::is_sending_audio, log_debug_every, log_error, LOG_RATE_DEFAULT, LOG_RATE_VERY_FAST, NET_TO_HOST_U32, opus_codec_decode(), client_info::opus_decoder, and VALIDATE_PACKET_NOT_NULL.