reading packets works

This commit is contained in:
mat 2021-12-10 16:27:51 +00:00
parent f6a3f088ac
commit f9f7e3498e
6 changed files with 15 additions and 14 deletions

View file

@ -36,7 +36,7 @@ impl Connection {
Ok(Connection { Ok(Connection {
state: ConnectionProtocol::Handshake, state: ConnectionProtocol::Handshake,
flow: PacketFlow::ClientToServer, flow: PacketFlow::ServerToClient,
stream, stream,
}) })
} }
@ -61,12 +61,15 @@ impl Connection {
// if we recognize the packet id, parse it // if we recognize the packet id, parse it
// TODO let packet = Packet::read(
packet_id.try_into().unwrap(),
&self.state,
&self.flow,
&mut buf,
)
.await?;
// otherwise, read the rest of the packet and throw it away println!("packet: {:?}", packet);
let mut packet_data = Vec::with_capacity((packet_size - packet_id_size as i32) as usize);
buf.read_buf(&mut packet_data).await.unwrap();
println!("packet {:?}", packet_data);
Ok(()) Ok(())
} }

View file

@ -5,8 +5,6 @@ use std::io::{Cursor, Write};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use tokio::io::{AsyncRead, AsyncReadExt, BufReader}; use tokio::io::{AsyncRead, AsyncReadExt, BufReader};
// const MAX_VARINT_SIZE: u32 = 5;
// const MAX_VARLONG_SIZE: u32 = 10;
// const DEFAULT_NBT_QUOTA: u32 = 2097152; // const DEFAULT_NBT_QUOTA: u32 = 2097152;
const MAX_STRING_LENGTH: u16 = 32767; const MAX_STRING_LENGTH: u16 = 32767;
// const MAX_COMPONENT_STRING_LENGTH: u32 = 262144; // const MAX_COMPONENT_STRING_LENGTH: u32 = 262144;

View file

@ -8,7 +8,7 @@ use crate::{
packets::{ConnectionProtocol, Packet, PacketTrait}, packets::{ConnectionProtocol, Packet, PacketTrait},
}; };
#[derive(Hash, Clone)] #[derive(Hash, Clone, Debug)]
pub struct ClientIntentionPacket { pub struct ClientIntentionPacket {
pub protocol_version: u32, pub protocol_version: u32,
pub hostname: String, pub hostname: String,

View file

@ -16,7 +16,7 @@ pub enum ConnectionProtocol {
Login = 2, Login = 2,
} }
#[derive(Clone)] #[derive(Clone, Debug)]
pub enum Packet { pub enum Packet {
// game // game
@ -55,8 +55,8 @@ impl Packet {
/// Read a packet by its id, ConnectionProtocol, and flow /// Read a packet by its id, ConnectionProtocol, and flow
pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
id: u32, id: u32,
protocol: ConnectionProtocol, protocol: &ConnectionProtocol,
flow: PacketFlow, flow: &PacketFlow,
buf: &mut BufReader<T>, buf: &mut BufReader<T>,
) -> Result<Packet, String> { ) -> Result<Packet, String> {
match protocol { match protocol {

View file

@ -7,7 +7,7 @@ use crate::{
packets::{Packet, PacketTrait}, packets::{Packet, PacketTrait},
}; };
#[derive(Hash, Clone)] #[derive(Hash, Clone, Debug)]
pub struct ClientboundStatusResponsePacket { pub struct ClientboundStatusResponsePacket {
status: String, status: String,
} }

View file

@ -7,7 +7,7 @@ use crate::{
packets::{Packet, PacketTrait}, packets::{Packet, PacketTrait},
}; };
#[derive(Hash, Clone)] #[derive(Hash, Clone, Debug)]
pub struct ServerboundStatusRequestPacket {} pub struct ServerboundStatusRequestPacket {}
#[async_trait] #[async_trait]