rbp_server/hosting/
handle.rs1use super::Client;
2use rbp_core::ID;
3use rbp_gameroom::Room;
4use std::sync::Arc;
5use tokio::sync::Mutex;
6use tokio::sync::mpsc::UnboundedReceiver;
7use tokio::sync::mpsc::UnboundedSender;
8use tokio::sync::mpsc::unbounded_channel;
9use tokio::sync::oneshot;
10
11pub struct RoomHandle {
14 pub id: ID<Room>,
15 pub tx: UnboundedSender<String>,
16 pub rx: Arc<Mutex<UnboundedReceiver<String>>>,
17 pub start: Option<oneshot::Sender<()>>,
18}
19
20pub struct RoomChannels {
22 pub handle: RoomHandle,
23 pub client: Client,
24 pub start: oneshot::Receiver<()>,
25 pub done_tx: oneshot::Sender<()>,
26 pub done_rx: oneshot::Receiver<()>,
27}
28
29impl RoomHandle {
30 pub fn pair(id: ID<Room>) -> RoomChannels {
33 let (tx_outgoing, rx_outgoing) = unbounded_channel::<String>();
34 let (tx_incoming, rx_incoming) = unbounded_channel::<String>();
35 let (start_tx, start_rx) = oneshot::channel();
36 let (done_tx, done_rx) = oneshot::channel();
37 let client = Client::new(tx_outgoing, Arc::new(Mutex::new(rx_incoming)));
38 let handle = RoomHandle {
39 id,
40 tx: tx_incoming,
41 rx: Arc::new(Mutex::new(rx_outgoing)),
42 start: Some(start_tx),
43 };
44 RoomChannels {
45 handle,
46 client,
47 start: start_rx,
48 done_tx,
49 done_rx,
50 }
51 }
52}