Skip to content

Commit

Permalink
compression does not need to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Dec 2, 2023
1 parent 175eacf commit 3eb9998
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 29 deletions.
14 changes: 0 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions azalea-client/src/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ fn update_read_packets_task_running_component(
commands.entity(entity).insert(IsConnectionAlive(running));
}
}

#[allow(clippy::type_complexity)]
fn disconnect_on_connection_dead(
query: Query<(Entity, &IsConnectionAlive), (Changed<IsConnectionAlive>, With<LocalEntity>)>,
mut disconnect_events: EventWriter<DisconnectEvent>,
Expand Down
6 changes: 1 addition & 5 deletions azalea-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ version = "0.8.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-compression = { version = "^0.4.5", features = [
"tokio",
"zlib",
], optional = true }
async-recursion = "1.0.5"
azalea-auth = { path = "../azalea-auth", version = "0.8.0" }
azalea-block = { path = "../azalea-block", default-features = false, version = "0.8.0" }
Expand Down Expand Up @@ -52,7 +48,7 @@ uuid = "1.5.0"
[features]
connecting = []
default = ["packets"]
packets = ["connecting", "dep:async-compression", "dep:azalea-core"]
packets = ["connecting", "dep:azalea-core"]
strict_registry = ["packets"]

[dev-dependencies]
Expand Down
4 changes: 1 addition & 3 deletions azalea-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions azalea-protocol/src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ pub async fn read_raw_packet<'a, R>(
stream: &'a mut R,
buffer: &mut BytesMut,
compression_threshold: Option<u32>,
// this has to be a &mut Option<T> instead of an Option<&mut T> because
// otherwise the borrow checker complains about the cipher being moved
cipher: &mut Option<Aes128CfbDec>,
) -> Result<Vec<u8>, Box<ReadPacketError>>
where
Expand Down
14 changes: 7 additions & 7 deletions azalea-protocol/src/write.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<Vec<u8>, PacketCompressError> {
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3eb9998

Please sign in to comment.