send time snaps

This commit is contained in:
vis2k 2022-10-03 12:37:40 +02:00
parent f3cb8f973f
commit 0fc90a105b
4 changed files with 26 additions and 2 deletions

View File

@ -3,6 +3,14 @@
namespace Mirror namespace Mirror
{ {
// need to send time every sendInterval.
// batching automatically includes remoteTimestamp.
// all we need to do is ensure that an empty message is sent.
// and react to it.
// => we don't want to insert a snapshot on every batch.
// => do it exactly every sendInterval on every TimeSnapshotMessage.
public struct TimeSnapshotMessage : NetworkMessage {}
public struct ReadyMessage : NetworkMessage {} public struct ReadyMessage : NetworkMessage {}
public struct NotReadyMessage : NetworkMessage {} public struct NotReadyMessage : NetworkMessage {}

View File

@ -237,6 +237,7 @@ internal static void RegisterSystemHandlers(bool hostMode)
} }
// These handlers are the same for host and remote clients // These handlers are the same for host and remote clients
RegisterHandler<TimeSnapshotMessage>(OnTimeSnapshotMessage);
RegisterHandler<ChangeOwnerMessage>(OnChangeOwner); RegisterHandler<ChangeOwnerMessage>(OnChangeOwner);
RegisterHandler<RpcMessage>(OnRPCMessage); RegisterHandler<RpcMessage>(OnRPCMessage);
} }

View File

@ -60,11 +60,22 @@ static void InitTimeInterpolation()
deliveryTimeEma = new ExponentialMovingAverage(NetworkServer.config.sendRate * config.deliveryTimeEmaDuration); deliveryTimeEma = new ExponentialMovingAverage(NetworkServer.config.sendRate * config.deliveryTimeEmaDuration);
} }
// server sends TimeSnapshotMessage every sendInterval.
// batching already includes the remoteTimestamp.
// we simply insert it on-message here.
// => only for reliable channel. unreliable would always arrive earlier.
static void OnTimeSnapshotMessage(TimeSnapshotMessage _)
{
// insert another snapshot for snapshot interpolation.
// before calling OnDeserialize so components can use
// NetworkTime.time and NetworkTime.timeStamp.
OnTimeSnapshot(new TimeSnapshot(connection.remoteTimeStamp, Time.timeAsDouble));
}
// see comments at the top of this file // see comments at the top of this file
public static void OnTimeSnapshot(TimeSnapshot snap) public static void OnTimeSnapshot(TimeSnapshot snap)
{ {
// set local timestamp (= when it was received on our end) // Debug.Log($"NetworkClient: OnTimeSnapshot @ {snap.remoteTime:F3}");
snap.localTime = Time.timeAsDouble;
// (optional) dynamic adjustment // (optional) dynamic adjustment
if (config.dynamicAdjustment) if (config.dynamicAdjustment)

View File

@ -1704,6 +1704,10 @@ static void Broadcast()
// pull in UpdateVarsMessage for each entity it observes // pull in UpdateVarsMessage for each entity it observes
if (connection.isReady) if (connection.isReady)
{ {
// send time for snapshot interpolation every sendInterval.
// BroadcastToConnection() may not send if nothing is new.
connection.Send(new TimeSnapshotMessage());
// broadcast world state to this connection // broadcast world state to this connection
BroadcastToConnection(connection); BroadcastToConnection(connection);
} }