Skip to content

Commit

Permalink
what is happeni
Browse files Browse the repository at this point in the history
  • Loading branch information
nothendev committed Oct 8, 2023
1 parent c57cc73 commit f21ec56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 9 additions & 6 deletions protocol/src/model/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,15 @@ impl SerializedPacket {

pub fn serialize_compressing(&self, compression: Option<usize>) -> Result<Bytes, Error> {
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::<i32>(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,
Expand Down
6 changes: 3 additions & 3 deletions protocol/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,21 +1037,21 @@ pub struct Zstd;
#[derive(Default, Clone, Copy, Debug)]
pub struct Gzip;
pub trait Compression {
fn encode(data: Bytes) -> Result<Bytes, crate::error::Error>;
fn encode(data: &[u8]) -> Result<Bytes, crate::error::Error>;
fn encode_serialize<T: Serialize>(
thing: &T,
buf: &mut BytesMut,
) -> Result<(), crate::error::Error>;
fn decode(data: &[u8]) -> Result<Bytes, crate::error::Error>;
}
impl Compression for Zlib {
fn encode(data: Bytes) -> Result<Bytes, crate::error::Error> {
fn encode(data: &[u8]) -> Result<Bytes, crate::error::Error> {
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())
}

Expand Down

0 comments on commit f21ec56

Please sign in to comment.