|
| 1 | +use super::router::{IncomingMessages, OutgoingMessages}; |
| 2 | +use crate::protocol::Protocol; |
| 3 | +use bevy::{ecs::system::SystemParam, prelude::*}; |
| 4 | + |
| 5 | +#[derive(SystemParam, Debug)] |
| 6 | +pub struct RtcClient<'w, M: Protocol> { |
| 7 | + // Option is none if it's send-only or read-only. |
| 8 | + pub(crate) incoming: Option<ResMut<'w, IncomingMessages<M>>>, |
| 9 | + pub(crate) outgoing: Option<ResMut<'w, OutgoingMessages<M>>>, |
| 10 | +} |
| 11 | + |
| 12 | +impl<'w, M: Protocol> RtcClient<'w, M> { |
| 13 | + /// Returns the capacity of incoming messages. |
| 14 | + pub fn capacity(&self) -> usize { |
| 15 | + self.incoming.as_ref().map(|v| v.bound).unwrap_or(0) |
| 16 | + } |
| 17 | + |
| 18 | + /// Returns the number of messages waiting in the buffer without draining them. |
| 19 | + pub fn len(&self) -> usize { |
| 20 | + self.incoming |
| 21 | + .as_ref() |
| 22 | + .map(|v| v.messages.len()) |
| 23 | + .unwrap_or(0) |
| 24 | + } |
| 25 | + |
| 26 | + /// Returns the number of messages waiting in the buffer without draining them. |
| 27 | + pub fn is_empty(&self) -> bool { |
| 28 | + self.incoming |
| 29 | + .as_ref() |
| 30 | + .map(|v| v.messages.is_empty()) |
| 31 | + .unwrap_or(true) |
| 32 | + } |
| 33 | + |
| 34 | + /// Clear all messages waiting in the buffer. |
| 35 | + pub fn clear(&mut self) { |
| 36 | + if let Some(ref mut incoming) = self.incoming { |
| 37 | + incoming.messages.clear() |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// Consumes all messages in the buffer and iterate on them. |
| 42 | + pub fn read(&mut self) -> Vec<M> { |
| 43 | + if let Some(ref mut incoming) = self.incoming { |
| 44 | + incoming.messages.drain(..).collect() |
| 45 | + } else { |
| 46 | + panic!( |
| 47 | + "Attempting to read from `{}` is not allowed, it is registered write only.", |
| 48 | + M::reflect_name() |
| 49 | + ); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// Send a payload to the host with reliability. The payload is created with |
| 54 | + /// lazy behavior, only when the send rate allows. |
| 55 | + pub fn reliable_to_host_with(&mut self, message_fn: impl Fn() -> M) { |
| 56 | + if let Some(ref mut outgoing) = self.outgoing { |
| 57 | + outgoing.reliable_to_host.push(message_fn()); |
| 58 | + } else { |
| 59 | + panic!( |
| 60 | + "Attempting to write `{}` is not allowed, it is registered read only.", |
| 61 | + M::reflect_name() |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// Send a payload to the host with no expectation of delivery. The payload |
| 67 | + /// is created with lazy behavior, only when the send rate allows. |
| 68 | + pub fn unreliable_to_host_with(&mut self, message_fn: impl Fn() -> M) { |
| 69 | + if let Some(ref mut outgoing) = self.outgoing { |
| 70 | + outgoing.unreliable_to_host.push(message_fn()); |
| 71 | + } else { |
| 72 | + panic!( |
| 73 | + "Attempting to write `{}` is not allowed, it is registered read only.", |
| 74 | + M::reflect_name() |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// Send a payload to the host with reliability. |
| 80 | + pub fn reliable_to_host(&mut self, message: M) { |
| 81 | + if let Some(ref mut outgoing) = self.outgoing { |
| 82 | + outgoing.reliable_to_host.push(message); |
| 83 | + } else { |
| 84 | + panic!( |
| 85 | + "Attempting to write `{}` is not allowed, it is registered read only.", |
| 86 | + M::reflect_name() |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// Send a payload to the host with no expectation of delivery. |
| 92 | + pub fn unreliable_to_host(&mut self, message: M) { |
| 93 | + if let Some(ref mut outgoing) = self.outgoing { |
| 94 | + outgoing.unreliable_to_host.push(message); |
| 95 | + } else { |
| 96 | + panic!( |
| 97 | + "Attempting to write `{}` is not allowed, it is registered read only.", |
| 98 | + M::reflect_name() |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments