From f21ec56703c7e43a19bb58f42e73542c08747be3 Mon Sep 17 00:00:00 2001 From: nothendev Date: Sun, 8 Oct 2023 15:15:06 +0300 Subject: [PATCH] what is happeni --- protocol/src/model/packets.rs | 15 +++++++++------ protocol/src/ser.rs | 6 +++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/protocol/src/model/packets.rs b/protocol/src/model/packets.rs index 6d63e90..be08db8 100644 --- a/protocol/src/model/packets.rs +++ b/protocol/src/model/packets.rs @@ -87,12 +87,15 @@ impl SerializedPacket { pub fn serialize_compressing(&self, compression: Option) -> Result { if let Some(cmp) = compression { - let data_length = (self.length >= cmp) - .then(|| self.id.length_of() + self.data.len()) - .unwrap_or(0); - let datalength = VarInt::(data_length.try_into().unwrap()); - let length = - datalength.length_of() + Compress((&self.id, &self.data), Zlib).serialize()?.len(); + let maybe_data_length = self.length + self.id.length_of(); + let (data_length, length) = if maybe_data_length >= cmp { + ( + maybe_data_length, + Zlib::encode(&self.data)?.len() + VarInt(maybe_data_length as i32).length_of(), + ) + } else { + (0, self.length) + }; let pack = SerializedPacketCompressed { length, data_length, diff --git a/protocol/src/ser.rs b/protocol/src/ser.rs index a6cf21c..c87c59f 100644 --- a/protocol/src/ser.rs +++ b/protocol/src/ser.rs @@ -1037,7 +1037,7 @@ pub struct Zstd; #[derive(Default, Clone, Copy, Debug)] pub struct Gzip; pub trait Compression { - fn encode(data: Bytes) -> Result; + fn encode(data: &[u8]) -> Result; fn encode_serialize( thing: &T, buf: &mut BytesMut, @@ -1045,13 +1045,13 @@ pub trait Compression { fn decode(data: &[u8]) -> Result; } impl Compression for Zlib { - fn encode(data: Bytes) -> Result { + fn encode(data: &[u8]) -> Result { use std::io::Write; let mut enc = flate2::write::ZlibEncoder::new( BytesMut::new().writer(), flate2::Compression::default(), ); - enc.write_all(&data)?; + enc.write_all(data)?; Ok(enc.finish()?.into_inner().freeze()) }