Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
fix: readonly/writeonly protocol resource handling
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Apr 11, 2024
1 parent 0a3c579 commit 0fbeff5
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 188 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe

## Unreleased

## 0.3.1

### fixed

- A panic that would occur for ro/wo protocols.

## 0.3.0

### added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"

[workspace.package]
edition = "2021"
version = "0.3.0"
version = "0.3.1"
license = "MIT OR Apache-2.0"
description = "A client-server library designed over WebRTC for Bevy"
repository = "https://github.com/loopystudios/bevy_rtc"
Expand Down
102 changes: 102 additions & 0 deletions bevy_rtc/src/client/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use super::router::{IncomingMessages, OutgoingMessages};
use crate::protocol::Protocol;
use bevy::{ecs::system::SystemParam, prelude::*};

#[derive(SystemParam, Debug)]
pub struct RtcClient<'w, M: Protocol> {
// Option is none if it's send-only or read-only.
pub(crate) incoming: Option<ResMut<'w, IncomingMessages<M>>>,
pub(crate) outgoing: Option<ResMut<'w, OutgoingMessages<M>>>,
}

impl<'w, M: Protocol> RtcClient<'w, M> {
/// Returns the capacity of incoming messages.
pub fn capacity(&self) -> usize {
self.incoming.as_ref().map(|v| v.bound).unwrap_or(0)
}

/// Returns the number of messages waiting in the buffer without draining them.
pub fn len(&self) -> usize {
self.incoming
.as_ref()
.map(|v| v.messages.len())
.unwrap_or(0)
}

/// Returns the number of messages waiting in the buffer without draining them.
pub fn is_empty(&self) -> bool {
self.incoming
.as_ref()
.map(|v| v.messages.is_empty())
.unwrap_or(true)
}

/// Clear all messages waiting in the buffer.
pub fn clear(&mut self) {
if let Some(ref mut incoming) = self.incoming {
incoming.messages.clear()
}
}

/// Consumes all messages in the buffer and iterate on them.
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()
);
}
}

/// Send a payload to the host with reliability. The payload is created with
/// lazy behavior, only when the send rate allows.
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()
);
}
}

/// Send a payload to the host with no expectation of delivery. The payload
/// is created with lazy behavior, only when the send rate allows.
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()
);
}
}

/// Send a payload to the host with reliability.
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()
);
}
}

/// Send a payload to the host with no expectation of delivery.
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()
);
}
}
}
5 changes: 3 additions & 2 deletions bevy_rtc/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#[allow(clippy::module_inception)]
mod client;
mod events;
mod plugin;
mod router;
mod state;
mod system_params;
mod systems;

pub use client::RtcClient;
pub use events::{RtcClientEvent, RtcClientRequestEvent};
pub use plugin::RtcClientPlugin;
pub use router::AddClientProtocolExt;
pub use state::{RtcClientState, RtcClientStatus};
pub use system_params::RtcClient;
58 changes: 0 additions & 58 deletions bevy_rtc/src/client/system_params.rs

This file was deleted.

Loading

0 comments on commit 0fbeff5

Please sign in to comment.