From 0a3c579e198657ae20ef6fed45beb7feae9557a8 Mon Sep 17 00:00:00 2001 From: "Spencer C. Imbleau" Date: Wed, 10 Apr 2024 23:16:20 -0400 Subject: [PATCH] refactor: use send only and readonly protocols --- demos/ping-client/src/main.rs | 17 ++++++++--------- demos/ping-server/src/main.rs | 18 ++++++++++-------- demos/protocol/src/lib.rs | 8 ++++---- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/demos/ping-client/src/main.rs b/demos/ping-client/src/main.rs index 2f93d6d..9de435c 100644 --- a/demos/ping-client/src/main.rs +++ b/demos/ping-client/src/main.rs @@ -1,6 +1,6 @@ use bevy::{log::LogPlugin, prelude::*, time::common_conditions::on_timer}; use bevy_rtc::prelude::*; -use protocol::PingPayload; +use protocol::{PingPayload, PongPayload}; use std::time::Duration; fn main() { @@ -8,7 +8,8 @@ fn main() { .add_plugins(MinimalPlugins) .add_plugins(LogPlugin::default()) .add_plugins(RtcClientPlugin) - .add_client_rw_protocol::(1) + .add_client_wo_protocol::() + .add_client_ro_protocol::(1) .add_systems( OnEnter(RtcClientStatus::Disconnected), // Automatically-reconnect |mut connection_requests: EventWriter| { @@ -20,8 +21,8 @@ fn main() { .add_systems( Update, { - |mut client: RtcClient| { - client.reliable_to_host(PingPayload::Ping); + |mut writer: RtcClient| { + writer.reliable_to_host(PingPayload); info!("Sent ping...") } } @@ -29,11 +30,9 @@ fn main() { on_timer(Duration::from_secs(1)).and_then(in_state(RtcClientStatus::Connected)), ), ) - .add_systems(Update, |mut reader: RtcClient| { - for payload in reader.read() { - if let PingPayload::Pong = payload { - info!("...Received pong!"); - } + .add_systems(Update, |mut reader: RtcClient| { + for _pong in reader.read() { + info!("...Received pong!"); } }) .run(); diff --git a/demos/ping-server/src/main.rs b/demos/ping-server/src/main.rs index 7b395c1..6043afd 100644 --- a/demos/ping-server/src/main.rs +++ b/demos/ping-server/src/main.rs @@ -1,20 +1,22 @@ use bevy::{log::LogPlugin, prelude::*}; use bevy_rtc::prelude::*; -use protocol::PingPayload; +use protocol::{PingPayload, PongPayload}; fn main() { App::new() .add_plugins(MinimalPlugins) .add_plugins(LogPlugin::default()) .add_plugins(RtcServerPlugin { port: 3536 }) - .add_server_rw_protocol::(1) - .add_systems(Update, |mut server: RtcServer| { - for (peer_id, packet) in server.read() { - if let PingPayload::Ping = packet { + .add_server_ro_protocol::(1) + .add_server_wo_protocol::() + .add_systems( + Update, + |mut reader: RtcServer, mut writer: RtcServer| { + for (peer_id, _ping) in reader.read() { info!("Received ping! Sending pong..."); - server.reliable_to_peer(peer_id, PingPayload::Pong); + writer.reliable_to_peer(peer_id, PongPayload); } - } - }) + }, + ) .run(); } diff --git a/demos/protocol/src/lib.rs b/demos/protocol/src/lib.rs index da66b25..8a59d77 100644 --- a/demos/protocol/src/lib.rs +++ b/demos/protocol/src/lib.rs @@ -20,7 +20,7 @@ pub struct ChatPayload { // Used by ping demo #[derive(Protocol, Serialize, Deserialize, Debug, Clone)] -pub enum PingPayload { - Ping, - Pong, -} +pub struct PingPayload; + +#[derive(Protocol, Serialize, Deserialize, Debug, Clone)] +pub struct PongPayload;