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