Skip to content

Commit

Permalink
feat(network-debug): Add info on ggrs synchronization + disconnects t…
Browse files Browse the repository at this point in the history
…o network debug window (#413)
  • Loading branch information
MaxCWhitehead committed Jun 9, 2024
1 parent f895957 commit 1c76233
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
21 changes: 21 additions & 0 deletions framework_crates/bones_framework/src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{fmt::Debug, marker::PhantomData, sync::Arc};

use bones_matchmaker_proto::{MATCH_ALPN, PLAY_ALPN};
use debug::PlayerSyncState;
use ggrs::{NetworkStats, P2PSession, PlayerHandle};
use instant::Duration;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -414,13 +415,33 @@ where
match event {
ggrs::GgrsEvent::Synchronizing { addr, total, count } => {
info!(player=%addr, %total, progress=%count, "Syncing network player");
NETWORK_DEBUG_CHANNEL
.sender
.try_send(NetworkDebugMessage::PlayerSync((
PlayerSyncState::SyncInProgress,
addr,
)))
.unwrap();
}
ggrs::GgrsEvent::Synchronized { addr } => {
info!(player=%addr, "Syncrhonized network client");
NETWORK_DEBUG_CHANNEL
.sender
.try_send(NetworkDebugMessage::PlayerSync((
PlayerSyncState::Sychronized,
addr,
)))
.unwrap();
}
ggrs::GgrsEvent::Disconnected { addr } => {
warn!(player=%addr, "Player Disconnected");
self.disconnected_players.push(addr);
NETWORK_DEBUG_CHANNEL
.sender
.try_send(NetworkDebugMessage::DisconnectedPlayers(
self.disconnected_players.clone(),
))
.unwrap();
} //return Err(SessionError::Disconnected)},
ggrs::GgrsEvent::NetworkInterrupted { addr, .. } => {
info!(player=%addr, "Network player interrupted");
Expand Down
56 changes: 53 additions & 3 deletions framework_crates/bones_framework/src/networking/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use async_channel::{Receiver, Sender};
use bones_asset::HasSchema;
use egui::CollapsingHeader;
use egui_plot::{Bar, BarChart, GridMark, Plot};
use ggrs::{NetworkStats, PlayerHandle};
use once_cell::sync::Lazy;
Expand All @@ -26,6 +27,11 @@ pub fn network_debug_session_plugin(session: &mut Session) {
session.add_system_to_stage(CoreStage::First, network_debug_window);
}

pub enum PlayerSyncState {
SyncInProgress,
Sychronized,
}

/// Messages used by network debug channel
pub enum NetworkDebugMessage {
/// Reset network diag on new session.
Expand All @@ -45,6 +51,10 @@ pub enum NetworkDebugMessage {
},
/// Set the max prediction window for y axis of plot
SetMaxPrediction(usize),
/// List of players that are disconnected
DisconnectedPlayers(Vec<usize>),
/// Update ggrs synchronization state of player
PlayerSync((PlayerSyncState, PlayerHandle)),
}

/// Sender and receiver for [`NetworkDebugMessage`] for network diagnostics debug tool.
Expand Down Expand Up @@ -111,6 +121,13 @@ pub struct NetworkDebug {
/// Max Prediction Window set in ggrs session runner.
/// Cached here to determine max y on plot.
pub max_prediction_window: usize,

/// List of player handles that have been disconnected
pub disconnected_players: Vec<usize>,

/// Track players that are synchronizing or synchronized. If player not listed,
/// no sync has been attempted.
pub player_sync_state: HashMap<PlayerHandle, PlayerSyncState>,
}

impl Default for NetworkDebug {
Expand All @@ -125,6 +142,8 @@ impl Default for NetworkDebug {
paused: false,
network_stats: vec![],
max_prediction_window: 0,
disconnected_players: vec![],
player_sync_state: default(),
}
}
}
Expand Down Expand Up @@ -200,6 +219,12 @@ pub fn network_debug_window(
NetworkDebugMessage::SetMaxPrediction(max_preiction_window) => {
diagnostics.max_prediction_window = max_preiction_window;
}
NetworkDebugMessage::DisconnectedPlayers(disconnected_players) => {
diagnostics.disconnected_players = disconnected_players;
}
NetworkDebugMessage::PlayerSync((sync_state, player)) => {
diagnostics.player_sync_state.insert(player, sync_state);
}
}
}

Expand Down Expand Up @@ -324,9 +349,34 @@ pub fn network_debug_window(
for (player_handle, stats) in diagnostics.network_stats.iter() {
// let label = format!("{} {}", localization.get("player"), player_handle);
let label = format!("{} {}", "player", player_handle);
ui.collapsing(label, |ui| {
ui.monospace(&format!("{stats:?}"));
});
CollapsingHeader::new(label)
.default_open(true)
.show(ui, |ui| {
if diagnostics.disconnected_players.contains(player_handle) {
ui.colored_label(Color::RED, "Disconnected!");
} else {
match diagnostics.player_sync_state.get(player_handle) {
Some(sync_state) => match sync_state {
PlayerSyncState::SyncInProgress => {
ui.colored_label(
Color::ORANGE,
"GGRS synchronization with player in progress...",
);
}
PlayerSyncState::Sychronized => {
ui.label("Synchronized with player.");
}
},
None => {
ui.colored_label(
Color::RED,
"Not synchronized with player.",
);
}
}
}
ui.monospace(&format!("{stats:?}"));
});
}
});
}
Expand Down

0 comments on commit 1c76233

Please sign in to comment.