Compare commits

..

2 commits

Author SHA1 Message Date
21b4e7fb3b rm rwlock 2024-12-13 14:38:08 -05:00
cae5c6802c update deps 2024-12-13 14:37:52 -05:00
2 changed files with 24 additions and 47 deletions

8
Cargo.lock generated
View file

@ -386,9 +386,9 @@ checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.2.3" version = "1.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27f657647bcff5394bf56c7317665bbf790a137a50eaaa5c6bfbb9e27a518f2d" checksum = "9157bbaa6b165880c27a4293a474c91cdcf265cc68cc829bf10be0964a391caf"
dependencies = [ dependencies = [
"jobserver", "jobserver",
"libc", "libc",
@ -2441,9 +2441,9 @@ dependencies = [
[[package]] [[package]]
name = "quanta" name = "quanta"
version = "0.12.3" version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e5167a477619228a0b284fac2674e3c388cba90631d7b7de620e6f1fcd08da5" checksum = "773ce68d0bb9bc7ef20be3536ffe94e223e1f365bd374108b2659fac0c65cfe6"
dependencies = [ dependencies = [
"crossbeam-utils", "crossbeam-utils",
"libc", "libc",

View file

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