diff --git a/src/services/game/matchmaking.rs b/src/services/game/matchmaking.rs index 7bbabb2..fbac46b 100644 --- a/src/services/game/matchmaking.rs +++ b/src/services/game/matchmaking.rs @@ -62,7 +62,11 @@ impl Matchmaking { ) { let queue = &mut *self.queue.lock(); - while let Some(entry) = queue.front() { + // Index of the queue entry currently being checked. Entries that don't + // match this game are skipped instead of being re-checked forever + let mut index = 0; + + while let Some(entry) = queue.get(index) { let join_state = { link.read().joinable_state(Some(&entry.rule_set)) }; // TODO: If player has been in queue long enough create @@ -70,8 +74,9 @@ impl Matchmaking { match join_state { GameJoinableState::Joinable => { + // Index is not advanced, the next entry moves into this position let entry = queue - .pop_front() + .remove(index) .expect("Expecting matchmaking entry but nothing was present"); debug!("Found player from queue adding them to the game (GID: {game_id})"); @@ -92,6 +97,9 @@ impl Matchmaking { GameJoinableState::NotMatch => { // TODO: Check started time and timeout // player if they've been waiting too long + + // Skip this entry and check the next one + index += 1; } } } diff --git a/src/services/tunnel/mappings.rs b/src/services/tunnel/mappings.rs index 2ee7e3d..a5ed326 100644 --- a/src/services/tunnel/mappings.rs +++ b/src/services/tunnel/mappings.rs @@ -75,6 +75,11 @@ pub struct TunnelMappings { /// Mapping from [AssociationId] (Client association) to [TunnelHandle] association_to_tunnel: HashMap, + /// Mapping from [AssociationId] to the game pool slot the client is in, + /// kept even if the client doesn't have a tunnel yet so a tunnel created + /// (or re-created) later on is still connected to the right slot + association_to_pool: HashMap, + pool: TunnelPoolMap, } @@ -138,7 +143,7 @@ impl TunnelMappings { // Retain all now expired tunnels self.id_to_tunnel.retain(|tunnel_id, data| { // Initial dead check based on keep alive timeout - let mut dead = data.last_alive.duration_since(now) > keep_alive_timeout; + let mut dead = now.duration_since(data.last_alive) > keep_alive_timeout; // Check for closed http tunnels if let TunnelHandle::Http(handle) = &data.handle @@ -171,7 +176,18 @@ impl TunnelMappings { // Insert the tunnel into the mappings self.id_to_tunnel.insert(tunnel_id, tunnel); - self.association_to_tunnel.insert(association, tunnel_id); + + // Replace any previous tunnel for this association + if let Some(previous) = self.association_to_tunnel.insert(association, tunnel_id) { + self.id_to_tunnel.remove(&previous); + self.pool.remove_by_tunnel(&previous); + } + + // Client is already in a game, connect the new tunnel to its slot + if let Some(pool_key) = self.association_to_pool.get(&association).copied() { + self.pool.remove_by_pool_key(&pool_key); + self.pool.insert(tunnel_id, pool_key); + } Some(tunnel_id) } @@ -196,16 +212,28 @@ impl TunnelMappings { /// Attempts to associate the tunnel from `address` to the provided /// `pool_id` and `pool_index` if there is a tunnel connected to /// `address` - pub fn associate_pool(&mut self, tunnel_id: TunnelId, pool_id: PoolId, pool_index: PoolIndex) { + pub fn associate_pool( + &mut self, + association: AssociationId, + pool_id: PoolId, + pool_index: PoolIndex, + ) { let key = PoolKey::new(pool_id, pool_index); - self.pool.insert(tunnel_id, key); + + // Remember the slot even if the tunnel doesn't exist yet + self.association_to_pool.insert(association, key); + + if let Some(tunnel_id) = self.get_association_tunnel(&association) { + self.pool.insert(tunnel_id, key); + } } /// Removes the association between a [PoolKey] and a [TunnelId] if /// one is present pub fn dissociate_pool(&mut self, pool_id: PoolId, pool_index: PoolIndex) { - self.pool - .remove_by_pool_key(&PoolKey::new(pool_id, pool_index)); + let key = PoolKey::new(pool_id, pool_index); + self.pool.remove_by_pool_key(&key); + self.association_to_pool.retain(|_, value| *value != key); } /// Get the pool key of a tunnel diff --git a/src/services/tunnel/mod.rs b/src/services/tunnel/mod.rs index c374312..79def3a 100644 --- a/src/services/tunnel/mod.rs +++ b/src/services/tunnel/mod.rs @@ -62,15 +62,9 @@ impl TunnelService { pool_id: PoolId, pool_index: PoolIndex, ) { - let mappings = &mut *self.mappings.write(); - let tunnel_id = { - match mappings.get_association_tunnel(&association) { - Some(value) => value, - None => return, - } - }; - - mappings.associate_pool(tunnel_id, pool_id, pool_index); + self.mappings + .write() + .associate_pool(association, pool_id, pool_index); } pub fn dissociate_pool(&self, pool_id: PoolId, pool_index: PoolIndex) { diff --git a/src/session/data.rs b/src/session/data.rs index 79c0e4d..90a9332 100644 --- a/src/session/data.rs +++ b/src/session/data.rs @@ -241,15 +241,20 @@ impl SessionData { } /// Sets the game the session is currently apart of - pub fn set_game(&self, game_id: GameID, game_ref: WeakGameRef) { + pub fn set_game(&self, game_id: GameID, game_ref: WeakGameRef) { // Set the current game - self.write_publish(|data| { - data.game = Some(SessionGameData { + let previous = self.write_publish(|data| { + data.game.replace(SessionGameData { player_id: data.player_assoc.player.id, game_id, game_ref, - }); + }) }); + + // Previous game MUST be dropped *after* the write lock is released + // as its drop removes the player from the game which requires + // accessing this session data again + drop(previous); } /// Clears the game the session is apart of @@ -258,7 +263,12 @@ impl SessionData { /// USER GAME, call [Self::clear_game_gm] instead which /// doesn't let the drop run preventing a deadlock pub fn clear_game(&self) { - self.write_publish(|data| data.game = None); + let previous = self.write_publish(|data| data.game.take()); + + // Game MUST be dropped *after* the write lock is released + // as its drop removes the player from the game which requires + // accessing this session data again + drop(previous); } /// Clears the game the session is apart of, safe to