attempt to fix timeout with high timescale

This commit is contained in:
Isse 2023-09-26 12:18:55 +02:00
parent 6d4be9bd01
commit 6b31ffea3e
8 changed files with 46 additions and 20 deletions

View File

@ -1929,9 +1929,9 @@ impl Client {
self.tick_terrain()?;
// Send a ping to the server once every second
if self.state.get_time() - self.last_server_ping > 1. {
if self.state.get_true_time() - self.last_server_ping > 1. {
self.send_msg_err(PingMsg::Ping)?;
self.last_server_ping = self.state.get_time();
self.last_server_ping = self.state.get_true_time();
}
// 6) Update the server about the player's physics attributes.
@ -2266,9 +2266,16 @@ impl Client {
return Err(Error::Other("Failed to find entity from uid.".into()));
}
},
ServerGeneral::TimeOfDay(time_of_day, calendar, new_time, time_scale) => {
ServerGeneral::TimeOfDay(
time_of_day,
calendar,
new_time,
new_true_time,
time_scale,
) => {
self.target_time_of_day = Some(time_of_day);
*self.state.ecs_mut().write_resource() = calendar;
*self.state.ecs_mut().write_resource() = new_true_time;
let mut time = self.state.ecs_mut().write_resource::<Time>();
// Avoid side-eye from Einstein
// If new time from server is at least 5 seconds ahead, replace client time.
@ -2592,8 +2599,8 @@ impl Client {
self.send_msg_err(PingMsg::Pong)?;
},
PingMsg::Pong => {
self.last_server_pong = self.state.get_time();
self.last_ping_delta = self.state.get_time() - self.last_server_ping;
self.last_server_pong = self.state.get_true_time();
self.last_ping_delta = self.state.get_true_time() - self.last_server_ping;
// Maintain the correct number of deltas for calculating the rolling average
// ping. The client sends a ping to the server every second so we should be
@ -2664,18 +2671,18 @@ impl Client {
// Check that we have an valid connection.
// Use the last ping time as a 1s rate limiter, we only notify the user once per
// second
if self.state.get_time() - self.last_server_ping > 1. {
let duration_since_last_pong = self.state.get_time() - self.last_server_pong;
if self.state.get_true_time() - self.last_server_ping > 1. {
let duration_since_last_pong = self.state.get_true_time() - self.last_server_pong;
// Dispatch a notification to the HUD warning they will be kicked in {n} seconds
const KICK_WARNING_AFTER_REL_TO_TIMEOUT_FRACTION: f64 = 0.75;
if duration_since_last_pong
>= (self.client_timeout.as_secs() as f64
* KICK_WARNING_AFTER_REL_TO_TIMEOUT_FRACTION)
&& self.state.get_time() - duration_since_last_pong > 0.
&& self.state.get_true_time() - duration_since_last_pong > 0.
{
frontend_events.push(Event::DisconnectionNotification(
(self.state.get_time() - duration_since_last_pong).round() as u64,
(self.state.get_true_time() - duration_since_last_pong).round() as u64,
));
}
}
@ -2683,7 +2690,8 @@ impl Client {
let msg_count = self.handle_messages(&mut frontend_events)?;
if msg_count == 0
&& self.state.get_time() - self.last_server_pong > self.client_timeout.as_secs() as f64
&& self.state.get_true_time() - self.last_server_pong
> self.client_timeout.as_secs() as f64
{
return Err(Error::ServerTimeout);
}
@ -2884,7 +2892,7 @@ impl Client {
// Advance state time manually since we aren't calling `State::tick`
self.state
.ecs()
.write_resource::<common::resources::Time>()
.write_resource::<common::resources::TrueTime>()
.0 += dt.as_secs_f64();
// Handle new messages from the server.

View File

@ -11,7 +11,7 @@ use common::{
lod,
outcome::Outcome,
recipe::{ComponentRecipeBook, RecipeBook, RepairRecipeBook},
resources::{Time, TimeOfDay, TimeScale},
resources::{Time, TimeOfDay, TimeScale, TrueTime},
shared_server_config::ServerConstants,
terrain::{Block, TerrainChunk, TerrainChunkMeta, TerrainChunkSize},
trade::{PendingTrade, SitePrices, TradeId, TradeResult},
@ -195,7 +195,7 @@ pub enum ServerGeneral {
ChatMsg(comp::ChatMsg),
ChatMode(comp::ChatMode),
SetPlayerEntity(Uid),
TimeOfDay(TimeOfDay, Calendar, Time, TimeScale),
TimeOfDay(TimeOfDay, Calendar, Time, TrueTime, TimeScale),
EntitySync(sync::EntitySyncPackage),
CompSync(sync::CompSyncPackage<EcsCompPacket>, u64),
CreateEntity(sync::EntityPackage<EcsCompPacket>),
@ -342,7 +342,7 @@ impl ServerMsg {
| ServerGeneral::ChatMsg(_)
| ServerGeneral::ChatMode(_)
| ServerGeneral::SetPlayerEntity(_)
| ServerGeneral::TimeOfDay(_, _, _, _)
| ServerGeneral::TimeOfDay(_, _, _, _, _)
| ServerGeneral::EntitySync(_)
| ServerGeneral::CompSync(_, _)
| ServerGeneral::CreateEntity(_)

View File

@ -11,6 +11,10 @@ pub struct TimeOfDay(pub f64);
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Time(pub f64);
/// A resource that stores the real tick, unaffected by `TimeScale`.
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct TrueTime(pub f64);
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct TimeScale(pub f64);

View File

@ -14,7 +14,7 @@ use common::{
outcome::Outcome,
resources::{
DeltaTime, EntitiesDiedLastTick, GameMode, PlayerEntity, PlayerPhysicsSettings, Time,
TimeOfDay, TimeScale,
TimeOfDay, TimeScale, TrueTime,
},
shared_server_config::ServerConstants,
slowjob::SlowJobPool,
@ -269,6 +269,7 @@ impl State {
ecs.insert(Calendar::default());
ecs.insert(WeatherGrid::new(Vec2::zero()));
ecs.insert(Time(0.0));
ecs.insert(TrueTime(0.0));
ecs.insert(TimeScale(1.0));
// Register unsynced resources used by the ECS.
@ -437,6 +438,11 @@ impl State {
/// Note that this does not correspond to the time of day.
pub fn get_time(&self) -> f64 { self.ecs.read_resource::<Time>().0 }
/// Get the current true in-game time, unaffected by time_scale.
///
/// Note that this does not correspond to the time of day.
pub fn get_true_time(&self) -> f64 { self.ecs.read_resource::<TrueTime>().0 }
/// Get the current delta time.
pub fn get_delta_time(&self) -> f32 { self.ecs.read_resource::<DeltaTime>().0 }
@ -637,6 +643,7 @@ impl State {
self.ecs.write_resource::<TimeOfDay>().0 +=
dt.as_secs_f64() * server_constants.day_cycle_coefficient * time_scale;
self.ecs.write_resource::<Time>().0 += dt.as_secs_f64() * time_scale;
self.ecs.write_resource::<TrueTime>().0 += dt.as_secs_f64();
// Update delta time.
// Beyond a delta time of MAX_DELTA_TIME, start lagging to avoid skipping

View File

@ -203,7 +203,7 @@ impl Client {
| ServerGeneral::ChatMsg(_)
| ServerGeneral::ChatMode(_)
| ServerGeneral::SetPlayerEntity(_)
| ServerGeneral::TimeOfDay(_, _, _, _)
| ServerGeneral::TimeOfDay(_, _, _, _, _)
| ServerGeneral::EntitySync(_)
| ServerGeneral::CompSync(_, _)
| ServerGeneral::CreateEntity(_)

View File

@ -43,7 +43,7 @@ use common::{
npc::{self, get_npc_name},
outcome::Outcome,
parse_cmd_args,
resources::{BattleMode, PlayerPhysicsSettings, Secs, Time, TimeOfDay, TimeScale},
resources::{BattleMode, PlayerPhysicsSettings, Secs, Time, TimeOfDay, TimeScale, TrueTime},
rtsim::{Actor, Role},
terrain::{Block, BlockKind, CoordinateConversions, SpriteKind, TerrainChunkSize},
uid::Uid,
@ -1092,6 +1092,7 @@ fn handle_time(
server.state.mut_resource::<TimeOfDay>().0 = new_time;
let time = server.state.ecs().read_resource::<Time>();
let true_time = server.state.ecs().read_resource::<TrueTime>();
// Update all clients with the new TimeOfDay (without this they would have to
// wait for the next 100th tick to receive the update).
@ -1105,6 +1106,7 @@ fn handle_time(
TimeOfDay(new_time),
(*calendar).clone(),
*time,
*true_time,
*time_scale,
))
});
@ -1158,6 +1160,7 @@ fn handle_time_scale(
let mut tod_lazymsg = None;
let clients = server.state.ecs().read_storage::<Client>();
let time = server.state.ecs().read_resource::<Time>();
let true_time = server.state.ecs().read_resource::<TrueTime>();
let time_of_day = server.state.ecs().read_resource::<TimeOfDay>();
let calendar = server.state.ecs().read_resource::<Calendar>();
for client in (&clients).join() {
@ -1166,6 +1169,7 @@ fn handle_time_scale(
*time_of_day,
(*calendar).clone(),
*time,
*true_time,
TimeScale(scale),
))
});

View File

@ -8,7 +8,7 @@ use common::{
mounting::Rider,
outcome::Outcome,
region::{Event as RegionEvent, RegionMap},
resources::{PlayerPhysicsSettings, Time, TimeOfDay, TimeScale},
resources::{PlayerPhysicsSettings, Time, TimeOfDay, TimeScale, TrueTime},
terrain::TerrainChunkSize,
uid::Uid,
vol::RectVolSize,
@ -32,6 +32,7 @@ impl<'a> System<'a> for Sys {
TrackedStorages<'a>,
ReadExpect<'a, TimeOfDay>,
ReadExpect<'a, Time>,
ReadExpect<'a, TrueTime>,
ReadExpect<'a, Calendar>,
ReadExpect<'a, TimeScale>,
ReadExpect<'a, RegionMap>,
@ -65,6 +66,7 @@ impl<'a> System<'a> for Sys {
tracked_storages,
time_of_day,
time,
true_time,
calendar,
time_scale,
region_map,
@ -434,6 +436,7 @@ impl<'a> System<'a> for Sys {
*time_of_day,
(*calendar).clone(),
*time,
*true_time,
*time_scale,
))
});

View File

@ -1,7 +1,7 @@
use crate::{client::Client, Settings};
use common::{
event::{EventBus, ServerEvent},
resources::Time,
resources::TrueTime,
};
use common_ecs::{Job, Origin, Phase, System};
use common_net::msg::PingMsg;
@ -26,7 +26,7 @@ impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
Read<'a, EventBus<ServerEvent>>,
Read<'a, Time>,
Read<'a, TrueTime>,
WriteStorage<'a, Client>,
Read<'a, Settings>,
);