Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Reconnecting AppWeb control client for the signaling room authority.
//!
//! This module is the *I/O driver* only: sockets, TLS, channels, timers, and
//! randomness (jitter sampling). The wire format is shared with the signaling
//! authority — [`AppControlRequest`]/[`AppControlResponse`] live in the shared
//! `signaling-proto` crate — and every protocol decision lives in [`crate::controller`],
//! unit-tested without sockets or a clock: the per-connection [`Controller`]
//! object owns heartbeat/backoff sequencing and the frame decisions, and
//! [`ControlResponseExt`] converts responses into domain values.

use crate::controller::{ControlResponseExt, Controller, FrameAction};
use async_trait::async_trait;
use futures_util::{SinkExt, StreamExt};
use signaling_proto::{Request as AppControlRequest, Response as AppControlResponse};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};
use tokio::net::TcpStream;
use tokio::sync::{mpsc, oneshot};
use tokio_tungstenite::{
    Connector, MaybeTlsStream, WebSocketStream, connect_async, connect_async_tls_with_config,
    tungstenite::Message,
};

pub use crate::controller::{Admission, StatusSnapshot};

#[async_trait]
pub trait RoomAuthority: Send + Sync {
    async fn admit(
        &self,
        roomid: String,
        clientid: String,
        is_loopback: bool,
    ) -> Result<Admission, String>;
    async fn remove(&self, roomid: String, clientid: String) -> Result<(), String>;
    async fn occupancy(&self, roomid: String) -> Result<usize, String>;
    async fn inject(&self, roomid: String, clientid: String, msg: String) -> Result<(), String>;
    async fn status(&self) -> Result<StatusSnapshot, String>;
}

#[derive(Clone)]
pub struct WebSocketAuthority {
    requests: mpsc::Sender<AuthorityRequest>,
    next_requestid: Arc<AtomicU64>,
}

type ControlSocket = WebSocketStream<MaybeTlsStream<TcpStream>>;

const REQUEST_QUEUE_CAPACITY: usize = 1024;
const REQUEST_TIMEOUT: Duration = Duration::from_secs(15);
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(30);
const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(10);
const RECONNECT_JITTER_MS: u64 = 250;

struct AuthorityRequest {
    request: AppControlRequest,
    response: oneshot::Sender<Result<AppControlResponse, String>>,
}

#[derive(Clone)]
struct ConnectionOptions {
    url: String,
    appid: String,
    token: String,
    insecure_tls: bool,
    next_requestid: Arc<AtomicU64>,
}

impl WebSocketAuthority {
    pub async fn connect(url: &str, appid: &str, token: &str) -> Result<Self, String> {
        Self::connect_with_options(url, appid, token, false).await
    }

    pub async fn connect_with_options(
        url: &str,
        appid: &str,
        token: &str,
        insecure_tls: bool,
    ) -> Result<Self, String> {
        let next_requestid = Arc::new(AtomicU64::new(1));
        let options = ConnectionOptions {
            url: url.to_owned(),
            appid: appid.to_owned(),
            token: token.to_owned(),
            insecure_tls,
            next_requestid: next_requestid.clone(),
        };
        let (requests, receiver) = mpsc::channel(REQUEST_QUEUE_CAPACITY);
        tokio::spawn(run_control_worker(options, receiver));
        Ok(Self {
            requests,
            next_requestid,
        })
    }

    async fn request(&self, request: AppControlRequest) -> Result<AppControlResponse, String> {
        let requestid = request.request_id;
        let operation = request.operation_name();
        let (response_tx, response_rx) = oneshot::channel();
        let started = Instant::now();
        let result = tokio::time::timeout(REQUEST_TIMEOUT, async {
            self.requests
                .send(AuthorityRequest {
                    request,
                    response: response_tx,
                })
                .await
                .map_err(|_| "signaling control worker stopped".to_string())?;
            response_rx
                .await
                .map_err(|_| "signaling control worker stopped".to_string())?
        })
        .await
        .unwrap_or_else(|_| {
            Err(format!(
                "signaling {operation} request {requestid} timed out"
            ))
        });
        match &result {
            Ok(_) => log::debug!(
                "Signaling control request completed: operation={operation} request_id={requestid} elapsed_ms={}",
                started.elapsed().as_millis()
            ),
            Err(error) => log::warn!(
                "Signaling control request failed: operation={operation} request_id={requestid} elapsed_ms={} error={error}",
                started.elapsed().as_millis()
            ),
        }
        result
    }
    fn requestid(&self) -> u64 {
        self.next_requestid.fetch_add(1, Ordering::Relaxed)
    }
}

async fn run_control_worker(
    options: ConnectionOptions,
    receiver: mpsc::Receiver<AuthorityRequest>,
) {
    let mut controller = Controller::new();
    let socket = match tokio::time::timeout(CONNECT_TIMEOUT, connect_control(&options, 0)).await {
        Ok(Ok(socket)) => socket,
        Ok(Err(error)) => {
            log::warn!(
                "Signaling control unavailable at AppWeb startup; retrying: app_id={} error={error}",
                options.appid
            );
            reconnect(&options, &mut controller).await
        }
        Err(_) => {
            log::warn!(
                "Signaling control connection timed out at AppWeb startup; retrying: app_id={} timeout_secs={}",
                options.appid,
                CONNECT_TIMEOUT.as_secs()
            );
            reconnect(&options, &mut controller).await
        }
    };
    run_connection(options, socket, receiver).await;
}

async fn open_socket(options: &ConnectionOptions) -> Result<ControlSocket, String> {
    let (socket, _) = if options.insecure_tls && options.url.starts_with("wss://") {
        let config = rustls::ClientConfig::builder()
            .dangerous()
            .with_custom_certificate_verifier(std::sync::Arc::new(NoCertificateVerification))
            .with_no_client_auth();
        connect_async_tls_with_config(
            &options.url,
            None,
            false,
            Some(Connector::Rustls(std::sync::Arc::new(config))),
        )
        .await
    } else {
        connect_async(&options.url).await
    }
    .map_err(|error| error.to_string())?;
    Ok(socket)
}

async fn connect_control(
    options: &ConnectionOptions,
    attempt: u32,
) -> Result<ControlSocket, String> {
    log::info!(
        "Connecting signaling control WebSocket: url={} app_id={} attempt={} insecure_tls={}",
        options.url,
        options.appid,
        attempt,
        options.insecure_tls
    );
    let started = Instant::now();
    let mut socket = open_socket(options).await?;
    let requestid = options.next_requestid.fetch_add(1, Ordering::Relaxed);
    let register =
        AppControlRequest::register(requestid, options.appid.clone(), options.token.clone());
    log::info!(
        "Signaling control registration request: url={} request_id={} app_id={}",
        options.url,
        requestid,
        options.appid
    );
    socket
        .send(Message::binary(register.encode_wire()))
        .await
        .map_err(|e| e.to_string())?;
    let Some(Ok(Message::Binary(frame))) = socket.next().await else {
        return Err("signaling control socket closed during registration".into());
    };
    Controller::registration_ack(&frame, requestid)?;
    log::info!(
        "Signaling control registration response: url={} request_id={} app_id={} result=OK registered=true attempt={} elapsed_ms={}",
        options.url,
        requestid,
        options.appid,
        attempt,
        started.elapsed().as_millis()
    );
    Ok(socket)
}

async fn run_connection(
    options: ConnectionOptions,
    mut socket: ControlSocket,
    mut requests: mpsc::Receiver<AuthorityRequest>,
) {
    let mut heartbeat = tokio::time::interval(HEARTBEAT_INTERVAL);
    heartbeat.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
    heartbeat.tick().await;
    let mut controller = Controller::new();

    loop {
        tokio::select! {
            request = requests.recv() => {
                let Some(request) = request else {
                    log::info!("Signaling control worker stopped: app_id={}", options.appid);
                    let _ = socket.close(None).await;
                    return;
                };
                if request.response.is_closed() {
                    continue;
                }
                let request_id = request.request.request_id;
                let outcome = match tokio::time::timeout(HEARTBEAT_TIMEOUT, exchange(&mut socket, request.request, request_id)).await {
                    Ok(outcome) => outcome,
                    Err(_) => Err(format!("signaling control request {request_id} timed out waiting for response")),
                };
                let failed = outcome.is_err();
                let _ = request.response.send(outcome);
                if failed {
                    log::warn!("Signaling control connection lost during request: app_id={} request_id={request_id}", options.appid);
                    socket = reconnect(&options, &mut controller).await;
                    heartbeat.reset();
                }
            }
            _ = heartbeat.tick() => {
                let sequence = controller.next_heartbeat();
                let started = Instant::now();
                let heartbeat_result = async {
                    socket.send(Message::Ping(sequence.to_be_bytes().to_vec().into())).await.map_err(|error| error.to_string())?;
                    loop {
                        let frame = socket.next().await.ok_or_else(|| "signaling control socket closed".to_string())?.map_err(|error| error.to_string())?;
                        match Controller::classify_frame(frame) {
                            FrameAction::Pong => return Ok::<(), String>(()),
                            FrameAction::Ping(payload) => socket.send(Message::Pong(payload)).await.map_err(|error| error.to_string())?,
                            FrameAction::Closed => return Err("signaling control socket closed".into()),
                            FrameAction::Binary(_) | FrameAction::Ignore => {}
                        }
                    }
                };
                match tokio::time::timeout(HEARTBEAT_TIMEOUT, heartbeat_result).await {
                    Ok(Ok(())) => log::info!("Signaling control heartbeat succeeded: app_id={} sequence={} latency_ms={}", options.appid, sequence, started.elapsed().as_millis()),
                    Ok(Err(error)) => {
                        log::warn!("Signaling control heartbeat failed: app_id={} sequence={} error={error}", options.appid, sequence);
                        socket = reconnect(&options, &mut controller).await;
                        heartbeat.reset();
                    }
                    Err(_) => {
                        log::warn!("Signaling control heartbeat timed out: app_id={} sequence={} timeout_secs={}", options.appid, sequence, HEARTBEAT_TIMEOUT.as_secs());
                        socket = reconnect(&options, &mut controller).await;
                        heartbeat.reset();
                    }
                }
            }
        }
    }
}

async fn exchange(
    socket: &mut ControlSocket,
    request: AppControlRequest,
    request_id: u64,
) -> Result<AppControlResponse, String> {
    let operation = request.operation_name();
    log::info!("Signaling control request: operation={operation} request_id={request_id}");
    socket
        .send(Message::binary(request.encode_wire()))
        .await
        .map_err(|error| error.to_string())?;
    loop {
        let frame = socket
            .next()
            .await
            .ok_or_else(|| "signaling control socket closed".to_string())?
            .map_err(|error| error.to_string())?;
        match Controller::classify_frame(frame) {
            FrameAction::Binary(bytes) => {
                if let Some(response) = Controller::correlate_response(&bytes, request_id)? {
                    log::info!(
                        "Signaling control response: operation={operation} request_id={request_id} result={} reason={}",
                        response.result_name(),
                        response.reason()
                    );
                    return Ok(response);
                }
            }
            FrameAction::Ping(payload) => socket
                .send(Message::Pong(payload))
                .await
                .map_err(|error| error.to_string())?,
            FrameAction::Closed => return Err("signaling control socket closed".into()),
            FrameAction::Pong | FrameAction::Ignore => {}
        }
    }
}

async fn reconnect(options: &ConnectionOptions, controller: &mut Controller) -> ControlSocket {
    loop {
        let jitter = Duration::from_millis(rand::random::<u64>() % RECONNECT_JITTER_MS);
        let (attempt, delay) = controller.schedule_reconnect(jitter);
        log::info!(
            "Signaling control reconnect scheduled: app_id={} attempt={} delay_ms={}",
            options.appid,
            attempt,
            delay.as_millis()
        );
        tokio::time::sleep(delay).await;
        match tokio::time::timeout(CONNECT_TIMEOUT, connect_control(options, attempt)).await {
            Err(_) => log::warn!(
                "Signaling control reconnect timed out: app_id={} attempt={} timeout_secs={}",
                options.appid,
                attempt,
                CONNECT_TIMEOUT.as_secs()
            ),
            Ok(Ok(socket)) => {
                log::info!(
                    "Signaling control reconnected: app_id={} attempt={}",
                    options.appid,
                    attempt
                );
                controller.reconnected();
                return socket;
            }
            Ok(Err(error)) => log::warn!(
                "Signaling control reconnect failed: app_id={} attempt={} error={error}",
                options.appid,
                attempt
            ),
        }
    }
}

#[derive(Debug)]
struct NoCertificateVerification;
impl rustls::client::danger::ServerCertVerifier for NoCertificateVerification {
    fn verify_server_cert(
        &self,
        _: &rustls::pki_types::CertificateDer<'_>,
        _: &[rustls::pki_types::CertificateDer<'_>],
        _: &rustls::pki_types::ServerName<'_>,
        _: &[u8],
        _: rustls::pki_types::UnixTime,
    ) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
        Ok(rustls::client::danger::ServerCertVerified::assertion())
    }
    fn verify_tls12_signature(
        &self,
        _: &[u8],
        _: &rustls::pki_types::CertificateDer<'_>,
        _: &rustls::DigitallySignedStruct,
    ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
    }
    fn verify_tls13_signature(
        &self,
        _: &[u8],
        _: &rustls::pki_types::CertificateDer<'_>,
        _: &rustls::DigitallySignedStruct,
    ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
    }
    fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
        rustls::crypto::ring::default_provider()
            .signature_verification_algorithms
            .supported_schemes()
            .to_vec()
    }
}

#[async_trait]
impl RoomAuthority for WebSocketAuthority {
    async fn admit(
        &self,
        roomid: String,
        clientid: String,
        is_loopback: bool,
    ) -> Result<Admission, String> {
        self.request(AppControlRequest::admit(
            self.requestid(),
            roomid,
            clientid,
            is_loopback,
        ))
        .await?
        .admission()
    }
    async fn remove(&self, roomid: String, clientid: String) -> Result<(), String> {
        self.request(AppControlRequest::remove(
            self.requestid(),
            roomid,
            clientid,
        ))
        .await?
        .ack("remove failed")
        .map(|_| ())
    }
    async fn occupancy(&self, roomid: String) -> Result<usize, String> {
        self.request(AppControlRequest::occupancy(self.requestid(), roomid))
            .await?
            .occupancy_count()
    }
    async fn inject(&self, roomid: String, clientid: String, msg: String) -> Result<(), String> {
        self.request(AppControlRequest::inject(
            self.requestid(),
            roomid,
            clientid,
            msg,
        ))
        .await?
        .ack("inject failed")
        .map(|_| ())
    }
    async fn status(&self) -> Result<StatusSnapshot, String> {
        self.request(AppControlRequest::status(self.requestid()))
            .await?
            .status_snapshot()
    }
}