From 4ee4687053b7442f518823b08099c156f4da4e83 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 29 Jul 2022 02:59:40 -0500 Subject: Split clientbound and serverbound packets --- azalea-protocol/src/connect.rs | 52 ++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 35 deletions(-) (limited to 'azalea-protocol/src/connect.rs') diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index 67771d8e..bf730fc4 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -1,29 +1,21 @@ //! parse sending and receiving packets with a server. -use crate::packets::game::GamePacket; -use crate::packets::handshake::HandshakePacket; -use crate::packets::login::LoginPacket; -use crate::packets::status::StatusPacket; +use crate::packets::game::{ClientboundGamePacket, ServerboundGamePacket}; +use crate::packets::handshake::{ClientboundHandshakePacket, ServerboundHandshakePacket}; +use crate::packets::login::{ClientboundLoginPacket, ServerboundLoginPacket}; +use crate::packets::status::{ClientboundStatusPacket, ServerboundStatusPacket}; use crate::read::read_packet; use crate::write::write_packet; use crate::ServerIpAddress; use azalea_crypto::{Aes128CfbDec, Aes128CfbEnc}; use tokio::net::TcpStream; -#[derive(Debug, Clone, Copy)] -pub enum PacketFlow { - ClientToServer, - ServerToClient, -} - pub struct HandshakeConnection { - pub flow: PacketFlow, /// The buffered writer pub stream: TcpStream, } pub struct GameConnection { - pub flow: PacketFlow, /// The buffered writer pub stream: TcpStream, pub compression_threshold: Option, @@ -32,13 +24,11 @@ pub struct GameConnection { } pub struct StatusConnection { - pub flow: PacketFlow, /// The buffered writer pub stream: TcpStream, } pub struct LoginConnection { - pub flow: PacketFlow, /// The buffered writer pub stream: TcpStream, pub compression_threshold: Option, @@ -60,15 +50,11 @@ impl HandshakeConnection { .set_nodelay(true) .expect("Error enabling tcp_nodelay"); - Ok(HandshakeConnection { - flow: PacketFlow::ServerToClient, - stream, - }) + Ok(HandshakeConnection { stream }) } pub fn login(self) -> LoginConnection { LoginConnection { - flow: self.flow, stream: self.stream, compression_threshold: None, enc_cipher: None, @@ -78,25 +64,23 @@ impl HandshakeConnection { pub fn status(self) -> StatusConnection { StatusConnection { - flow: self.flow, stream: self.stream, } } - pub async fn read(&mut self) -> Result { - read_packet::(&self.flow, &mut self.stream, None, &mut None).await + pub async fn read(&mut self) -> Result { + read_packet::(&mut self.stream, None, &mut None).await } /// Write a packet to the server - pub async fn write(&mut self, packet: HandshakePacket) { + pub async fn write(&mut self, packet: ServerboundHandshakePacket) { write_packet(packet, &mut self.stream, None, &mut None).await; } } impl GameConnection { - pub async fn read(&mut self) -> Result { - read_packet::( - &self.flow, + pub async fn read(&mut self) -> Result { + read_packet::( &mut self.stream, self.compression_threshold, &mut self.dec_cipher, @@ -105,7 +89,7 @@ impl GameConnection { } /// Write a packet to the server - pub async fn write(&mut self, packet: GamePacket) { + pub async fn write(&mut self, packet: ServerboundGamePacket) { write_packet( packet, &mut self.stream, @@ -117,20 +101,19 @@ impl GameConnection { } impl StatusConnection { - pub async fn read(&mut self) -> Result { - read_packet::(&self.flow, &mut self.stream, None, &mut None).await + pub async fn read(&mut self) -> Result { + read_packet::(&mut self.stream, None, &mut None).await } /// Write a packet to the server - pub async fn write(&mut self, packet: StatusPacket) { + pub async fn write(&mut self, packet: ServerboundStatusPacket) { write_packet(packet, &mut self.stream, None, &mut None).await; } } impl LoginConnection { - pub async fn read(&mut self) -> Result { - read_packet::( - &self.flow, + pub async fn read(&mut self) -> Result { + read_packet::( &mut self.stream, self.compression_threshold, &mut self.dec_cipher, @@ -139,7 +122,7 @@ impl LoginConnection { } /// Write a packet to the server - pub async fn write(&mut self, packet: LoginPacket) { + pub async fn write(&mut self, packet: ServerboundLoginPacket) { write_packet( packet, &mut self.stream, @@ -167,7 +150,6 @@ impl LoginConnection { pub fn game(self) -> GameConnection { GameConnection { - flow: self.flow, stream: self.stream, compression_threshold: self.compression_threshold, enc_cipher: self.enc_cipher, -- cgit v1.2.3 From 0c2ce00bae1d0a4711ad45f5558286dccb04b0ae Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 29 Jul 2022 16:29:06 -0500 Subject: make Connection a single struct with generics This isn't that good of a solution but I couldn't come up with a better one and this was pretty simple to implement --- azalea-client/src/client.rs | 8 +-- azalea-client/src/ping.rs | 4 +- azalea-protocol/src/connect.rs | 150 +++++++++++++++++------------------------ 3 files changed, 69 insertions(+), 93 deletions(-) (limited to 'azalea-protocol/src/connect.rs') diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index f5dfdf98..28154bce 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -3,7 +3,7 @@ use azalea_auth::game_profile::GameProfile; use azalea_core::{ChunkPos, EntityPos, PositionDelta, PositionDeltaTrait, ResourceLocation}; use azalea_entity::Entity; use azalea_protocol::{ - connect::{GameConnection, HandshakeConnection}, + connect::Connection, packets::{ game::{ clientbound_player_chat_packet::ClientboundPlayerChatPacket, @@ -12,7 +12,7 @@ use azalea_protocol::{ serverbound_custom_payload_packet::ServerboundCustomPayloadPacket, serverbound_keep_alive_packet::ServerboundKeepAlivePacket, serverbound_move_player_packet_pos_rot::ServerboundMovePlayerPacketPosRot, - ClientboundGamePacket, + ClientboundGamePacket, ServerboundGamePacket, }, handshake::client_intention_packet::ClientIntentionPacket, login::{ @@ -61,7 +61,7 @@ pub enum ChatPacket { #[derive(Clone)] pub struct Client { game_profile: GameProfile, - pub conn: Arc>, + pub conn: Arc>>, pub player: Arc>, pub dimension: Arc>>, // game_loop @@ -81,7 +81,7 @@ impl Client { ) -> Result<(Self, UnboundedReceiver), String> { let resolved_address = resolver::resolve_address(address).await?; - let mut conn = HandshakeConnection::new(&resolved_address).await?; + let mut conn = Connection::new(&resolved_address).await?; // handshake conn.write( diff --git a/azalea-client/src/ping.rs b/azalea-client/src/ping.rs index c4ef2915..e4acfc34 100755 --- a/azalea-client/src/ping.rs +++ b/azalea-client/src/ping.rs @@ -1,6 +1,6 @@ ///! Ping Minecraft servers. use azalea_protocol::{ - connect::HandshakeConnection, + connect::Connection, packets::{ handshake::client_intention_packet::ClientIntentionPacket, status::{ @@ -18,7 +18,7 @@ pub async fn ping_server( ) -> Result { let resolved_address = resolver::resolve_address(address).await?; - let mut conn = HandshakeConnection::new(&resolved_address).await?; + let mut conn = Connection::new(&resolved_address).await?; // send the client intention packet and switch to the status state conn.write( diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index bf730fc4..a438ccfb 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -4,40 +4,58 @@ use crate::packets::game::{ClientboundGamePacket, ServerboundGamePacket}; use crate::packets::handshake::{ClientboundHandshakePacket, ServerboundHandshakePacket}; use crate::packets::login::{ClientboundLoginPacket, ServerboundLoginPacket}; use crate::packets::status::{ClientboundStatusPacket, ServerboundStatusPacket}; +use crate::packets::ProtocolPacket; use crate::read::read_packet; use crate::write::write_packet; use crate::ServerIpAddress; use azalea_crypto::{Aes128CfbDec, Aes128CfbEnc}; +use std::fmt::Debug; +use std::marker::PhantomData; use tokio::net::TcpStream; -pub struct HandshakeConnection { - /// The buffered writer - pub stream: TcpStream, -} +pub struct Handshake; +pub struct Game; +pub struct Status; +pub struct Login; -pub struct GameConnection { +pub struct Connection { /// The buffered writer pub stream: TcpStream, pub compression_threshold: Option, pub enc_cipher: Option, pub dec_cipher: Option, + _reading: PhantomData, + _writing: PhantomData, } -pub struct StatusConnection { - /// The buffered writer - pub stream: TcpStream, -} +impl Connection +where + R: ProtocolPacket + Debug, + W: ProtocolPacket + Debug, +{ + pub async fn read(&mut self) -> Result { + read_packet::( + &mut self.stream, + self.compression_threshold, + &mut self.dec_cipher, + ) + .await + } -pub struct LoginConnection { - /// The buffered writer - pub stream: TcpStream, - pub compression_threshold: Option, - pub enc_cipher: Option, - pub dec_cipher: Option, + /// Write a packet to the server + pub async fn write(&mut self, packet: W) { + write_packet( + packet, + &mut self.stream, + self.compression_threshold, + &mut self.enc_cipher, + ) + .await; + } } -impl HandshakeConnection { - pub async fn new(address: &ServerIpAddress) -> Result { +impl Connection { + pub async fn new(address: &ServerIpAddress) -> Result { let ip = address.ip; let port = address.port; @@ -50,88 +68,44 @@ impl HandshakeConnection { .set_nodelay(true) .expect("Error enabling tcp_nodelay"); - Ok(HandshakeConnection { stream }) - } - - pub fn login(self) -> LoginConnection { - LoginConnection { - stream: self.stream, + Ok(Connection { + stream, compression_threshold: None, enc_cipher: None, dec_cipher: None, - } + _reading: PhantomData, + _writing: PhantomData, + }) } - pub fn status(self) -> StatusConnection { - StatusConnection { + pub fn login(self) -> Connection { + Connection { stream: self.stream, + compression_threshold: self.compression_threshold, + enc_cipher: self.enc_cipher, + dec_cipher: self.dec_cipher, + _reading: PhantomData, + _writing: PhantomData, } } - pub async fn read(&mut self) -> Result { - read_packet::(&mut self.stream, None, &mut None).await - } - - /// Write a packet to the server - pub async fn write(&mut self, packet: ServerboundHandshakePacket) { - write_packet(packet, &mut self.stream, None, &mut None).await; - } -} - -impl GameConnection { - pub async fn read(&mut self) -> Result { - read_packet::( - &mut self.stream, - self.compression_threshold, - &mut self.dec_cipher, - ) - .await - } - - /// Write a packet to the server - pub async fn write(&mut self, packet: ServerboundGamePacket) { - write_packet( - packet, - &mut self.stream, - self.compression_threshold, - &mut self.enc_cipher, - ) - .await; - } -} - -impl StatusConnection { - pub async fn read(&mut self) -> Result { - read_packet::(&mut self.stream, None, &mut None).await - } - - /// Write a packet to the server - pub async fn write(&mut self, packet: ServerboundStatusPacket) { - write_packet(packet, &mut self.stream, None, &mut None).await; + pub fn status(self) -> Connection { + Connection { + stream: self.stream, + compression_threshold: self.compression_threshold, + enc_cipher: self.enc_cipher, + dec_cipher: self.dec_cipher, + _reading: PhantomData, + _writing: PhantomData, + } } } -impl LoginConnection { - pub async fn read(&mut self) -> Result { - read_packet::( - &mut self.stream, - self.compression_threshold, - &mut self.dec_cipher, - ) - .await - } +impl Connection {} - /// Write a packet to the server - pub async fn write(&mut self, packet: ServerboundLoginPacket) { - write_packet( - packet, - &mut self.stream, - self.compression_threshold, - &mut self.enc_cipher, - ) - .await; - } +impl Connection {} +impl Connection { pub fn set_compression_threshold(&mut self, threshold: i32) { // if you pass a threshold of 0 or less, compression is disabled if threshold > 0 { @@ -148,12 +122,14 @@ impl LoginConnection { self.dec_cipher = Some(dec_cipher); } - pub fn game(self) -> GameConnection { - GameConnection { + pub fn game(self) -> Connection { + Connection { stream: self.stream, compression_threshold: self.compression_threshold, enc_cipher: self.enc_cipher, dec_cipher: self.dec_cipher, + _reading: PhantomData, + _writing: PhantomData, } } } -- cgit v1.2.3 From 637e0e09840ac3c4c3b6695ab940c0906215b49a Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 29 Jul 2022 16:29:57 -0500 Subject: remove unused structs --- azalea-protocol/src/connect.rs | 5 ----- 1 file changed, 5 deletions(-) (limited to 'azalea-protocol/src/connect.rs') diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index a438ccfb..cf251576 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -13,11 +13,6 @@ use std::fmt::Debug; use std::marker::PhantomData; use tokio::net::TcpStream; -pub struct Handshake; -pub struct Game; -pub struct Status; -pub struct Login; - pub struct Connection { /// The buffered writer pub stream: TcpStream, -- cgit v1.2.3 From 1eef0a537e13b56d55a5a96e61a7e1361dbb02d6 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 29 Jul 2022 17:17:14 -0500 Subject: simplify switching packet states --- azalea-protocol/src/connect.rs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'azalea-protocol/src/connect.rs') diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index cf251576..75c64517 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -74,25 +74,11 @@ impl Connection { } pub fn login(self) -> Connection { - Connection { - stream: self.stream, - compression_threshold: self.compression_threshold, - enc_cipher: self.enc_cipher, - dec_cipher: self.dec_cipher, - _reading: PhantomData, - _writing: PhantomData, - } + Connection::from(self) } pub fn status(self) -> Connection { - Connection { - stream: self.stream, - compression_threshold: self.compression_threshold, - enc_cipher: self.enc_cipher, - dec_cipher: self.dec_cipher, - _reading: PhantomData, - _writing: PhantomData, - } + Connection::from(self) } } @@ -118,11 +104,27 @@ impl Connection { } pub fn game(self) -> Connection { + Connection::from(self) + } +} + +// rust doesn't let us implement From because allegedly it conflicts with +// `core`'s "impl From for T" so we do this instead +impl Connection +where + R1: ProtocolPacket + Debug, + W1: ProtocolPacket + Debug, +{ + fn from(connection: Connection) -> Connection + where + R2: ProtocolPacket + Debug, + W2: ProtocolPacket + Debug, + { Connection { - stream: self.stream, - compression_threshold: self.compression_threshold, - enc_cipher: self.enc_cipher, - dec_cipher: self.dec_cipher, + stream: connection.stream, + compression_threshold: connection.compression_threshold, + enc_cipher: connection.enc_cipher, + dec_cipher: connection.dec_cipher, _reading: PhantomData, _writing: PhantomData, } -- cgit v1.2.3 From 9ef1e8d653acd7e8e026cbde9c0320cedb8cf1d3 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 30 Jul 2022 17:56:36 -0500 Subject: remove unnecessary empty impls --- azalea-protocol/src/connect.rs | 4 ---- 1 file changed, 4 deletions(-) (limited to 'azalea-protocol/src/connect.rs') diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index 75c64517..cd51f86c 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -82,10 +82,6 @@ impl Connection { } } -impl Connection {} - -impl Connection {} - impl Connection { pub fn set_compression_threshold(&mut self, threshold: i32) { // if you pass a threshold of 0 or less, compression is disabled -- cgit v1.2.3