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

Commit

Permalink
refactor: use send only and readonly protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Apr 11, 2024
1 parent 3d6bd54 commit 0a3c579
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
17 changes: 8 additions & 9 deletions demos/ping-client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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() {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins(LogPlugin::default())
.add_plugins(RtcClientPlugin)
.add_client_rw_protocol::<PingPayload>(1)
.add_client_wo_protocol::<PingPayload>()
.add_client_ro_protocol::<PongPayload>(1)
.add_systems(
OnEnter(RtcClientStatus::Disconnected), // Automatically-reconnect
|mut connection_requests: EventWriter<RtcClientRequestEvent>| {
Expand All @@ -20,20 +21,18 @@ fn main() {
.add_systems(
Update,
{
|mut client: RtcClient<PingPayload>| {
client.reliable_to_host(PingPayload::Ping);
|mut writer: RtcClient<PingPayload>| {
writer.reliable_to_host(PingPayload);
info!("Sent ping...")
}
}
.run_if(
on_timer(Duration::from_secs(1)).and_then(in_state(RtcClientStatus::Connected)),
),
)
.add_systems(Update, |mut reader: RtcClient<PingPayload>| {
for payload in reader.read() {
if let PingPayload::Pong = payload {
info!("...Received pong!");
}
.add_systems(Update, |mut reader: RtcClient<PongPayload>| {
for _pong in reader.read() {
info!("...Received pong!");
}
})
.run();
Expand Down
18 changes: 10 additions & 8 deletions demos/ping-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -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::<PingPayload>(1)
.add_systems(Update, |mut server: RtcServer<PingPayload>| {
for (peer_id, packet) in server.read() {
if let PingPayload::Ping = packet {
.add_server_ro_protocol::<PingPayload>(1)
.add_server_wo_protocol::<PongPayload>()
.add_systems(
Update,
|mut reader: RtcServer<PingPayload>, mut writer: RtcServer<PongPayload>| {
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();
}
8 changes: 4 additions & 4 deletions demos/protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 0a3c579

Please sign in to comment.