Skip to content

Commit 6fc206e

Browse files
authored
feat: Broadcast time sync success (#325)
Makes peer sync success public, so now every peer can check whether another peer has completed their time sync handshake. Supersedes #311
1 parent fe011dd commit 6fc206e

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed

addons/netfox.extras/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="netfox.extras"
44
description="Game-specific utilities for Netfox"
55
author="Tamas Galffy"
6-
version="1.11.1"
6+
version="1.12.0"
77
script="netfox-extras.gd"

addons/netfox.internals/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="netfox.internals"
44
description="Shared internals for netfox addons"
55
author="Tamas Galffy"
6-
version="1.11.1"
6+
version="1.12.0"
77
script="plugin.gd"

addons/netfox.noray/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="netfox.noray"
44
description="Bulletproof your connectivity with noray integration for netfox"
55
author="Tamas Galffy"
6-
version="1.11.1"
6+
version="1.12.0"
77
script="netfox-noray.gd"

addons/netfox/network-time.gd

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,7 @@ var _last_process_time: float = 0.
345345
var _clock: NetworkClocks.SteppingClock = NetworkClocks.SteppingClock.new()
346346
var _clock_stretch_factor: float = 1.
347347

348-
# Cache the synced clients, as the rpc call itself may arrive multiple times
349-
# ( for some reason? )
350-
var _synced_clients: Dictionary = {}
348+
var _synced_peers: Dictionary = {}
351349

352350
static var _logger: _NetfoxLogger = _NetfoxLogger.for_netfox("NetworkTime")
353351

@@ -367,9 +365,8 @@ func start():
367365
_tick = 0
368366
_initial_sync_done = false
369367

370-
after_client_sync.connect(func(pid):
371-
_logger.debug("Client #%s is now on time!" % [pid])
372-
)
368+
# Host is always synced, as their time is considered ground truth
369+
_synced_peers[1] = true
373370

374371
NetworkTimeSynchronizer.start()
375372

@@ -380,13 +377,13 @@ func start():
380377
_initial_sync_done = true
381378
_active = true
382379

383-
_submit_sync_success.rpc_id(1)
380+
_submit_sync_success.rpc()
384381
else:
385382
_active = true
386383
_initial_sync_done = true
387-
388-
# Remove clients from the synced cache when disconnected
389-
multiplayer.peer_disconnected.connect(func(peer): _synced_clients.erase(peer))
384+
385+
# Remove clients from the synced cache when disconnected
386+
multiplayer.peer_disconnected.connect(func(peer): _synced_peers.erase(peer))
390387

391388
_clock.set_time(NetworkTimeSynchronizer.get_time())
392389
_last_process_time = _clock.get_time()
@@ -400,6 +397,7 @@ func start():
400397
func stop():
401398
NetworkTimeSynchronizer.stop()
402399
_active = false
400+
_synced_peers.clear()
403401

404402
## Check if the initial time sync is done.
405403
func is_initial_sync_done() -> bool:
@@ -409,11 +407,7 @@ func is_initial_sync_done() -> bool:
409407
##
410408
## Using this from a client is considered an error.
411409
func is_client_synced(peer_id: int) -> bool:
412-
if not multiplayer.is_server():
413-
_logger.error("Trying to check if client is synced from another client!")
414-
return false
415-
else:
416-
return _synced_clients.has(peer_id)
410+
return _synced_peers.has(peer_id)
417411

418412
## Convert a duration of ticks to seconds.
419413
func ticks_to_seconds(ticks: int) -> float:
@@ -497,10 +491,14 @@ func _notification(what):
497491
if what == NOTIFICATION_UNPAUSED:
498492
_was_paused = true
499493

500-
@rpc("any_peer", "reliable", "call_remote")
494+
@rpc("any_peer", "reliable", "call_local")
501495
func _submit_sync_success():
502496
var peer_id = multiplayer.get_remote_sender_id()
503497

504-
if not _synced_clients.has(peer_id):
505-
_synced_clients[peer_id] = true
506-
after_client_sync.emit(multiplayer.get_remote_sender_id())
498+
_logger.trace("Received time sync success from #%s, synced peers: %s" % [peer_id, _synced_peers.keys()])
499+
500+
# Check is necessary, RPC arrives twice for some reason; see trace log above
501+
if not _synced_peers.has(peer_id):
502+
_synced_peers[peer_id] = true
503+
after_client_sync.emit(peer_id)
504+
_logger.debug("Peer #%s is now on time!" % [peer_id])

addons/netfox/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="netfox"
44
description="Shared internals for netfox addons"
55
author="Tamas Galffy"
6-
version="1.11.1"
6+
version="1.12.0"
77
script="netfox.gd"

0 commit comments

Comments
 (0)