ascii-chat 0.8.38
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
19#pragma once
20
21#include <stdint.h>
22#include <stdbool.h>
23#include <time.h>
24#include <ascii-chat/common.h>
25#include <ascii-chat/network/acip/transport.h>
26#include <ascii-chat/platform/abstraction.h>
27
28/* ============================================================================
29 * Forward Declarations
30 * ============================================================================ */
31
32struct tcp_client; // Forward declaration for TCP client instance
33struct websocket_client; // Forward declaration for WebSocket client instance
34
35/* ============================================================================
36 * Connection State Enumeration
37 * ============================================================================ */
38
56
57/* ============================================================================
58 * Timeout Constants (in nanoseconds)
59 * ============================================================================ */
60
61#define CONN_TIMEOUT_TCP (3LL * NS_PER_SEC_INT)
62
63/* ============================================================================
64 * Connection Attempt Context
65 * ============================================================================ */
66
78typedef struct {
79 // State Machine
82
83 // Timeout Tracking (in nanoseconds)
85 uint64_t timeout_ns;
86
87 // Transport Instances (one will be non-NULL at a time)
88 acip_transport_t *active_transport;
89 struct tcp_client *tcp_client_instance;
90 struct websocket_client *ws_client_instance;
91
92 // Statistics
95
96 // Reconnection Strategy
98
100
101/* ============================================================================
102 * Function Declarations
103 * ============================================================================ */
104
114
126
136
144
154
171asciichat_error_t connection_attempt_tcp(connection_attempt_context_t *ctx, const char *server_address,
172 uint16_t server_port, struct tcp_client *pre_created_tcp_client);
173
188asciichat_error_t connection_attempt_websocket(connection_attempt_context_t *ctx, const char *ws_url);
asciichat_error_t connection_context_init(connection_attempt_context_t *ctx)
Initialize connection attempt context.
connection_state_t
Simple TCP connection state machine.
@ CONN_STATE_CONNECTED
Successfully connected via TCP.
@ CONN_STATE_IDLE
Not connected, no attempt in progress.
@ CONN_STATE_DISCONNECTED
Clean disconnect (user initiated)
@ CONN_STATE_FAILED
Connection attempt failed.
@ CONN_STATE_ATTEMPTING
Attempting TCP connection.
asciichat_error_t connection_attempt_websocket(connection_attempt_context_t *ctx, const char *ws_url)
Attempt WebSocket connection.
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 connection attempt has exceeded timeout.
asciichat_error_t connection_state_transition(connection_attempt_context_t *ctx, connection_state_t new_state)
Transition to next connection state.
const char * connection_state_name(connection_state_t state)
Get human-readable state name for logging.
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.
Context for TCP connection attempts.
connection_state_t current_state
Current connection state.
connection_state_t previous_state
Previous state (for debugging)
uint32_t reconnect_attempt
Reconnection attempt number (1st, 2nd, etc.)
struct tcp_client * tcp_client_instance
TCP client instance - owned by context.
acip_transport_t * active_transport
Currently active transport.
bool is_reconnection
True if reconnecting after successful connection (not first connect)
uint64_t timeout_ns
Timeout for connection attempt.
struct websocket_client * ws_client_instance
WebSocket client instance - owned by context.
uint32_t total_transitions
Total state transitions (for metrics)
uint64_t attempt_start_time_ns
When current attempt began.