diff --git a/minecraft-protocol/src/connection.rs b/minecraft-protocol/src/connection.rs index 96514486..b4e3595c 100644 --- a/minecraft-protocol/src/connection.rs +++ b/minecraft-protocol/src/connection.rs @@ -1,9 +1,9 @@ use crate::{mc_buf, packets::Packet, ServerIpAddress}; use bytes::BytesMut; -use std::io::{Cursor, Read, Seek, SeekFrom, Write}; +use std::io::{Cursor, Read, Write}; use tokio::io::AsyncWriteExt; use tokio::{ - io::{AsyncReadExt, BufReader, BufWriter}, + io::{AsyncReadExt, BufReader, BufWriter, SeekFrom, AsyncSeek, AsyncSeekExt}, net::TcpStream, }; @@ -46,21 +46,23 @@ impl Connection { // the first thing minecraft sends us is the length as a varint, which can be up to 5 bytes long let mut buf = BufReader::with_capacity(5 * 1024, &mut self.stream); - - let packet_size = mc_buf::read_varint(&mut buf).await?; - - println!("packet size from varint: {}", packet_size); - + let (packet_size, packet_size_varint_size) = mc_buf::read_varint(&mut buf).await?; + // then, minecraft tells us the packet id as a single byte let packet_id = mc_buf::read_byte(&mut buf).await?; // read the rest of the packet - let mut packet_data = Vec::with_capacity(packet_size as usize); + let mut packet_data = Vec::with_capacity( + ( + packet_size // the total size of the packet + - 1 // we just read the packet id, so we don't read that byte again + ) as usize); buf.read_buf(&mut packet_data).await.unwrap(); - println!( - "packet id {}: {}", - packet_id, - String::from_utf8(packet_data.clone()).unwrap() - ); + println!("packet {}", packet_id); + // println!( + // "packet id {}: {}", + // packet_id, + // String::from_utf8(packet_data.clone()).unwrap() + // ); Ok(()) } diff --git a/minecraft-protocol/src/mc_buf.rs b/minecraft-protocol/src/mc_buf.rs index 6c2b9f1b..f657626f 100644 --- a/minecraft-protocol/src/mc_buf.rs +++ b/minecraft-protocol/src/mc_buf.rs @@ -11,6 +11,7 @@ use tokio::io::{AsyncRead, AsyncReadExt, BufReader}; const MAX_STRING_LENGTH: u16 = 32767; // const MAX_COMPONENT_STRING_LENGTH: u32 = 262144; +/// Read a single byte from the reader pub async fn read_byte( buf: &mut BufReader, ) -> Result { @@ -29,9 +30,10 @@ pub fn write_bytes(buf: &mut Vec, bytes: &[u8]) { } // fast varints stolen from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L67 +/// Read a single varint from the reader and return the value, along with the number of bytes read pub async fn read_varint( buf: &mut BufReader, -) -> Result { +) -> Result<(u32, u8), String> { let mut buffer = [0]; let mut ans = 0; for i in 0..4 { @@ -40,10 +42,10 @@ pub async fn read_varint( .or_else(|_| Err("Invalid VarInt".to_string()))?; ans |= ((buffer[0] & 0b0111_1111) as u32) << 7 * i; if buffer[0] & 0b1000_0000 == 0 { - break; + return Ok((ans, i + 1)); } } - Ok(ans) + Ok((ans, 5)) } pub fn write_varint(buf: &mut Vec, mut value: u32) { @@ -71,13 +73,13 @@ mod tests { #[tokio::test] async fn test_read_varint() { let mut buf = BufReader::new(Cursor::new(vec![192, 196, 7])); - assert_eq!(read_varint(&mut buf).await.unwrap(), 123456); + assert_eq!(read_varint(&mut buf).await.unwrap(), (123456, 3)); } #[tokio::test] async fn test_read_varint_longer() { let mut buf = BufReader::new(Cursor::new(vec![138, 56, 0, 135, 56, 123])); - assert_eq!(read_varint(&mut buf).await.unwrap(), 7178); + assert_eq!(read_varint(&mut buf).await.unwrap(), (7178, 2)); } }