From fe967d70b9fdd7515c5104eeb4d5361d81b8053c Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Sat, 25 Oct 2025 22:22:11 -0400 Subject: [PATCH] Remove unused packet variant, move peer UUID to start of packet --- src/net/distribution.rs | 8 +++--- src/net/heartbeat.rs | 9 +++---- src/net/io.rs | 8 +++--- src/net/packet.rs | 55 +++++------------------------------------ src/net/peer.rs | 4 +-- 5 files changed, 18 insertions(+), 66 deletions(-) diff --git a/src/net/distribution.rs b/src/net/distribution.rs index 9d97287..d991399 100644 --- a/src/net/distribution.rs +++ b/src/net/distribution.rs @@ -40,7 +40,7 @@ where } } -fn spawner( +fn incoming_network_entity( mut inbound: MessageReader, mut commands: Commands, ) { @@ -59,7 +59,7 @@ fn new_peer( ) -> Result { let peer = peers.get(add.entity)?; for component in components { - outbound.write(Packet::create(component.encode(), peer.id)); + outbound.write(Packet::create(peer.id, component.encode())); } Ok(()) } @@ -72,13 +72,13 @@ fn new_entity( ) { if let Ok(component) = components.get(add.entity) { for peer in peers { - outbound.write(Packet::create(component.encode(), peer.id)); + outbound.write(Packet::create(peer.id, component.encode())); } } } pub fn distribution_plugin(app: &mut App) { - app.add_systems(FixedUpdate, spawner::) + app.add_systems(FixedUpdate, incoming_network_entity::) .add_observer(new_peer::) .add_observer(new_entity::); } diff --git a/src/net/heartbeat.rs b/src/net/heartbeat.rs index 3e2003e..4408862 100644 --- a/src/net/heartbeat.rs +++ b/src/net/heartbeat.rs @@ -2,7 +2,7 @@ use std::time::Duration; use bevy::prelude::*; -use crate::net::{packet::PacketType, peer::PotentialPeers}; +use crate::net::peer::PotentialPeers; use super::{ io::{Config, format_message}, @@ -24,7 +24,7 @@ pub fn heartbeat( if last.time() + PING_FREQUENCY > time.elapsed() { continue; } - outbound.write(Packet::create(Vec::new(), peer.id)); + outbound.write(Packet::create(peer.id, Vec::new())); } Ok(()) } @@ -48,10 +48,7 @@ pub fn ping_potential_peers( config: Res, ) -> Result { for peer in &peers.addresses { - to_socket.send( - format_message(&Vec::new(), PacketType::Peer, config.id), - *peer, - )?; + to_socket.send(format_message(config.id, &Vec::new()), *peer)?; } Ok(()) } diff --git a/src/net/io.rs b/src/net/io.rs index 23afa93..8cb6cd7 100644 --- a/src/net/io.rs +++ b/src/net/io.rs @@ -1,8 +1,6 @@ use bevy::prelude::*; use uuid::Uuid; -use crate::net::packet::PacketType; - use super::{ packet::{InboundPacket, OutboundPacket, Packet}, peer::{PeerChangeMessage, PeerData, PeerMap, PeerReceiveTiming, PeerSendTiming}, @@ -20,8 +18,8 @@ impl Default for Config { } } -pub fn format_message(data: &Vec, variant: PacketType, id: Uuid) -> Vec { - [data.as_slice(), &[variant as u8], id.as_bytes()].concat() +pub fn format_message(id: Uuid, data: &Vec) -> Vec { + [id.as_bytes(), data.as_slice()].concat() } pub fn handle_network_input( @@ -65,7 +63,7 @@ pub fn handle_network_output( for OutboundPacket(packet) in from_app.read() { let peer_id = peer_map.try_get(&packet.peer)?; let (peer, mut last) = peers.get_mut(*peer_id)?; - let message = format_message(&packet.message, packet.variant, config.id); + let message = format_message(config.id, &packet.message); to_socket.send(message, peer.addr.into())?; last.update(&time); } diff --git a/src/net/packet.rs b/src/net/packet.rs index f11987f..8177f51 100644 --- a/src/net/packet.rs +++ b/src/net/packet.rs @@ -5,58 +5,19 @@ use uuid::Uuid; pub enum TryFromBytesError { InsufficientLength, NotUUID, - NotVariant, -} - -#[derive(Clone, Copy, Debug)] -pub enum PacketType { - Standard = 0x00, - Peer = 0x01, -} - -impl From for u8 { - fn from(value: PacketType) -> Self { - value as u8 - } -} - -impl TryFrom for PacketType { - type Error = TryFromBytesError; - - fn try_from(value: u8) -> std::result::Result { - match value { - value if value == PacketType::Standard as u8 => Ok(PacketType::Standard), - value if value == PacketType::Peer as u8 => Ok(PacketType::Peer), - _ => Err(TryFromBytesError::NotVariant), - } - } } pub const UUID_SIZE: usize = 16; #[derive(Clone, Debug)] pub struct Packet { - pub message: Vec, - pub variant: PacketType, pub peer: Uuid, + pub message: Vec, } impl Packet { - pub fn new(message: Vec, variant: PacketType, peer: Uuid) -> Self { - Self { - message, - variant, - peer, - } - } - - pub fn create>(message: Vec, peer: Uuid) -> T { - Self { - message, - variant: PacketType::Standard, - peer, - } - .into() + pub fn create>(peer: Uuid, message: Vec) -> T { + Self { peer, message }.into() } } @@ -67,13 +28,9 @@ impl TryFrom> for Packet { if value.len() < UUID_SIZE { return Err(TryFromBytesError::InsufficientLength); } - let uuid = Uuid::from_slice(value.split_off(value.len() - UUID_SIZE).as_slice()) - .map_err(|_| TryFromBytesError::NotUUID)?; - let variant = value - .pop() - .ok_or(TryFromBytesError::InsufficientLength)? - .try_into()?; - Ok(Packet::new(value, variant, uuid)) + let message = value.split_off(UUID_SIZE); + let uuid = Uuid::from_slice(value.as_slice()).map_err(|_| TryFromBytesError::NotUUID)?; + Ok(Packet::create(uuid, message)) } } diff --git a/src/net/peer.rs b/src/net/peer.rs index 3b7bbfc..a59ae39 100644 --- a/src/net/peer.rs +++ b/src/net/peer.rs @@ -8,7 +8,7 @@ use std::{ use bevy::prelude::*; use uuid::Uuid; -use super::packet::{InboundPacket, OutboundPacket, Packet, PacketType}; +use super::packet::{InboundPacket, OutboundPacket, Packet}; #[derive(Component, Debug, Default)] pub struct PeerSendTiming(Duration); @@ -280,7 +280,7 @@ pub fn handle_new_peer( if change.is_added() { for (_, other, data) in peers { if peer.id != other.id { - outbound.write(Packet::new(data.addr.into(), PacketType::Peer, peer.id).into()); + outbound.write(Packet::create(peer.id, data.addr.into())); } } }