Skip to main content

rbp_server/hosting/
handle.rs

1use 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
11/// Handle to communicate with a running room.
12/// Stores channel endpoints for bridging WebSocket to Client player.
13pub 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
20/// Channels for room lifecycle coordination.
21pub 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    /// Creates paired channels for room communication.
31    /// Returns channels for Casino (handle, done_rx), Room (client, start, done_tx).
32    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}