From 04eaa5c3d01a8f3a599a3a1abf7205eed80df4a2 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 25 Dec 2024 06:16:10 +0000 Subject: remove dependency on bytes crate for azalea-protocol and fix memory leak --- azalea-protocol/src/connect.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'azalea-protocol/src/connect.rs') diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index f33ce2a5..ef202378 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -8,7 +8,6 @@ use std::net::SocketAddr; use azalea_auth::game_profile::GameProfile; use azalea_auth::sessionserver::{ClientSessionServerError, ServerSessionServerError}; use azalea_crypto::{Aes128CfbDec, Aes128CfbEnc}; -use bytes::BytesMut; use thiserror::Error; use tokio::io::{AsyncWriteExt, BufStream}; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf, ReuniteError}; @@ -28,7 +27,7 @@ use crate::write::{serialize_packet, write_raw_packet}; pub struct RawReadConnection { pub read_stream: OwnedReadHalf, - pub buffer: BytesMut, + pub buffer: Cursor>, pub compression_threshold: Option, pub dec_cipher: Option, } @@ -135,7 +134,7 @@ pub struct Connection { } impl RawReadConnection { - pub async fn read(&mut self) -> Result, Box> { + pub async fn read(&mut self) -> Result, Box> { read_raw_packet::<_>( &mut self.read_stream, &mut self.buffer, @@ -145,7 +144,7 @@ impl RawReadConnection { .await } - pub fn try_read(&mut self) -> Result>, Box> { + pub fn try_read(&mut self) -> Result>, Box> { try_read_raw_packet::<_>( &mut self.read_stream, &mut self.buffer, @@ -190,7 +189,7 @@ where /// Read a packet from the stream. pub async fn read(&mut self) -> Result> { let raw_packet = self.raw.read().await?; - deserialize_packet(&mut Cursor::new(raw_packet.as_slice())) + deserialize_packet(&mut Cursor::new(&raw_packet)) } /// Try to read a packet from the stream, or return Ok(None) if there's no @@ -199,9 +198,7 @@ where let Some(raw_packet) = self.raw.try_read()? else { return Ok(None); }; - Ok(Some(deserialize_packet(&mut Cursor::new( - raw_packet.as_slice(), - ))?)) + Ok(Some(deserialize_packet(&mut Cursor::new(&raw_packet))?)) } } impl WriteConnection @@ -304,7 +301,7 @@ impl Connection { reader: ReadConnection { raw: RawReadConnection { read_stream, - buffer: BytesMut::new(), + buffer: Cursor::new(Vec::new()), compression_threshold: None, dec_cipher: None, }, @@ -562,7 +559,7 @@ where reader: ReadConnection { raw: RawReadConnection { read_stream, - buffer: BytesMut::new(), + buffer: Cursor::new(Vec::new()), compression_threshold: None, dec_cipher: None, }, -- cgit v1.2.3