From 2909cad0811525f5fe128b71809d62901de7cdd7 Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Mon, 12 Aug 2024 17:31:43 +0200 Subject: [PATCH] Avoid Packet cloning --- .../src/client/login/c_encryption_request.rs | 2 +- .../src/client/play/c_entity_animation.rs | 2 +- .../src/client/play/c_entity_metadata.rs | 4 +-- .../src/client/play/c_head_rot.rs | 2 +- .../src/client/play/c_open_screen.rs | 2 +- .../src/client/play/c_player_chat_message.rs | 2 +- .../src/client/play/c_player_remove.rs | 2 +- .../src/client/play/c_remove_entities.rs | 2 +- .../src/client/play/c_spawn_player.rs | 2 +- .../src/client/play/c_system_chat_message.rs | 2 +- .../client/play/c_update_entitiy_pos_rot.rs | 2 +- .../src/client/play/c_update_entity_pos.rs | 2 +- .../src/client/play/c_update_entity_rot.rs | 2 +- pumpkin-protocol/src/packet_encoder.rs | 2 +- pumpkin/src/client/client_packet.rs | 18 ++++++------- pumpkin/src/client/mod.rs | 16 ++++++------ pumpkin/src/client/player_packet.rs | 2 +- pumpkin/src/rcon/packet.rs | 2 +- pumpkin/src/server.rs | 26 +++++++++---------- 19 files changed, 46 insertions(+), 48 deletions(-) diff --git a/pumpkin-protocol/src/client/login/c_encryption_request.rs b/pumpkin-protocol/src/client/login/c_encryption_request.rs index 6b86cbb3d..77aa7a599 100644 --- a/pumpkin-protocol/src/client/login/c_encryption_request.rs +++ b/pumpkin-protocol/src/client/login/c_encryption_request.rs @@ -1,7 +1,7 @@ use pumpkin_macros::packet; use serde::Serialize; -use crate::{VarInt}; +use crate::VarInt; #[derive(Serialize)] #[packet(0x01)] diff --git a/pumpkin-protocol/src/client/play/c_entity_animation.rs b/pumpkin-protocol/src/client/play/c_entity_animation.rs index a4c7f36da..28542fac4 100644 --- a/pumpkin-protocol/src/client/play/c_entity_animation.rs +++ b/pumpkin-protocol/src/client/play/c_entity_animation.rs @@ -4,7 +4,7 @@ use serde::Serialize; use crate::VarInt; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x03)] pub struct CEntityAnimation { entity_id: VarInt, diff --git a/pumpkin-protocol/src/client/play/c_entity_metadata.rs b/pumpkin-protocol/src/client/play/c_entity_metadata.rs index 506adcb32..2127cf0ab 100644 --- a/pumpkin-protocol/src/client/play/c_entity_metadata.rs +++ b/pumpkin-protocol/src/client/play/c_entity_metadata.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::VarInt; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x58)] pub struct CSetEntityMetadata { entity_id: VarInt, @@ -19,7 +19,7 @@ impl CSetEntityMetadata { } } -#[derive(Serialize, Clone)] +#[derive(Serialize)] pub struct Metadata { index: u8, typ: VarInt, diff --git a/pumpkin-protocol/src/client/play/c_head_rot.rs b/pumpkin-protocol/src/client/play/c_head_rot.rs index 48f04cde5..06061c731 100644 --- a/pumpkin-protocol/src/client/play/c_head_rot.rs +++ b/pumpkin-protocol/src/client/play/c_head_rot.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::VarInt; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x48)] pub struct CHeadRot { entity_id: VarInt, diff --git a/pumpkin-protocol/src/client/play/c_open_screen.rs b/pumpkin-protocol/src/client/play/c_open_screen.rs index 0e0480584..1b5905230 100644 --- a/pumpkin-protocol/src/client/play/c_open_screen.rs +++ b/pumpkin-protocol/src/client/play/c_open_screen.rs @@ -4,7 +4,7 @@ use serde::Serialize; use crate::VarInt; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x33)] pub struct COpenScreen { window_id: VarInt, diff --git a/pumpkin-protocol/src/client/play/c_player_chat_message.rs b/pumpkin-protocol/src/client/play/c_player_chat_message.rs index 22da96a0c..47726a1f3 100644 --- a/pumpkin-protocol/src/client/play/c_player_chat_message.rs +++ b/pumpkin-protocol/src/client/play/c_player_chat_message.rs @@ -5,7 +5,7 @@ use serde::Serialize; use crate::{uuid::UUID, BitSet, VarInt}; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x39)] pub struct CPlayerChatMessage<'a> { sender: UUID, diff --git a/pumpkin-protocol/src/client/play/c_player_remove.rs b/pumpkin-protocol/src/client/play/c_player_remove.rs index 7cd5db84e..4d128a816 100644 --- a/pumpkin-protocol/src/client/play/c_player_remove.rs +++ b/pumpkin-protocol/src/client/play/c_player_remove.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::{uuid::UUID, VarInt}; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x3D)] pub struct CRemovePlayerInfo<'a> { players_count: VarInt, diff --git a/pumpkin-protocol/src/client/play/c_remove_entities.rs b/pumpkin-protocol/src/client/play/c_remove_entities.rs index d110b138e..9e89ec260 100644 --- a/pumpkin-protocol/src/client/play/c_remove_entities.rs +++ b/pumpkin-protocol/src/client/play/c_remove_entities.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::VarInt; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x42)] pub struct CRemoveEntities<'a> { count: VarInt, diff --git a/pumpkin-protocol/src/client/play/c_spawn_player.rs b/pumpkin-protocol/src/client/play/c_spawn_player.rs index 295860788..ca3a7e3cc 100644 --- a/pumpkin-protocol/src/client/play/c_spawn_player.rs +++ b/pumpkin-protocol/src/client/play/c_spawn_player.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::{uuid::UUID, VarInt}; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x01)] pub struct CSpawnEntity { entity_id: VarInt, diff --git a/pumpkin-protocol/src/client/play/c_system_chat_message.rs b/pumpkin-protocol/src/client/play/c_system_chat_message.rs index 17b2bedce..d0d2727eb 100644 --- a/pumpkin-protocol/src/client/play/c_system_chat_message.rs +++ b/pumpkin-protocol/src/client/play/c_system_chat_message.rs @@ -2,7 +2,7 @@ use pumpkin_macros::packet; use pumpkin_text::TextComponent; use serde::Serialize; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x6C)] pub struct CSystemChatMessge { content: TextComponent, diff --git a/pumpkin-protocol/src/client/play/c_update_entitiy_pos_rot.rs b/pumpkin-protocol/src/client/play/c_update_entitiy_pos_rot.rs index ba50ac52b..39c7e0607 100644 --- a/pumpkin-protocol/src/client/play/c_update_entitiy_pos_rot.rs +++ b/pumpkin-protocol/src/client/play/c_update_entitiy_pos_rot.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::VarInt; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x2F)] pub struct CUpdateEntityPosRot { entity_id: VarInt, diff --git a/pumpkin-protocol/src/client/play/c_update_entity_pos.rs b/pumpkin-protocol/src/client/play/c_update_entity_pos.rs index dc437eecd..11c9d8240 100644 --- a/pumpkin-protocol/src/client/play/c_update_entity_pos.rs +++ b/pumpkin-protocol/src/client/play/c_update_entity_pos.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::VarInt; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x2E)] pub struct CUpdateEntityPos { entity_id: VarInt, diff --git a/pumpkin-protocol/src/client/play/c_update_entity_rot.rs b/pumpkin-protocol/src/client/play/c_update_entity_rot.rs index 4b374a716..50755952b 100644 --- a/pumpkin-protocol/src/client/play/c_update_entity_rot.rs +++ b/pumpkin-protocol/src/client/play/c_update_entity_rot.rs @@ -3,7 +3,7 @@ use serde::Serialize; use crate::VarInt; -#[derive(Serialize, Clone)] +#[derive(Serialize)] #[packet(0x30)] pub struct CUpdateEntityRot { entity_id: VarInt, diff --git a/pumpkin-protocol/src/packet_encoder.rs b/pumpkin-protocol/src/packet_encoder.rs index 9e3455e59..1c545804b 100644 --- a/pumpkin-protocol/src/packet_encoder.rs +++ b/pumpkin-protocol/src/packet_encoder.rs @@ -22,7 +22,7 @@ pub struct PacketEncoder { } impl PacketEncoder { - pub fn append_packet(&mut self, packet: P) -> Result<(), PacketError> { + pub fn append_packet(&mut self, packet: &P) -> Result<(), PacketError> { let start_len = self.buf.len(); let mut writer = (&mut self.buf).writer(); diff --git a/pumpkin/src/client/client_packet.rs b/pumpkin/src/client/client_packet.rs index 7b8c03fce..3737f04a5 100644 --- a/pumpkin/src/client/client_packet.rs +++ b/pumpkin/src/client/client_packet.rs @@ -40,12 +40,12 @@ impl Client { } pub fn handle_status_request(&mut self, server: &mut Server, _status_request: SStatusRequest) { - self.send_packet(CStatusResponse::new(&server.status_response_json)); + self.send_packet(&CStatusResponse::new(&server.status_response_json)); } pub fn handle_ping_request(&mut self, _server: &mut Server, ping_request: SPingRequest) { dbg!("ping"); - self.send_packet(CPingResponse::new(ping_request.payload)); + self.send_packet(&CPingResponse::new(ping_request.payload)); self.close(); } @@ -70,7 +70,7 @@ impl Client { &verify_token, server.base_config.online_mode, // TODO ); - self.send_packet(packet); + self.send_packet(&packet); } pub async fn handle_encryption_response( @@ -144,13 +144,13 @@ impl Client { .packet_compression .compression_threshold; let level = server.advanced_config.packet_compression.compression_level; - self.send_packet(CSetCompression::new(threshold.into())); + self.send_packet(&CSetCompression::new(threshold.into())); self.set_compression(Some((threshold, level))); } if let Some(profile) = self.gameprofile.as_ref().cloned() { let packet = CLoginSuccess::new(profile.id, &profile.name, &profile.properties, false); - self.send_packet(packet); + self.send_packet(&packet); } else { self.kick("game profile is none"); } @@ -178,7 +178,7 @@ impl Client { } else { Some(TextComponent::from(resource_config.prompt_message.clone())) }; - self.send_packet(CConfigAddResourcePack::new( + self.send_packet(&CConfigAddResourcePack::new( uuid::Uuid::from_str(&resource_config.resource_pack_url).unwrap(), resource_config.resource_pack_url.clone(), resource_config.resource_pack_sha1.clone(), @@ -188,7 +188,7 @@ impl Client { } // known data packs - self.send_packet(CKnownPacks::new(&[KnownPack { + self.send_packet(&CKnownPacks::new(&[KnownPack { namespace: "minecraft", id: "core", version: "1.21", @@ -226,7 +226,7 @@ impl Client { pub fn handle_known_packs(&mut self, server: &mut Server, _config_acknowledged: SKnownPacks) { for registry in &server.cached_registry { - self.send_packet(CRegistryData::new( + self.send_packet(&CRegistryData::new( ®istry.registry_id, ®istry.registry_entries, )); @@ -234,7 +234,7 @@ impl Client { // We are done with configuring dbg!("finish config"); - self.send_packet(CFinishConfig::new()); + self.send_packet(&CFinishConfig::new()); } pub fn handle_config_acknowledged( diff --git a/pumpkin/src/client/mod.rs b/pumpkin/src/client/mod.rs index 9f60a8933..3996c63b0 100644 --- a/pumpkin/src/client/mod.rs +++ b/pumpkin/src/client/mod.rs @@ -122,7 +122,7 @@ impl Client { } /// Send a Clientbound Packet to the Client - pub fn send_packet(&mut self, packet: P) { + pub fn send_packet(&mut self, packet: &P) { self.enc .append_packet(packet) .unwrap_or_else(|e| self.kick(&e.to_string())); @@ -132,7 +132,7 @@ impl Client { .unwrap_or_else(|e| self.kick(&e.to_string())); } - pub fn try_send_packet(&mut self, packet: P) -> Result<(), PacketError> { + pub fn try_send_packet(&mut self, packet: &P) -> Result<(), PacketError> { self.enc.append_packet(packet)?; self.connection .write_all(&self.enc.take()) @@ -155,11 +155,11 @@ impl Client { entity.yaw = yaw; entity.pitch = pitch; player.awaiting_teleport = Some(id.into()); - self.send_packet(CSyncPlayerPostion::new(x, y, z, yaw, pitch, 0, id.into())); + self.send_packet(&CSyncPlayerPostion::new(x, y, z, yaw, pitch, 0, id.into())); } pub fn set_gamemode(&mut self, gamemode: GameMode) { - self.send_packet(CGameEvent::new(3, gamemode.to_f32().unwrap())); + self.send_packet(&CGameEvent::new(3, gamemode.to_f32().unwrap())); } pub async fn process_packets(&mut self, server: &mut Server) { @@ -323,7 +323,7 @@ impl Client { } pub fn send_system_message(&mut self, text: TextComponent) { - self.send_packet(CSystemChatMessge::new(text, false)); + self.send_packet(&CSystemChatMessge::new(text, false)); } /// Kicks the Client with a reason depending on the connection state @@ -331,17 +331,17 @@ impl Client { dbg!(reason); match self.connection_state { ConnectionState::Login => { - self.try_send_packet(CLoginDisconnect::new( + self.try_send_packet(&CLoginDisconnect::new( &serde_json::to_string_pretty(&reason).unwrap(), )) .unwrap_or_else(|_| self.close()); } ConnectionState::Config => { - self.try_send_packet(CConfigDisconnect::new(reason)) + self.try_send_packet(&CConfigDisconnect::new(reason)) .unwrap_or_else(|_| self.close()); } ConnectionState::Play => { - self.try_send_packet(CPlayDisconnect::new(TextComponent::from(reason))) + self.try_send_packet(&CPlayDisconnect::new(TextComponent::from(reason))) .unwrap_or_else(|_| self.close()); } _ => { diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index b077a5034..ff7ea586b 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -186,7 +186,7 @@ impl Client { }; let player = self.player.as_mut().unwrap(); let id = player.entity_id(); - server.broadcast_packet_expect(self, CEntityAnimation::new(id.into(), animation as u8)) + server.broadcast_packet_expect(self, &CEntityAnimation::new(id.into(), animation as u8)) } pub fn handle_chat_message(&mut self, server: &mut Server, chat_message: SChatMessage) { diff --git a/pumpkin/src/rcon/packet.rs b/pumpkin/src/rcon/packet.rs index 5d1abd195..e6dd16033 100644 --- a/pumpkin/src/rcon/packet.rs +++ b/pumpkin/src/rcon/packet.rs @@ -3,7 +3,7 @@ use std::io::{self, BufRead, Cursor, Write}; use bytes::{BufMut, BytesMut}; use mio::net::TcpStream; use thiserror::Error; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::io::AsyncReadExt; #[derive(Debug, Clone, Copy, PartialEq)] pub enum PacketType { diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index c639014ca..8f743888c 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -145,7 +145,7 @@ impl Server { client.player = Some(player); // login packet for our new player - client.send_packet(CLogin::new( + client.send_packet(&CLogin::new( entity_id, self.base_config.hardcore, &["minecraft:overworld"], @@ -173,7 +173,7 @@ impl Server { )); dbg!("sending abilities"); // player abilities - client.send_packet(CPlayerAbilities::new(0x02, 00.2, 0.1)); + client.send_packet(&CPlayerAbilities::new(0x02, 00.2, 0.1)); // teleport let x = 10.0; @@ -220,10 +220,10 @@ impl Server { }) } } - client.send_packet(CPlayerInfoUpdate::new(0x01 | 0x08, &entries)); + client.send_packet(&CPlayerInfoUpdate::new(0x01 | 0x08, &entries)); // Start waiting for level chunks - client.send_packet(CGameEvent::new(13, 0.0)); + client.send_packet(&CGameEvent::new(13, 0.0)); let gameprofile = client.gameprofile.as_ref().unwrap(); @@ -231,7 +231,7 @@ impl Server { self.broadcast_packet_expect( client, // TODO: add velo - CSpawnEntity::new( + &CSpawnEntity::new( entity_id.into(), UUID(gameprofile.id), EntityType::Player.to_i32().unwrap().into(), @@ -254,7 +254,7 @@ impl Server { if let Some(player) = &existing_client.player { let entity = &player.entity; let gameprofile = existing_client.gameprofile.as_ref().unwrap(); - client.send_packet(CSpawnEntity::new( + client.send_packet(&CSpawnEntity::new( player.entity_id().into(), UUID(gameprofile.id), EntityType::Player.to_i32().unwrap().into(), @@ -287,31 +287,29 @@ impl Server { pub fn broadcast_packet

(&mut self, from: &mut Client, packet: P) where P: ClientPacket, - P: Clone, { // we can't borrow twice at same time - from.send_packet(packet.clone()); + from.send_packet(&packet); for (_, client) in self.current_clients.iter().filter(|c| c.0 != &from.token) { // Check if client is a player let mut client = client.borrow_mut(); if client.is_player() { // we need to clone, Because we send a new packet to every client - client.send_packet(packet.clone()); + client.send_packet(&packet); } } } - pub fn broadcast_packet_expect

(&mut self, from: &mut Client, packet: P) + pub fn broadcast_packet_expect

(&mut self, from: &mut Client, packet: &P) where P: ClientPacket, - P: Clone, { for (_, client) in self.current_clients.iter().filter(|c| c.0 != &from.token) { // Check if client is a player let mut client = client.borrow_mut(); if client.is_player() { // we need to clone, Because we send a new packet to every client - client.send_packet(packet.clone()); + client.send_packet(packet); } } } @@ -319,7 +317,7 @@ impl Server { // TODO: do this in a world fn _spawn_test_chunk(client: &mut Client) { let test_chunk = TestChunk::new(); - client.send_packet(CChunkDataUpdateLight::new( + client.send_packet(&CChunkDataUpdateLight::new( 10, 10, test_chunk.heightmap, @@ -348,7 +346,7 @@ impl Server { pub fn send_brand(&self, client: &mut Client) { // send server brand - client.send_packet(CPluginMessage::new( + client.send_packet(&CPluginMessage::new( "minecraft:brand", &self.cached_server_brand, ));