|
ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
|
Connection state machine for TCP client connections. More...
Go to the source code of this file.
Data Structures | |
| struct | connection_attempt_context_t |
| Context for TCP connection attempts. More... | |
Macros | |
| #define | CONN_TIMEOUT_TCP (3LL * NS_PER_SEC_INT) |
| TCP connection timeout (3s) | |
Enumerations | |
| enum | connection_state_t { CONN_STATE_IDLE = 0 , CONN_STATE_ATTEMPTING = 1 , CONN_STATE_CONNECTED = 2 , CONN_STATE_DISCONNECTED = 3 , CONN_STATE_FAILED = 4 } |
| Simple TCP connection state machine. More... | |
Functions | |
| asciichat_error_t | connection_context_init (connection_attempt_context_t *ctx) |
| Initialize connection attempt context. | |
| asciichat_error_t | connection_state_transition (connection_attempt_context_t *ctx, connection_state_t new_state) |
| Transition to next connection state. | |
| bool | connection_check_timeout (const connection_attempt_context_t *ctx) |
| Check if connection attempt has exceeded timeout. | |
| const char * | connection_state_name (connection_state_t state) |
| Get human-readable state name for logging. | |
| void | connection_context_cleanup (connection_attempt_context_t *ctx) |
| Cleanup connection attempt context. | |
| asciichat_error_t | connection_attempt_tcp (connection_attempt_context_t *ctx, const char *server_address, uint16_t server_port, struct tcp_client *pre_created_tcp_client) |
| Attempt TCP connection. | |
| asciichat_error_t | connection_attempt_websocket (connection_attempt_context_t *ctx, const char *ws_url) |
| Attempt WebSocket connection. | |
Connection state machine for TCP client connections.
Defines the connection state machine for direct TCP connections. Client mode uses direct TCP only - no WebRTC fallback.
The state machine tracks:
States: IDLE, ATTEMPTING, CONNECTED, DISCONNECTED, FAILED
Definition in file connection_state.h.
| #define CONN_TIMEOUT_TCP (3LL * NS_PER_SEC_INT) |
TCP connection timeout (3s)
Definition at line 61 of file connection_state.h.
| enum connection_state_t |
Simple TCP connection state machine.
States:
Definition at line 49 of file connection_state.h.
| asciichat_error_t connection_attempt_tcp | ( | connection_attempt_context_t * | ctx, |
| const char * | server_address, | ||
| uint16_t | server_port, | ||
| struct tcp_client * | pre_created_tcp_client | ||
| ) |
Attempt TCP connection.
Attempts a direct TCP connection to the specified server. On success, sets active_transport in the context.
| ctx | Connection context (initialized via connection_context_init) |
| server_address | Server hostname or IP address |
| server_port | Server port number |
| pre_created_tcp_client | Optional pre-created TCP client (from framework). If NULL, creates one. |
Attempt TCP connection.
Connects to server via TCP, performs crypto handshake if enabled, and creates ACIP transport for protocol communication.
Definition at line 177 of file connection_attempt.c.
References acip_tcp_transport_create(), connection_attempt_context_t::active_transport, connection_attempt_context_t::attempt_start_time_ns, client_crypto_handshake(), client_crypto_init(), client_crypto_set_mode(), CONN_STATE_ATTEMPTING, CONN_STATE_CONNECTED, CONN_STATE_FAILED, CONN_TIMEOUT_TCP, connection_state_transition(), crypto_client_get_context(), crypto_client_is_ready(), connection_attempt_context_t::reconnect_attempt, server_connection_set_ip(), should_exit(), tcp_client_connect(), tcp_client_create(), tcp_client_destroy(), tcp_client_get_socket(), connection_attempt_context_t::tcp_client_instance, time_get_realtime_ns(), connection_attempt_context_t::timeout_ns, url_is_websocket(), url_parse(), url_parts_destroy(), websocket_client_connect(), websocket_client_create(), websocket_client_destroy(), and connection_attempt_context_t::ws_client_instance.
| asciichat_error_t connection_attempt_websocket | ( | connection_attempt_context_t * | ctx, |
| const char * | ws_url | ||
| ) |
Attempt WebSocket connection.
Attempts a WebSocket connection to the specified server (ws:// or wss://). On success, sets active_transport in the context.
| ctx | Connection context (initialized via connection_context_init) |
| ws_url | WebSocket URL (e.g., "ws://localhost:27226" or "wss://server.com:27226") |
Attempt WebSocket connection.
Connects to server via WebSocket, performs crypto handshake if enabled, and creates ACIP transport for protocol communication.
Definition at line 423 of file connection_attempt.c.
References connection_attempt_context_t::active_transport, connection_attempt_context_t::attempt_start_time_ns, client_crypto_init(), CONN_STATE_ATTEMPTING, CONN_STATE_CONNECTED, CONN_STATE_FAILED, CONN_TIMEOUT_TCP, connection_state_transition(), crypto_client_get_context(), crypto_client_is_ready(), should_exit(), time_get_realtime_ns(), connection_attempt_context_t::timeout_ns, websocket_client_connect(), websocket_client_create(), websocket_client_destroy(), and connection_attempt_context_t::ws_client_instance.
| bool connection_check_timeout | ( | const connection_attempt_context_t * | ctx | ) |
Check if connection attempt has exceeded timeout.
Compares elapsed time since attempt start against timeout.
| ctx | Connection context |
Definition at line 152 of file connection_attempt.c.
References connection_attempt_context_t::attempt_start_time_ns, time_get_realtime_ns(), and connection_attempt_context_t::timeout_ns.
| void connection_context_cleanup | ( | connection_attempt_context_t * | ctx | ) |
Cleanup connection attempt context.
Closes and releases any active transports, frees WebRTC peer manager. Called on connection success, failure, or shutdown.
| ctx | Connection context to cleanup |
Definition at line 102 of file connection_attempt.c.
References acip_transport_destroy(), connection_attempt_context_t::active_transport, tcp_client_destroy(), connection_attempt_context_t::tcp_client_instance, websocket_client_destroy(), and connection_attempt_context_t::ws_client_instance.
Referenced by client_main().
| asciichat_error_t connection_context_init | ( | connection_attempt_context_t * | ctx | ) |
Initialize connection attempt context.
Sets up initial state, resets timeouts, and prepares for connection attempt.
| ctx | Connection context to initialize |
Definition at line 74 of file connection_attempt.c.
References connection_attempt_context_t::attempt_start_time_ns, CONN_STATE_IDLE, CONN_TIMEOUT_TCP, connection_attempt_context_t::current_state, connection_attempt_context_t::previous_state, connection_attempt_context_t::reconnect_attempt, time_get_realtime_ns(), connection_attempt_context_t::timeout_ns, and connection_attempt_context_t::total_transitions.
Referenced by client_main().
| const char * connection_state_name | ( | connection_state_t | state | ) |
Get human-readable state name for logging.
| state | Connection state |
Definition at line 50 of file connection_attempt.c.
References CONN_STATE_ATTEMPTING, CONN_STATE_CONNECTED, CONN_STATE_DISCONNECTED, CONN_STATE_FAILED, and CONN_STATE_IDLE.
Referenced by connection_state_transition().
| asciichat_error_t connection_state_transition | ( | connection_attempt_context_t * | ctx, |
| connection_state_t | new_state | ||
| ) |
Transition to next connection state.
Updates current/previous states, logs transition, and updates timeout if stage changed. Validates state transitions to prevent invalid sequences.
| ctx | Connection context |
| new_state | Next state to transition to |
Transition to next connection state.
Definition at line 134 of file connection_attempt.c.
References connection_state_name(), connection_attempt_context_t::current_state, connection_attempt_context_t::previous_state, and connection_attempt_context_t::total_transitions.
Referenced by connection_attempt_tcp(), and connection_attempt_websocket().