werewolves/werewolves-server/src/communication/host.rs

40 lines
970 B
Rust

use tokio::sync::{broadcast::Sender, mpsc::Receiver};
use werewolves_proto::{
error::GameError,
message::host::{HostMessage, ServerToHostMessage},
};
pub struct HostComms {
send: Sender<ServerToHostMessage>,
recv: Receiver<HostMessage>,
}
impl HostComms {
pub const fn new(
host_send: Sender<ServerToHostMessage>,
host_recv: Receiver<HostMessage>,
) -> Self {
Self {
send: host_send,
recv: host_recv,
}
}
#[cfg(debug_assertions)]
pub async fn recv(&mut self) -> Option<HostMessage> {
self.recv.recv().await
}
#[cfg(not(debug_assertions))]
pub async fn recv(&mut self) -> Option<HostMessage> {
self.recv.recv().await
}
pub fn send(&mut self, message: ServerToHostMessage) -> Result<(), GameError> {
self.send
.send(message)
.map_err(|err| GameError::GenericError(err.to_string()))?;
Ok(())
}
}