Skip to content

Commit 04eaa5c

Browse files
committed
remove dependency on bytes crate for azalea-protocol and fix memory leak
1 parent 0ee9ed5 commit 04eaa5c

File tree

8 files changed

+74
-59
lines changed

8 files changed

+74
-59
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ bevy_log = "0.15.0"
4040
bevy_tasks = "0.15.0"
4141
bevy_time = "0.15.0"
4242
byteorder = "1.5.0"
43-
bytes = "1.9.0"
4443
cfb8 = "0.8.1"
4544
chrono = { version = "0.4.39", default-features = false }
4645
criterion = "0.5.1"

azalea-client/src/raw_connection.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ pub struct RawConnection {
3333

3434
#[derive(Clone)]
3535
struct RawConnectionReader {
36-
pub incoming_packet_queue: Arc<Mutex<Vec<Vec<u8>>>>,
36+
pub incoming_packet_queue: Arc<Mutex<Vec<Box<[u8]>>>>,
3737
pub run_schedule_sender: mpsc::UnboundedSender<()>,
3838
}
3939
#[derive(Clone)]
4040
struct RawConnectionWriter {
41-
pub outgoing_packets_sender: mpsc::UnboundedSender<Vec<u8>>,
41+
pub outgoing_packets_sender: mpsc::UnboundedSender<Box<[u8]>>,
4242
}
4343

4444
#[derive(Error, Debug)]
@@ -54,7 +54,7 @@ pub enum WritePacketError {
5454
SendError {
5555
#[from]
5656
#[backtrace]
57-
source: SendError<Vec<u8>>,
57+
source: SendError<Box<[u8]>>,
5858
},
5959
}
6060

@@ -93,7 +93,7 @@ impl RawConnection {
9393
}
9494
}
9595

96-
pub fn write_raw_packet(&self, raw_packet: Vec<u8>) -> Result<(), WritePacketError> {
96+
pub fn write_raw_packet(&self, raw_packet: Box<[u8]>) -> Result<(), WritePacketError> {
9797
self.writer.outgoing_packets_sender.send(raw_packet)?;
9898
Ok(())
9999
}
@@ -120,7 +120,7 @@ impl RawConnection {
120120
!self.read_packets_task.is_finished()
121121
}
122122

123-
pub fn incoming_packet_queue(&self) -> Arc<Mutex<Vec<Vec<u8>>>> {
123+
pub fn incoming_packet_queue(&self) -> Arc<Mutex<Vec<Box<[u8]>>>> {
124124
self.reader.incoming_packet_queue.clone()
125125
}
126126

@@ -161,7 +161,7 @@ impl RawConnectionWriter {
161161
pub async fn write_task(
162162
self,
163163
mut write_conn: RawWriteConnection,
164-
mut outgoing_packets_receiver: mpsc::UnboundedReceiver<Vec<u8>>,
164+
mut outgoing_packets_receiver: mpsc::UnboundedReceiver<Box<[u8]>>,
165165
) {
166166
while let Some(raw_packet) = outgoing_packets_receiver.recv().await {
167167
if let Err(err) = write_conn.write(&raw_packet).await {

azalea-protocol/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ azalea-protocol-macros = { path = "./azalea-protocol-macros", version = "0.11.0"
3333
azalea-registry = { path = "../azalea-registry", version = "0.11.0" }
3434
azalea-world = { path = "../azalea-world", version = "0.11.0" }
3535
bevy_ecs = { workspace = true }
36-
#byteorder = { workspace = true }
37-
bytes = { workspace = true }
36+
# byteorder = { workspace = true }
3837
flate2 = { workspace = true }
3938
futures = { workspace = true }
4039
futures-lite = { workspace = true }
41-
#futures-util = { workspace = true }
40+
# futures-util = { workspace = true }
4241
serde = { workspace = true, features = ["serde_derive"] }
4342
serde_json = { workspace = true }
4443
simdnbt = { workspace = true }

azalea-protocol/src/connect.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::net::SocketAddr;
88
use azalea_auth::game_profile::GameProfile;
99
use azalea_auth::sessionserver::{ClientSessionServerError, ServerSessionServerError};
1010
use azalea_crypto::{Aes128CfbDec, Aes128CfbEnc};
11-
use bytes::BytesMut;
1211
use thiserror::Error;
1312
use tokio::io::{AsyncWriteExt, BufStream};
1413
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf, ReuniteError};
@@ -28,7 +27,7 @@ use crate::write::{serialize_packet, write_raw_packet};
2827

2928
pub struct RawReadConnection {
3029
pub read_stream: OwnedReadHalf,
31-
pub buffer: BytesMut,
30+
pub buffer: Cursor<Vec<u8>>,
3231
pub compression_threshold: Option<u32>,
3332
pub dec_cipher: Option<Aes128CfbDec>,
3433
}
@@ -135,7 +134,7 @@ pub struct Connection<R: ProtocolPacket, W: ProtocolPacket> {
135134
}
136135

137136
impl RawReadConnection {
138-
pub async fn read(&mut self) -> Result<Vec<u8>, Box<ReadPacketError>> {
137+
pub async fn read(&mut self) -> Result<Box<[u8]>, Box<ReadPacketError>> {
139138
read_raw_packet::<_>(
140139
&mut self.read_stream,
141140
&mut self.buffer,
@@ -145,7 +144,7 @@ impl RawReadConnection {
145144
.await
146145
}
147146

148-
pub fn try_read(&mut self) -> Result<Option<Vec<u8>>, Box<ReadPacketError>> {
147+
pub fn try_read(&mut self) -> Result<Option<Box<[u8]>>, Box<ReadPacketError>> {
149148
try_read_raw_packet::<_>(
150149
&mut self.read_stream,
151150
&mut self.buffer,
@@ -190,7 +189,7 @@ where
190189
/// Read a packet from the stream.
191190
pub async fn read(&mut self) -> Result<R, Box<ReadPacketError>> {
192191
let raw_packet = self.raw.read().await?;
193-
deserialize_packet(&mut Cursor::new(raw_packet.as_slice()))
192+
deserialize_packet(&mut Cursor::new(&raw_packet))
194193
}
195194

196195
/// Try to read a packet from the stream, or return Ok(None) if there's no
@@ -199,9 +198,7 @@ where
199198
let Some(raw_packet) = self.raw.try_read()? else {
200199
return Ok(None);
201200
};
202-
Ok(Some(deserialize_packet(&mut Cursor::new(
203-
raw_packet.as_slice(),
204-
))?))
201+
Ok(Some(deserialize_packet(&mut Cursor::new(&raw_packet))?))
205202
}
206203
}
207204
impl<W> WriteConnection<W>
@@ -304,7 +301,7 @@ impl Connection<ClientboundHandshakePacket, ServerboundHandshakePacket> {
304301
reader: ReadConnection {
305302
raw: RawReadConnection {
306303
read_stream,
307-
buffer: BytesMut::new(),
304+
buffer: Cursor::new(Vec::new()),
308305
compression_threshold: None,
309306
dec_cipher: None,
310307
},
@@ -562,7 +559,7 @@ where
562559
reader: ReadConnection {
563560
raw: RawReadConnection {
564561
read_stream,
565-
buffer: BytesMut::new(),
562+
buffer: Cursor::new(Vec::new()),
566563
compression_threshold: None,
567564
dec_cipher: None,
568565
},

azalea-protocol/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//!
1010
//! See [`crate::connect::Connection`] for an example.
1111
12-
// these two are necessary for thiserror backtraces
12+
// this is necessary for thiserror backtraces
1313
#![feature(error_generic_member_access)]
1414

1515
use std::{fmt::Display, net::SocketAddr, str::FromStr};
@@ -111,7 +111,6 @@ impl serde::Serialize for ServerAddress {
111111
mod tests {
112112
use std::io::Cursor;
113113

114-
use bytes::BytesMut;
115114
use uuid::Uuid;
116115

117116
use crate::{
@@ -135,11 +134,16 @@ mod tests {
135134
.await
136135
.unwrap();
137136

137+
assert_eq!(
138+
stream,
139+
[22, 0, 4, 116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
140+
);
141+
138142
let mut stream = Cursor::new(stream);
139143

140144
let _ = read_packet::<ServerboundLoginPacket, _>(
141145
&mut stream,
142-
&mut BytesMut::new(),
146+
&mut Cursor::new(Vec::new()),
143147
None,
144148
&mut None,
145149
)
@@ -163,7 +167,7 @@ mod tests {
163167
.unwrap();
164168
let mut stream = Cursor::new(stream);
165169

166-
let mut buffer = BytesMut::new();
170+
let mut buffer = Cursor::new(Vec::new());
167171

168172
let _ = read_packet::<ServerboundLoginPacket, _>(&mut stream, &mut buffer, None, &mut None)
169173
.await

0 commit comments

Comments
 (0)