From c96ae8fce4e53ea9fad111ac7f479f2fa33ef859 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 13 Dec 2021 00:07:21 -0600 Subject: start implementing joining servers --- minecraft-protocol/src/packets/status/mod.rs | 78 ++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'minecraft-protocol/src/packets/status/mod.rs') diff --git a/minecraft-protocol/src/packets/status/mod.rs b/minecraft-protocol/src/packets/status/mod.rs index efc03631..7e327ca7 100644 --- a/minecraft-protocol/src/packets/status/mod.rs +++ b/minecraft-protocol/src/packets/status/mod.rs @@ -1,2 +1,80 @@ pub mod clientbound_status_response_packet; pub mod serverbound_status_request_packet; + +use async_trait::async_trait; +use tokio::io::BufReader; + +use crate::connect::PacketFlow; + +use super::{ConnectionProtocol, PacketTrait, ProtocolPacket}; + +#[derive(Clone, Debug)] +pub enum StatusPacket { + ServerboundStatusRequestPacket( + serverbound_status_request_packet::ServerboundStatusRequestPacket, + ), + ClientboundStatusResponsePacket( + clientbound_status_response_packet::ClientboundStatusResponsePacket, + ), +} + +// #[async_trait] +// impl ProtocolPacket for StatusPacket { +impl StatusPacket { + fn get_inner(self) -> impl PacketTrait { + match self { + StatusPacket::ServerboundStatusRequestPacket(packet) => packet, + StatusPacket::ClientboundStatusResponsePacket(packet) => packet, + } + } + // fn get_inner(&self) -> StatusPacket { + // match self { + // StatusPacket::ServerboundStatusRequestPacket(packet) => packet, + // StatusPacket::ClientboundStatusResponsePacket(packet) => packet, + // } + // } + + fn id(&self) -> u32 { + match self { + StatusPacket::ServerboundStatusRequestPacket(_packet) => 0x00, + StatusPacket::ClientboundStatusResponsePacket(_packet) => 0x00, + } + } + + fn write(&self, buf: &mut Vec) { + match self { + StatusPacket::ServerboundStatusRequestPacket(packet) => packet.write(buf), + StatusPacket::ClientboundStatusResponsePacket(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, + P: ProtocolPacket, + >( + id: u32, + flow: &PacketFlow, + buf: &mut BufReader, + ) -> Result + where + Self: Sized, + { + match flow { + PacketFlow::ServerToClient => match id { + 0x00 => Ok( + clientbound_status_response_packet::ClientboundStatusResponsePacket::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?, + ), + _ => Err(format!("Unknown ClientToServer status packet id: {}", id)), + }, + } + } +} -- cgit v1.2.3