ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
connection_state.h
Go to the documentation of this file.
1
25#pragma once
26
27#include <stdint.h>
28#include <stdbool.h>
29#include <time.h>
30#include "common.h"
34
35/* ============================================================================
36 * Forward Declarations
37 * ============================================================================ */
38
39struct tcp_client; // Forward declaration for TCP client instance
40
41/* ============================================================================
42 * Connection State Enumeration
43 * ============================================================================ */
44
78
79/* ============================================================================
80 * Timeout Constants (in seconds)
81 * ============================================================================ */
82
83#define CONN_TIMEOUT_DIRECT_TCP 3
84#define CONN_TIMEOUT_WEBRTC_STUN 8
85#define CONN_TIMEOUT_WEBRTC_TURN 15
86
87/* ============================================================================
88 * Session Context (for WebRTC connections)
89 * ============================================================================ */
90
97typedef struct {
98 char session_string[128];
102 char server_address[64];
104
105/* ============================================================================
106 * STUN/TURN Configuration
107 * ============================================================================ */
108
112typedef struct {
113 char stun_server[256];
115
116 char turn_server[256];
118 char turn_username[128];
119 char turn_password[256];
121
122/* ============================================================================
123 * Connection Attempt Context
124 * ============================================================================ */
125
140typedef struct {
141 // ─────────────────────────────────────────────────────────────
142 // State Machine
143 // ─────────────────────────────────────────────────────────────
144
147
148 // ─────────────────────────────────────────────────────────────
149 // Timeout Tracking
150 // ─────────────────────────────────────────────────────────────
151
154
155 // ─────────────────────────────────────────────────────────────
156 // Active Transports
157 // ─────────────────────────────────────────────────────────────
158
163
165
166 // ─────────────────────────────────────────────────────────────
167 // WebRTC Session Context
168 // ─────────────────────────────────────────────────────────────
169
172
173 // ─────────────────────────────────────────────────────────────
174 // WebRTC Connection Synchronization
175 // ─────────────────────────────────────────────────────────────
176
180
181 // ─────────────────────────────────────────────────────────────
182 // STUN/TURN Configuration
183 // ─────────────────────────────────────────────────────────────
184
186
187 // ─────────────────────────────────────────────────────────────
188 // Connection Preferences (from CLI flags)
189 // ─────────────────────────────────────────────────────────────
190
195
196 // ─────────────────────────────────────────────────────────────
197 // Statistics
198 // ─────────────────────────────────────────────────────────────
199
203
205
206/* ============================================================================
207 * Function Declarations
208 * ============================================================================ */
209
222asciichat_error_t connection_context_init(connection_attempt_context_t *ctx, bool prefer_webrtc, bool no_webrtc,
223 bool webrtc_skip_stun, bool webrtc_disable_turn);
224
236
246
254
262
272
297 uint16_t server_port, const char *acds_server, uint16_t acds_port);
🔌 Cross-platform abstraction layer umbrella header for ascii-chat
asciichat_error_t connection_context_init(connection_attempt_context_t *ctx, bool prefer_webrtc, bool no_webrtc, bool webrtc_skip_stun, bool webrtc_disable_turn)
Initialize connection attempt context.
asciichat_error_t connection_attempt_with_fallback(connection_attempt_context_t *ctx, const char *server_address, uint16_t server_port, const char *acds_server, uint16_t acds_port)
Attempt connection with 3-stage fallback sequence.
connection_state_t
13-state connection state machine
@ CONN_STATE_WEBRTC_STUN_SIGNALING
Exchanging SDP/ICE candidates via ACDS.
@ CONN_STATE_CONNECTED
Successfully connected (any transport)
@ CONN_STATE_WEBRTC_TURN_SIGNALING
Exchanging SDP/ICE candidates with TURN relay.
@ CONN_STATE_WEBRTC_TURN_CONNECTED
WebRTC + TURN connection established.
@ CONN_STATE_WEBRTC_STUN_CONNECTED
WebRTC + STUN connection established.
@ CONN_STATE_ATTEMPTING_WEBRTC_TURN
Initiating WebRTC + TURN connection.
@ CONN_STATE_IDLE
Not connected, no attempt in progress.
@ CONN_STATE_DIRECT_TCP_FAILED
Direct TCP failed, falling back to STUN.
@ CONN_STATE_DISCONNECTED
Clean disconnect (user initiated)
@ CONN_STATE_DIRECT_TCP_CONNECTED
Direct TCP connection established.
@ CONN_STATE_FAILED
All fallback stages exhausted.
@ CONN_STATE_ATTEMPTING_WEBRTC_STUN
Initiating WebRTC + STUN connection.
@ CONN_STATE_WEBRTC_STUN_FAILED
STUN failed, falling back to TURN.
@ CONN_STATE_ATTEMPTING_DIRECT_TCP
Attempting direct TCP connection.
@ CONN_STATE_WEBRTC_TURN_FAILED
All stages exhausted.
void connection_context_cleanup(connection_attempt_context_t *ctx)
Cleanup connection attempt context.
bool connection_check_timeout(const connection_attempt_context_t *ctx)
Check if current stage has exceeded timeout.
asciichat_error_t connection_state_transition(connection_attempt_context_t *ctx, connection_state_t new_state)
Transition to next connection state.
uint32_t connection_get_stage(connection_state_t state)
Get current stage number (1, 2, or 3)
const char * connection_state_name(connection_state_t state)
Get human-readable state name for logging.
unsigned short uint16_t
Definition common.h:57
unsigned int uint32_t
Definition common.h:58
unsigned char uint8_t
Definition common.h:56
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:46
pthread_mutex_t mutex_t
Mutex type (POSIX: pthread_mutex_t)
Definition mutex.h:38
pthread_cond_t cond_t
Condition variable type (POSIX: pthread_cond_t)
Definition cond.h:38
uint8_t session_id[16]
uint8_t participant_id[16]
Transport instance structure.
Definition transport.h:169
Master context for connection attempt with fallback.
acip_transport_t * acds_transport
ACDS signaling transport (Stages 2/3) - may be NULL.
time_t stage_start_time
When current stage began.
connection_state_t current_state
Current connection state.
webrtc_peer_manager_t * peer_manager
WebRTC peer manager (Stages 2/3)
uint32_t current_stage_timeout_seconds
Timeout for current stage (3/8/15)
connection_state_t previous_state
Previous state (for debugging)
uint32_t stage_failures
How many stages have failed.
uint32_t reconnect_attempt
Reconnection attempt number (1st, 2nd, etc.)
bool prefer_webrtc
–prefer-webrtc flag
struct tcp_client * tcp_client_instance
TCP client instance (Direct TCP only) - owned by context.
mutex_t webrtc_mutex
Mutex for WebRTC callback synchronization.
acip_transport_t * active_transport
Currently active transport (whichever succeeded)
bool no_webrtc
–no-webrtc flag (disable WebRTC, TCP only)
bool webrtc_skip_stun
–webrtc-skip-stun flag (skip Stage 2 STUN)
bool webrtc_disable_turn
–webrtc-disable-turn flag (skip Stage 3 TURN)
connection_stun_turn_config_t stun_turn_cfg
STUN/TURN server config.
acip_transport_t * webrtc_transport
WebRTC transport (Stages 2/3) - may be NULL.
acip_transport_t * tcp_transport
TCP transport (Stage 1) - may be NULL.
cond_t webrtc_ready_cond
Condition variable for on_transport_ready.
connection_session_context_t session_ctx
Session context from ACDS.
uint32_t total_transitions
Total state transitions (for metrics)
bool webrtc_transport_received
Flag: transport_ready callback fired.
Session context for WebRTC signaling.
uint16_t server_port
Server port for connection.
STUN and TURN server configuration for WebRTC fallback.
uint16_t stun_port
STUN server port.
uint16_t turn_port
TURN relay port.
TCP client connection and state management.
WebRTC peer manager structure.
⏱️ High-precision timing utilities using sokol_time.h and uthash
Transport abstraction layer for ACIP protocol.