Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set current_time to absolute system time to prevent drift #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

CuddlyBunion341
Copy link

@CuddlyBunion341 CuddlyBunion341 commented Feb 4, 2025

Prevent current time update from drifting

Current setup

The current_time gets updated by a fixed amount every system tick. This is error prone especially for long running sessions, like described in #157.

Proposal

Instead of having the delay be updated by an relative amount, the current_time should be set to an absolute value in order to prevent a drift of the internal clock.

In addition to setting the current_time at the beginning of the session, the current_time should be updated on every tick accordingly.

Problems / Considerations

  • Maybe there are reasons as to why the client and server clocks should not stay in sync.
  • Maybe there are situations where it is expected for the duration to be set manually.

Alternatives

  • Introduce a flag like auto_sync which periodically updates the client and server time to current system time, to avoid drifting.

fn update_internal_state(&mut self, duration: Duration) -> Result<(), NetcodeError> {
self.current_time += duration;
fn update_internal_state(&mut self) -> Result<(), NetcodeError> {
self.current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not the best approach.

Instead there should be some method to synchronize the clocks.
Otherwise it is not possible to set a custom delay.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the current_time should be synchronized every so often by passing in a special duration.

@CuddlyBunion341 CuddlyBunion341 marked this pull request as ready for review February 4, 2025 13:55
@CuddlyBunion341 CuddlyBunion341 force-pushed the fix-time-drift-on-update branch from 4f23740 to e9258be Compare February 4, 2025 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant