Skip to content

Commit

Permalink
Discard old diffs on client
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Sep 19, 2023
1 parent 0fc7044 commit 55303ff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
20 changes: 8 additions & 12 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::{
ecs::{system::SystemState, world::EntityMut},
ecs::world::EntityMut,
prelude::*,
utils::{Entry, HashMap},
};
Expand Down Expand Up @@ -45,17 +45,13 @@ impl Plugin for ClientPlugin {
}

impl ClientPlugin {
fn diff_receiving_system(world: &mut World, state: &mut SystemState<ResMut<RenetClient>>) {
let mut client = state.get_mut(world);
let mut last_message = None;
while let Some(message) = client.receive_message(REPLICATION_CHANNEL_ID) {
last_message = Some(message);
}

if let Some(last_message) = last_message {
WorldDiff::deserialize_to_world(world, last_message)
.expect("server should send only valid world diffs");
}
fn diff_receiving_system(world: &mut World) {
world.resource_scope(|world, mut client: Mut<RenetClient>| {
while let Some(message) = client.receive_message(REPLICATION_CHANNEL_ID) {
WorldDiff::deserialize_to_world(world, message)
.expect("server should send only valid world diffs");
}
});
}

fn ack_sending_system(last_tick: Res<LastTick>, mut client: ResMut<RenetClient>) {
Expand Down
17 changes: 12 additions & 5 deletions src/replicon_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,24 @@ impl WorldDiff<'_> {
}

/// Deserializes itself from bytes directly into the world by applying all changes.
///
/// Does nothing if world already received a more recent diff.
/// See also [`LastTick`].
pub(super) fn deserialize_to_world(
world: &mut World,
message: Bytes,
) -> Result<(), bincode::Error> {
world.resource_scope(|world, replication_rules: Mut<ReplicationRules>| {
world.resource_scope(|world, mut entity_map: Mut<NetworkEntityMap>| {
let mut cursor = Cursor::new(message);
let mut cursor = Cursor::new(message);

let tick = bincode::deserialize_from(&mut cursor)?;
world.resource_mut::<LastTick>().0 = tick;
let tick = bincode::deserialize_from(&mut cursor)?;
let mut last_tick = world.resource_mut::<LastTick>();
if last_tick.0 >= tick {
return Ok(());
}
last_tick.0 = tick;

world.resource_scope(|world, replication_rules: Mut<ReplicationRules>| {
world.resource_scope(|world, mut entity_map: Mut<NetworkEntityMap>| {
let entities_count: usize = bincode::deserialize_from(&mut cursor)?;
for _ in 0..entities_count {
let entity = bincode::deserialize_from(&mut cursor)?;
Expand Down

0 comments on commit 55303ff

Please sign in to comment.