Skip to content

Commit

Permalink
Merge branch 'master' into reset_cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
UkoeHB authored Jan 10, 2024
2 parents 2019bab + 2232fa3 commit cb5739e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Refactor `ServerEventQueue` to have an explicit interface.
- Moved server event reset system to new set `ClientSet::ResetEvents` in `PreUpdate`.

## [0.19.0] - 2024-01-07
Expand Down
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn apply_update_message(

let (last_change_tick, message_tick, update_index) = bincode::deserialize_from(&mut cursor)?;
if last_change_tick > replicon_tick {
trace!("buffering update message for {replicon_tick:?}");
trace!("buffering update message for {message_tick:?}");
buffered_updates.0.push(BufferedUpdate {
last_change_tick,
message_tick,
Expand All @@ -282,7 +282,7 @@ fn apply_update_message(
return Ok(update_index);
}

trace!("applying update message for {replicon_tick:?}");
trace!("applying update message for {message_tick:?}");
apply_update_components(
&mut cursor,
world,
Expand Down
33 changes: 26 additions & 7 deletions src/network_event/server_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,7 @@ fn queue_system<T: Event>(
mut server_events: EventWriter<T>,
mut event_queue: ResMut<ServerEventQueue<T>>,
) {
while event_queue
.front()
.filter(|(&tick, _)| tick <= *replicon_tick)
.is_some()
{
let (_, event) = event_queue.pop_front().unwrap();
while let Some(event) = event_queue.pop_next(*replicon_tick) {
server_events.send(event);
}
}
Expand Down Expand Up @@ -347,9 +342,33 @@ pub enum SendMode {
///
/// Stores data sorted by ticks and maintains order of arrival.
/// Needed to ensure that when an event is triggered, all the data that it affects or references already exists.
#[derive(Deref, DerefMut, Resource)]
#[derive(Resource)]
pub struct ServerEventQueue<T>(ListOrderedMultimap<RepliconTick, T>);

impl<T> ServerEventQueue<T> {
/// Clears the event queue.
pub fn clear(&mut self) {
self.0.clear();
}

/// Inserts a new event.
///
/// The event will be queued until [`Self::pop_next`] is called with a replicon tick >= the tick specified here,
/// or until [`Self::clear`] is called.
pub fn insert(&mut self, tick: RepliconTick, event: T) {
self.0.insert(tick, event);
}

/// Pops the next event that is at least as old as the specified replicon tick.
pub fn pop_next(&mut self, replicon_tick: RepliconTick) -> Option<T> {
let (tick, _) = self.0.front()?;
if *tick > replicon_tick {
return None;
}
self.0.pop_front().map(|(_, event)| event)
}
}

impl<T> Default for ServerEventQueue<T> {
fn default() -> Self {
Self(Default::default())
Expand Down
5 changes: 1 addition & 4 deletions tests/replication.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mod connect;

use std::ops::DerefMut;

use bevy::{prelude::*, utils::Duration};
use bevy_replicon::{prelude::*, scene};

Expand Down Expand Up @@ -820,12 +818,11 @@ fn diagnostics() {
server_app.update();
client_app.update();

// Trigger change detection.
server_app
.world
.get_mut::<TableComponent>(server_entity)
.unwrap()
.deref_mut();
.set_changed();

server_app.update();
client_app.update();
Expand Down

0 comments on commit cb5739e

Please sign in to comment.