Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up events more rigorously #163

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ pub mod diagnostics;
use std::io::Cursor;

use bevy::{prelude::*, utils::EntityHashMap};
use bevy_renet::{client_connected, renet::Bytes};
use bevy_renet::{renet::RenetClient, transport::NetcodeClientPlugin, RenetClientPlugin};
use bevy_renet::{
client_connected, client_just_connected, client_just_disconnected,
renet::{Bytes, RenetClient},
transport::NetcodeClientPlugin,
RenetClientPlugin,
};
use bincode::{DefaultOptions, Options};
use varint_rs::VarintReader;

Expand All @@ -27,19 +31,20 @@ impl Plugin for ClientPlugin {
.init_resource::<BufferedUpdates>()
.configure_sets(
PreUpdate,
ClientSet::Receive.after(NetcodeClientPlugin::update_system),
ClientSet::ResetEvents
.after(NetcodeClientPlugin::update_system)
.before(ClientSet::Receive)
.run_if(client_just_connected()),
)
.configure_sets(
PreUpdate,
ClientSet::Reset
.after(NetcodeClientPlugin::update_system)
.run_if(bevy_renet::client_just_disconnected()),
ClientSet::Receive.after(NetcodeClientPlugin::update_system),
)
.configure_sets(
PreUpdate,
ClientSet::ResetEvents
ClientSet::Reset
.after(NetcodeClientPlugin::update_system)
.run_if(bevy_renet::client_just_disconnected()),
.run_if(client_just_disconnected()),
)
.configure_sets(
PostUpdate,
Expand Down Expand Up @@ -484,18 +489,22 @@ pub enum ClientSet {
Send,
/// Systems that reset the client.
///
/// Runs in `PreUpdate`.
/// Runs in `PreUpdate` when the client just disconnected.
///
/// If this set is disabled, then you need to manually clean up the client after a disconnect or when
/// reconnecting.
/// You may want to disable this set if you want to preserve client replication state across reconnects.
/// In that case, you need to manually repair the client state (or use something like
/// [bevy_replicon_repair](https://github.com/UkoeHB/bevy_replicon_repair)).
Reset,
/// Systems that reset queued server events.
///
/// Runs in `PreUpdate`.
/// Runs in `PreUpdate` immediately after the client connects to ensure client sessions have a fresh start.
///
/// This is a separate set from `ClientSet::Reset` in case you want to enable/disable it separately.
/// In general it is best practice to clear queued server events after a disconnect, whereas you may want to
/// preserve replication state (in which case `ClientSet::Reset` should be disabled).
Comment on lines -496 to -498
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe keep this part? Looks like a useful explanation.

/// This is a separate set from `ClientSet::Reset` because the reset requirements for events are different
/// from the replicon client internals.
/// It is best practice to discard client-sent and server-received events while the client is not connected
/// in order to guarantee clean separation between connection sessions.
ResetEvents,
}

Expand Down
19 changes: 16 additions & 3 deletions src/network_event/client_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,12 @@ impl ClientEventAppExt for App {
.insert_resource(ClientEventChannel::<T>::new(channel_id))
.add_systems(
PreUpdate,
receiving_system
.in_set(ServerSet::Receive)
.run_if(resource_exists::<RenetServer>()),
(
reset_system::<T>.in_set(ClientSet::ResetEvents),
receiving_system
.in_set(ServerSet::Receive)
.run_if(resource_exists::<RenetServer>()),
),
)
.add_systems(
PostUpdate,
Expand Down Expand Up @@ -220,6 +223,16 @@ fn local_resending_system<T: Event>(
}
}

/// Discards all pending events.
///
/// We discard events while waiting to connect to ensure clean reconnects.
fn reset_system<T: Event>(mut events: ResMut<Events<T>>) {
let drained_count = events.drain().count();
if drained_count > 0 {
warn!("discarded {drained_count} client events due to a disconnect");
}
}

/// An event indicating that a message from client was received.
/// Emited only on server.
#[derive(Clone, Copy, Event)]
Expand Down
23 changes: 17 additions & 6 deletions src/network_event/server_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,15 @@ impl ServerEventAppExt for App {
.insert_resource(ServerEventChannel::<T>::new(channel_id))
.add_systems(
PreUpdate,
(queue_system::<T>, receiving_system)
.chain()
.after(ClientPlugin::replication_receiving_system)
.in_set(ClientSet::Receive)
.run_if(client_connected()),
(
reset_system::<T>.in_set(ClientSet::ResetEvents),
(queue_system::<T>, receiving_system)
.chain()
.after(ClientPlugin::replication_receiving_system)
.in_set(ClientSet::Receive)
.run_if(client_connected()),
),
)
.add_systems(PreUpdate, reset_system::<T>.in_set(ClientSet::ResetEvents))
.add_systems(
PostUpdate,
(
Expand Down Expand Up @@ -290,7 +292,16 @@ fn local_resending_system<T: Event>(
}
}

/// Clears queued events.
///
/// We clear events while waiting for a connection to ensure clean reconnects.
fn reset_system<T: Event>(mut event_queue: ResMut<ServerEventQueue<T>>) {
if !event_queue.0.is_empty() {
warn!(
"discarding {} queued server events due to a disconnect",
event_queue.0.values_len()
);
}
event_queue.0.clear();
}

Expand Down