Codechange: [Network] replace _realtime_tick with std::chrono

This commit is contained in:
Patric Stout
2021-02-25 20:30:16 +01:00
committed by Patric Stout
parent dc7ba33b51
commit 53c28a8ec9
10 changed files with 39 additions and 49 deletions

View File

@@ -39,8 +39,8 @@ static std::mutex _network_udp_mutex;
/** Session key to register ourselves to the master server */
static uint64 _session_key = 0;
static const uint32 ADVERTISE_NORMAL_INTERVAL = 15 * 60 * 1000; ///< interval between advertising in ms (15 minutes)
static const uint32 ADVERTISE_RETRY_INTERVAL = 10 * 1000; ///< re-advertise when no response after this many ms (10 seconds)
static const std::chrono::minutes ADVERTISE_NORMAL_INTERVAL(15); ///< interval between advertising.
static const std::chrono::seconds ADVERTISE_RETRY_INTERVAL(10); ///< re-advertise when no response after this amount of time.
static const uint32 ADVERTISE_RETRY_TIMES = 3; ///< give up re-advertising after this much failed retries
NetworkUDPSocketHandler *_udp_client_socket = nullptr; ///< udp client socket
@@ -570,37 +570,29 @@ static void NetworkUDPAdvertiseThread()
*/
void NetworkUDPAdvertise()
{
static uint32 _last_advertisement = 0; ///< The time of the last advertisement (used to check for wrapping of time)
static uint32 _next_advertisement = 0; ///< The next time we should perform a normal advertisement.
static uint32 _next_retry = 0; ///< The next time we should perform a retry of an advertisement.
static std::chrono::steady_clock::time_point _last_advertisement = {}; ///< The last time we performed an advertisement.
/* Check if we should send an advertise */
if (!_networking || !_network_server || !_network_udp_server || !_settings_client.network.server_advertise) return;
if (_network_need_advertise || _realtime_tick < _last_advertisement) {
/* Forced advertisement, or a wrapping of time in which case we determine the advertisement/retry times again. */
if (_network_need_advertise) {
/* Forced advertisement. */
_network_need_advertise = false;
_network_advertise_retries = ADVERTISE_RETRY_TIMES;
} else {
/* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
if (_network_advertise_retries == 0) {
if (_realtime_tick <= _next_advertisement) return;
if (std::chrono::steady_clock::now() <= _last_advertisement + ADVERTISE_NORMAL_INTERVAL) return;
_network_advertise_retries = ADVERTISE_RETRY_TIMES;
} else {
/* An actual retry. */
if (_realtime_tick <= _next_retry) return;
if (std::chrono::steady_clock::now() <= _last_advertisement + ADVERTISE_RETRY_INTERVAL) return;
}
}
_network_advertise_retries--;
_last_advertisement = _realtime_tick;
_next_advertisement = _realtime_tick + ADVERTISE_NORMAL_INTERVAL;
_next_retry = _realtime_tick + ADVERTISE_RETRY_INTERVAL;
/* Make sure we do not have an overflow when checking these; when time wraps, we simply force an advertisement. */
if (_next_advertisement < _last_advertisement) _next_advertisement = UINT32_MAX;
if (_next_retry < _last_advertisement) _next_retry = UINT32_MAX;
_last_advertisement = std::chrono::steady_clock::now();
if (!StartNewThread(nullptr, "ottd:udp-advert", &NetworkUDPAdvertiseThread)) {
NetworkUDPAdvertiseThread();