Skip to content

Commit 0b0b776

Browse files
committed
refactor: move player networking obtain out of game player data
1 parent f45aa96 commit 0b0b776

5 files changed

Lines changed: 33 additions & 32 deletions

File tree

src/services/game/mod.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
RemoveReason, SettingChange, SlotType, StateChange, UNSPECIFIED_TEAM_INDEX,
1414
},
1515
util::LOCALE_NZ,
16+
NetworkAddress,
1617
},
1718
packet::Packet,
1819
router::RawBlaze,
@@ -129,8 +130,6 @@ pub struct GamePlayer {
129130
pub player: Arc<Player>,
130131
/// Weak reference to the associated session
131132
pub link: WeakSessionLink,
132-
/// Networking information for the player
133-
pub net: Arc<NetData>,
134133
/// The mesh state of the player
135134
pub state: PlayerState,
136135
}
@@ -154,15 +153,26 @@ impl GamePlayer {
154153
/// `player` The session player
155154
/// `net` The player networking details
156155
/// `addr` The session address
157-
pub fn new(player: Arc<Player>, net: Arc<NetData>, link: WeakSessionLink) -> Self {
156+
pub fn new(player: Arc<Player>, link: WeakSessionLink) -> Self {
158157
Self {
159158
player,
160159
link,
161-
net,
162160
state: PlayerState::ActiveConnecting,
163161
}
164162
}
165163

164+
pub fn net(&self) -> Option<Arc<NetData>> {
165+
let session = self.link.upgrade()?;
166+
session.data.net()
167+
}
168+
169+
pub fn network_address(&self) -> NetworkAddress {
170+
match self.net() {
171+
Some(net) => net.addr.clone(),
172+
None => NetworkAddress::Unset,
173+
}
174+
}
175+
166176
pub fn try_clear_game(&self) {
167177
if let Some(link) = self.link.upgrade() {
168178
link.data.clear_game_gm();
@@ -198,11 +208,7 @@ impl GamePlayer {
198208
GamePlayerSnapshot {
199209
player_id: self.player.id,
200210
display_name: Box::from(self.player.display_name.as_ref()),
201-
net: if include_net {
202-
Some(self.net.clone())
203-
} else {
204-
None
205-
},
211+
net: if include_net { self.net() } else { None },
206212
}
207213
}
208214

@@ -221,7 +227,7 @@ impl GamePlayer {
221227
// Player ID
222228
w.tag_u32(b"PID", self.player.id);
223229
// Player network data
224-
w.tag_ref(b"PNET", &self.net.addr);
230+
w.tag_ref(b"PNET", &self.network_address());
225231
// Slot ID
226232
w.tag_owned(b"SID", slot);
227233
// Slot type

src/session/data.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,6 @@ impl SessionData {
218218
.map(|value| value.player_assoc.player.clone())
219219
}
220220

221-
/// Obtains the parts required to create a game player
222-
pub fn get_game_player_data(&self) -> Option<(Arc<Player>, Arc<NetData>)> {
223-
self.read()
224-
.auth
225-
.as_ref()
226-
.map(|value| (value.player_assoc.player.clone(), value.net.clone()))
227-
}
228-
229221
/// Updates the session hardware flags
230222
pub fn set_hardware_flags(&self, value: HardwareFlags) {
231223
self.write_publish(|data| data.net = Arc::new(data.net.with_hardware_flags(value)));
@@ -318,6 +310,12 @@ impl SessionData {
318310
pub fn remove_subscriber(&self, player_id: PlayerID) {
319311
self.write_silent(|data| data.remove_subscriber(player_id));
320312
}
313+
314+
pub fn net(&self) -> Option<Arc<NetData>> {
315+
let ext = self.read();
316+
let auth = ext.auth.as_ref()?;
317+
Some(auth.net.clone())
318+
}
321319
}
322320

323321
/// Extended session data, present when the user is authenticated

src/session/models/game_manager.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,11 @@ impl TdfSerialize for GameSetupResponse<'_> {
804804
// Game Type used for game reporting as passed up in the request.
805805
w.tag_str_empty(b"GTYP");
806806

807+
let host_net = host.net().unwrap_or_default();
808+
807809
// Whether to tunnel the connection
808810
let tunnel = match &self.config.tunnel {
809-
TunnelConfig::Stricter => !matches!(host.net.qos.natt, NatType::Open),
811+
TunnelConfig::Stricter => !matches!(host_net.qos.natt, NatType::Open),
810812
TunnelConfig::Always => true,
811813
TunnelConfig::Disabled => false,
812814
};
@@ -828,7 +830,7 @@ impl TdfSerialize for GameSetupResponse<'_> {
828830
);
829831
} else {
830832
// Open NATs can directly have players connect normally
831-
if let NetworkAddress::AddressPair(pair) = &host.net.addr {
833+
if let NetworkAddress::AddressPair(pair) = &host_net.addr {
832834
w.write_byte(2 /* Address pair type */);
833835
TdfSerialize::serialize(pair, w)
834836
} else {
@@ -847,7 +849,7 @@ impl TdfSerialize for GameSetupResponse<'_> {
847849
w.tag_usize(b"MCAP", Game::MAX_PLAYERS);
848850

849851
// Host network qos data
850-
w.tag_ref(b"NQOS", &host.net.qos);
852+
w.tag_ref(b"NQOS", &host_net.qos);
851853

852854
// Flag to indicate that this game is not resettable. This applies only to the CLIENT_SERVER_DEDICATED topology.
853855
// The game will be prevented from ever going into the RESETTABlE state.
@@ -952,9 +954,9 @@ impl TdfSerialize for GetGameDetails<'_> {
952954
// Topology host network list (The heat bug is present so this encoded as a group even though its a union)
953955
w.tag_list_start(b"HNET", TdfType::Group, 1);
954956

955-
if let NetworkAddress::AddressPair(pair) = &host.net.addr {
957+
if let NetworkAddress::AddressPair(pair) = host.network_address() {
956958
w.write_byte(2 /* Address pair type */);
957-
TdfSerialize::serialize(pair, w)
959+
TdfSerialize::serialize(&pair, w)
958960
} else {
959961
// Uh oh.. host networking is missing...?
960962
w.write_byte(TAGGED_UNSET_KEY);

src/session/router.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,13 @@ impl FromPacketRequest for GamePlayer {
226226
Self: 'a,
227227
{
228228
Box::pin(async move {
229-
let (player, net_data) = req
229+
let player = req
230230
.state
231231
.data
232-
.get_game_player_data()
232+
.get_player()
233233
.ok_or(GlobalError::AuthenticationRequired)?;
234234

235-
Ok(GamePlayer::new(
236-
player,
237-
net_data,
238-
Arc::downgrade(&req.state),
239-
))
235+
Ok(GamePlayer::new(player, Arc::downgrade(&req.state)))
240236
})
241237
}
242238
}

src/session/routes/game_manager.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,7 @@ pub async fn handle_cancel_matchmaking(
536536

537537
/// Handles preparing a game for being replayed
538538
///
539-
/// Occurs when a game is finished successfully, is not sent if a game
540-
/// is failed
539+
/// Occurs when a game finishes
541540
///
542541
/// ```
543542
/// Route: GameManager(ReplayGame)

0 commit comments

Comments
 (0)