diff --git a/src/lib.rs b/src/lib.rs index b5a1bf1..74fce2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use std::{error::Error, fmt, io::{Read, Write}, net::{TcpStream, ToSocketAddrs}, sync::{Mutex, Arc}}; +use std::{error::Error, fmt, io::{Read, Write}, net::{TcpStream, ToSocketAddrs}, sync::{Mutex, Arc, atomic::{AtomicUsize, Ordering}}}; use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression}; use bytebuffer::ByteBuffer; use uuid::Uuid; @@ -496,12 +496,12 @@ impl MinecraftConnection { /// Read [`Packet`](Packet) from connection pub fn read_packet(&mut self) -> Result { - read_packet(&mut self.stream, Arc::new(Mutex::new(self.compression))) + read_packet(&mut self.stream, Arc::new(AtomicUsize::new(if self.compression.is_none() {usize::MAX} else {self.compression.unwrap()}))) } /// Write [`Packet`](Packet) to connection pub fn write_packet(&mut self, packet: &Packet) -> Result<(), ProtocolError> { - write_packet(&mut self.stream, Arc::new(Mutex::new(self.compression)), packet) + write_packet(&mut self.stream, Arc::new(AtomicUsize::new(if self.compression.is_none() {usize::MAX} else {self.compression.unwrap()})), packet) } } @@ -526,59 +526,57 @@ pub type MCConn = MinecraftConnection; pub type MCConnTcp = MinecraftConnection; /// Read [`Packet`](Packet) from stream -pub fn read_packet(stream: &mut T, compression: Arc>>) -> Result { +pub fn read_packet(stream: &mut T, compression: Arc) -> Result { let mut data: Vec; let packet_length = stream.read_usize_varint_size()?; - match compression.lock().unwrap().as_mut() { - Some(_) => { - let data_length = stream.read_usize_varint_size()?; + let compress_threashold = compression.load(Ordering::Relaxed); - data = stream.read_bytes(packet_length.0 - data_length.1)?; + if compress_threashold != usize::MAX { + let data_length = stream.read_usize_varint_size()?; - if data_length.0 != 0 { - data = decompress_zlib(&data, data_length.0)?; - } - }, - None => { - data = stream.read_bytes(packet_length.0)?; - }, + data = stream.read_bytes(packet_length.0 - data_length.1)?; + + if data_length.0 != 0 { + data = decompress_zlib(&data, data_length.0)?; + } + } else { + data = stream.read_bytes(packet_length.0)?; } Ok(Packet::from_data(&data)?) } /// Write [`Packet`](Packet) to stream -pub fn write_packet(stream: &mut T, compression: Arc>>, packet: &Packet) -> Result<(), ProtocolError> { +pub fn write_packet(stream: &mut T, compression: Arc, packet: &Packet) -> Result<(), ProtocolError> { let mut buf = ByteBuffer::new(); let mut data_buf = ByteBuffer::new(); data_buf.write_u8_varint(packet.id)?; data_buf.write_buffer(&packet.buffer)?; - match compression.lock().unwrap().as_mut() { - Some(compress_threashold) => { - let mut packet_buf = ByteBuffer::new(); + let compress_threashold = compression.load(Ordering::Relaxed); - let mut data = data_buf.as_bytes().to_vec(); - let mut data_length = 0; + if compress_threashold != usize::MAX { + let mut packet_buf = ByteBuffer::new(); - if data.len() >= *compress_threashold { - data_length = data.len(); - data = compress_zlib(&data)?; - } + let mut data = data_buf.as_bytes().to_vec(); + let mut data_length = 0; + + if data.len() >= compress_threashold { + data_length = data.len(); + data = compress_zlib(&data)?; + } - packet_buf.write_usize_varint(data_length)?; - DataBufferWriter::write_bytes(&mut packet_buf, &data)?; + packet_buf.write_usize_varint(data_length)?; + DataBufferWriter::write_bytes(&mut packet_buf, &data)?; - buf.write_usize_varint(packet_buf.len())?; - buf.write_buffer(&packet_buf)?; - }, - None => { - buf.write_usize_varint(data_buf.len())?; - buf.write_buffer(&data_buf)?; - }, + buf.write_usize_varint(packet_buf.len())?; + buf.write_buffer(&packet_buf)?; + } else { + buf.write_usize_varint(data_buf.len())?; + buf.write_buffer(&data_buf)?; } stream.write_buffer(&buf)?;