diff options
Diffstat (limited to 'minecraft-protocol/src')
6 files changed, 78 insertions, 33 deletions
diff --git a/minecraft-protocol/src/packets/handshake/client_intention_packet.rs b/minecraft-protocol/src/packets/handshake/client_intention_packet.rs index 76957c6f..54a08f39 100644 --- a/minecraft-protocol/src/packets/handshake/client_intention_packet.rs +++ b/minecraft-protocol/src/packets/handshake/client_intention_packet.rs @@ -8,6 +8,8 @@ use crate::{ packets::{ConnectionProtocol, Packet}, }; +use super::HandshakePacket; + #[derive(Hash, Clone, Debug)] pub struct ClientIntentionPacket { pub protocol_version: u32, @@ -17,22 +19,21 @@ pub struct ClientIntentionPacket { pub intention: ConnectionProtocol, } -#[async_trait] impl ClientIntentionPacket { - fn get(self) -> Packet { - Packet::ClientIntentionPacket(self) + pub fn get(self) -> HandshakePacket { + HandshakePacket::ClientIntentionPacket(self) } - fn write(&self, buf: &mut Vec<u8>) { + pub fn write(&self, buf: &mut Vec<u8>) { mc_buf::write_varint(buf, self.protocol_version as i32); mc_buf::write_utf(buf, &self.hostname); mc_buf::write_short(buf, self.port); mc_buf::write_varint(buf, self.intention.clone() as i32); } - async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( - _buf: &mut BufReader<T>, - ) -> Result<Packet, String> { + pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( + buf: &mut BufReader<T>, + ) -> Result<HandshakePacket, String> { Err("ClientIntentionPacket::parse not implemented".to_string()) // Ok(ClientIntentionPacket {}.get()) } diff --git a/minecraft-protocol/src/packets/handshake/mod.rs b/minecraft-protocol/src/packets/handshake/mod.rs index d2716299..792588b5 100644 --- a/minecraft-protocol/src/packets/handshake/mod.rs +++ b/minecraft-protocol/src/packets/handshake/mod.rs @@ -1,6 +1,51 @@ pub mod client_intention_packet; +use std::f32::consts::E; + +use async_trait::async_trait; +use tokio::io::BufReader; + +use crate::connect::PacketFlow; + +use super::ProtocolPacket; + #[derive(Clone, Debug)] -pub enum HandshakePacket { +pub enum HandshakePacket +where + Self: Sized, +{ ClientIntentionPacket(client_intention_packet::ClientIntentionPacket), } + +#[async_trait] +impl ProtocolPacket for HandshakePacket { + fn id(&self) -> u32 { + match self { + HandshakePacket::ClientIntentionPacket(_packet) => 0x00, + } + } + + fn write(&self, buf: &mut Vec<u8>) { + match self { + HandshakePacket::ClientIntentionPacket(packet) => packet.write(buf), + } + } + + /// Read a packet by its id, ConnectionProtocol, and flow + async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( + id: u32, + flow: &PacketFlow, + buf: &mut BufReader<T>, + ) -> Result<HandshakePacket, String> + where + Self: Sized, + { + match flow { + PacketFlow::ServerToClient => Err("HandshakePacket::read not implemented".to_string()), + PacketFlow::ClientToServer => match id { + 0x00 => Ok(client_intention_packet::ClientIntentionPacket::read(buf).await?), + _ => Err(format!("Unknown ClientToServer status packet id: {}", id)), + }, + } + } +} diff --git a/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs b/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs index c0ec85f3..7e69e3ce 100644 --- a/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs +++ b/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs @@ -4,6 +4,8 @@ use tokio::io::BufReader; use crate::{mc_buf, packets::Packet}; +use super::LoginPacket; + #[derive(Hash, Clone, Debug)] pub struct ClientboundHelloPacket { pub server_id: String, @@ -12,16 +14,17 @@ pub struct ClientboundHelloPacket { } impl ClientboundHelloPacket { - fn get(self) -> Packet { - Packet::ClientboundHelloPacket(self) + pub fn get(self) -> LoginPacket { + LoginPacket::ClientboundHelloPacket(self) } - fn write(&self, _buf: &mut Vec<u8>) { + + pub fn write(&self, _buf: &mut Vec<u8>) { panic!("ClientboundHelloPacket::write not implemented") } - 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>( buf: &mut BufReader<T>, - ) -> Result<Packet, String> { + ) -> Result<LoginPacket, String> { let server_id = mc_buf::read_utf_with_len(buf, 20).await?; let public_key = mc_buf::read_byte_array(buf).await?; let nonce = mc_buf::read_byte_array(buf).await?; diff --git a/minecraft-protocol/src/packets/login/mod.rs b/minecraft-protocol/src/packets/login/mod.rs index 4d4f13d3..70799e22 100644 --- a/minecraft-protocol/src/packets/login/mod.rs +++ b/minecraft-protocol/src/packets/login/mod.rs @@ -21,15 +21,15 @@ where impl ProtocolPacket for LoginPacket { fn id(&self) -> u32 { match self { - LoginPacket::ServerboundStatusRequestPacket(_packet) => 0x00, - LoginPacket::ClientboundStatusResponsePacket(_packet) => 0x00, + LoginPacket::ServerboundHelloPacket(_packet) => 0x00, + LoginPacket::ClientboundHelloPacket(_packet) => 0x01, } } fn write(&self, buf: &mut Vec<u8>) { match self { - LoginPacket::ServerboundStatusRequestPacket(packet) => packet.write(buf), - LoginPacket::ClientboundStatusResponsePacket(packet) => packet.write(buf), + LoginPacket::ServerboundHelloPacket(packet) => packet.write(buf), + LoginPacket::ClientboundHelloPacket(packet) => packet.write(buf), } } @@ -44,17 +44,11 @@ impl ProtocolPacket for LoginPacket { { match flow { PacketFlow::ServerToClient => match id { - 0x00 => Ok( - clientbound_status_response_packet::ClientboundStatusResponsePacket::read(buf) - .await?, - ), + 0x01 => Ok(clientbound_hello_packet::ClientboundHelloPacket::read(buf).await?), _ => Err(format!("Unknown ServerToClient status packet id: {}", id)), }, PacketFlow::ClientToServer => match id { - 0x00 => Ok( - serverbound_status_request_packet::ServerboundStatusRequestPacket::read(buf) - .await?, - ), + 0x00 => Ok(serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?), _ => Err(format!("Unknown ClientToServer status packet id: {}", id)), }, } diff --git a/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs b/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs index 345b11f6..9415fe82 100644 --- a/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs +++ b/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs @@ -4,23 +4,25 @@ use tokio::io::BufReader; use crate::{mc_buf, packets::Packet}; +use super::LoginPacket; + #[derive(Hash, Clone, Debug)] pub struct ServerboundHelloPacket { pub username: String, } -#[async_trait] impl ServerboundHelloPacket { - fn get(self) -> Packet { - Packet::ServerboundHelloPacket(self) + pub fn get(self) -> LoginPacket { + LoginPacket::ServerboundHelloPacket(self) } - fn write(&self, buf: &mut Vec<u8>) { + + pub fn write(&self, buf: &mut Vec<u8>) { mc_buf::write_utf(buf, &self.username); } - async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( - _buf: &mut BufReader<T>, - ) -> Result<Packet, String> { + pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( + buf: &mut BufReader<T>, + ) -> Result<LoginPacket, String> { Err("ServerboundHelloPacket::read not implemented".to_string()) } } diff --git a/minecraft-protocol/src/packets/status/mod.rs b/minecraft-protocol/src/packets/status/mod.rs index aa302e58..2696cc07 100644 --- a/minecraft-protocol/src/packets/status/mod.rs +++ b/minecraft-protocol/src/packets/status/mod.rs @@ -6,7 +6,7 @@ use tokio::io::BufReader; use crate::connect::PacketFlow; -use super::{ProtocolPacket}; +use super::ProtocolPacket; #[derive(Clone, Debug)] pub enum StatusPacket |
