ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
server.c File Reference

🌐 Client connection manager: TCP connection, reconnection with exponential backoff, and thread-safe transmission More...

Go to the source code of this file.

Macros

#define DEBUG_NETWORK   1
 
#define DEBUG_THREADS   1
 
#define DEBUG_MEMORY   1
 
#define MAX_RECONNECT_DELAY   (5 * 1000 * 1000)
 

Functions

int server_connection_init ()
 Initialize the server connection management subsystem.
 
int server_connection_establish (const char *address, int port, int reconnect_attempt, bool first_connection, bool has_ever_connected)
 Establish connection to ascii-chat server.
 
bool server_connection_is_active ()
 Check if server connection is currently active.
 
socket_t server_connection_get_socket ()
 Get current socket file descriptor.
 
acip_transport_tserver_connection_get_transport (void)
 Get ACIP transport instance.
 
void server_connection_set_transport (acip_transport_t *transport)
 Set ACIP transport instance from connection fallback.
 
uint32_t server_connection_get_client_id ()
 Get client ID assigned by server.
 
const char * server_connection_get_ip ()
 Get resolved server IP address.
 
void server_connection_set_ip (const char *ip)
 Set the server IP address.
 
void server_connection_close ()
 Close the server connection gracefully.
 
void server_connection_shutdown ()
 Emergency connection shutdown for signal handlers.
 
void server_connection_lost ()
 Signal that connection has been lost.
 
bool server_connection_is_lost ()
 Check if connection loss has been detected.
 
void server_connection_cleanup ()
 Cleanup connection management subsystem.
 
asciichat_error_t threaded_send_packet (packet_type_t type, const void *data, size_t len)
 Thread-safe packet transmission.
 
int threaded_send_audio_batch_packet (const float *samples, int num_samples, int batch_count)
 Thread-safe batched audio packet transmission.
 
asciichat_error_t threaded_send_audio_opus (const uint8_t *opus_data, size_t opus_size, int sample_rate, int frame_duration)
 Thread-safe Opus audio frame transmission.
 
asciichat_error_t threaded_send_audio_opus_batch (const uint8_t *opus_data, size_t opus_size, const uint16_t *frame_sizes, int frame_count)
 Thread-safe Opus audio batch packet transmission.
 
int threaded_send_ping_packet (void)
 Thread-safe ping packet transmission.
 
int threaded_send_pong_packet (void)
 Thread-safe pong packet transmission.
 
int threaded_send_stream_start_packet (uint32_t stream_type)
 Thread-safe stream start packet transmission.
 
int threaded_send_terminal_size_with_auto_detect (unsigned short width, unsigned short height)
 Thread-safe terminal size packet transmission with auto-detection.
 
int threaded_send_client_join_packet (const char *display_name, uint32_t capabilities)
 Thread-safe client join packet transmission.
 

Variables

crypto_handshake_context_t g_crypto_ctx = {0}
 Per-connection crypto handshake context.
 

Detailed Description

🌐 Client connection manager: TCP connection, reconnection with exponential backoff, and thread-safe transmission

The connection management follows a robust state machine:

  1. Initialization: Socket creation and address resolution
  2. Connection: TCP handshake with configurable timeout
  3. Capability Exchange: Send terminal capabilities and client info
  4. Active Monitoring: Health checks and keepalive management
  5. Disconnection: Graceful or forced connection teardown
  6. Reconnection: Exponential backoff retry logic

Thread Safety

All packet transmission functions use a global send mutex to prevent interleaved packets on the wire. The socket file descriptor is protected with atomic operations for thread-safe access across multiple threads.

Reconnection Strategy

Implements exponential backoff with jitter:

  • Initial delay: 10ms
  • Exponential growth: delay = 10ms + (200ms * attempt)
  • Maximum delay: 5 seconds
  • Jitter: Small random component to prevent thundering herd

Platform Compatibility

Uses platform abstraction layer for:

  • Socket creation and management (Winsock vs BSD sockets)
  • Network error handling and error string conversion
  • Address resolution and connection timeout handling
  • Socket options (keepalive, nodelay)

Integration Points

  • main.c: Calls connection establishment and monitoring functions
  • protocol.c: Uses thread-safe send functions for packet transmission
  • keepalive.c: Monitors connection health and triggers reconnection
  • capture.c: Sends media data through connection
  • audio.c: Sends audio data through connection

Error Handling

Connection errors are classified into:

  • Temporary: Network congestion, temporary DNS failures (retry)
  • Permanent: Invalid address, authentication failure (report and exit)
  • Timeout: Connection establishment timeout (retry with backoff)
  • Loss: Existing connection broken (immediate reconnection attempt)
Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
2025
Version
2.0

Definition in file src/client/server.c.

Macro Definition Documentation

◆ DEBUG_MEMORY

#define DEBUG_MEMORY   1

Definition at line 96 of file src/client/server.c.

◆ DEBUG_NETWORK

#define DEBUG_NETWORK   1

Definition at line 94 of file src/client/server.c.

◆ DEBUG_THREADS

#define DEBUG_THREADS   1

Definition at line 95 of file src/client/server.c.

◆ MAX_RECONNECT_DELAY

#define MAX_RECONNECT_DELAY   (5 * 1000 * 1000)

Maximum delay between reconnection attempts (microseconds)

Definition at line 215 of file src/client/server.c.