#[cfg(feature = "bevy")]
pub mod plugin;
use std::fmt::Display;
use crate::{Message, RecvError, SessionError};
pub trait ServerTransport<C2S, S2C>
where
C2S: Message,
S2C: Message,
{
type ClientInfo;
fn recv(&mut self) -> Result<ServerEvent<C2S>, RecvError>;
fn send(&mut self, client: ClientId, msg: impl Into<S2C>);
fn disconnect(&mut self, client: ClientId);
fn client_info(&self, client: ClientId) -> Option<Self::ClientInfo>;
fn connected(&self, client: ClientId) -> bool;
}
#[derive(Debug)]
#[cfg_attr(feature = "bevy", derive(bevy::prelude::Event))]
pub enum ServerEvent<C2S> {
Connecting {
client: ClientId,
},
Connected {
client: ClientId,
},
Recv {
client: ClientId,
msg: C2S,
},
Disconnected {
client: ClientId,
reason: SessionError,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ClientId(usize);
impl ClientId {
pub fn from_raw(raw: usize) -> Self {
Self(raw)
}
pub fn into_raw(self) -> usize {
self.0
}
}
impl Display for ClientId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}