rm rwlock
This commit is contained in:
parent
cae5c6802c
commit
21b4e7fb3b
1 changed files with 20 additions and 43 deletions
|
@ -52,14 +52,14 @@ pub struct UserHandler {
|
|||
name_generator: NameGenerator,
|
||||
reserved_names: HashSet<String>,
|
||||
offline_users: HashMap<String, Arc<RwLock<User>>>,
|
||||
online_users: RwLock<HashMap<SocketAddr, Arc<RwLock<User>>>>,
|
||||
online_users: HashMap<SocketAddr, Arc<RwLock<User>>>,
|
||||
}
|
||||
|
||||
impl UserHandler {
|
||||
/// Returns new UserHandler
|
||||
pub fn new(state: Arc<AppState>) -> Self {
|
||||
let offline_users = HashMap::<String, Arc<RwLock<User>>>::new();
|
||||
let online_users = RwLock::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new());
|
||||
let online_users = HashMap::<SocketAddr, Arc<RwLock<User>>>::new();
|
||||
UserHandler {
|
||||
offline_users,
|
||||
online_users,
|
||||
|
@ -88,13 +88,7 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
async fn join_game(&self, game_id: String, addr: SocketAddr) {
|
||||
let user = self
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.clone();
|
||||
let user = self.online_users.get(&addr).unwrap().clone();
|
||||
|
||||
if let Err(e) = self
|
||||
.state
|
||||
|
@ -107,13 +101,7 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
async fn move_request(&self, move_request: PlayerMoveRequest, addr: SocketAddr) {
|
||||
let player_user = self
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.clone();
|
||||
let player_user = self.online_users.get(&addr).unwrap().clone();
|
||||
if let Err(e) = self
|
||||
.state
|
||||
.tx_game_handler
|
||||
|
@ -125,13 +113,7 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
async fn get_user(&self, new_game: NewGameRequest, addr: SocketAddr) {
|
||||
let host = self
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.clone();
|
||||
let host = self.online_users.get(&addr).unwrap().clone();
|
||||
if let Err(e) = self
|
||||
.state
|
||||
.tx_game_handler
|
||||
|
@ -145,8 +127,6 @@ impl UserHandler {
|
|||
async fn get_player_user_id(&self, judge_decision: JudgeDecisionRequest, addr: SocketAddr) {
|
||||
let player_user_id = self
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.read()
|
||||
|
@ -168,7 +148,8 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
async fn pass_chat_message(&self, msg: String, addr: SocketAddr) {
|
||||
let text = format! {"{0}: {1}", self.online_users.read().unwrap().get(&addr).unwrap().read().unwrap().name, msg};
|
||||
let text =
|
||||
format! {"{0}: {1}", self.online_users.get(&addr).unwrap().read().unwrap().name, msg};
|
||||
if let Err(e) = self
|
||||
.state
|
||||
.tx_outgoing_message_handler
|
||||
|
@ -198,7 +179,7 @@ impl UserHandler {
|
|||
}
|
||||
}
|
||||
Addr(addr) => {
|
||||
if let Some(user) = self.online_users.read().unwrap().get(&addr) {
|
||||
if let Some(user) = self.online_users.get(&addr) {
|
||||
tx = user.read().unwrap().tx.clone();
|
||||
} else {
|
||||
tracing::error!("Attempted to send message to offline user!");
|
||||
|
@ -336,7 +317,7 @@ impl UserHandler {
|
|||
|
||||
let old_name;
|
||||
|
||||
if let Some(user) = self.online_users.read().unwrap().get(&addr) {
|
||||
if let Some(user) = self.online_users.get(&addr) {
|
||||
old_name = user.read().unwrap().name.clone();
|
||||
|
||||
// User abandons current name by requesting a new one
|
||||
|
@ -350,7 +331,7 @@ impl UserHandler {
|
|||
if self.offline_users.contains_key(&new_name) {
|
||||
let buf;
|
||||
// Copy over new tx
|
||||
if let Some(online_user) = self.online_users.write().unwrap().remove(&addr) {
|
||||
if let Some(online_user) = self.online_users.remove(&addr) {
|
||||
if let Some(offline_user) = self.offline_users.remove(&new_name) {
|
||||
offline_user.write().unwrap().tx = online_user.write().unwrap().tx.clone();
|
||||
buf = offline_user;
|
||||
|
@ -364,7 +345,7 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
// Move offline user object to online
|
||||
self.online_users.write().unwrap().insert(addr, buf);
|
||||
self.online_users.insert(addr, buf);
|
||||
|
||||
// Send welcome back messages
|
||||
let msg = format! {
|
||||
|
@ -406,7 +387,7 @@ impl UserHandler {
|
|||
self.reserved_names.insert(new_name.clone());
|
||||
|
||||
// Change user's name
|
||||
if let Some(user) = self.online_users.write().unwrap().get_mut(&addr) {
|
||||
if let Some(user) = self.online_users.get_mut(&addr) {
|
||||
user.write().unwrap().change_name(new_name.clone());
|
||||
} else {
|
||||
tracing::error!("Error updating username: Can't find user!");
|
||||
|
@ -441,7 +422,7 @@ impl UserHandler {
|
|||
)
|
||||
.await;
|
||||
// Update games this user is in
|
||||
if let Some(user) = &self.online_users.read().unwrap().get(&addr) {
|
||||
if let Some(user) = &self.online_users.get(&addr) {
|
||||
let user_id = user.read().unwrap().uuid.to_string();
|
||||
let msg = GameHandlerMessage::SendGameUserUpdate(user_id);
|
||||
let tx = self.state.tx_game_handler.clone();
|
||||
|
@ -458,7 +439,7 @@ impl UserHandler {
|
|||
/// Broadcast updated user count
|
||||
fn broadcast_user_count(&self) {
|
||||
let tx = self.state.tx_outgoing_message_handler.clone();
|
||||
let online_users: u32 = self.online_users.read().unwrap().len().try_into().unwrap();
|
||||
let online_users: u32 = self.online_users.len().try_into().unwrap();
|
||||
let msg = ServerToClientMessage::ServerOnlineUsers(ServerOnlineUsers { online_users });
|
||||
tokio::spawn(async move { tx.send(Broadcast(msg)).await });
|
||||
}
|
||||
|
@ -467,8 +448,6 @@ impl UserHandler {
|
|||
async fn user_cleanup(&mut self, addr: SocketAddr) {
|
||||
let user_name = self
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.read()
|
||||
|
@ -499,16 +478,14 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
/// Set user status to online
|
||||
fn set_user_online(&self, addr: SocketAddr, user: Arc<RwLock<User>>) {
|
||||
self.online_users.write().unwrap().insert(addr, user);
|
||||
fn set_user_online(&mut self, addr: SocketAddr, user: Arc<RwLock<User>>) {
|
||||
self.online_users.insert(addr, user);
|
||||
}
|
||||
|
||||
/// Set user status to offline
|
||||
fn set_user_offline(&mut self, user_name: String, addr: &SocketAddr) {
|
||||
self.offline_users.insert(
|
||||
user_name,
|
||||
self.online_users.write().unwrap().remove(addr).unwrap(),
|
||||
);
|
||||
self.offline_users
|
||||
.insert(user_name, self.online_users.remove(addr).unwrap());
|
||||
}
|
||||
|
||||
/// Update chat stuff like users list
|
||||
|
@ -527,7 +504,7 @@ impl UserHandler {
|
|||
/// Generate chatroom join announcement
|
||||
fn meta_announce_user_join(&self, addr: &SocketAddr) -> ChatMessage {
|
||||
let msg = format!("{} joined.", {
|
||||
if let Some(user) = self.online_users.read().unwrap().get(addr) {
|
||||
if let Some(user) = self.online_users.get(addr) {
|
||||
user.read().unwrap().name.clone()
|
||||
} else {
|
||||
"Error!".to_string()
|
||||
|
@ -541,7 +518,7 @@ impl UserHandler {
|
|||
// TODO: this may get expensive if there are many users
|
||||
let mut names = vec![];
|
||||
|
||||
for user in self.online_users.read().unwrap().iter() {
|
||||
for user in self.online_users.iter() {
|
||||
names.push(user.1.read().unwrap().name.clone());
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue