use super::router::{IncomingMessages, OutgoingMessages};
use crate::protocol::Protocol;
use bevy::{ecs::system::SystemParam, prelude::*};
#[derive(SystemParam, Debug)]
pub struct RtcClient<'w, M: Protocol> {
pub(crate) incoming: Option<ResMut<'w, IncomingMessages<M>>>,
pub(crate) outgoing: Option<ResMut<'w, OutgoingMessages<M>>>,
}
impl<'w, M: Protocol> RtcClient<'w, M> {
pub fn capacity(&self) -> usize {
self.incoming.as_ref().map(|v| v.bound).unwrap_or(0)
}
pub fn len(&self) -> usize {
self.incoming
.as_ref()
.map(|v| v.messages.len())
.unwrap_or(0)
}
pub fn is_empty(&self) -> bool {
self.incoming
.as_ref()
.map(|v| v.messages.is_empty())
.unwrap_or(true)
}
pub fn clear(&mut self) {
if let Some(ref mut incoming) = self.incoming {
incoming.messages.clear()
}
}
pub fn read(&mut self) -> Vec<M> {
if let Some(ref mut incoming) = self.incoming {
incoming.messages.drain(..).collect()
} else {
panic!(
"Attempting to read from `{}` is not allowed, it is registered write only.",
M::reflect_name()
);
}
}
pub fn reliable_to_host_with(&mut self, message_fn: impl Fn() -> M) {
if let Some(ref mut outgoing) = self.outgoing {
outgoing.reliable_to_host.push(message_fn());
} else {
panic!(
"Attempting to write `{}` is not allowed, it is registered read only.",
M::reflect_name()
);
}
}
pub fn unreliable_to_host_with(&mut self, message_fn: impl Fn() -> M) {
if let Some(ref mut outgoing) = self.outgoing {
outgoing.unreliable_to_host.push(message_fn());
} else {
panic!(
"Attempting to write `{}` is not allowed, it is registered read only.",
M::reflect_name()
);
}
}
pub fn reliable_to_host(&mut self, message: M) {
if let Some(ref mut outgoing) = self.outgoing {
outgoing.reliable_to_host.push(message);
} else {
panic!(
"Attempting to write `{}` is not allowed, it is registered read only.",
M::reflect_name()
);
}
}
pub fn unreliable_to_host(&mut self, message: M) {
if let Some(ref mut outgoing) = self.outgoing {
outgoing.unreliable_to_host.push(message);
} else {
panic!(
"Attempting to write `{}` is not allowed, it is registered read only.",
M::reflect_name()
);
}
}
}