ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
src/discovery/session.h
Go to the documentation of this file.
1
2
3/* Storage capacities in elements; text buffers include the terminating NUL. */
4#define DISCOVERY_SESSION_ACDS_ADDRESS_BUFFER_SIZE 64
5#define DISCOVERY_SESSION_ACDS_URL_BUFFER_SIZE 512
6
23
24#pragma once
26
28#include "negotiate.h"
29#include <libplatform/socket.h>
31#include "session/host.h"
32#include "session/participant.h"
37#include <libplatform/compiler.h>
38#include <libplatform/system.h>
39#include <libplatform/mutex.h>
40#include <libplatform/terminal.h>
41#include <stdbool.h>
42#include <stdint.h>
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
66
76typedef bool (*discovery_should_exit_fn)(void *user_data);
77
95
99typedef struct {
100 // State
104
105 // Session info
114
115 // Identity keys (Ed25519) - Multi-key support
118
119 // Multi-key support: all identity keys for multi-key handshake
123
124 // ACDS connection
132
133 // Host info (when not us)
138
139 // WebRTC credentials (for WebRTC sessions)
142
143 // Negotiation (initial host negotiation)
145
146 // Optional NAT hint for fresh measured handoff probes
150
151 // NAT detection threading (non-blocking background detection)
152 platform_thread_t nat_detection_thread;
158
159 // Host liveness detection (participants only)
161
169
171
172 // WebRTC support (NEW)
184
185 // WebRTC ICE servers (for STUN/TURN support)
186 // These must be stored in the session to keep memory valid for peer manager lifetime
187 // Forward declare - actual types defined in network/webrtc/stun.h and network/webrtc/turn.h
189 size_t stun_count;
191 size_t turn_count;
192
193 // Role contexts (only one is active at a time)
198
199 // Callbacks for state changes
200 void (*on_state_change)(discovery_state_t new_state, void *user_data);
201 void (*on_session_ready)(const char *session_string, void *user_data);
202 void (*on_error)(asciichat_error_t error, const char *message, void *user_data);
204
205 // Exit callback for graceful shutdown during blocking operations
209
213typedef struct {
214 // ACDS server
215 const char *acds_address;
217 const char *acds_url;
218
219 // Session to join (NULL = create new)
220 const char *session_string;
221 const char *password;
222
223 // Local server config (if we become host)
225
226 // Callbacks
227 void (*on_state_change)(discovery_state_t new_state, void *user_data);
228 void (*on_session_ready)(const char *session_string, void *user_data);
229 void (*on_error)(asciichat_error_t error, const char *message, void *user_data);
231
232 // Exit callback for graceful shutdown during blocking operations
238
246
252
262
273
279
286
293
299const char *discovery_session_get_string(const discovery_session_t *session);
300
307
314
321
328
331
339
340#ifdef __cplusplus
341}
342#endif
343
#define ACIP_PASSWORD_SIZE
Definition acds_sizes.h:8
#define ACIP_ADDRESS_SIZE
Definition acds_sizes.h:7
#define ACIP_TURN_AUTH_SIZE
Definition acds_sizes.h:12
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
Common declarations and data structures for cryptographic handshake.
unsigned short uint16_t
Definition common.h:61
unsigned int uint32_t
Definition common.h:62
unsigned long long uint64_t
Definition common.h:63
unsigned char uint8_t
Definition common.h:60
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
#define SESSION_ID_SIZE
#define MAX_SESSION_KEYS
struct session_participant session_participant_t
Client session state spanning connection, transport, media workers, and callbacks.
struct session_host session_host_t
Opaque session host handle.
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:506
stun_server_t
Definition stun.h:78
turn_server_t
Definition turn.h:106
🏠 Server-side session hosting abstraction
Transport abstraction layer for ACIP protocol.
struct network_connection network_connection_t
Host negotiation logic for discovery mode.
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
👤 Client-side session participation abstraction
#define ED25519_PUBLIC_KEY_SIZE
Ed25519 public key size in bytes.
Definition sizes.h:33
#define ED25519_PRIVATE_KEY_SIZE
Ed25519 private key size (seed + public) in bytes.
Definition sizes.h:35
bool discovery_session_is_active(const discovery_session_t *session)
Check if session is active (call in progress).
#define DISCOVERY_SESSION_ACDS_URL_BUFFER_SIZE
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.
void discovery_session_host_pong(discovery_session_t *session)
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)
Configuration for discovery session.
asciichat_error_t discovery_session_start(discovery_session_t *session)
Start the discovery session.
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_process(discovery_session_t *session, int64_t timeout_ns)
Process session events (call in main loop).
#define DISCOVERY_SESSION_ACDS_ADDRESS_BUFFER_SIZE
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)
Report host disconnect and await the ACDS recovery decision.
asciichat_error_t discovery_session_apply_handoff(discovery_session_t *session)
Replace the old role after a committed handoff and media-worker shutdown. Releases borrowed-role reso...
asciichat_error_t 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).
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.
#define SESSION_STRING_BUFFER_SIZE
Configuration for discovery session.
const char * acds_address
ACDS address (default: "127.0.0.1").
void(* on_error)(asciichat_error_t error, const char *message, void *user_data)
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).
void(* on_state_change)(discovery_state_t new_state, void *user_data)
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).
void(* on_session_ready)(const char *session_string, void *user_data)
Discovery session context.
bool nat_detection_completed
Protected by nat_quality_mutex.
char acds_url[512]
instead of TCP)
uint64_t webrtc_last_attempt_time_ms
Timestamp of last connection attempt (monotonic time).
network_connection_t * host_control_connection
Ownership transferred with the control connection.
uint8_t identity_pubkeys[MAX_SESSION_KEYS][ED25519_PUBLIC_KEY_SIZE]
All public keys for multi-key protocol.
acip_transport_t * host_control_transport
Host control connection retained during WebRTC media.
uint8_t initiator_id[SESSION_ID_SIZE]
uint64_t webrtc_disconnected_since_ms
Start of a transient peer disconnection.
uint8_t identity_pubkey[ED25519_PUBLIC_KEY_SIZE]
Ed25519 public key for this participant (primary key).
uint8_t session_type
0 = DIRECT_TCP, 1 = WEBRTC
uint8_t identity_seckey[ED25519_PRIVATE_KEY_SIZE]
Ed25519 secret key for signing (primary key).
platform_thread_t nat_detection_thread
char password[ACIP_PASSWORD_SIZE]
Session password used when joining password-protected sessions.
uint8_t identity_seckeys[MAX_SESSION_KEYS][ED25519_PRIVATE_KEY_SIZE]
Matching Ed25519 signing keys.
uint8_t participant_id[SESSION_ID_SIZE]
int webrtc_retry_attempt
Current retry attempt number (0 = initial, 1+ = retries).
char turn_password[ACIP_PASSWORD_SIZE]
char turn_username[ACIP_TURN_AUTH_SIZE]
char host_address[ACIP_ADDRESS_SIZE]
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.
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.
void(* on_error)(asciichat_error_t error, const char *message, void *user_data)
size_t num_identity_keys
Number of identity keys loaded.
uint8_t session_id[SESSION_ID_SIZE]
stun_server_t * stun_servers
STUN servers from ACDS.
void(* on_session_ready)(const char *session_string, void *user_data)
char session_string[SESSION_STRING_BUFFER_SIZE]
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
network_connection_t * acds_connection
bool host_elected
True if we've been elected as host (even if waiting for peer).
uint64_t host_loss_detected_ms
Local deadline origin while awaiting ACDS recovery.
void(* on_state_change)(discovery_state_t new_state, void *user_data)
struct webrtc_peer_manager * peer_manager
The useful part of discovery session is in state, error, waiting_peer_start_ns, is_initiator: togethe...
uint8_t host_id[SESSION_ID_SIZE]
When code needs to work with host liveness, it keeps last_ping_sent_ms, last_pong_received_ms,...
uint32_t consecutive_failures
Number of consecutive ping failures.
uint32_t max_failures
Threshold for triggering migration (default: 3).
atomic_t last_pong_received_ms
Timestamp of last pong received (monotonic).
uint64_t ping_interval_ms
Time between pings (default: 3000ms).
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).
NAT quality assessment result.
Definition nat.h:28
Host negotiation context.
Definition negotiate.h:44
For pending ice candidate, this is the live record that ties candidate, mid, next together....