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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
use super::{
SignallerBuilder,
error::{ChannelError, SignalingError},
};
use crate::{
Error,
webrtc_socket::{
MessageLoopFuture, Packet, PeerEvent, PeerRequest, UseMessenger, UseSignallerBuilder,
message_loop, signaling_loop,
},
};
use bytes::Bytes;
use futures::{
AsyncRead, AsyncWrite, Future, FutureExt, Sink, SinkExt, Stream, StreamExt, TryStreamExt,
future::Fuse, select,
};
use futures_channel::mpsc::{SendError, TrySendError, UnboundedReceiver, UnboundedSender};
use log::{debug, error};
use matchbox_protocol::PeerId;
use std::{collections::HashMap, future::ready, pin::Pin, sync::Arc, task::Poll, time::Duration};
use tokio_util::{
compat::TokioAsyncWriteCompatExt,
io::{CopyToBytes, SinkWriter},
};
/// Configuration options for an ICE server connection.
/// See also: <https://developer.mozilla.org/en-US/docs/Web/API/RTCIceServer#example>
#[derive(Debug, Clone)]
pub struct RtcIceServerConfig {
/// An ICE server instance can have several URLs
pub urls: Vec<String>,
/// A username for authentication with the ICE server
///
/// See: <https://developer.mozilla.org/en-US/docs/Web/API/RTCIceServer/username>
pub username: Option<String>,
/// A password or token when authenticating with a turn server
///
/// See: <https://developer.mozilla.org/en-US/docs/Web/API/RTCIceServer/credential>
pub credential: Option<String>,
}
/// Configuration options for a data channel
/// See also: <https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel>
#[derive(Debug, Clone, Copy)]
pub struct ChannelConfig {
/// Whether messages sent on the channel are guaranteed to arrive in order
/// See also: <https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/ordered>
pub ordered: bool,
/// Maximum number of retransmit attempts of a message before giving up
/// See also: <https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/maxRetransmits>
pub max_retransmits: Option<u16>,
}
impl ChannelConfig {
/// Messages sent via an unreliable channel may arrive in any order or not at all, but arrive as
/// quickly as possible.
pub fn unreliable() -> Self {
ChannelConfig {
ordered: false,
max_retransmits: Some(0),
}
}
/// Messages sent via a reliable channel are guaranteed to arrive (and guaranteed to arrive in
/// order the order they were sent) and will be retransmitted until they arrive.
pub fn reliable() -> Self {
ChannelConfig {
ordered: true,
max_retransmits: None,
}
}
}
impl Default for RtcIceServerConfig {
fn default() -> Self {
Self {
urls: vec![
"stun:stun.l.google.com:19302".to_string(),
"stun:stun1.l.google.com:19302".to_string(),
],
username: Default::default(),
credential: Default::default(),
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct SocketConfig {
/// The url for the room to connect to
///
/// This is a websocket url, starting with `ws://` or `wss://` followed by
/// the hostname and path to a matchbox server, followed by a room id and
/// optional query parameters.
///
/// e.g.: `wss://matchbox.example.com/your_game`
///
/// or: `wss://matchbox.example.com/your_game?next=2`
///
/// The last form will pair player in the order they connect.
pub(crate) room_url: String,
/// Configuration for the (single) ICE server
pub(crate) ice_server: RtcIceServerConfig,
/// Configuration for one or multiple reliable or unreliable data channels
pub(crate) channels: Vec<ChannelConfig>,
/// The amount of attempts to initiate connection
pub(crate) attempts: Option<u16>,
/// Interval at which to send empty requests to the signaling server
pub(crate) keep_alive_interval: Option<Duration>,
}
/// Builder for [`WebRtcSocket`]s.
///
/// Begin with [`WebRtcSocketBuilder::new`] and add at least one channel with
/// [`WebRtcSocketBuilder::add_channel`] before calling
/// [`WebRtcSocketBuilder::build`] to produce the desired [`WebRtcSocket`].
#[derive(Debug, Clone)]
pub struct WebRtcSocketBuilder {
pub(crate) config: SocketConfig,
pub(crate) signaller_builder: Option<Arc<dyn SignallerBuilder>>,
}
impl WebRtcSocketBuilder {
/// Creates a new builder for a connection to a given room with the default ICE
/// server configuration, and three reconnection attempts.
///
/// You must add at least one channel with [`WebRtcSocketBuilder::add_channel`]
/// before you can build the [`WebRtcSocket`]
pub fn new(room_url: impl Into<String>) -> Self {
Self {
config: SocketConfig {
room_url: room_url.into(),
ice_server: RtcIceServerConfig::default(),
channels: Vec::default(),
attempts: Some(3),
keep_alive_interval: Some(Duration::from_secs(10)),
},
signaller_builder: None,
}
}
/// Sets the socket ICE server configuration.
pub fn ice_server(mut self, ice_server: RtcIceServerConfig) -> Self {
self.config.ice_server = ice_server;
self
}
/// Sets the number of attempts to make at reconnecting to the signaling server,
/// if `None` the socket will attempt to connect indefinitely.
///
/// The default is 3 reconnection attempts.
pub fn reconnect_attempts(mut self, attempts: Option<u16>) -> Self {
self.config.attempts = attempts;
self
}
/// Sets the interval at which to send empty requests to the signaling server.
///
/// Some web services (like e.g. nginx as a reverse proxy) will close idle
/// web sockets. Setting this interval will periodically send empty requests
/// to let the signaling server know the client is still connected and
/// prevent disconnections.
///
/// The defaults is 10 seconds.
pub fn signaling_keep_alive_interval(mut self, interval: Option<Duration>) -> Self {
self.config.keep_alive_interval = interval;
self
}
/// Adds a new channel to the [`WebRtcSocket`] configuration according to a [`ChannelConfig`].
pub fn add_channel(mut self, config: ChannelConfig) -> WebRtcSocketBuilder {
self.config.channels.push(config);
self
}
/// Adds a new unreliable channel to the [`WebRtcSocket`] configuration.
pub fn add_unreliable_channel(mut self) -> WebRtcSocketBuilder {
self.config.channels.push(ChannelConfig::unreliable());
self
}
/// Adds a new reliable channel to the [`WebRtcSocket`] configuration.
pub fn add_reliable_channel(mut self) -> WebRtcSocketBuilder {
self.config.channels.push(ChannelConfig::reliable());
self
}
/// Sets an alternative signalling implementation for this [`WebRtcSocket`].
pub fn signaller_builder(mut self, signaller_builder: Arc<dyn SignallerBuilder>) -> Self {
self.signaller_builder = Some(signaller_builder);
self
}
/// Creates a [`WebRtcSocket`] and the corresponding [`MessageLoopFuture`] according to the
/// configuration supplied.
///
/// The returned [`MessageLoopFuture`] should be awaited in order for messages to be sent and
/// received.
pub fn build(self) -> (WebRtcSocket, MessageLoopFuture) {
assert!(
!self.config.channels.is_empty(),
"Must have added at least one channel"
);
let (peer_state_tx, peer_state_rx) = futures_channel::mpsc::unbounded();
let mut peer_messages_out_rx = Vec::with_capacity(self.config.channels.len());
let mut messages_from_peers_tx = Vec::with_capacity(self.config.channels.len());
let mut channels = Vec::with_capacity(self.config.channels.len());
for channel_config in self.config.channels.iter() {
let (messages_from_peers_tx_curr, messages_from_peers_rx_curr) =
futures_channel::mpsc::unbounded();
let (peer_messages_out_tx_curr, peer_messages_out_rx_curr) =
futures_channel::mpsc::unbounded();
peer_messages_out_rx.push(peer_messages_out_rx_curr);
messages_from_peers_tx.push(messages_from_peers_tx_curr);
channels.push(Some(WebRtcChannel {
config: *channel_config,
rx: messages_from_peers_rx_curr,
tx: peer_messages_out_tx_curr,
}));
}
let (id_tx, id_rx) = futures_channel::oneshot::channel();
let signaller_builder = self
.signaller_builder
.unwrap_or_else(|| Arc::new(UseSignallerBuilder::default()));
let socket_fut = run_socket(
signaller_builder,
id_tx,
self.config,
peer_messages_out_rx,
peer_state_tx,
messages_from_peers_tx,
)
// Transform the source into a user-error.
.map(|f| {
f.map_err(|e| match e {
SignalingError::UndeliverableSignal(e) => Error::Disconnected(e.into()),
SignalingError::NegotiationFailed(e) => Error::ConnectionFailed(*e),
SignalingError::WebSocket(e) => Error::Disconnected(e.into()),
SignalingError::UserImplementationError(_) => Error::ConnectionFailed(e),
SignalingError::UnknownFormat | SignalingError::StreamExhausted => {
unimplemented!("these errors should never be propagated here")
}
})
});
(
WebRtcSocket {
id: Default::default(),
id_rx,
peer_state_rx,
peers: Default::default(),
channels,
},
Box::pin(socket_fut),
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// The state of a connection to a peer
pub enum PeerState {
/// The peer is connected
///
/// This means all of the following should be true:
///
/// - The requested data channels have been established and are healthy
/// - The peer hasn't left the signaling server
Connected,
/// We no longer have a connection to this peer:
///
/// This means either:
///
/// - Some of the the data channels got disconnected/closed
/// - The peer left the signaling server
Disconnected,
}
/// Used to send and receive packets on a given WebRTC channel. Must be created as part of a
/// [`WebRtcSocket`].
#[derive(Debug)]
pub struct WebRtcChannel {
config: ChannelConfig,
tx: UnboundedSender<(PeerId, Packet)>,
rx: UnboundedReceiver<(PeerId, Packet)>,
}
impl WebRtcChannel {
/// Split the channel into a reader and writer.
/// Useful for concurrently sending and receiving messages using async code.
#[allow(clippy::type_complexity)]
pub fn split(
self,
) -> (
UnboundedSender<(PeerId, Packet)>,
UnboundedReceiver<(PeerId, Packet)>,
) {
(self.tx, self.rx)
}
/// Clone a sender for this channel.
/// Useful for sending messages to the same channel from multiple threads/async tasks.
pub fn sender_clone(&self) -> UnboundedSender<(PeerId, Packet)> {
self.tx.clone()
}
/// Returns the [`ChannelConfig`] used to create this channel.
pub fn config(&self) -> &ChannelConfig {
&self.config
}
/// Returns whether it's still possible to send messages.
pub fn is_closed(&self) -> bool {
self.tx.is_closed()
}
/// Close this channel.
///
/// This prevents sending and receiving any messages in the future, but does not drain messages
/// that are buffered.
pub fn close(&mut self) {
self.tx.close_channel();
self.rx.close();
}
/// Call this where you want to handle new received messages. Returns immediately.
///
/// Messages are removed from the socket when called.
pub fn receive(&mut self) -> Vec<(PeerId, Packet)> {
let mut messages = vec![];
while let Ok(Some(x)) = self.rx.try_next() {
messages.push(x);
}
messages
}
/// Try to send a packet to the given peer. An error is propagated if the socket future
/// is dropped. `Ok` is not a guarantee of delivery.
pub fn try_send(&mut self, packet: Packet, peer: PeerId) -> Result<(), SendError> {
self.tx
.unbounded_send((peer, packet))
.map_err(TrySendError::into_send_error)
}
/// Send a packet to the given peer. There is no guarantee of delivery.
///
/// # Panics
/// Panics if the socket future is dropped.
pub fn send(&mut self, packet: Packet, peer: PeerId) {
self.try_send(packet, peer).expect("Send failed");
}
}
impl Stream for WebRtcChannel {
type Item = (PeerId, Packet);
fn poll_next(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
let mut rx = Pin::new(&mut self.get_mut().rx);
rx.as_mut().poll_next(cx)
}
}
impl Sink<(PeerId, Packet)> for WebRtcChannel {
type Error = SendError;
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
let mut tx = Pin::new(&mut self.get_mut().tx);
tx.as_mut().poll_ready(cx)
}
fn start_send(self: Pin<&mut Self>, item: (PeerId, Packet)) -> Result<(), Self::Error> {
let mut tx = Pin::new(&mut self.get_mut().tx);
tx.as_mut().start_send(item)
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
let mut tx = Pin::new(&mut self.get_mut().tx);
tx.as_mut().poll_flush(cx)
}
fn poll_close(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
let mut tx = Pin::new(&mut self.get_mut().tx);
tx.as_mut().poll_close(cx)
}
}
/// A channel which supports reading and writing raw bytes.
pub struct RawPeerChannel<R, W> {
id: Option<PeerId>,
remote: PeerId,
reader: R,
writer: W,
}
impl<R, W> RawPeerChannel<R, W> {
/// Returns the id of this peer.
///
/// Also see [`WebRtcSocket::id`].
pub fn id(&self) -> Option<PeerId> {
self.id
}
/// Returns the id of the remote peer to which this channel is connected.
pub fn remote(&self) -> PeerId {
self.remote
}
}
impl<R, W> AsyncRead for RawPeerChannel<R, W>
where
Self: Unpin,
R: AsyncRead + Unpin,
{
fn poll_read(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &mut [u8],
) -> Poll<std::io::Result<usize>> {
let mut reader = Pin::new(&mut self.get_mut().reader);
reader.as_mut().poll_read(cx, buf)
}
}
impl<R, W> AsyncWrite for RawPeerChannel<R, W>
where
Self: Unpin,
W: AsyncWrite + Unpin,
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
let mut writer = Pin::new(&mut self.get_mut().writer);
writer.as_mut().poll_write(cx, buf)
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<std::io::Result<()>> {
let mut writer = Pin::new(&mut self.get_mut().writer);
writer.as_mut().poll_flush(cx)
}
fn poll_close(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<std::io::Result<()>> {
let mut writer = Pin::new(&mut self.get_mut().writer);
writer.as_mut().poll_close(cx)
}
}
/// Contains a set of [`WebRtcChannel`]s and connection metadata.
#[derive(Debug)]
pub struct WebRtcSocket {
id: once_cell::race::OnceBox<PeerId>,
id_rx: futures_channel::oneshot::Receiver<PeerId>,
peer_state_rx: futures_channel::mpsc::UnboundedReceiver<(PeerId, PeerState)>,
peers: HashMap<PeerId, PeerState>,
channels: Vec<Option<WebRtcChannel>>,
}
impl WebRtcSocket {
/// Creates a new builder for a connection to a given room with a given number of
/// re-connection attempts.
///
/// You must add at least one channel with [`WebRtcSocketBuilder::add_channel`]
/// before you can build the [`WebRtcSocket`]
pub fn builder(room_url: impl Into<String>) -> WebRtcSocketBuilder {
WebRtcSocketBuilder::new(room_url)
}
/// Creates a [`WebRtcSocket`] and the corresponding [`MessageLoopFuture`] for a
/// socket with a single unreliable channel.
///
/// The returned [`MessageLoopFuture`] should be awaited in order for messages to
/// be sent and received.
///
/// Please use the [`WebRtcSocketBuilder`] to create non-trivial sockets.
pub fn new_unreliable(room_url: impl Into<String>) -> (WebRtcSocket, MessageLoopFuture) {
WebRtcSocketBuilder::new(room_url)
.add_channel(ChannelConfig::unreliable())
.build()
}
/// Creates a [`WebRtcSocket`] and the corresponding [`MessageLoopFuture`] for a
/// socket with a single reliable channel.
///
/// The returned [`MessageLoopFuture`] should be awaited in order for messages to
/// be sent and received.
///
/// Please use the [`WebRtcSocketBuilder`] to create non-trivial sockets.
pub fn new_reliable(room_url: impl Into<String>) -> (WebRtcSocket, MessageLoopFuture) {
WebRtcSocketBuilder::new(room_url)
.add_channel(ChannelConfig::reliable())
.build()
}
}
impl Stream for WebRtcSocket {
type Item = (PeerId, PeerState);
fn poll_next(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
let mut peer_state_rx = Pin::new(&mut self.get_mut().peer_state_rx);
peer_state_rx.as_mut().poll_next(cx)
}
}
impl WebRtcSocket {
// Todo: Disconnect from the peer, severing all communication channels.
// pub fn disconnect(&mut self, peer: PeerId) {}
/// Close this socket, disconnecting all channels.
pub fn close(&mut self) {
self.channels
.iter_mut()
.filter_map(Option::as_mut)
.for_each(|c| c.close());
}
/// Handle peers connecting or disconnecting
///
/// Constructed using [`WebRtcSocketBuilder`].
///
/// Update the set of peers used by [`WebRtcSocket::connected_peers`] and
/// [`WebRtcSocket::disconnected_peers`].
///
/// Returns the peers that connected or disconnected since the last time
/// this method was called.
///
/// See also: [`PeerState`]
///
/// # Panics
///
/// Will panic if the socket future has been dropped.
///
/// [`WebRtcSocket::try_update_peers`] is the equivalent method that will instead return a
/// `Result`.
pub fn update_peers(&mut self) -> Vec<(PeerId, PeerState)> {
self.try_update_peers().unwrap()
}
/// Similar to [`WebRtcSocket::update_peers`]. Will instead return a Result::Err if the
/// socket is closed.
pub fn try_update_peers(&mut self) -> Result<Vec<(PeerId, PeerState)>, ChannelError> {
let mut changes = Vec::new();
while let Ok(res) = self.peer_state_rx.try_next() {
match res {
Some((id, state)) => {
let old = self.peers.insert(id, state);
if old != Some(state) {
changes.push((id, state));
}
}
None => return Err(ChannelError::Closed),
}
}
Ok(changes)
}
/// Returns an iterator of the ids of the connected peers.
///
/// Note: You have to call [`WebRtcSocket::update_peers`] for this list to be accurate.
///
/// See also: [`WebRtcSocket::disconnected_peers`]
pub fn connected_peers(&'_ self) -> impl std::iter::Iterator<Item = PeerId> + '_ {
self.peers.iter().filter_map(|(id, state)| {
if state == &PeerState::Connected {
Some(*id)
} else {
None
}
})
}
/// Returns an iterator of the ids of peers that are no longer connected.
///
/// Note: You have to call [`WebRtcSocket::update_peers`] for this list to be
/// accurate.
///
/// See also: [`WebRtcSocket::connected_peers`]
pub fn disconnected_peers(&self) -> impl std::iter::Iterator<Item = &PeerId> {
self.peers.iter().filter_map(|(id, state)| {
if state == &PeerState::Disconnected {
Some(id)
} else {
None
}
})
}
/// Returns the id of this peer, this may be `None` if an id has not yet
/// been assigned by the server.
pub fn id(&mut self) -> Option<PeerId> {
if let Some(id) = self.id.get() {
Some(*id)
} else if let Ok(Some(id)) = self.id_rx.try_recv() {
let id = self.id.get_or_init(|| id.into());
Some(*id)
} else {
None
}
}
/// Gets an immutable reference to the [`WebRtcChannel`] of a given id.
///
/// ```
/// use matchbox_socket::*;
///
/// let (mut socket, message_loop) = WebRtcSocketBuilder::new("wss://example.invalid/")
/// .add_channel(ChannelConfig::reliable())
/// .add_channel(ChannelConfig::unreliable())
/// .build();
/// let is_closed = socket.channel(0).is_closed();
/// ```
///
/// See also: [`WebRtcSocket::channel_mut`], [`WebRtcSocket::get_channel`],
/// [`WebRtcSocket::take_channel`]
///
/// # Panics
///
/// will panic if the channel cannot be found.
pub fn channel(&self, channel: usize) -> &WebRtcChannel {
self.get_channel(channel).unwrap()
}
/// Gets a mutable reference to the [`WebRtcChannel`] of a given id.
///
/// ```
/// use matchbox_socket::*;
///
/// let (mut socket, message_loop) = WebRtcSocketBuilder::new("wss://example.invalid/")
/// .add_channel(ChannelConfig::reliable())
/// .add_channel(ChannelConfig::unreliable())
/// .build();
/// let reliable_channel_messages = socket.channel_mut(0).receive();
/// ```
///
/// See also: [`WebRtcSocket::channel`], [`WebRtcSocket::get_channel_mut`],
/// [`WebRtcSocket::take_channel`]
///
/// # Panics
///
/// will panic if the channel cannot be found.
pub fn channel_mut(&mut self, channel: usize) -> &mut WebRtcChannel {
self.get_channel_mut(channel).unwrap()
}
/// Gets an immutable reference to the [`WebRtcChannel`] of a given id.
///
/// Returns an error if the channel was not found.
///
/// ```
/// use matchbox_socket::*;
///
/// let (mut socket, message_loop) = WebRtcSocketBuilder::new("wss://example.invalid/")
/// .add_channel(ChannelConfig::reliable())
/// .add_channel(ChannelConfig::unreliable())
/// .build();
/// let is_closed = socket.get_channel(0).unwrap().is_closed();
/// ```
///
/// See also: [`WebRtcSocket::get_channel_mut`], [`WebRtcSocket::take_channel`]
pub fn get_channel(&self, channel: usize) -> Result<&WebRtcChannel, ChannelError> {
self.channels
.get(channel)
.ok_or(ChannelError::NotFound)?
.as_ref()
.ok_or(ChannelError::Taken)
}
/// Gets a mutable reference to the [`WebRtcChannel`] of a given id.
///
/// Returns an error if the channel was not found.
///
/// ```
/// use matchbox_socket::*;
///
/// let (mut socket, message_loop) = WebRtcSocketBuilder::new("wss://example.invalid/")
/// .add_channel(ChannelConfig::reliable())
/// .add_channel(ChannelConfig::unreliable())
/// .build();
/// let reliable_channel_messages = socket.get_channel_mut(0).unwrap().receive();
/// ```
///
/// See also: [`WebRtcSocket::channel`], [`WebRtcSocket::take_channel`]
pub fn get_channel_mut(&mut self, channel: usize) -> Result<&mut WebRtcChannel, ChannelError> {
self.channels
.get_mut(channel)
.ok_or(ChannelError::NotFound)?
.as_mut()
.ok_or(ChannelError::Taken)
}
/// Takes the [`WebRtcChannel`] of a given id.
///
/// ```
/// use matchbox_socket::*;
///
/// let (mut socket, message_loop) = WebRtcSocketBuilder::new("wss://example.invalid/")
/// .add_channel(ChannelConfig::reliable())
/// .add_channel(ChannelConfig::unreliable())
/// .build();
/// let reliable_channel = socket.take_channel(0).unwrap();
/// let unreliable_channel = socket.take_channel(1).unwrap();
/// ```
///
/// See also: [`WebRtcSocket::channel`]
pub fn take_channel(&mut self, channel: usize) -> Result<WebRtcChannel, ChannelError> {
self.channels
.get_mut(channel)
.ok_or(ChannelError::NotFound)?
.take()
.ok_or(ChannelError::Taken)
}
/// Takes the [`WebRtcChannel`] of a given [`PeerId`].
pub fn take_channel_by_id(&mut self, id: PeerId) -> Result<WebRtcChannel, ChannelError> {
let pos = self
.connected_peers()
.position(|peer_id| peer_id == id)
.ok_or(ChannelError::NotFound)?;
self.take_channel(pos)
}
/// Converts the [`WebRtcChannel`] of a given [`PeerId`] into a [`RawPeerChannel`].
pub fn take_raw_by_id(
&mut self,
remote: PeerId,
) -> Result<RawPeerChannel<impl AsyncRead + use<>, impl AsyncWrite + use<>>, ChannelError> {
let channel = self.take_channel_by_id(remote)?;
let id = self.id();
let (reader, writer) = compat_read_write(remote, channel.rx, channel.tx);
let peer_channel = RawPeerChannel {
id,
remote,
reader,
writer,
};
Ok(peer_channel)
}
/// Returns whether any socket channel is closed
pub fn any_channel_closed(&self) -> bool {
self.channels
.iter()
.filter_map(Option::as_ref)
.any(|c| c.is_closed())
}
/// Returns whether all socket channels are closed
pub fn all_channels_closed(&self) -> bool {
self.channels
.iter()
.filter_map(Option::as_ref)
.all(|c| c.is_closed())
}
}
pub(crate) fn create_data_channels_ready_fut(
channel_configs: &[ChannelConfig],
) -> (
Vec<futures_channel::mpsc::Sender<()>>,
Pin<Box<Fuse<impl Future<Output = ()> + use<>>>>,
) {
let (senders, receivers) = (0..channel_configs.len())
.map(|_| futures_channel::mpsc::channel(1))
.unzip();
(senders, Box::pin(wait_for_ready(receivers).fuse()))
}
async fn wait_for_ready(channel_ready_rx: Vec<futures_channel::mpsc::Receiver<()>>) {
for mut receiver in channel_ready_rx {
if receiver.next().await.is_none() {
panic!("Sender closed before channel was ready");
}
}
}
/// All the channels needed for the messaging loop.
pub struct MessageLoopChannels {
pub requests_sender: futures_channel::mpsc::UnboundedSender<PeerRequest>,
pub events_receiver: futures_channel::mpsc::UnboundedReceiver<PeerEvent>,
pub peer_messages_out_rx: Vec<futures_channel::mpsc::UnboundedReceiver<(PeerId, Packet)>>,
pub peer_state_tx: futures_channel::mpsc::UnboundedSender<(PeerId, PeerState)>,
pub messages_from_peers_tx: Vec<futures_channel::mpsc::UnboundedSender<(PeerId, Packet)>>,
}
async fn run_socket(
builder: Arc<dyn SignallerBuilder>,
id_tx: futures_channel::oneshot::Sender<PeerId>,
config: SocketConfig,
peer_messages_out_rx: Vec<futures_channel::mpsc::UnboundedReceiver<(PeerId, Packet)>>,
peer_state_tx: futures_channel::mpsc::UnboundedSender<(PeerId, PeerState)>,
messages_from_peers_tx: Vec<futures_channel::mpsc::UnboundedSender<(PeerId, Packet)>>,
) -> Result<(), SignalingError> {
debug!("Starting WebRtcSocket");
let (requests_sender, requests_receiver) = futures_channel::mpsc::unbounded::<PeerRequest>();
let (events_sender, events_receiver) = futures_channel::mpsc::unbounded::<PeerEvent>();
let signaling_loop_fut = signaling_loop(
builder,
config.attempts,
config.room_url,
requests_receiver,
events_sender,
);
let channels = MessageLoopChannels {
requests_sender,
events_receiver,
peer_messages_out_rx,
peer_state_tx,
messages_from_peers_tx,
};
let message_loop_fut = message_loop::<UseMessenger>(
id_tx,
&config.ice_server,
&config.channels,
channels,
config.keep_alive_interval,
);
let mut message_loop_done = Box::pin(message_loop_fut.fuse());
let mut signaling_loop_done = Box::pin(signaling_loop_fut.fuse());
loop {
select! {
msgloop = message_loop_done => {
match msgloop {
Ok(()) | Err(SignalingError::StreamExhausted) => {
debug!("Message loop completed");
break Ok(())
},
Err(e) => {
// TODO: Reconnect X attempts if configured to reconnect.
error!("The message loop finished with an error: {e:?}");
break Err(e);
},
}
}
sigloop = signaling_loop_done => {
match sigloop {
Ok(()) => debug!("Signaling loop completed"),
Err(SignalingError::StreamExhausted) => {
debug!("Signaling loop completed");
break Ok(());
},
Err(e) => {
// TODO: Reconnect X attempts if configured to reconnect.
error!("The signaling loop finished with an error: {e:?}");
break Err(e);
},
}
}
complete => break Ok(())
}
}
}
fn compat_read_write(
remote: PeerId,
stream: UnboundedReceiver<(PeerId, Packet)>,
sink: UnboundedSender<(PeerId, Packet)>,
) -> (impl AsyncRead, impl AsyncWrite) {
let reader = stream
.then(|(_, packet)| ready(Ok::<_, std::io::Error>(packet)))
.into_async_read();
let writer = sink
.with(move |packet: Bytes| ready(Ok::<_, SendError>((remote, Box::from(packet.as_ref())))));
let writer = writer.sink_map_err(std::io::Error::other);
let writer = CopyToBytes::new(writer);
let writer = SinkWriter::new(writer);
let writer = TokioAsyncWriteCompatExt::compat_write(writer);
(reader, writer)
}
#[cfg(test)]
mod test {
use crate::{ChannelConfig, Error, WebRtcSocketBuilder};
#[futures_test::test]
async fn unreachable_server() {
// .invalid is a reserved tld for testing and documentation
let (_socket, fut) = WebRtcSocketBuilder::new("wss://example.invalid")
.add_channel(ChannelConfig::unreliable())
.build();
let result = fut.await;
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
Error::ConnectionFailed { .. }
));
}
#[futures_test::test]
async fn test_signaling_attempts() {
let (_socket, loop_fut) = WebRtcSocketBuilder::new("wss://example.invalid/")
.reconnect_attempts(Some(3))
.add_channel(ChannelConfig::reliable())
.build();
let result = loop_fut.await;
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
Error::ConnectionFailed { .. },
));
}
}