From 48f0d381b0b9a5e8a81e4a62a4912865a3081af6 Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Mon, 5 Apr 2021 11:04:19 -0300 Subject: Replace State tokio::sync::Mutex by std::sync::RwLock --- mumd/src/network/tcp.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'mumd/src/network/tcp.rs') diff --git a/mumd/src/network/tcp.rs b/mumd/src/network/tcp.rs index 6402a89..93b5103 100644 --- a/mumd/src/network/tcp.rs +++ b/mumd/src/network/tcp.rs @@ -10,7 +10,7 @@ use mumble_protocol::control::{msgs, ClientControlCodec, ControlCodec, ControlPa use mumble_protocol::crypt::ClientCryptState; use mumble_protocol::voice::VoicePacket; use mumble_protocol::{Clientbound, Serverbound}; -use std::collections::HashMap; +use std::{collections::HashMap, sync::RwLock}; use std::convert::{Into, TryInto}; use std::net::SocketAddr; use std::sync::Arc; @@ -79,7 +79,7 @@ impl TcpEventQueue { } pub async fn handle( - state: Arc>, + state: Arc>, mut connection_info_receiver: watch::Receiver>, crypt_state_sender: mpsc::Sender, packet_sender: mpsc::UnboundedSender>, @@ -103,7 +103,7 @@ pub async fn handle( .await?; // Handshake (omitting `Version` message for brevity) - let state_lock = state.lock().await; + let state_lock = state.read().unwrap(); let username = state_lock.username().unwrap().to_string(); let password = state_lock.password().map(|x| x.to_string()); authenticate(&mut sink, username, password).await?; @@ -241,7 +241,7 @@ async fn send_voice( } async fn listen( - state: Arc>, + state: Arc>, mut stream: TcpReceiver, crypt_state_sender: mpsc::Sender, event_queue: TcpEventQueue, @@ -260,7 +260,7 @@ async fn listen( // We end up here if the login was rejected. We probably want // to exit before that. warn!("TCP stream gone"); - state.lock().await.broadcast_phase(StatePhase::Disconnected); + state.read().unwrap().broadcast_phase(StatePhase::Disconnected); break; } }; @@ -299,7 +299,7 @@ async fn listen( .await; } event_queue.resolve(TcpEventData::Connected(Ok(&msg))).await; - let mut state = state.lock().await; + let mut state = state.write().unwrap(); let server = state.server_mut().unwrap(); server.parse_server_sync(*msg); match &server.welcome_text { @@ -323,24 +323,24 @@ async fn listen( } } ControlPacket::UserState(msg) => { - state.lock().await.parse_user_state(*msg); + state.write().unwrap().parse_user_state(*msg); } ControlPacket::UserRemove(msg) => { - state.lock().await.remove_client(*msg); + state.write().unwrap().remove_client(*msg); } ControlPacket::ChannelState(msg) => { debug!("Channel state received"); state - .lock() - .await + .write() + .unwrap() .server_mut() .unwrap() .parse_channel_state(*msg); //TODO parse initial if initial } ControlPacket::ChannelRemove(msg) => { state - .lock() - .await + .write() + .unwrap() .server_mut() .unwrap() .parse_channel_remove(*msg); @@ -356,8 +356,8 @@ async fn listen( .. } => { state - .lock() - .await + .read() + .unwrap() .audio() .decode_packet_payload( VoiceStreamType::TCP, -- cgit v1.2.1 From e01383af1c417666d42a802e44a1d1e98bbcf14e Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Tue, 6 Apr 2021 15:18:55 -0300 Subject: Fix the import location and a possible deadlock --- mumd/src/network/tcp.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'mumd/src/network/tcp.rs') diff --git a/mumd/src/network/tcp.rs b/mumd/src/network/tcp.rs index 93b5103..02477dc 100644 --- a/mumd/src/network/tcp.rs +++ b/mumd/src/network/tcp.rs @@ -10,10 +10,10 @@ use mumble_protocol::control::{msgs, ClientControlCodec, ControlCodec, ControlPa use mumble_protocol::crypt::ClientCryptState; use mumble_protocol::voice::VoicePacket; use mumble_protocol::{Clientbound, Serverbound}; -use std::{collections::HashMap, sync::RwLock}; +use std::collections::HashMap; use std::convert::{Into, TryInto}; use std::net::SocketAddr; -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use tokio::net::TcpStream; use tokio::sync::{mpsc, watch, Mutex}; use tokio::time::{self, Duration}; @@ -103,13 +103,17 @@ pub async fn handle( .await?; // Handshake (omitting `Version` message for brevity) - let state_lock = state.read().unwrap(); - let username = state_lock.username().unwrap().to_string(); - let password = state_lock.password().map(|x| x.to_string()); + let (username, password) = { + let state_lock = state.read().unwrap(); + (state_lock.username().unwrap().to_string(), + state_lock.password().map(|x| x.to_string())) + }; authenticate(&mut sink, username, password).await?; - let phase_watcher = state_lock.phase_receiver(); - let input_receiver = state_lock.audio().input_receiver(); - drop(state_lock); + let (phase_watcher, input_receiver) = { + let state_lock = state.read().unwrap(); + (state_lock.phase_receiver(), + state_lock.audio().input_receiver()) + }; let event_queue = TcpEventQueue::new(); info!("Logging in..."); -- cgit v1.2.1