aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoey Sacchini <joey@sacchini.net>2020-12-02 20:07:32 -0500
committerJoey Sacchini <joey@sacchini.net>2020-12-02 20:07:32 -0500
commitd03d9bf2d0a3496286b3a08931d0f07bc6a40033 (patch)
tree2507c4678beca5a957d309c58cfdf9a025a5eac5
parenta75dccefd966560793e9776bc44d09fa22733a43 (diff)
downloadcraftio-rs-d03d9bf2d0a3496286b3a08931d0f07bc6a40033.tar.gz
craftio-rs-d03d9bf2d0a3496286b3a08931d0f07bc6a40033.tar.bz2
craftio-rs-d03d9bf2d0a3496286b3a08931d0f07bc6a40033.zip
fix some bugs
-rw-r--r--src/reader.rs38
-rw-r--r--src/tcp.rs1
-rw-r--r--src/writer.rs3
3 files changed, 22 insertions, 20 deletions
diff --git a/src/reader.rs b/src/reader.rs
index 68b9a9b..001a561 100644
--- a/src/reader.rs
+++ b/src/reader.rs
@@ -120,9 +120,10 @@ where
where
P: RawPacket<'a>,
{
- let primary_packet_len = rr_unwrap!(self.read_one_varint_sync()).0 as usize;
- rr_unwrap!(self.read_n(primary_packet_len));
- self.read_packet_in_buf::<'a, P>(primary_packet_len)
+ let (primary_packet_len, len_bytes) = rr_unwrap!(self.read_one_varint_sync());
+ let primary_packet_len = primary_packet_len.0 as usize;
+ rr_unwrap!(self.read_n(VAR_INT_BUF_SIZE, primary_packet_len - VAR_INT_BUF_SIZE + len_bytes));
+ self.read_packet_in_buf::<'a, P>(len_bytes, primary_packet_len)
}
}
@@ -136,9 +137,10 @@ where
where
P: RawPacket<'a>,
{
- let primary_packet_len = rr_unwrap!(self.read_one_varint_async().await).0 as usize;
- rr_unwrap!(self.read_n_async(primary_packet_len).await);
- self.read_packet_in_buf::<P>(primary_packet_len)
+ let (primary_packet_len, len_bytes) = rr_unwrap!(self.read_one_varint_async().await);
+ let primary_packet_len = primary_packet_len.0 as usize;
+ rr_unwrap!(self.read_n_async(VAR_INT_BUF_SIZE, primary_packet_len - VAR_INT_BUF_SIZE + len_bytes).await);
+ self.read_packet_in_buf::<P>(len_bytes, primary_packet_len)
}
}
@@ -146,12 +148,12 @@ impl<R> CraftReader<R>
where
R: std::io::Read,
{
- fn read_one_varint_sync(&mut self) -> ReadResult<VarInt> {
- deserialize_varint(rr_unwrap!(self.read_n(VAR_INT_BUF_SIZE)))
+ fn read_one_varint_sync(&mut self) -> ReadResult<(VarInt, usize)> {
+ deserialize_varint(rr_unwrap!(self.read_n(0, VAR_INT_BUF_SIZE)))
}
- fn read_n(&mut self, n: usize) -> ReadResult<&mut [u8]> {
- let buf = get_sized_buf(&mut self.raw_buf, 0, n);
+ fn read_n(&mut self, offset: usize, n: usize) -> ReadResult<&mut [u8]> {
+ let buf = get_sized_buf(&mut self.raw_buf, offset, n);
check_unexpected_eof!(self.inner.read_exact(buf));
Ok(Some(buf))
}
@@ -162,12 +164,12 @@ impl<R> CraftReader<R>
where
R: futures::io::AsyncRead + Unpin + Sync + Send,
{
- async fn read_one_varint_async(&mut self) -> ReadResult<VarInt> {
- deserialize_varint(rr_unwrap!(self.read_n_async(VAR_INT_BUF_SIZE).await))
+ async fn read_one_varint_async(&mut self) -> ReadResult<(VarInt, usize)> {
+ deserialize_varint(rr_unwrap!(self.read_n_async(0, VAR_INT_BUF_SIZE).await))
}
- async fn read_n_async(&mut self, n: usize) -> ReadResult<&mut [u8]> {
- let buf = get_sized_buf(&mut self.raw_buf, 0, n);
+ async fn read_n_async(&mut self, offset: usize, n: usize) -> ReadResult<&mut [u8]> {
+ let buf = get_sized_buf(&mut self.raw_buf, offset, n);
check_unexpected_eof!(self.inner.read_exact(buf).await);
Ok(Some(buf))
}
@@ -204,12 +206,12 @@ impl<R> CraftReader<R> {
}
}
- fn read_packet_in_buf<'a, P>(&'a mut self, size: usize) -> ReadResult<P>
+ fn read_packet_in_buf<'a, P>(&'a mut self, offset: usize, size: usize) -> ReadResult<P>
where
P: RawPacket<'a>,
{
// find data in buf
- let buf = &mut self.raw_buf.as_mut().expect("should exist right now")[..size];
+ let buf = &mut self.raw_buf.as_mut().expect("should exist right now")[offset..offset+size];
// decrypt the packet if encryption is enabled
if let Some(encryption) = self.encryption.as_mut() {
encryption.decrypt(buf);
@@ -265,9 +267,9 @@ where
}
}
-fn deserialize_varint(buf: &[u8]) -> ReadResult<VarInt> {
+fn deserialize_varint(buf: &[u8]) -> ReadResult<(VarInt, usize)> {
match VarInt::mc_deserialize(buf) {
- Ok(v) => Ok(Some(v.value)),
+ Ok(v) => Ok(Some((v.value, buf.len() - v.data.len()))),
Err(err) => Err(ReadError::PacketHeaderErr(err)),
}
}
diff --git a/src/tcp.rs b/src/tcp.rs
index e2ead90..49490b7 100644
--- a/src/tcp.rs
+++ b/src/tcp.rs
@@ -2,7 +2,6 @@ use crate::connection::CraftConnection;
use crate::reader::CraftReader;
use crate::writer::CraftWriter;
use mcproto_rs::protocol::{PacketDirection, State};
-use std::convert::TryFrom;
use std::io::BufReader as StdBufReader;
use std::net::TcpStream;
diff --git a/src/writer.rs b/src/writer.rs
index 6c76b23..a1f97c1 100644
--- a/src/writer.rs
+++ b/src/writer.rs
@@ -183,7 +183,7 @@ impl<W> CraftWriter<W> {
) -> WriteResult<(&[u8], &mut W)> {
// assume id and body are in raw buf from HEADER_OFFSET .. size + HEADER_OFFSET
let body_size = prepared.id_size + prepared.data_size;
- let buf = get_sized_buf(&mut self.raw_buf, 0, body_size);
+ let buf = get_sized_buf(&mut self.raw_buf, 0, HEADER_OFFSET + body_size);
let packet_data = if let Some(threshold) = self.compression_threshold {
if threshold >= 0 && (threshold as usize) <= body_size {
@@ -334,6 +334,7 @@ struct GrowVecSerializer<'a> {
impl<'a> Serializer for GrowVecSerializer<'a> {
fn serialize_bytes(&mut self, data: &[u8]) -> SerializeResult {
get_sized_buf(self.target, self.at + self.offset, data.len()).copy_from_slice(data);
+ self.at += data.len();
Ok(())
}
}