Skip to content

Commit 68ee9f5

Browse files
authored
Merge pull request #121 from 0xnim/fix/keep_alive
Fix/keep alive
2 parents 4c05395 + 0a24ddf commit 68ee9f5

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/bin/src/systems/keep_alive_system.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use async_trait::async_trait;
33
use ferrumc_core::identity::player_identity::PlayerIdentity;
44
use ferrumc_net::connection::{ConnectionState, StreamWriter};
55
use ferrumc_net::packets::outgoing::keep_alive::{KeepAlive, KeepAlivePacket};
6+
use ferrumc_net::utils::broadcast::{BroadcastOptions, BroadcastToAll};
67
use ferrumc_net::GlobalState;
78
use std::sync::atomic::{AtomicBool, Ordering};
89
use std::sync::Arc;
910
use tracing::{error, info, trace, warn};
10-
use ferrumc_net::utils::broadcast::{BroadcastOptions, BroadcastToAll};
1111

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

7273
let packet = KeepAlivePacket::default();
7374

74-
let broadcast_opts = BroadcastOptions::default().only(entities)
75+
let broadcast_opts = BroadcastOptions::default()
76+
.only(entities)
7577
.with_sync_callback(move |entity, state| {
7678
let Ok(mut keep_alive) = state.universe.get_mut::<KeepAlive>(entity) else {
7779
warn!("Failed to get <KeepAlive> component for entity {}", entity);
@@ -84,7 +86,8 @@ impl System for KeepAliveSystem {
8486
if let Err(e) = state.broadcast(&packet, broadcast_opts).await {
8587
error!("Error sending keep alive packet: {}", e);
8688
};
87-
89+
// 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
90+
tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
8891
}
8992
}
9093

0 commit comments

Comments
 (0)