From e46577a214ae30159d14c128c45488e3772c8f84 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Mon, 19 Sep 2022 21:21:46 -0500 Subject: Fix connection writer being locked (#23) * Split connection struct in az-protocol * az-client uses split conns * fix errors * add a convenience write_packet fn to az-client --- azalea-client/src/client.rs | 39 +++++++++++++++++++++------------------ azalea-client/src/movement.rs | 2 +- 2 files changed, 22 insertions(+), 19 deletions(-) (limited to 'azalea-client/src') diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 14ebf9e9..9a6fd762 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -4,7 +4,7 @@ use azalea_block::BlockState; use azalea_chat::component::Component; use azalea_core::{ChunkPos, ResourceLocation, Vec3}; use azalea_protocol::{ - connect::{Connection, ConnectionError}, + connect::{Connection, ConnectionError, ReadConnection, WriteConnection}, packets::{ game::{ clientbound_player_chat_packet::ClientboundPlayerChatPacket, @@ -67,7 +67,8 @@ impl ChatPacket { #[derive(Clone)] pub struct Client { game_profile: GameProfile, - pub conn: Arc>>, + pub read_conn: Arc>>, + pub write_conn: Arc>>, pub player: Arc>, pub dimension: Arc>, pub physics_state: Arc>, @@ -185,14 +186,18 @@ impl Client { } }; - let conn = Arc::new(tokio::sync::Mutex::new(conn)); + let (read_conn, write_conn) = conn.into_split(); + + let read_conn = Arc::new(tokio::sync::Mutex::new(read_conn)); + let write_conn = Arc::new(tokio::sync::Mutex::new(write_conn)); let (tx, rx) = mpsc::unbounded_channel(); // we got the GameConnection, so the server is now connected :) let client = Client { game_profile, - conn, + read_conn, + write_conn, player: Arc::new(Mutex::new(Player::default())), dimension: Arc::new(Mutex::new(Dimension::default())), physics_state: Arc::new(Mutex::new(PhysicsState::default())), @@ -209,9 +214,14 @@ impl Client { Ok((client, rx)) } + /// Write a packet directly to the server. + pub async fn write_packet(&self, packet: ServerboundGamePacket) -> Result<(), std::io::Error> { + self.write_conn.lock().await.write(packet).await + } + async fn protocol_loop(client: Client, tx: UnboundedSender) { loop { - let r = client.conn.lock().await.read().await; + let r = client.read_conn.lock().await.read().await; match r { Ok(packet) => match Self::handle(&packet, &client, &tx).await { Ok(_) => {} @@ -323,10 +333,7 @@ impl Client { } client - .conn - .lock() - .await - .write( + .write_packet( ServerboundCustomPayloadPacket { identifier: ResourceLocation::new("brand").unwrap(), // they don't have to know :) @@ -444,12 +451,11 @@ impl Client { (new_pos, y_rot, x_rot) }; - let mut conn_lock = client.conn.lock().await; - conn_lock - .write(ServerboundAcceptTeleportationPacket { id: p.id }.get()) + client + .write_packet(ServerboundAcceptTeleportationPacket { id: p.id }.get()) .await?; - conn_lock - .write( + client + .write_packet( ServerboundMovePlayerPosRotPacket { x: new_pos.x, y: new_pos.y, @@ -567,10 +573,7 @@ impl Client { ClientboundGamePacket::KeepAlive(p) => { debug!("Got keep alive packet {:?}", p); client - .conn - .lock() - .await - .write(ServerboundKeepAlivePacket { id: p.id }.get()) + .write_packet(ServerboundKeepAlivePacket { id: p.id }.get()) .await?; } ClientboundGamePacket::RemoveEntities(p) => { diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs index ddc44c0a..0a4a05e8 100644 --- a/azalea-client/src/movement.rs +++ b/azalea-client/src/movement.rs @@ -121,7 +121,7 @@ impl Client { }; if let Some(packet) = packet { - self.conn.lock().await.write(packet).await?; + self.write_packet(packet).await?; } Ok(()) -- cgit v1.2.3