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

Commit 0fbeff5

Browse files
committed
fix: readonly/writeonly protocol resource handling
1 parent 0a3c579 commit 0fbeff5

File tree

8 files changed

+333
-188
lines changed

8 files changed

+333
-188
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe
66

77
## Unreleased
88

9+
## 0.3.1
10+
11+
### fixed
12+
13+
- A panic that would occur for ro/wo protocols.
14+
915
## 0.3.0
1016

1117
### added

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ resolver = "2"
44

55
[workspace.package]
66
edition = "2021"
7-
version = "0.3.0"
7+
version = "0.3.1"
88
license = "MIT OR Apache-2.0"
99
description = "A client-server library designed over WebRTC for Bevy"
1010
repository = "https://github.com/loopystudios/bevy_rtc"

bevy_rtc/src/client/client.rs

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

bevy_rtc/src/client/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
#[allow(clippy::module_inception)]
2+
mod client;
13
mod events;
24
mod plugin;
35
mod router;
46
mod state;
5-
mod system_params;
67
mod systems;
78

9+
pub use client::RtcClient;
810
pub use events::{RtcClientEvent, RtcClientRequestEvent};
911
pub use plugin::RtcClientPlugin;
1012
pub use router::AddClientProtocolExt;
1113
pub use state::{RtcClientState, RtcClientStatus};
12-
pub use system_params::RtcClient;

bevy_rtc/src/client/system_params.rs

-58
This file was deleted.

0 commit comments

Comments
 (0)