Skip to content

Commit

Permalink
cargo fmt (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
cBournhonesque authored Aug 31, 2024
1 parent a3e54a6 commit df475e9
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lightyear/src/client/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Default for NetcodeConfig {
Self {
num_disconnect_packets: 10,
keepalive_packet_send_rate: 1.0 / 10.0,
client_timeout_secs: 3,
client_timeout_secs: -1,
token_expire_secs: 30,
}
}
Expand Down
9 changes: 8 additions & 1 deletion lightyear/src/client/input/leafwing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ impl<A: LeafwingUserAction> Plugin for LeafwingInputPlugin<A>
.and_then(should_run.clone())
.and_then(not(is_in_rollback)),
),
prepare_input_message::<A>.in_set(InputSystemSet::PrepareInputMessage),
prepare_input_message::<A>
.in_set(InputSystemSet::PrepareInputMessage)
// no need to prepare messages to send if in rollback
.run_if(not(is_in_rollback)),
),
);

Expand Down Expand Up @@ -649,6 +652,10 @@ fn send_input_messages<A: LeafwingUserAction>(
mut connection: ResMut<ConnectionManager>,
mut message_buffer: ResMut<MessageBuffer<A>>,
) {
trace!(
"Number of input messages to send: {:?}",
message_buffer.0.len()
);
for mut message in message_buffer.0.drain(..) {
connection
.send_message::<InputChannel, InputMessage<A>>(&mut message)
Expand Down
2 changes: 2 additions & 0 deletions lightyear/src/client/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ pub(crate) fn sync_update(
// TODO: how to adjust this for replication groups that have a custom send_interval?
config.shared.server_replication_send_interval,
) {
debug!("Triggering TickSync event: {tick_event:?}");
commands.trigger(tick_event);
}

Expand All @@ -279,6 +280,7 @@ pub(crate) fn sync_update(
tick_manager.deref_mut(),
&connection.ping_manager,
) {
debug!("Triggering TickSync event: {tick_event:?}");
commands.trigger(tick_event);
}
let relative_speed = time_manager.get_relative_speed();
Expand Down
2 changes: 1 addition & 1 deletion lightyear/src/client/prediction/rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy::prelude::{
};
use bevy::reflect::Reflect;
use parking_lot::RwLock;
use tracing::{debug, error, info, trace, trace_span};
use tracing::{debug, error, trace, trace_span};

use crate::client::components::{Confirmed, SyncComponent};
use crate::client::config::ClientConfig;
Expand Down
16 changes: 10 additions & 6 deletions lightyear/src/packet/message_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashMap, VecDeque};

use byteorder::ReadBytesExt;
use bytes::Bytes;
use crossbeam_channel::{Receiver, Sender};
use std::collections::{HashMap, VecDeque};
use tracing::trace;
#[cfg(feature = "trace")]
use tracing::{instrument, Level};
Expand All @@ -23,8 +23,7 @@ use crate::packet::priority_manager::{PriorityConfig, PriorityManager};
use crate::protocol::channel::{ChannelId, ChannelKind, ChannelRegistry};
use crate::protocol::registry::NetId;
use crate::serialize::reader::Reader;
use crate::serialize::varint::VarIntReadExt;
use crate::serialize::ToBytes;
use crate::serialize::{SerializationError, ToBytes};
use crate::shared::ping::manager::PingManager;
use crate::shared::tick_manager::Tick;
use crate::shared::tick_manager::TickManager;
Expand Down Expand Up @@ -219,6 +218,9 @@ impl MessageManager {
let packets =
self.packet_manager
.build_packets(current_tick, single_data, fragment_data)?;
// for packet in packets.iter() {
// trace!(?packet, "packet to send");
// }

let mut bytes = Vec::new();
for mut packet in packets {
Expand Down Expand Up @@ -276,12 +278,13 @@ impl MessageManager {
/// Returns the tick of the packet
#[cfg_attr(feature = "trace", instrument(level = Level::INFO, skip_all))]
pub fn recv_packet(&mut self, packet: RecvPayload) -> Result<Tick, PacketError> {
trace!(?packet, "Received packet");
trace!(packet = ?packet.as_ref(), "Received packet");
let mut cursor = Reader::from(packet);

// Step 1. Parse the packet
let header = PacketHeader::from_bytes(&mut cursor)?;
let tick = header.tick;
trace!(?header);

// TODO: if it's fragmented, put it in a buffer? while we wait for all the parts to be ready?
// maybe the channel can handle the fragmentation?
Expand Down Expand Up @@ -336,7 +339,8 @@ impl MessageManager {
// read single message data
while cursor.has_remaining() {
let channel_id = ChannelId::from_bytes(&mut cursor)?;
let num_messages = cursor.read_varint()?;
let num_messages = cursor.read_u8().map_err(SerializationError::from)?;
trace!(?channel_id, ?num_messages);
for i in 0..num_messages {
let single_data = SingleData::from_bytes(&mut cursor)?;
self.get_channel_mut(channel_id)?
Expand Down
13 changes: 7 additions & 6 deletions lightyear/src/packet/packet_builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
//! Module to take a buffer of messages to send and build packets
use crate::connection::netcode::MAX_PACKET_SIZE;
use byteorder::WriteBytesExt;
use bytes::Bytes;
use std::collections::VecDeque;
#[cfg(feature = "trace")]
use tracing::{instrument, Level};

use crate::packet::header::PacketHeaderManager;
use crate::packet::message::{FragmentData, MessageAck, SingleData};
use crate::packet::packet::{Packet, FRAGMENT_SIZE};
Expand All @@ -15,6 +9,12 @@ use crate::protocol::channel::ChannelId;
use crate::protocol::registry::NetId;
use crate::serialize::varint::varint_len;
use crate::serialize::{SerializationError, ToBytes};
use byteorder::WriteBytesExt;
use bytes::Bytes;
use std::collections::VecDeque;
use tracing::trace;
#[cfg(feature = "trace")]
use tracing::{instrument, Level};

pub type Payload = Vec<u8>;

Expand Down Expand Up @@ -303,6 +303,7 @@ impl PacketBuilder {
.checked_sub(varint_len(channel_id as u64) + 1)
.ok_or(SerializationError::SubstractionOverflow)?;
if *num_messages > 0 {
trace!("Writing packet with {} messages", *num_messages);
channel_id.to_bytes(&mut packet.payload)?;
// write the number of messages for the current channel
packet.payload.write_u8(*num_messages as u8).unwrap();
Expand Down
1 change: 1 addition & 0 deletions lightyear/src/serialize/reader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bytes::{Buf, Bytes};
use std::io::{Cursor, Read, Seek, SeekFrom};

#[derive(Clone)]
pub struct Reader(Cursor<Bytes>);

impl From<Bytes> for Reader {
Expand Down
3 changes: 1 addition & 2 deletions lightyear/src/serialize/varint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ pub trait VarIntReadExt: ReadBytesExt + Seek {
/// the current offset and advances the buffer.
fn read_varint(&mut self) -> Result<u64, SerializationError> {
let first = self.read_u8()?;

let len = varint_parse_len(first);
let out = match len {
1 => u64::from(first),
2 => {
// TODO: we actually don't need seek, no? we can just read the next few bytes ...
// go back 1 byte because we read the first byte above
self.seek(std::io::SeekFrom::Current(-1))?;
u64::from(self.read_u16::<NetworkEndian>()? & 0x3fff)
Expand All @@ -81,7 +81,6 @@ pub trait VarIntReadExt: ReadBytesExt + Seek {
self.seek(std::io::SeekFrom::Current(-1))?;
self.read_u64::<NetworkEndian>()? & 0x3fffffffffffffff
}

_ => return Err(std::io::Error::other("value is too large for varint").into()),
};
Ok(out)
Expand Down

0 comments on commit df475e9

Please sign in to comment.