diff options
| author | mat <github@matdoes.dev> | 2021-12-13 00:07:21 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-13 00:07:21 -0600 |
| commit | c96ae8fce4e53ea9fad111ac7f479f2fa33ef859 (patch) | |
| tree | 2d35061fa712464b225317f56a806d0dd269ca82 /minecraft-protocol/src/packets/login | |
| parent | 2c3bf3b79e133acd01580144771a7cf238ecc4ee (diff) | |
| download | azalea-drasl-c96ae8fce4e53ea9fad111ac7f479f2fa33ef859.tar.xz | |
start implementing joining servers
Diffstat (limited to 'minecraft-protocol/src/packets/login')
3 files changed, 76 insertions, 0 deletions
diff --git a/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs b/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs new file mode 100644 index 00000000..878ca456 --- /dev/null +++ b/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs @@ -0,0 +1,40 @@ +use async_trait::async_trait; +use std::hash::Hash; +use tokio::io::BufReader; + +use crate::{ + mc_buf, + packets::{Packet, PacketTrait}, +}; + +#[derive(Hash, Clone, Debug)] +pub struct ClientboundHelloPacket { + pub server_id: String, + pub public_key: Vec<u8>, + pub nonce: Vec<u8>, +} + +#[async_trait] +impl PacketTrait for ClientboundHelloPacket { + fn get(self) -> Packet { + Packet::ClientboundHelloPacket(self) + } + 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>( + buf: &mut BufReader<T>, + ) -> Result<Packet, 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?; + + Ok(ClientboundHelloPacket { + server_id, + public_key, + nonce, + } + .get()) + } +} diff --git a/minecraft-protocol/src/packets/login/mod.rs b/minecraft-protocol/src/packets/login/mod.rs index 8b137891..47193f92 100644 --- a/minecraft-protocol/src/packets/login/mod.rs +++ b/minecraft-protocol/src/packets/login/mod.rs @@ -1 +1,8 @@ +pub mod clientbound_hello_packet; +pub mod serverbound_hello_packet; +#[derive(Clone, Debug)] +pub enum LoginPacket { + ServerboundHelloPacket(serverbound_hello_packet::ServerboundHelloPacket), + ClientboundHelloPacket(clientbound_hello_packet::ClientboundHelloPacket), +} diff --git a/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs b/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs new file mode 100644 index 00000000..cadf9f7b --- /dev/null +++ b/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs @@ -0,0 +1,29 @@ +use async_trait::async_trait; +use std::hash::Hash; +use tokio::io::BufReader; + +use crate::{ + mc_buf, + packets::{Packet, PacketTrait}, +}; + +#[derive(Hash, Clone, Debug)] +pub struct ServerboundHelloPacket { + pub username: String, +} + +#[async_trait] +impl PacketTrait for ServerboundHelloPacket { + fn get(self) -> Packet { + Packet::ServerboundHelloPacket(self) + } + 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> { + Err("ServerboundHelloPacket::read not implemented".to_string()) + } +} |
