Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/services/game/matchmaking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,21 @@ 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
// a game matching their specifics

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})");
Expand All @@ -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;
}
}
}
Expand Down
41 changes: 35 additions & 6 deletions src/services/tunnel/mappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ pub struct TunnelMappings {
/// Mapping from [AssociationId] (Client association) to [TunnelHandle]
association_to_tunnel: HashMap<AssociationId, TunnelId>,

/// 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<AssociationId, PoolKey>,

pool: TunnelPoolMap,
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
self.pool.remove_by_pool_key(pool_key);
self.pool.insert(tunnel_id, *pool_key);
}

Some(tunnel_id)
}
Expand All @@ -196,16 +212,29 @@ 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);

// Associate the tunnel to the pool if theres an associated tunnel
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
Expand Down
12 changes: 3 additions & 9 deletions src/services/tunnel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 15 additions & 5 deletions src/session/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,18 @@ impl SessionData {
/// Sets the game the session is currently apart of
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
Expand All @@ -258,10 +263,15 @@ 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
/// Clears the game the session is apart of, safe to
/// call from the game manager as it prevents the default
/// drop behavior of trying to remove the player from the game
pub fn clear_game_gm(&self) {
Expand Down
Loading