dead peer discovery and available games updates
This commit is contained in:
@@ -40,6 +40,9 @@ use crate::{
|
||||
peer::{send_game_file_chunk, send_game_file_data},
|
||||
};
|
||||
|
||||
const PEER_PING_INTERVAL_SECS: u64 = 5;
|
||||
const PEER_STALE_TIMEOUT_SECS: u64 = 12;
|
||||
|
||||
/// Custom error types for peer operations
|
||||
#[derive(Debug)]
|
||||
pub enum PeerError {
|
||||
@@ -1469,7 +1472,14 @@ async fn run_server_component(
|
||||
}
|
||||
async fn handle_list_games_command(ctx: &Ctx, tx_notify_ui: &UnboundedSender<PeerEvent>) {
|
||||
log::info!("ListGames command received");
|
||||
let all_games = { ctx.peer_game_db.read().await.get_all_games() };
|
||||
emit_peer_game_list(&ctx.peer_game_db, tx_notify_ui).await;
|
||||
}
|
||||
|
||||
async fn emit_peer_game_list(
|
||||
peer_game_db: &Arc<RwLock<PeerGameDB>>,
|
||||
tx_notify_ui: &UnboundedSender<PeerEvent>,
|
||||
) {
|
||||
let all_games = { peer_game_db.read().await.get_all_games() };
|
||||
if let Err(e) = tx_notify_ui.send(PeerEvent::ListGames(all_games)) {
|
||||
log::error!("Failed to send ListGames event: {e}");
|
||||
}
|
||||
@@ -2081,9 +2091,12 @@ async fn run_ping_service(
|
||||
tx_notify_ui: UnboundedSender<PeerEvent>,
|
||||
peer_game_db: Arc<RwLock<PeerGameDB>>,
|
||||
) {
|
||||
log::info!("Starting ping service (10s interval)");
|
||||
log::info!(
|
||||
"Starting ping service ({PEER_PING_INTERVAL_SECS}s interval, \
|
||||
{PEER_STALE_TIMEOUT_SECS}s timeout)"
|
||||
);
|
||||
|
||||
let mut interval = tokio::time::interval(Duration::from_secs(10));
|
||||
let mut interval = tokio::time::interval(Duration::from_secs(PEER_PING_INTERVAL_SECS));
|
||||
|
||||
loop {
|
||||
interval.tick().await;
|
||||
@@ -2125,6 +2138,8 @@ async fn run_ping_service(
|
||||
{
|
||||
log::error!("Failed to send PeerCountUpdated event: {e}");
|
||||
}
|
||||
|
||||
emit_peer_game_list(&peer_game_db_clone, &tx_notify_ui_clone).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2148,6 +2163,8 @@ async fn run_ping_service(
|
||||
{
|
||||
log::error!("Failed to send PeerCountUpdated event: {e}");
|
||||
}
|
||||
|
||||
emit_peer_game_list(&peer_game_db_clone, &tx_notify_ui_clone).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2159,8 +2176,9 @@ async fn run_ping_service(
|
||||
peer_game_db
|
||||
.read()
|
||||
.await
|
||||
.get_stale_peers(Duration::from_secs(30))
|
||||
.get_stale_peers(Duration::from_secs(PEER_STALE_TIMEOUT_SECS))
|
||||
};
|
||||
let mut removed_any = false;
|
||||
for stale_addr in stale_peers {
|
||||
let removed_peer = peer_game_db.write().await.remove_peer(&stale_addr);
|
||||
if removed_peer.is_some() {
|
||||
@@ -2174,8 +2192,13 @@ async fn run_ping_service(
|
||||
if let Err(e) = tx_notify_ui.send(PeerEvent::PeerCountUpdated(current_peer_count)) {
|
||||
log::error!("Failed to send PeerCountUpdated event: {e}");
|
||||
}
|
||||
removed_any = true;
|
||||
}
|
||||
}
|
||||
|
||||
if removed_any {
|
||||
emit_peer_game_list(&peer_game_db, &tx_notify_ui).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user