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

Cleanup events on disconnect #166

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Refactor `ServerEventQueue` to have an explicit interface.
- Cleanup all client events on disconnect.
- Moved server event reset system to new set `ClientSet::ResetEvents` in `PreUpdate`.

## [0.19.0] - 2024-01-07
Expand Down
16 changes: 10 additions & 6 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_disconnected,
renet::{Bytes, RenetClient},
transport::NetcodeClientPlugin,
RenetClientPlugin,
};
use bincode::{DefaultOptions, Options};
use varint_rs::VarintReader;

Expand Down Expand Up @@ -33,13 +37,13 @@ impl Plugin for ClientPlugin {
PreUpdate,
ClientSet::Reset
.after(NetcodeClientPlugin::update_system)
.run_if(bevy_renet::client_just_disconnected()),
.run_if(client_just_disconnected()),
)
.configure_sets(
PreUpdate,
ClientSet::ResetEvents
.after(NetcodeClientPlugin::update_system)
.run_if(bevy_renet::client_just_disconnected()),
.run_if(client_just_disconnected()),
)
.configure_sets(
PostUpdate,
Expand Down Expand Up @@ -484,14 +488,14 @@ 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.
Reset,
/// Systems that reset queued server events.
///
/// Runs in `PreUpdate`.
/// Runs in `PreUpdate` when the client just disconnected.
///
/// 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
Expand Down
20 changes: 13 additions & 7 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()),
(
(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),
),
)
.add_systems(PreUpdate, reset_system::<T>.in_set(ClientSet::ResetEvents))
.add_systems(
PostUpdate,
(
Expand Down Expand Up @@ -290,7 +292,11 @@ fn local_resending_system<T: Event>(
}
}

fn reset_system<T: Event>(mut event_queue: ResMut<ServerEventQueue<T>>) {
fn reset_system<T: Event>(
mut events: ResMut<Events<T>>,
mut event_queue: ResMut<ServerEventQueue<T>>,
) {
events.clear();
event_queue.clear();
}

Expand Down