Skip to content

Commit

Permalink
Trim empty arrays in message (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur authored Sep 22, 2023
1 parent 5bd125a commit a42e964
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ impl ClientPlugin {
world.resource_scope(|world, mut entity_map: Mut<NetworkEntityMap>| {
world.resource_scope(|world, replication_rules: Mut<ReplicationRules>| {
while let Some(message) = client.receive_message(REPLICATION_CHANNEL_ID) {
let end_pos = message.len().try_into().unwrap();
let mut cursor = Cursor::new(message);

if !deserialize_tick(&mut cursor, world)? {
continue;
}
if cursor.position() == end_pos {
continue;
}

deserialize_component_diffs(
&mut cursor,
Expand All @@ -67,13 +71,21 @@ impl ClientPlugin {
&replication_rules,
DiffKind::Change,
)?;
if cursor.position() == end_pos {
continue;
}

deserialize_component_diffs(
&mut cursor,
world,
&mut entity_map,
&replication_rules,
DiffKind::Removal,
)?;
if cursor.position() == end_pos {
continue;
}

deserialize_despawns(&mut cursor, world, &mut entity_map)?;
}

Expand Down
21 changes: 20 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ impl ServerPlugin {
debug_assert_eq!(buffer.entity_data_len, 0);

if buffer.arrays_with_data > 0 {
buffer.trim_empty_arrays();

set.p1().send_message(
buffer.client_id,
REPLICATION_CHANNEL_ID,
Expand Down Expand Up @@ -439,7 +441,10 @@ struct ReplicationBuffer {
array_len: u16,

/// The number of non-empty arrays stored.
arrays_with_data: u8,
arrays_with_data: usize,

/// The number of empty arrays at the end. Can be removed using [`Self::trim_empty_arrays`]
trailing_empty_arrays: usize,

/// Position of the entity map from last call of [`Self::start_entity_data`] or [`Self::write_current_entity`].
entity_data_pos: u64,
Expand Down Expand Up @@ -468,6 +473,7 @@ impl ReplicationBuffer {
array_pos: Default::default(),
array_len: Default::default(),
arrays_with_data: Default::default(),
trailing_empty_arrays: Default::default(),
entity_data_pos: Default::default(),
entity_data_len: Default::default(),
current_entity: Entity::PLACEHOLDER,
Expand Down Expand Up @@ -520,14 +526,27 @@ impl ReplicationBuffer {
self.message.set_position(previous_pos);
self.array_len = 0;
self.arrays_with_data += 1;
self.trailing_empty_arrays = 0;
} else {
self.trailing_empty_arrays += 1;
self.message.set_position(self.array_pos);
bincode::serialize_into(&mut self.message, &self.array_len)?;
}

Ok(())
}

/// Crops empty arrays at the end.
///
/// Should only be called after all arrays have been written, because
/// removed array somewhere the middle cannot be detected during deserialization.
fn trim_empty_arrays(&mut self) {
let used_len = self.message.get_ref().len()
- self.trailing_empty_arrays * mem::size_of_val(&self.array_len);
self.message.get_mut().truncate(used_len);
self.trailing_empty_arrays = 0;
}

/// Starts writing entity and its data by remembering [`Entity`].
///
/// Arrays can contain component changes or removals inside.
Expand Down

0 comments on commit a42e964

Please sign in to comment.