Skip to content

Commit

Permalink
atomic ptrgit add *
Browse files Browse the repository at this point in the history
  • Loading branch information
MeexReay committed Jul 14, 2024
1 parent 6e7e6d7 commit edd5ffe
Showing 1 changed file with 32 additions and 34 deletions.
66 changes: 32 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -496,12 +496,12 @@ impl<T: Read + Write> MinecraftConnection<T> {

/// Read [`Packet`](Packet) from connection
pub fn read_packet(&mut self) -> Result<Packet, ProtocolError> {
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)
}
}

Expand All @@ -526,59 +526,57 @@ pub type MCConn<T> = MinecraftConnection<T>;
pub type MCConnTcp = MinecraftConnection<TcpStream>;

/// Read [`Packet`](Packet) from stream
pub fn read_packet<T: Read>(stream: &mut T, compression: Arc<Mutex<Option<usize>>>) -> Result<Packet, ProtocolError> {
pub fn read_packet<T: Read>(stream: &mut T, compression: Arc<AtomicUsize>) -> Result<Packet, ProtocolError> {
let mut data: Vec<u8>;

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<T: Write>(stream: &mut T, compression: Arc<Mutex<Option<usize>>>, packet: &Packet) -> Result<(), ProtocolError> {
pub fn write_packet<T: Write>(stream: &mut T, compression: Arc<AtomicUsize>, 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)?;
Expand Down

0 comments on commit edd5ffe

Please sign in to comment.