From 3eb9998c989cbf9305f4584af94daae72780fea7 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 1 Dec 2023 23:09:24 -0600 Subject: compression does not need to be async --- azalea-protocol/src/lib.rs | 4 +--- azalea-protocol/src/read.rs | 2 ++ azalea-protocol/src/write.rs | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) (limited to 'azalea-protocol/src') diff --git a/azalea-protocol/src/lib.rs b/azalea-protocol/src/lib.rs index 38acd705..8683233e 100644 --- a/azalea-protocol/src/lib.rs +++ b/azalea-protocol/src/lib.rs @@ -180,9 +180,7 @@ mod tests { ) .unwrap(); - let buf = compression_encoder(&buf, compression_threshold) - .await - .unwrap(); + let buf = compression_encoder(&buf, compression_threshold).unwrap(); println!("{:?}", buf); diff --git a/azalea-protocol/src/read.rs b/azalea-protocol/src/read.rs index d1985b31..65e497c6 100755 --- a/azalea-protocol/src/read.rs +++ b/azalea-protocol/src/read.rs @@ -244,6 +244,8 @@ pub async fn read_raw_packet<'a, R>( stream: &'a mut R, buffer: &mut BytesMut, compression_threshold: Option, + // this has to be a &mut Option instead of an Option<&mut T> because + // otherwise the borrow checker complains about the cipher being moved cipher: &mut Option, ) -> Result, Box> where diff --git a/azalea-protocol/src/write.rs b/azalea-protocol/src/write.rs index 6ce01bf7..532f4651 100755 --- a/azalea-protocol/src/write.rs +++ b/azalea-protocol/src/write.rs @@ -1,12 +1,12 @@ //! Write packets to a stream. use crate::{packets::ProtocolPacket, read::MAXIMUM_UNCOMPRESSED_LENGTH}; -use async_compression::tokio::bufread::ZlibEncoder; use azalea_buf::McBufVarWritable; use azalea_crypto::Aes128CfbEnc; -use std::fmt::Debug; +use flate2::{bufread::ZlibEncoder, Compression}; +use std::{fmt::Debug, io::Read}; use thiserror::Error; -use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt}; +use tokio::io::{AsyncWrite, AsyncWriteExt}; use tracing::trace; /// Prepend the length of the packet to it. @@ -51,7 +51,7 @@ pub enum PacketCompressError { Io(#[from] std::io::Error), } -pub async fn compression_encoder( +pub fn compression_encoder( data: &[u8], compression_threshold: u32, ) -> Result, PacketCompressError> { @@ -64,10 +64,10 @@ pub async fn compression_encoder( Ok(buf) } else { // otherwise, compress - let mut deflater = ZlibEncoder::new(data); + let mut deflater = ZlibEncoder::new(data, Compression::default()); // write deflated data to buf let mut compressed_data = Vec::new(); - deflater.read_to_end(&mut compressed_data).await?; + deflater.read_to_end(&mut compressed_data)?; // prepend the length let mut len_prepended_compressed_data = Vec::new(); @@ -105,7 +105,7 @@ where trace!("Writing raw packet: {raw_packet:?}"); let mut raw_packet = raw_packet.to_vec(); if let Some(threshold) = compression_threshold { - raw_packet = compression_encoder(&raw_packet, threshold).await.unwrap(); + raw_packet = compression_encoder(&raw_packet, threshold).unwrap(); } raw_packet = frame_prepender(raw_packet).unwrap(); // if we were given a cipher, encrypt the packet -- cgit v1.2.3