summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/network/network.cpp3
-rw-r--r--src/network/network_func.h1
-rw-r--r--src/network/network_udp.cpp28
3 files changed, 20 insertions, 12 deletions
diff --git a/src/network/network.cpp b/src/network/network.cpp
index ce37aaa3f..0bbdd0d06 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -63,7 +63,6 @@ NetworkCompanyState *_network_company_states = NULL; ///< Statistics about some
ClientID _network_own_client_id; ///< Our client identifier.
ClientID _redirect_console_to_client; ///< If not invalid, redirect the console output to a client.
bool _network_need_advertise; ///< Whether we need to advertise.
-uint32 _network_last_advertise_frame; ///< Last time we did advertise.
uint8 _network_reconnect; ///< Reconnect timeout
StringList _network_bind_list; ///< The addresses to bind on.
StringList _network_host_list; ///< The servers we know.
@@ -759,7 +758,6 @@ bool NetworkServerStart()
if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
/* Try to register us to the master server */
- _network_last_advertise_frame = 0;
_network_need_advertise = true;
NetworkUDPAdvertise();
@@ -1076,7 +1074,6 @@ void NetworkStartUp()
/* Network is available */
_network_available = NetworkCoreInitialize();
_network_dedicated = false;
- _network_last_advertise_frame = 0;
_network_need_advertise = true;
_network_advertise_retries = 0;
diff --git a/src/network/network_func.h b/src/network/network_func.h
index 375cc3da5..4f1525b5a 100644
--- a/src/network/network_func.h
+++ b/src/network/network_func.h
@@ -34,7 +34,6 @@ extern NetworkCompanyState *_network_company_states;
extern ClientID _network_own_client_id;
extern ClientID _redirect_console_to_client;
extern bool _network_need_advertise;
-extern uint32 _network_last_advertise_frame;
extern uint8 _network_reconnect;
extern StringList _network_bind_list;
extern StringList _network_host_list;
diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp
index 731d51ca0..1cccbf644 100644
--- a/src/network/network_udp.cpp
+++ b/src/network/network_udp.cpp
@@ -42,9 +42,9 @@ static ThreadMutex *_network_udp_mutex = ThreadMutex::New();
/** Session key to register ourselves to the master server */
static uint64 _session_key = 0;
-static const uint ADVERTISE_NORMAL_INTERVAL = 30000; ///< interval between advertising in ticks (15 minutes)
-static const uint ADVERTISE_RETRY_INTERVAL = 300; ///< re-advertise when no response after this many ticks (9 seconds)
-static const uint ADVERTISE_RETRY_TIMES = 3; ///< give up re-advertising after this much failed retries
+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 uint32 ADVERTISE_RETRY_TIMES = 3; ///< give up re-advertising after this much failed retries
NetworkUDPSocketHandler *_udp_client_socket = NULL; ///< udp client socket
NetworkUDPSocketHandler *_udp_server_socket = NULL; ///< udp server socket
@@ -616,25 +616,37 @@ static void NetworkUDPAdvertiseThread(void *pntr)
*/
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.
+
/* Check if we should send an advertise */
if (!_networking || !_network_server || !_network_udp_server || !_settings_client.network.server_advertise) return;
- if (_network_need_advertise) {
+ 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. */
_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 ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter) return;
+ if (_realtime_tick <= _next_advertisement) return;
_network_advertise_retries = ADVERTISE_RETRY_TIMES;
+ } else {
+ /* An actual retry. */
+ if (_realtime_tick <= _next_retry) return;
}
-
- if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter) return;
}
_network_advertise_retries--;
- _network_last_advertise_frame = _frame_counter;
+ _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;
if (!ThreadObject::New(NetworkUDPAdvertiseThread, NULL)) {
NetworkUDPAdvertiseThread(NULL);