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

Codechange: replace _realtime_tick with std::chrono #8747

Merged
merged 3 commits into from Feb 26, 2021

Conversation

@TrueBrain
Copy link
Member

@TrueBrain TrueBrain commented Feb 25, 2021

Motivation / Problem

_realtime_tick is a global variable that counts time, driven by the video-drivers mainloop. In many cases there is no longer a need to do it this way, as we can just use std::chrono to get the time any moment we want in our code.

Additionally, because we have std::chrono::seconds etc, there is less bla about what unit a const has, making the code slightly more readable.

Description

For the first commit

Pretty straight-forward. I did rework some functions slightly to make them simpler. Mostly, wrapping no longer happens (well, every 250+ years (REAL LIFE YEARS @ldpl , not game years!), so .. yeah .. never), so that code can be removed.
Most noticeable change would be the advertisement; instead of keeping 2 variables for "next", just keep one "last" and check if the duration elapsed.

I tested all timeouts except one.

  • Admin, nc connect and don't do anything. Works.
  • Client timeout, iptables drop the TCP traffic after connect. client gets dialog counting up to 15 seconds.
  • Server lag timeout, harder to test, but modified the code to trigger this path.
  • Chat, by typing something and waiting for the timeout,
  • Advertisement, both failed and success advertisement, by letting a server do that.
  • Content service, clicking the button, waiting 60 boring seconds, checking netstat -n that the connection is gone.

All works as expected.

For the second commit

Two cases were straight-forward. Double click and hover. Nothing special about it. Tested, works as expected.

The third case was a bit more odd, as it was abusing _realtime_tick to now if any draw-ticks happened. This is because every mouse interaction can cause a call to HandleMouseEvents. Luckily, we already have a counter to check if any call to that function has been done during the draw-tick, so instead of using _realtime_tick like that, use the variable that was intended for it.
To have it slightly more readable, this did mean the function had to be moved up a bit.

For the third commit

Yes, _realtime_tick was in debug.h ... seems it was originally meant for debugging :D Super weird ..

Limitations

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')
@TrueBrain TrueBrain force-pushed the less-realtime-more-chrono branch from f400597 to af6d2ea Feb 25, 2021
@TrueBrain TrueBrain changed the title Codechange: [Network] replace _realtime_tick with std::chrono Codechange: replace _realtime_tick with std::chrono Feb 26, 2021
@TrueBrain TrueBrain merged commit fe451b8 into OpenTTD:master Feb 26, 2021
11 checks passed
@TrueBrain TrueBrain deleted the less-realtime-more-chrono branch Feb 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants