atomic ptrgit add *
This commit is contained in:
parent
6e7e6d7c12
commit
edd5ffe115
30
src/lib.rs
30
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<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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -526,13 +526,14 @@ 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 compress_threashold = compression.load(Ordering::Relaxed);
|
||||
|
||||
if compress_threashold != usize::MAX {
|
||||
let data_length = stream.read_usize_varint_size()?;
|
||||
|
||||
data = stream.read_bytes(packet_length.0 - data_length.1)?;
|
||||
@ -540,31 +541,30 @@ pub fn read_packet<T: Read>(stream: &mut T, compression: Arc<Mutex<Option<usize>
|
||||
if data_length.0 != 0 {
|
||||
data = decompress_zlib(&data, data_length.0)?;
|
||||
}
|
||||
},
|
||||
None => {
|
||||
} 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 compress_threashold = compression.load(Ordering::Relaxed);
|
||||
|
||||
if compress_threashold != usize::MAX {
|
||||
let mut packet_buf = ByteBuffer::new();
|
||||
|
||||
let mut data = data_buf.as_bytes().to_vec();
|
||||
let mut data_length = 0;
|
||||
|
||||
if data.len() >= *compress_threashold {
|
||||
if data.len() >= compress_threashold {
|
||||
data_length = data.len();
|
||||
data = compress_zlib(&data)?;
|
||||
}
|
||||
@ -574,11 +574,9 @@ pub fn write_packet<T: Write>(stream: &mut T, compression: Arc<Mutex<Option<usiz
|
||||
|
||||
buf.write_usize_varint(packet_buf.len())?;
|
||||
buf.write_buffer(&packet_buf)?;
|
||||
},
|
||||
None => {
|
||||
} else {
|
||||
buf.write_usize_varint(data_buf.len())?;
|
||||
buf.write_buffer(&data_buf)?;
|
||||
},
|
||||
}
|
||||
|
||||
stream.write_buffer(&buf)?;
|
||||
|
Loading…
Reference in New Issue
Block a user