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 f87fbf3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 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

- Correctly discard old changes on packed reordering.
- Bevy's `Tick` was replaced with dedicated type `NetworkTick` that increments on server update to provide information to client about time. `AckedTick` was replaced with `ServerTicks` that also contains mappings from `NetworkTick` to Bevy's `Tick` and current `NetworkTick`.
- Functions in `AppReplicationExt::replicate_with` now accept bytes cursor for memory reuse and return serialization errors.
- Rename `ReplicationCore` into `RepliconCore` with its module for clarity.
Expand Down
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 f87fbf3

Please sign in to comment.