From 5f243e71fe1de4a7dab491c31f9d9ea61d7fbea8 Mon Sep 17 00:00:00 2001 From: MeexReay Date: Tue, 12 Nov 2024 00:07:18 +0300 Subject: [PATCH] connectionClosedError --- src/data_buffer/reader.rs | 12 ++++++++++-- src/lib.rs | 24 ++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/data_buffer/reader.rs b/src/data_buffer/reader.rs index 60cad0b..2aac9ec 100644 --- a/src/data_buffer/reader.rs +++ b/src/data_buffer/reader.rs @@ -204,8 +204,16 @@ pub trait DataBufferReader { impl DataBufferReader for R { fn read_bytes(&mut self, size: usize) -> Result, ProtocolError> { let mut buf = vec![0; size]; - match self.read_exact(&mut buf) { - Ok(_) => Ok(buf), + match self.read(&mut buf) { + Ok(i) => if i == size { + Ok(buf) + } else if i == 0 { + Err(ProtocolError::ConnectionClosedError) + } else { + buf.truncate(i); + buf.append(&mut self.read_bytes(size-i)?); + Ok(buf) + }, Err(_) => Err(ProtocolError::ReadError), } } diff --git a/src/lib.rs b/src/lib.rs index 9e76d04..7bb650b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -251,15 +251,27 @@ impl MinecraftConnection { #[cfg(feature = "atomic_clone")] { - return read_packet_atomic( - &mut self.stream, - self.compression.clone(), - Ordering::Relaxed, - ) + return match read_packet_atomic( + &mut self.stream, + self.compression.clone(), + Ordering::Relaxed, + ) { + Err(ProtocolError::ConnectionClosedError) => { + self.set_alive(false); + Err(ProtocolError::ConnectionClosedError) + }, + i => i + }; } #[cfg(not(feature = "atomic_clone"))] - read_packet(&mut self.stream, self.compression) + match read_packet(&mut self.stream, self.compression) { + Err(ProtocolError::ConnectionClosedError) => { + self.set_alive(false); + Err(ProtocolError::ConnectionClosedError) + }, + i => i + } } /// Write [`Packet`](Packet) to connection