Skip to content

Commit

Permalink
Merge pull request #121 from 0xnim/fix/keep_alive
Browse files Browse the repository at this point in the history
Fix/keep alive
  • Loading branch information
Sweattypalms authored Nov 10, 2024
2 parents 4c05395 + 0a24ddf commit 68ee9f5
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/bin/src/systems/keep_alive_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use async_trait::async_trait;
use ferrumc_core::identity::player_identity::PlayerIdentity;
use ferrumc_net::connection::{ConnectionState, StreamWriter};
use ferrumc_net::packets::outgoing::keep_alive::{KeepAlive, KeepAlivePacket};
use ferrumc_net::utils::broadcast::{BroadcastOptions, BroadcastToAll};
use ferrumc_net::GlobalState;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tracing::{error, info, trace, warn};
use ferrumc_net::utils::broadcast::{BroadcastOptions, BroadcastToAll};

pub struct KeepAliveSystem {
shutdown: AtomicBool,
Expand All @@ -24,6 +24,7 @@ impl KeepAliveSystem {
#[async_trait]
impl System for KeepAliveSystem {
async fn start(self: Arc<Self>, state: GlobalState) {
info!("Started keep_alive");
let mut last_time = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("Time went backwards")
Expand Down Expand Up @@ -71,7 +72,8 @@ impl System for KeepAliveSystem {

let packet = KeepAlivePacket::default();

let broadcast_opts = BroadcastOptions::default().only(entities)
let broadcast_opts = BroadcastOptions::default()
.only(entities)
.with_sync_callback(move |entity, state| {
let Ok(mut keep_alive) = state.universe.get_mut::<KeepAlive>(entity) else {
warn!("Failed to get <KeepAlive> component for entity {}", entity);
Expand All @@ -84,7 +86,8 @@ impl System for KeepAliveSystem {
if let Err(e) = state.broadcast(&packet, broadcast_opts).await {
error!("Error sending keep alive packet: {}", e);
};

// TODO, this should be configurable as some people may have bad network so the clients may end up disconnecting from the server moments before the keep alive is sent
tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
}
}

Expand Down

0 comments on commit 68ee9f5

Please sign in to comment.