Skip to content

Commit

Permalink
refactor: move session addr and association id to session data
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtread committed Oct 13, 2024
1 parent b24e435 commit f841113
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/services/game/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 39 additions & 15 deletions src/session/data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{net::Ipv4Addr, sync::Arc};

use parking_lot::{Mutex, MutexGuard};
use serde::Serialize;
Expand All @@ -7,7 +7,7 @@ use crate::{
database::entities::Player,
services::{
game::{GameRef, WeakGameRef},
sessions::SessionPlayerAssociation,
sessions::{AssociationId, SessionPlayerAssociation},
},
utils::{
components::user_sessions,
Expand All @@ -29,33 +29,56 @@ use super::{
SessionNotifyHandle,
};

#[derive(Default)]
pub struct SessionData {
inner: Mutex<Option<SessionDataInner>>,
/// Extended session data for authenticated sessions
ext: Mutex<Option<SessionDataExt>>,

/// IP address associated with the session
addr: Ipv4Addr,

/// User will not have an association if they are using an outdated
/// client version.
association: Option<AssociationId>,
}

impl SessionData {
pub fn new(addr: Ipv4Addr, association: Option<AssociationId>) -> Self {
Self {
ext: Default::default(),
addr,
association,
}
}

pub fn get_addr(&self) -> Ipv4Addr {
self.addr
}

pub fn get_association(&self) -> Option<AssociationId> {
self.association
}

// Read from the underlying session data
fn read(&self) -> MutexGuard<'_, Option<SessionDataInner>> {
self.inner.lock()
fn read(&self) -> MutexGuard<'_, Option<SessionDataExt>> {
self.ext.lock()
}

/// Writes to the underlying session data without publishing the changes
fn write_silent<F, O>(&self, update: F) -> Option<O>
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
/// subscribers
#[inline]
fn write_publish<F, O>(&self, update: F) -> Option<O>
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
Expand All @@ -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<Player> {
self.inner
self.ext
.lock()
.insert(SessionDataInner::new(player))
.insert(SessionDataExt::new(player))
// Obtain the player to return
.player_assoc
.player
Expand Down Expand Up @@ -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<SessionPlayerAssociation>,
/// Networking information for current session
Expand All @@ -183,7 +207,7 @@ pub struct SessionDataInner {
subscribers: Vec<SessionSubscription>,
}

impl SessionDataInner {
impl SessionDataExt {
fn new(player: SessionPlayerAssociation) -> Self {
Self {
player_assoc: Arc::new(player),
Expand Down
11 changes: 1 addition & 10 deletions src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssociationId>,

/// 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<tokio::sync::Mutex<()>>,
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions src/session/routes/user_sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ pub async fn handle_update_network(
ping_site_latency,
}): Blaze<UpdateNetworkRequest>,
) {
let session_addr = session.data.get_addr();

match &config.qos {
QosServerConfig::Disabled => {}
// Hamachi should override local addresses
Expand All @@ -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;
}
}
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit f841113

Please sign in to comment.