diff --git a/src/services/game/manager.rs b/src/services/game/manager.rs index 7aecb55..4027fbd 100644 --- a/src/services/game/manager.rs +++ b/src/services/game/manager.rs @@ -170,7 +170,7 @@ impl GameManager { }; // Allocate tunnel if supported by client - if let Some(association) = session.association { + if let Some(association) = session.data.get_association() { self.tunnel_service .associate_pool(association, game_id, index as u8); self.udp_tunnel_service diff --git a/src/session/data.rs b/src/session/data.rs index 88a64b2..0073475 100644 --- a/src/session/data.rs +++ b/src/session/data.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{net::Ipv4Addr, sync::Arc}; use parking_lot::{Mutex, MutexGuard}; use serde::Serialize; @@ -7,7 +7,7 @@ use crate::{ database::entities::Player, services::{ game::{GameRef, WeakGameRef}, - sessions::SessionPlayerAssociation, + sessions::{AssociationId, SessionPlayerAssociation}, }, utils::{ components::user_sessions, @@ -29,23 +29,46 @@ use super::{ SessionNotifyHandle, }; -#[derive(Default)] pub struct SessionData { - inner: Mutex>, + /// Extended session data for authenticated sessions + ext: Mutex>, + + /// IP address associated with the session + addr: Ipv4Addr, + + /// User will not have an association if they are using an outdated + /// client version. + association: Option, } impl SessionData { + pub fn new(addr: Ipv4Addr, association: Option) -> Self { + Self { + ext: Default::default(), + addr, + association, + } + } + + pub fn get_addr(&self) -> Ipv4Addr { + self.addr + } + + pub fn get_association(&self) -> Option { + self.association + } + // Read from the underlying session data - fn read(&self) -> MutexGuard<'_, Option> { - self.inner.lock() + fn read(&self) -> MutexGuard<'_, Option> { + self.ext.lock() } /// Writes to the underlying session data without publishing the changes fn write_silent(&self, update: F) -> Option where - F: FnOnce(&mut SessionDataInner) -> O, + F: FnOnce(&mut SessionDataExt) -> O, { - self.inner.lock().as_mut().map(update) + self.ext.lock().as_mut().map(update) } /// Writes to the underlying session data, publishes changes to @@ -53,9 +76,9 @@ impl SessionData { #[inline] fn write_publish(&self, update: F) -> Option where - F: FnOnce(&mut SessionDataInner) -> O, + F: FnOnce(&mut SessionDataExt) -> O, { - self.inner.lock().as_mut().map(|data| { + self.ext.lock().as_mut().map(|data| { let value = update(data); data.publish_update(); value @@ -64,14 +87,14 @@ impl SessionData { /// Clears the underlying session data pub fn clear(&self) { - self.inner.lock().take(); + self.ext.lock().take(); } /// Starts a session from the provided player association pub fn start_session(&self, player: SessionPlayerAssociation) -> Arc { - self.inner + self.ext .lock() - .insert(SessionDataInner::new(player)) + .insert(SessionDataExt::new(player)) // Obtain the player to return .player_assoc .player @@ -172,7 +195,8 @@ impl SessionData { } } -pub struct SessionDataInner { +/// Extended session data, present when the user is authenticated +struct SessionDataExt { /// Session -> Player association, currently authenticated player player_assoc: Arc, /// Networking information for current session @@ -183,7 +207,7 @@ pub struct SessionDataInner { subscribers: Vec, } -impl SessionDataInner { +impl SessionDataExt { fn new(player: SessionPlayerAssociation) -> Self { Self { player_assoc: Arc::new(player), diff --git a/src/session/mod.rs b/src/session/mod.rs index aa007ea..9f1bb70 100644 --- a/src/session/mod.rs +++ b/src/session/mod.rs @@ -43,13 +43,6 @@ pub struct Session { /// Unique ID for this session id: u32, - /// IP address associated with the session - pub addr: Ipv4Addr, - - /// User will not have an association if they are using an outdated - /// client version. - pub association: Option, - /// Lock for handling packets with a session, ensures only one packet is /// processed at a time and in the same order that it was received / sent busy_lock: Arc>, @@ -98,11 +91,9 @@ impl Session { let session = Arc::new(Self { id, - association, busy_lock: Default::default(), tx, - data: Default::default(), - addr, + data: SessionData::new(addr, association), }); SessionFuture { diff --git a/src/session/routes/user_sessions.rs b/src/session/routes/user_sessions.rs index 54ddf74..17b5e23 100644 --- a/src/session/routes/user_sessions.rs +++ b/src/session/routes/user_sessions.rs @@ -132,6 +132,8 @@ pub async fn handle_update_network( ping_site_latency, }): Blaze, ) { + let session_addr = session.data.get_addr(); + match &config.qos { QosServerConfig::Disabled => {} // Hamachi should override local addresses @@ -140,10 +142,10 @@ pub async fn handle_update_network( if let NetworkAddress::AddressPair(pair) = &mut address { let int = &mut pair.internal; - if session.addr.is_loopback() { + if session_addr.is_loopback() { int.addr = *host; } else { - int.addr = session.addr; + int.addr = session_addr; } } } @@ -156,7 +158,7 @@ pub async fn handle_update_network( // If address is missing if ext.addr.is_unspecified() { // Replace address with new address and port with same as local port - ext.addr = session.addr; + ext.addr = session_addr; ext.port = pair.internal.port; } }