diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-09-19 21:21:46 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-19 21:21:46 -0500 |
| commit | e46577a214ae30159d14c128c45488e3772c8f84 (patch) | |
| tree | ca4932b8f3f81f6eaa6d37c5f4d7ea390968d54d /azalea-client/src | |
| parent | 528c1a07e3ac089ff70036a5e403eb2b8d3ae21f (diff) | |
| download | azalea-drasl-e46577a214ae30159d14c128c45488e3772c8f84.tar.xz | |
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
Diffstat (limited to 'azalea-client/src')
| -rw-r--r-- | azalea-client/src/client.rs | 39 | ||||
| -rw-r--r-- | azalea-client/src/movement.rs | 2 |
2 files changed, 22 insertions, 19 deletions
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<tokio::sync::Mutex<Connection<ClientboundGamePacket, ServerboundGamePacket>>>, + pub read_conn: Arc<tokio::sync::Mutex<ReadConnection<ClientboundGamePacket>>>, + pub write_conn: Arc<tokio::sync::Mutex<WriteConnection<ServerboundGamePacket>>>, pub player: Arc<Mutex<Player>>, pub dimension: Arc<Mutex<Dimension>>, pub physics_state: Arc<Mutex<PhysicsState>>, @@ -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<Event>) { 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(()) |
