diff --git a/azalea-protocol/src/lib.rs b/azalea-protocol/src/lib.rs index 7c21f493..d9860a8b 100644 --- a/azalea-protocol/src/lib.rs +++ b/azalea-protocol/src/lib.rs @@ -79,14 +79,18 @@ impl Display for ServerAddress { #[cfg(test)] mod tests { - use std::io::Cursor; + use std::{ + io::Cursor, + time::{SystemTime, UNIX_EPOCH}, + }; use crate::{ - packets::login::{ - serverbound_hello_packet::ServerboundHelloPacket, ServerboundLoginPacket, + packets::{ + game::serverbound_chat_packet::{LastSeenMessagesUpdate, ServerboundChatPacket}, + login::{serverbound_hello_packet::ServerboundHelloPacket, ServerboundLoginPacket}, }, - read::read_packet, - write::write_packet, + read::{compression_decoder, read_packet}, + write::{compression_encoder, packet_encoder, write_packet}, }; use bytes::BytesMut; use uuid::Uuid; @@ -140,4 +144,29 @@ mod tests { .await .unwrap(); } + + #[tokio::test] + async fn test_read_long_compressed_chat() { + let compression_threshold = 256; + + let buf = packet_encoder( + &ServerboundChatPacket { + message: "a".repeat(256), + timestamp: 0, + salt: 0, + signature: None, + last_seen_messages: LastSeenMessagesUpdate::default(), + } + .get(), + ) + .unwrap(); + + let buf = compression_encoder(&buf, compression_threshold) + .await + .unwrap(); + + println!("{:?}", buf); + + compression_decoder(&mut Cursor::new(&buf), compression_threshold).unwrap(); + } } diff --git a/azalea-protocol/src/read.rs b/azalea-protocol/src/read.rs index 753717b9..1018c085 100755 --- a/azalea-protocol/src/read.rs +++ b/azalea-protocol/src/read.rs @@ -163,7 +163,7 @@ pub enum DecompressionError { /// Get the decompressed bytes from a packet. It must have been decrypted /// first. -fn compression_decoder( +pub fn compression_decoder( stream: &mut Cursor<&[u8]>, compression_threshold: u32, ) -> Result, DecompressionError> { @@ -264,8 +264,7 @@ mod tests { use crate::packets::game::ClientboundGamePacket; use std::io::Cursor; - #[tokio::test] - async fn test_read_packet() { + fn test_read_packet() { let mut buf: Cursor<&[u8]> = Cursor::new(&[ 56, 64, 85, 58, 141, 138, 71, 146, 193, 64, 88, 0, 0, 0, 0, 0, 0, 64, 60, 224, 105, 34, 119, 8, 228, 67, 50, 51, 68, 194, 177, 230, 101, 0, 17, 0, diff --git a/azalea-protocol/src/write.rs b/azalea-protocol/src/write.rs index a674bcfe..d3416147 100755 --- a/azalea-protocol/src/write.rs +++ b/azalea-protocol/src/write.rs @@ -29,7 +29,7 @@ pub enum PacketEncodeError { }, } -fn packet_encoder( +pub fn packet_encoder( packet: &P, ) -> Result, PacketEncodeError> { let mut buf = Vec::new(); @@ -51,7 +51,7 @@ pub enum PacketCompressError { Io(#[from] std::io::Error), } -async fn compression_encoder( +pub async fn compression_encoder( data: &[u8], compression_threshold: u32, ) -> Result, PacketCompressError> { @@ -60,15 +60,18 @@ async fn compression_encoder( if n < compression_threshold as usize { let mut buf = Vec::new(); 0.var_write_into(&mut buf)?; - buf.write_all(data).await?; + std::io::Write::write_all(&mut buf, data)?; Ok(buf) } else { // otherwise, compress let mut deflater = ZlibEncoder::new(data); // write deflated data to buf - let mut buf = Vec::new(); - deflater.read_to_end(&mut buf).await?; - Ok(buf) + let mut data = Vec::new(); + deflater.read_to_end(&mut data).await?; + let mut len_prepended_buf = Vec::new(); + (len_prepended_buf.len() as u32).var_write_into(&mut len_prepended_buf)?; + len_prepended_buf.append(&mut data); + Ok(len_prepended_buf) } } @@ -82,7 +85,7 @@ where P: ProtocolPacket + Debug, W: AsyncWrite + Unpin + Send, { - trace!("Sending packet: {:?}", packet); + trace!("Sending packet: {:?}", packet,); let mut buf = packet_encoder(packet).unwrap(); if let Some(threshold) = compression_threshold { buf = compression_encoder(&buf, threshold).await.unwrap(); diff --git a/azalea/examples/echo.rs b/azalea/examples/echo.rs index 292e12cd..46853bf4 100755 --- a/azalea/examples/echo.rs +++ b/azalea/examples/echo.rs @@ -4,6 +4,8 @@ use azalea::prelude::*; #[tokio::main] async fn main() { + env_logger::init(); + let account = Account::offline("bot"); // or let account = Account::microsoft("email").await;