mirror of
https://github.com/mat-1/azalea.git
synced 2024-09-19 14:42:32 +00:00
reading packet almost complete
This commit is contained in:
parent
fcaca28ff1
commit
500754eab0
2 changed files with 22 additions and 18 deletions
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -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<T: AsyncRead + std::marker::Unpin>(
|
||||
buf: &mut BufReader<T>,
|
||||
) -> Result<u8, String> {
|
||||
|
@ -29,9 +30,10 @@ pub fn write_bytes(buf: &mut Vec<u8>, 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<T: AsyncRead + std::marker::Unpin>(
|
||||
buf: &mut BufReader<T>,
|
||||
) -> Result<u32, String> {
|
||||
) -> 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<T: AsyncRead + std::marker::Unpin>(
|
|||
.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<u8>, 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue