Skip to content

Commit

Permalink
Use varint encoding where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Sep 21, 2023
1 parent a7dd5e0 commit e36d65b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy::{
};
use bevy_renet::{renet::Bytes, transport::client_connected};
use bevy_renet::{renet::RenetClient, transport::NetcodeClientPlugin, RenetClientPlugin};
use bincode::{DefaultOptions, Options};

use crate::{
replicon_core::{Mapper, NetworkTick, ReplicationRules, REPLICATION_CHANNEL_ID},
Expand Down Expand Up @@ -98,7 +99,7 @@ impl ClientPlugin {
///
/// Returns true if [`LastTick`] has been updated.
fn deserialize_tick(cursor: &mut Cursor<Bytes>, world: &mut World) -> Result<bool, bincode::Error> {
let tick = bincode::deserialize_from(cursor)?;
let tick = DefaultOptions::new().deserialize_from(cursor)?;

let mut last_tick = world.resource_mut::<LastTick>();
if last_tick.0 < tick {
Expand Down Expand Up @@ -127,7 +128,7 @@ fn deserialize_component_diffs(
let mut entity = entity_map.get_by_server_or_spawn(world, entity);
let components_count: u8 = bincode::deserialize_from(&mut *cursor)?;
for _ in 0..components_count {
let replication_id = bincode::deserialize_from(&mut *cursor)?;
let replication_id = DefaultOptions::new().deserialize_from(&mut *cursor)?;
let replication_info = replication_rules.get_info(replication_id);
match diff_kind {
DiffKind::Change => {
Expand Down Expand Up @@ -161,7 +162,7 @@ fn deserialize_despawns(
// The entity might have already been deleted with the last diff,
// but the server might not yet have received confirmation from the
// client and could include the deletion in the latest diff.
let server_entity = bincode::deserialize_from(&mut *cursor)?;
let server_entity = DefaultOptions::new().deserialize_from(&mut *cursor)?;
if let Some(client_entity) = entity_map.remove_by_server(server_entity) {
world.entity_mut(client_entity).despawn_recursive();
}
Expand Down
14 changes: 9 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use bevy_renet::{
transport::NetcodeServerPlugin,
RenetServerPlugin,
};
use bincode::{DefaultOptions, Options};
use derive_more::Constructor;

use crate::replicon_core::{
Expand Down Expand Up @@ -145,6 +146,7 @@ impl ServerPlugin {
for buffer in buffers {
debug_assert_eq!(buffer.array_len, 0);
debug_assert_eq!(buffer.entity_map_len, 0);
println!("{:?}", buffer.message.get_ref().len());

set.p1().send_message(
buffer.client_id,
Expand Down Expand Up @@ -451,7 +453,7 @@ impl ReplicationBuffer {
current_tick: NetworkTick,
) -> Result<Self, bincode::Error> {
let mut message = Default::default();
bincode::serialize_into(&mut message, &current_tick)?;
DefaultOptions::new().serialize_into(&mut message, &current_tick)?;
Ok(Self {
client_id,
system_tick,
Expand All @@ -477,7 +479,7 @@ impl ReplicationBuffer {
self.system_tick = system_tick;
self.message.set_position(0);
self.message.get_mut().clear();
bincode::serialize_into(&mut self.message, &current_tick)?;
DefaultOptions::new().serialize_into(&mut self.message, &current_tick)?;

Ok(())
}
Expand All @@ -496,6 +498,7 @@ impl ReplicationBuffer {

/// Ends writing array by writing its length into the last remembered position.
///
/// Length is written without varint encoding.
/// See also [`Self::start_array`].
fn end_array(&mut self) -> Result<(), bincode::Error> {
if self.array_len != 0 {
Expand Down Expand Up @@ -529,6 +532,7 @@ impl ReplicationBuffer {
/// Ends writing array by writing its length and associated [`Entity`] into the last remembered position.
///
/// If map is empty, only map length will be written.
/// [`Entity`] and length are written without varint encoding.
/// See also [`Self::start_array`].
fn end_entity_map(&mut self, entity: Entity) -> Result<(), bincode::Error> {
if self.entity_map_len != 0 {
Expand Down Expand Up @@ -561,7 +565,7 @@ impl ReplicationBuffer {
replication_id: ReplicationId,
ptr: Ptr,
) -> Result<(), bincode::Error> {
bincode::serialize_into(&mut self.message, &replication_id)?;
DefaultOptions::new().serialize_into(&mut self.message, &replication_id)?;
(replication_info.serialize)(ptr, &mut self.message)?;
self.entity_map_len += 1;

Expand All @@ -572,7 +576,7 @@ impl ReplicationBuffer {
///
/// Increases map length by 1.
fn write_removal(&mut self, replication_id: ReplicationId) -> Result<(), bincode::Error> {
bincode::serialize_into(&mut self.message, &replication_id)?;
DefaultOptions::new().serialize_into(&mut self.message, &replication_id)?;
self.entity_map_len += 1;

Ok(())
Expand All @@ -582,7 +586,7 @@ impl ReplicationBuffer {
///
/// Increases array length by 1.
fn write_despawn(&mut self, entity: Entity) -> Result<(), bincode::Error> {
bincode::serialize_into(&mut self.message, &entity)?;
DefaultOptions::new().serialize_into(&mut self.message, &entity)?;
self.array_len = self
.array_len
.checked_add(1)
Expand Down

0 comments on commit e36d65b

Please sign in to comment.