ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
src/discovery/session.h
Go to the documentation of this file.
1
14#pragma once
15
17#include "negotiate.h"
20#include "session/host.h"
21#include "session/participant.h"
27#include <stdbool.h>
28#include <stdint.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34// Forward declarations
35#if !defined(MAX_PARTICIPANTS)
36#define MAX_PARTICIPANTS 16
37#endif
38
52typedef struct {
53 // Future host information (pre-elected by host every 5 minutes)
54 uint8_t future_host_id[16];
55 char future_host_address[64];
60
61 // Timing for next proactive election
64
80
94
104
114typedef bool (*discovery_should_exit_fn)(void *user_data);
115
133
137typedef struct {
138 // State
142
143 // Session info
146 uint8_t initiator_id[16];
147 char session_string[SESSION_STRING_BUFFER_SIZE];
148 char password[128];
152
153 // Identity keys (Ed25519) - Multi-key support
154 uint8_t identity_pubkey[32];
155 uint8_t identity_seckey[64];
156
157 // Multi-key support: all identity keys for multi-key handshake
158 uint8_t identity_pubkeys[MAX_IDENTITY_KEYS][32];
160
161 // ACDS connection
164 char acds_address[64];
166 char acds_url[512];
167
168 // Host info (when not us)
169 uint8_t host_id[16];
170 char host_address[64];
173
174 // WebRTC credentials (for WebRTC sessions)
175 char turn_username[128];
176 char turn_password[128];
177
178 // Negotiation (initial host negotiation)
180
181 // Cached NAT quality (gathered once, reused for NETWORK_QUALITY packets)
184
185 // NAT detection threading (non-blocking background detection)
190
191 // Ring Consensus (NEW P2P design)
193
194 // Host liveness detection (participants only)
196
197 // Migration state (host failover)
199
200 // WebRTC support (NEW)
207
208 // WebRTC ICE servers (for STUN/TURN support)
209 // These must be stored in the session to keep memory valid for peer manager lifetime
210 // Forward declare - actual types defined in network/webrtc/stun.h and network/webrtc/turn.h
212 size_t stun_count;
214 size_t turn_count;
215
216 // Role contexts (only one is active at a time)
219
220 // Callbacks for state changes
221 void (*on_state_change)(discovery_state_t new_state, void *user_data);
222 void (*on_session_ready)(const char *session_string, void *user_data);
223 void (*on_error)(asciichat_error_t error, const char *message, void *user_data);
225
226 // Exit callback for graceful shutdown during blocking operations
230
234typedef struct {
235 // ACDS server
236 const char *acds_address;
238 const char *acds_url;
239
240 // Session to join (NULL = create new)
241 const char *session_string;
242 const char *password;
243
244 // Local server config (if we become host)
246
247 // Callbacks
248 void (*on_state_change)(discovery_state_t new_state, void *user_data);
249 void (*on_session_ready)(const char *session_string, void *user_data);
250 void (*on_error)(asciichat_error_t error, const char *message, void *user_data);
252
253 // Exit callback for graceful shutdown during blocking operations
257
264
270
280
291
297
304
311
317const char *discovery_session_get_string(const discovery_session_t *session);
318
325
332
339
350
357
364
372
379
386
397 char out_address[64], uint16_t *out_port,
398 uint8_t *out_connection_type);
399
406
407#ifdef __cplusplus
408}
409#endif
Platform abstraction layer umbrella header providing unified cross-platform API.
⚠️‼️ Comprehensive thread-local error context system for ascii-chat
unsigned short uint16_t
Definition common.h:57
unsigned int uint32_t
Definition common.h:58
unsigned long long uint64_t
Definition common.h:59
unsigned char uint8_t
Definition common.h:56
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
#define MAX_IDENTITY_KEYS
Maximum number of identity keys that can be loaded (for multi-key support)
stun_server_t
Definition stun.h:75
turn_server_t
Definition turn.h:101
🏠 Server-side session hosting abstraction
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
void * asciichat_thread_t
Host negotiation logic for discovery mode.
👤 Client-side session participation abstraction
Cross-platform socket interface for ascii-chat.
uint8_t session_id[16]
uint8_t participant_id[16]
migration_state_t
Migration detection and failover state.
@ MIGRATION_STATE_DETECTED
Host disconnect detected.
@ MIGRATION_STATE_COMPLETE
Failover complete, call resumed.
@ MIGRATION_STATE_NONE
No migration in progress.
@ MIGRATION_STATE_FAILOVER
Failing over to pre-elected future host.
bool discovery_session_is_active(const discovery_session_t *session)
Check if session is active (call in progress)
bool discovery_session_is_future_host(const discovery_session_t *session)
Check if we are the future host.
void discovery_session_stop(discovery_session_t *session)
Stop the discovery session.
bool discovery_session_is_host(const discovery_session_t *session)
Check if we are the host.
bool(* discovery_should_exit_fn)(void *user_data)
Callback to check if session should exit.
discovery_session_t * discovery_session_create(const discovery_config_t *config)
Create a new discovery session.
asciichat_error_t discovery_session_start(discovery_session_t *session)
Start the discovery session.
asciichat_error_t discovery_session_init_ring(discovery_session_t *session)
Initialize ring consensus state.
discovery_state_t discovery_session_get_state(const discovery_session_t *session)
Get current session state.
asciichat_error_t discovery_session_check_host_alive(discovery_session_t *session)
Detect host disconnect (check connection status)
session_host_t * discovery_session_get_host(discovery_session_t *session)
Get host context (if we are host)
asciichat_error_t discovery_session_get_future_host(const discovery_session_t *session, uint8_t out_id[16], char out_address[64], uint16_t *out_port, uint8_t *out_connection_type)
Get future host information.
asciichat_error_t discovery_session_process(discovery_session_t *session, int64_t timeout_ns)
Process session events (call in main loop)
asciichat_error_t discovery_session_start_ring_round(discovery_session_t *session)
Start a new ring consensus round (every 5 minutes or on new joiner)
const char * discovery_session_get_string(const discovery_session_t *session)
Get session string.
asciichat_error_t discovery_session_handle_host_disconnect(discovery_session_t *session, uint32_t disconnect_reason)
Handle host disconnect with automatic failover to future host.
asciichat_error_t discovery_session_connect_to_future_host(discovery_session_t *session)
Connect to pre-elected future host (called when NOT future host)
void discovery_session_destroy(discovery_session_t *session)
Destroy discovery session and free resources.
session_participant_t * discovery_session_get_participant(discovery_session_t *session)
Get participant context (if we are participant)
asciichat_error_t discovery_session_become_host(discovery_session_t *session)
Become the host (called when elected as future host)
discovery_state_t
Discovery session state.
@ DISCOVERY_STATE_ENDED
Session ended.
@ DISCOVERY_STATE_JOINING_SESSION
Joining existing session.
@ DISCOVERY_STATE_INIT
Initial state.
@ DISCOVERY_STATE_ACTIVE
Session active (call in progress)
@ DISCOVERY_STATE_MIGRATING
Host migration in progress.
@ DISCOVERY_STATE_NEGOTIATING
NAT negotiation in progress.
@ DISCOVERY_STATE_CONNECTING_HOST
Connecting to host as participant.
@ DISCOVERY_STATE_CREATING_SESSION
Creating new session.
@ DISCOVERY_STATE_CONNECTING_ACDS
Connecting to ACDS.
@ DISCOVERY_STATE_WAITING_PEER
Waiting for peer to join (initiator)
@ DISCOVERY_STATE_FAILED
Session failed.
@ DISCOVERY_STATE_STARTING_HOST
Starting as host.
ClangTool/LibTooling compatibility shim for stdbool.h.
#define bool
Definition stdbool.h:61
#define SESSION_STRING_BUFFER_SIZE
Transport instance structure.
Definition transport.h:214
Configuration for discovery session.
const char * acds_address
ACDS address (default: "127.0.0.1")
uint16_t acds_port
ACDS port (default: 27225)
const char * password
Session password (optional)
discovery_should_exit_fn should_exit_callback
uint16_t local_port
Local port for hosting (default: 27224)
const char * session_string
Session string to join (or NULL to create)
const char * acds_url
ACDS WebSocket URL (ws:// or wss://, mutually exclusive with acds_port)
Discovery session context.
uint64_t webrtc_last_attempt_time_ms
Timestamp of last connection attempt (monotonic time)
uint64_t webrtc_disconnected_since_ms
Start of a transient peer disconnection.
uint8_t session_type
0 = DIRECT_TCP, 1 = WEBRTC
int webrtc_retry_attempt
Current retry attempt number (0 = initial, 1+ = retries)
uint64_t last_acds_keepalive_ns
Last keepalive sent to the discovery service.
bool webrtc_transport_ready
True when DataChannel is open and transport created.
asciichat_thread_t nat_detection_thread
turn_server_t * turn_servers
TURN servers from ACDS.
uint64_t waiting_peer_start_ns
Timestamp when entered WAITING_PEER state (for timeout)
size_t stun_count
Number of STUN servers.
size_t num_identity_keys
Number of identity keys loaded.
stun_server_t * stun_servers
STUN servers from ACDS.
discovery_should_exit_fn should_exit_callback
size_t turn_count
Number of TURN servers.
bool webrtc_connection_initiated
True when we've called webrtc_peer_manager_connect()
acip_transport_t * acds_transport
Persistent transport for ACDS communication.
session_participant_t * participant_ctx
bool host_elected
True if we've been elected as host (even if waiting for peer)
struct webrtc_peer_manager * peer_manager
WebRTC peer connection manager (NULL for TCP sessions)
Host liveness detection state.
uint32_t consecutive_failures
Number of consecutive ping failures.
uint32_t max_failures
Threshold for triggering migration (default: 3)
uint64_t ping_interval_ms
Time between pings (default: 3000ms)
uint64_t last_pong_received_ms
Timestamp of last pong received (monotonic)
uint64_t last_ping_sent_ms
Timestamp of last ping sent (monotonic)
bool ping_in_flight
True if waiting for pong.
uint64_t timeout_ms
Timeout for ping response (default: 10000ms)
Host migration context for tracking failover state.
uint64_t detection_time_ms
When host disconnect detected (Unix ms)
migration_state_t state
Current migration state.
uint32_t disconnect_reason
Reason for disconnect (from HOST_LOST packet)
Mutex type (POSIX: pthread_mutex_t with debug tracking)
NAT quality assessment result.
Definition nat.h:27
Host negotiation context.
Definition negotiate.h:40
Ring consensus state (Host-Mediated Proactive Election)
uint8_t future_host_connection_type
acip_connection_type_t (DIRECT, UPNP, STUN, TURN)
uint16_t future_host_port
Port number.
uint64_t future_host_elected_round
Which 5-minute round this was elected in.
uint64_t last_ring_round_ms
When host last ran election (for 5-min timer)
bool am_future_host
Am I the elected future host?
Internal session host structure.
Definition host.c:106
Internal session participant structure.
Definition participant.c:45
WebRTC peer manager structure.
Transport abstraction layer for ACIP protocol.