ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
Client Main Entry Point

🚀 Main entry point and lifecycle orchestration for the client application More...

Files

file  main.h
 ascii-chat Client Mode Entry Point Header
 

Detailed Description

🚀 Main entry point and lifecycle orchestration for the client application

Client Main Entry Point

Overview

The client main entry point orchestrates the complete client lifecycle including initialization, connection management, reconnection logic, and graceful shutdown.

Implementation: src/client/main.c, src/client/main.h

Responsibilities

  • Command-line argument parsing
  • Platform and subsystem initialization (display, audio, webcam)
  • Connection establishment with exponential backoff
  • Signal handling (SIGINT/Ctrl+C)
  • Main event loop and thread coordination
  • Reconnection orchestration on connection loss
  • Cleanup and resource deallocation

Application Lifecycle

Initialization

// 1. Parse command-line options
// 2. Initialize platform subsystems
// 3. Setup signal handlers
signal(SIGINT, sigint_handler);
int audio_client_init()
Initialize audio subsystem.
int capture_init()
Initialize capture subsystem.
Definition capture.c:428
int display_init()
Initialize what is necessary to display ascii frames.
Definition display.c:265
asciichat_error_t options_init(int argc, char **argv)
Initialize options by parsing command-line arguments.
Definition options.c:144
@ MODE_CLIENT
Client mode - network client options.
Definition options.h:428
asciichat_error_t platform_init(void)
Initialize platform-specific subsystems.
ASCIICHAT_API unsigned short int opt_audio_enabled

Connection Loop

int reconnect_attempt = 0;
bool first_connection = true;
while (!should_exit()) {
// Attempt connection with exponential backoff
reconnect_attempt, first_connection,
has_ever_connected);
if (result == 0 || result == CONNECTION_WARNING_NO_CLIENT_AUTH) {
// Connection successful - start threads
first_connection = false;
has_ever_connected = true;
reconnect_attempt = 0;
// Wait for connection loss or exit signal
platform_sleep_usec(100000); // 100ms poll
}
// Stop all threads
// Wait for threads to exit
platform_sleep_usec(10000); // 10ms poll
}
// Close connection
if (should_exit()) break; // User requested exit
// Connection lost - prepare for reconnection
log_info("Connection lost - will attempt reconnection");
}
else if (result == CONNECTION_ERROR_AUTH_FAILED ||
// Fatal auth errors - no retry
break;
}
// Exponential backoff with cap at 5 seconds
int delay_ms = 10 + (200 * reconnect_attempt);
if (delay_ms > 5000) delay_ms = 5000;
platform_sleep_usec(delay_ms * 1000);
reconnect_attempt++;
}
bool should_exit()
Check if client should exit.
void audio_stop_thread()
Stop audio capture thread.
bool audio_thread_exited()
Check if audio capture thread has exited.
int audio_start_thread()
Start audio capture thread.
int capture_start_thread()
Start capture thread.
Definition capture.c:449
void capture_stop_thread()
Stop capture thread.
Definition capture.c:481
bool capture_thread_exited()
Check if capture thread has exited.
Definition capture.c:515
void server_connection_close()
Close the server connection gracefully.
bool server_connection_is_active()
Check if server connection is currently active.
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.
void display_reset_for_new_connection()
Reset display state for new connection.
Definition display.c:315
bool keepalive_thread_exited()
Check if keepalive thread has exited.
Definition keepalive.c:292
int keepalive_start_thread()
Start keepalive/ping thread.
Definition keepalive.c:234
void keepalive_stop_thread()
Stop keepalive/ping thread.
Definition keepalive.c:260
void protocol_stop_connection()
Stop protocol connection handling.
int protocol_start_connection()
Start protocol connection handling.
bool protocol_connection_lost()
Check if connection has been lost.
#define log_info(...)
Log an INFO message.
void platform_sleep_usec(unsigned int usec)
High-precision sleep function with microsecond precision.
ASCIICHAT_API char opt_address[OPTIONS_BUFF_SIZE]
ASCIICHAT_API char opt_port[OPTIONS_BUFF_SIZE]
@ CONNECTION_ERROR_HOST_KEY_FAILED
Host key verification failed (no retry)
@ CONNECTION_ERROR_AUTH_FAILED
Authentication failure (no retry)
@ CONNECTION_WARNING_NO_CLIENT_AUTH
Server not using client verification (warning)

Cleanup

// Cleanup in reverse initialization order
return exit_code;
void audio_cleanup()
Cleanup audio subsystem.
void capture_cleanup()
Cleanup capture subsystem.
Definition capture.c:526
void display_cleanup()
Cleanup display subsystem.
Definition display.c:385
void platform_cleanup(void)
Cleanup platform-specific subsystems.

Signal Handling

SIGINT Handler

static void sigint_handler(int sig) {
(void)sig;
signal_exit(); // Sets g_should_exit atomically
}
void signal_exit()
Signal client to exit.

Signal Safety:

  • Only sets atomic flag - no complex operations
  • Main loop detects flag and performs graceful shutdown
  • No mutex operations or malloc/free in handler

Exit Codes

  • 0: Success (clean exit or snapshot completed)
  • 1: Configuration/initialization error
  • 2: Connection error (fatal auth failure or max retries)
  • 130: SIGINT received (Ctrl+C)
See also
src/client/main.c
src/client/main.h
Client Overview