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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! [WebSocket] client.
//!
//! [WebSocket]: https://developer.mozilla.org/ru/docs/WebSockets

use std::{cell::RefCell, fmt, rc::Rc, time::Duration};

use derive_more::Display;
use futures::{
    channel::{mpsc, oneshot},
    future::LocalBoxFuture,
    stream::{LocalBoxStream, StreamExt as _},
};
use medea_client_api_proto::{
    ClientMsg, CloseReason as CloseByServerReason, Command, Credential, Event,
    MemberId, RoomId, RpcSettings, ServerMsg,
};
use medea_macro::dispatchable;
use medea_reactive::ObservableCell;
use serde::Serialize;
use tracerr::Traced;

use crate::{
    platform,
    rpc::{
        ApiUrl, CloseMsg, CloseReason, ClosedStateReason, ConnectionLostReason,
        Heartbeat, IdleTimeout, PingInterval, RpcClientError,
    },
};

/// Reasons of closing WebSocket RPC connection by a client side.
#[derive(Copy, Clone, Display, Debug, Eq, PartialEq, Serialize)]
pub enum ClientDisconnect {
    /// [`Room`] was dropped without any [`CloseReason`].
    ///
    /// [`Room`]: crate::room::Room
    RoomUnexpectedlyDropped,

    /// [`Room`] was normally closed bu client.
    ///
    /// [`Room`]: crate::room::Room
    RoomClosed,

    /// [`WebSocketRpcClient`] was unexpectedly dropped.
    RpcClientUnexpectedlyDropped,

    /// [`platform::RpcTransport`] was unexpectedly dropped.
    RpcTransportUnexpectedlyDropped,

    /// [`WebSocketRpcSession`] was unexpectedly dropped.
    ///
    /// [`WebSocketRpcSession`]: crate::rpc::WebSocketRpcSession
    SessionUnexpectedlyDropped,
}

impl ClientDisconnect {
    /// Indicates whether this [`ClientDisconnect`] is considered as error.
    #[must_use]
    pub const fn is_err(self) -> bool {
        match self {
            Self::RoomUnexpectedlyDropped
            | Self::RpcClientUnexpectedlyDropped
            | Self::RpcTransportUnexpectedlyDropped
            | Self::SessionUnexpectedlyDropped => true,
            Self::RoomClosed => false,
        }
    }
}

impl From<ClientDisconnect> for CloseReason {
    fn from(v: ClientDisconnect) -> Self {
        Self::ByClient {
            is_err: v.is_err(),
            reason: v,
        }
    }
}

/// State of a [`WebSocketRpcClient`] and a [`platform::RpcTransport`].
#[derive(Clone, Debug, PartialEq)]
pub enum ClientState {
    /// [`WebSocketRpcClient`] is currently establishing a connection to RPC
    /// server.
    Connecting,

    /// Connection with RPC Server is active.
    Open,

    /// Connection with RPC server is currently closed.
    Closed(ClosedStateReason),
}

/// Inner state of [`WebSocketRpcClient`].
struct Inner {
    /// Transport connection with remote media server.
    sock: Option<Rc<dyn platform::RpcTransport>>,

    /// Connection loss detector via ping/pong mechanism.
    heartbeat: Option<Heartbeat>,

    /// Event's subscribers list.
    subs: Vec<mpsc::UnboundedSender<RpcEvent>>,

    /// Subscribers that will be notified with [`CloseReason`] when underlying
    /// transport is gracefully closed.
    on_close_subscribers: Vec<oneshot::Sender<CloseReason>>,

    /// Reason of [`WebSocketRpcClient`] closing.
    ///
    /// This reason will be provided to the underlying
    /// [`platform::RpcTransport`].
    close_reason: ClientDisconnect,

    /// Subscribers that will be notified when underlying transport connection
    /// is lost.
    on_connection_loss_subs: Vec<mpsc::UnboundedSender<ConnectionLostReason>>,

    /// Closure which will create new [`platform::RpcTransport`]s for this
    /// [`WebSocketRpcClient`] on each
    /// [`WebSocketRpcClient:: establish_connection`] call.
    rpc_transport_factory: RpcTransportFactory,

    /// URL that [`platform::RpcTransport`] will connect to.
    ///
    /// [`None`] if this [`WebSocketRpcClient`] has never been connected to
    /// a sever.
    url: Option<ApiUrl>,

    /// Current [`ClientState`] of this [`WebSocketRpcClient`].
    state: ObservableCell<ClientState>,
}

impl fmt::Debug for Inner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Inner")
            .field("heartbeat", &self.heartbeat)
            .field("subs", &self.subs)
            .field("on_close_subscribers", &self.on_close_subscribers)
            .field("close_reason", &self.close_reason)
            .field("on_connection_loss_subs", &self.on_connection_loss_subs)
            .field("url", &self.url)
            .field("state", &self.state)
            .finish_non_exhaustive()
    }
}

/// Factory closure producing a [`platform::RpcTransport`].
pub type RpcTransportFactory = Box<dyn Fn() -> Rc<dyn platform::RpcTransport>>;

impl Inner {
    /// Instantiates new [`Inner`] state of [`WebSocketRpcClient`].
    fn new(rpc_transport_factory: RpcTransportFactory) -> RefCell<Self> {
        RefCell::new(Self {
            sock: None,
            on_close_subscribers: Vec::new(),
            subs: Vec::new(),
            heartbeat: None,
            close_reason: ClientDisconnect::RpcClientUnexpectedlyDropped,
            on_connection_loss_subs: Vec::new(),
            rpc_transport_factory,
            url: None,
            state: ObservableCell::new(ClientState::Closed(
                ClosedStateReason::NeverConnected,
            )),
        })
    }
}

/// Events which can be thrown by [`WebSocketRpcClient`].
#[dispatchable(self: &Self)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RpcEvent {
    /// Notification of the subscribers that [`WebSocketRpcClient`] is joined
    /// [`Room`] on Media Server.
    ///
    /// [`Room`]: crate::room::Room
    JoinedRoom {
        /// ID of the joined [`Room`].
        ///
        /// [`Room`]: crate::room::Room
        room_id: RoomId,

        /// ID of the joined `Member`.
        member_id: MemberId,
    },

    /// Notification of the subscribers that [`WebSocketRpcClient`] left
    /// [`Room`] on Media Server.
    ///
    /// [`Room`]: crate::room::Room
    LeftRoom {
        /// ID of the [`Room`] being left.
        ///
        /// [`Room`]: crate::room::Room
        room_id: RoomId,

        /// Reason of why the [`Room`] has been left.
        ///
        /// [`Room`]: crate::room::Room
        close_reason: CloseReason,
    },

    /// [`WebSocketRpcClient`] received [`Event`] from Media Server.
    Event {
        /// ID of the [`Room`] for that this [`Event`] has been received for.
        ///
        /// [`Room`]: crate::room::Room
        room_id: RoomId,

        /// Received [`Event`].
        event: Event,
    },
}

/// Client API RPC client to talk with server via [WebSocket].
///
/// [WebSocket]: https://developer.mozilla.org/ru/docs/WebSockets
#[derive(Debug)]
pub struct WebSocketRpcClient(RefCell<Inner>);

impl WebSocketRpcClient {
    /// Creates new [`WebSocketRpcClient`] with provided [`RpcTransportFactory`]
    /// closure.
    #[must_use]
    pub fn new(rpc_transport_factory: RpcTransportFactory) -> Self {
        Self(Inner::new(rpc_transport_factory))
    }

    /// Authorizes [`WebSocketRpcClient`] on the Media Server.
    pub fn authorize(
        &self,
        room_id: RoomId,
        member_id: MemberId,
        credential: Credential,
    ) {
        self.send_command(
            room_id,
            Command::JoinRoom {
                member_id,
                credential,
            },
        );
    }

    /// Leaves `Room` with a provided [`RoomId`].
    pub fn leave_room(&self, room_id: RoomId, member_id: MemberId) {
        self.send_command(room_id, Command::LeaveRoom { member_id });
    }

    /// Stops [`Heartbeat`] and notifies all
    /// [`WebSocketRpcClient::on_connection_loss`] subs about connection
    /// loss.
    fn handle_connection_loss(&self, close_msg: ConnectionLostReason) {
        self.0.borrow().state.set(ClientState::Closed(
            ClosedStateReason::ConnectionLost(close_msg),
        ));
        drop(self.0.borrow_mut().heartbeat.take());
        self.0
            .borrow_mut()
            .on_connection_loss_subs
            .retain(|sub| sub.unbounded_send(close_msg).is_ok());
    }

    /// Handles [`CloseMsg`] from a remote server.
    ///
    /// This function will be called on every WebSocket close (normal and
    /// abnormal) regardless of the [`CloseReason`].
    fn handle_close_message(&self, close_msg: CloseMsg) {
        drop(self.0.borrow_mut().heartbeat.take());

        match close_msg {
            CloseMsg::Normal(_, reason) => match reason {
                CloseByServerReason::Reconnected => (),
                CloseByServerReason::Idle => {
                    self.handle_connection_loss(ConnectionLostReason::Idle);
                }
                CloseByServerReason::Finished
                | CloseByServerReason::Rejected
                | CloseByServerReason::InternalError
                | CloseByServerReason::Evicted => {
                    self.0.borrow().state.set(ClientState::Closed(
                        ClosedStateReason::ConnectionLost(
                            ConnectionLostReason::WithMessage(close_msg),
                        ),
                    ));
                    drop(self.0.borrow_mut().sock.take());
                    self.0
                        .borrow_mut()
                        .on_close_subscribers
                        .drain(..)
                        .for_each(|sub| {
                            _ = sub.send(CloseReason::ByServer(reason));
                        });
                }
            },
            CloseMsg::Abnormal(_) => {
                self.handle_connection_loss(ConnectionLostReason::WithMessage(
                    close_msg,
                ));
            }
        }
    }

    /// Handles [`ServerMsg`]s from a remote server.
    fn on_transport_message(&self, msg: ServerMsg) {
        let msg = match msg {
            ServerMsg::Event { room_id, event } => match event {
                Event::RoomJoined { member_id } => {
                    Some(RpcEvent::JoinedRoom { room_id, member_id })
                }
                Event::RoomLeft { close_reason } => Some(RpcEvent::LeftRoom {
                    room_id,
                    close_reason: CloseReason::ByServer(close_reason),
                }),
                Event::PeerCreated { .. }
                | Event::SdpAnswerMade { .. }
                | Event::LocalDescriptionApplied { .. }
                | Event::IceCandidateDiscovered { .. }
                | Event::PeersRemoved { .. }
                | Event::PeerUpdated { .. }
                | Event::ConnectionQualityUpdated { .. }
                | Event::StateSynchronized { .. } => {
                    Some(RpcEvent::Event { room_id, event })
                }
            },
            ServerMsg::RpcSettings(settings) => {
                self.0.borrow_mut().heartbeat.as_ref().map_or_else(
                    || {
                        log::error!(
                            "Failed to update socket settings because \
                             Heartbeat is None",
                        );
                    },
                    |heartbeat| {
                        heartbeat.update_settings(
                            IdleTimeout(Duration::from_millis(
                                settings.idle_timeout_ms.into(),
                            )),
                            PingInterval(Duration::from_millis(
                                settings.ping_interval_ms.into(),
                            )),
                        );
                    },
                );
                None
            }
            ServerMsg::Ping(_) => None,
        };
        if let Some(m) = msg {
            self.0
                .borrow_mut()
                .subs
                .retain(|sub| sub.unbounded_send(m.clone()).is_ok());
        }
    }

    /// Starts [`Heartbeat`] with provided [`RpcSettings`] for provided
    /// [`platform::RpcTransport`].
    async fn start_heartbeat(
        self: Rc<Self>,
        transport: Rc<dyn platform::RpcTransport>,
        rpc_settings: RpcSettings,
    ) -> Result<(), Traced<RpcClientError>> {
        let idle_timeout = IdleTimeout(Duration::from_millis(
            rpc_settings.idle_timeout_ms.into(),
        ));
        let ping_interval = PingInterval(Duration::from_millis(
            rpc_settings.ping_interval_ms.into(),
        ));

        let heartbeat =
            Heartbeat::start(transport, ping_interval, idle_timeout);

        let mut on_idle = heartbeat.on_idle();
        let weak_this = Rc::downgrade(&self);
        platform::spawn(async move {
            while on_idle.next().await.is_some() {
                if let Some(this) = weak_this.upgrade() {
                    this.handle_connection_loss(ConnectionLostReason::Idle);
                }
            }
        });
        self.0.borrow_mut().heartbeat = Some(heartbeat);

        Ok(())
    }

    /// Tries to establish [`WebSocketRpcClient`] connection.
    async fn establish_connection(
        self: Rc<Self>,
        url: ApiUrl,
    ) -> Result<(), Traced<RpcClientError>> {
        self.0.borrow_mut().url = Some(url.clone());
        self.0.borrow().state.set(ClientState::Connecting);

        // wait for transport open
        let transport = (self.0.borrow().rpc_transport_factory)();
        let mut on_message = transport.on_message();
        transport.connect(url).await.map_err(|e| {
            let transport_err = e.into_inner();
            self.0.borrow().state.set(ClientState::Closed(
                ClosedStateReason::CouldNotEstablish(transport_err.clone()),
            ));
            tracerr::new!(RpcClientError::from(
                ClosedStateReason::CouldNotEstablish(transport_err)
            ))
        })?;

        // wait for ServerMsg::RpcSettings
        if let Some(msg) = on_message.next().await {
            if let ServerMsg::RpcSettings(rpc_settings) = msg {
                Rc::clone(&self)
                    .start_heartbeat(Rc::clone(&transport), rpc_settings)
                    .await?;
            } else {
                let close_reason =
                    ClosedStateReason::FirstServerMsgIsNotRpcSettings;
                self.0
                    .borrow()
                    .state
                    .set(ClientState::Closed(close_reason.clone()));
                return Err(tracerr::new!(RpcClientError::ConnectionFailed(
                    close_reason
                )));
            }
        } else {
            self.0.borrow().state.set(ClientState::Closed(
                ClosedStateReason::FirstServerMsgIsNotRpcSettings,
            ));
            return Err(tracerr::new!(RpcClientError::ConnectionFailed(
                ClosedStateReason::FirstServerMsgIsNotRpcSettings
            )));
        }

        // subscribe to transport close
        {
            let mut transport_state_changes = transport.on_state_change();
            let weak_this = Rc::downgrade(&self);
            platform::spawn(async move {
                while let Some(state) = transport_state_changes.next().await {
                    if let Some(this) = weak_this.upgrade() {
                        if let platform::TransportState::Closed(msg) = state {
                            this.handle_close_message(msg);
                        }
                    }
                }
            });
        }

        // subscribe to transport message received
        {
            let weak_this = Rc::downgrade(&self);
            let mut on_socket_message = transport.on_message();
            platform::spawn(async move {
                while let Some(msg) = on_socket_message.next().await {
                    if let Some(this) = weak_this.upgrade() {
                        this.on_transport_message(msg);
                    }
                }
            });
        }

        drop(self.0.borrow_mut().sock.replace(transport));
        self.0.borrow().state.set(ClientState::Open);

        Ok(())
    }

    /// Subscribes to [`WebSocketRpcClient`]'s [`ClientState`] changes and when
    /// [`ClientState::Connecting`] will be changed to something else, then this
    /// [`Future`] will be resolved and based on new [`ClientState`] [`Result`]
    /// will be returned.
    ///
    /// [`Future`]: std::future::Future
    async fn connecting_result(&self) -> Result<(), Traced<RpcClientError>> {
        let mut state_changes = self.0.borrow().state.subscribe();
        while let Some(state) = state_changes.next().await {
            match state {
                ClientState::Open => {
                    return Ok(());
                }
                ClientState::Closed(reason) => {
                    return Err(tracerr::new!(
                        RpcClientError::ConnectionFailed(reason)
                    ));
                }
                ClientState::Connecting => (),
            }
        }
        Err(tracerr::new!(RpcClientError::RpcClientGone))
    }

    /// Tries to upgrade [`ClientState`] of this [`WebSocketRpcClient`] to
    /// [`ClientState::Open`].
    ///
    /// This function is also used for reconnecting this [`WebSocketRpcClient`].
    ///
    /// If [`WebSocketRpcClient`] is closed than this function will try to
    /// establish new RPC connection.
    ///
    /// If [`WebSocketRpcClient`] already in [`ClientState::Connecting`] then
    /// this function will not perform one more connection try. It will
    /// subscribe to [`ClientState`] changes and wait for first connection
    /// result, and, based on this result, this function will be resolved.
    ///
    /// If [`WebSocketRpcClient`] already in [`ClientState::Open`] then this
    /// function will be instantly resolved.
    ///
    /// # Errors
    ///
    /// Errors if [`WebSocketRpcClient`] fails to establish connection with a
    /// server.
    pub async fn connect(
        self: Rc<Self>,
        url: ApiUrl,
    ) -> Result<(), Traced<RpcClientError>> {
        let current_url = self.0.borrow().url.clone();
        if current_url.as_ref() == Some(&url) {
            let state = self.0.borrow().state.borrow().clone();
            match state {
                ClientState::Open => Ok(()),
                ClientState::Connecting => self.connecting_result().await,
                ClientState::Closed(_) => self.establish_connection(url).await,
            }
        } else {
            self.establish_connection(url).await
        }
    }

    /// Subscribes on this [`WebSocketRpcClient`]'s [`RpcEvent`]s.
    pub fn subscribe(&self) -> LocalBoxStream<'static, RpcEvent> {
        let (tx, rx) = mpsc::unbounded();
        self.0.borrow_mut().subs.push(tx);

        Box::pin(rx)
    }

    /// Sends [`Command`] for the provided [`RoomId`] to server.
    pub fn send_command(&self, room_id: RoomId, command: Command) {
        let socket_borrow = &self.0.borrow().sock;

        if let Some(socket) = socket_borrow.as_ref() {
            if let Err(e) = socket
                .send(&ClientMsg::Command { room_id, command })
                .map_err(tracerr::map_from_and_wrap!(=> RpcClientError))
            {
                log::error!("{e}");
            }
        }
    }

    /// [`Future`] resolving on normal [`WebSocketRpcClient`] connection
    /// closing.
    ///
    /// This [`Future`] wouldn't be resolved on abnormal closes.
    /// An [`WebSocketRpcClient::on_connection_loss`] will be thrown instead.
    ///
    /// [`Future`]: std::future::Future
    pub fn on_normal_close(
        &self,
    ) -> LocalBoxFuture<'static, Result<CloseReason, oneshot::Canceled>> {
        let (tx, rx) = oneshot::channel();
        self.0.borrow_mut().on_close_subscribers.push(tx);
        Box::pin(rx)
    }

    /// Subscribe to connection loss events.
    ///
    /// Connection loss is any unexpected [`platform::RpcTransport`] close. In
    /// case of connection loss, client side user should select reconnection
    /// strategy with [`ReconnectHandle`] (or simply close [`Room`]).
    ///
    /// [`ReconnectHandle`]: crate::rpc::ReconnectHandle
    /// [`Room`]: crate::room::Room
    /// [`Stream`]: futures::Stream
    pub fn on_connection_loss(
        &self,
    ) -> LocalBoxStream<'static, ConnectionLostReason> {
        let (tx, rx) = mpsc::unbounded();
        self.0.borrow_mut().on_connection_loss_subs.push(tx);
        Box::pin(rx)
    }

    /// Sets reason being passed to the underlying transport when this client is
    /// dropped.
    pub fn set_close_reason(&self, close_reason: ClientDisconnect) {
        self.0.borrow_mut().close_reason = close_reason;
    }
}

impl Drop for Inner {
    /// Drops the related connection and its [`Heartbeat`].
    fn drop(&mut self) {
        if let Some(socket) = self.sock.take() {
            socket.set_close_reason(self.close_reason);
        }
    }
}