Skip to content

feat: snapshot. MTT-685 MTT-822 #1021

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

Merged
merged 28 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
94313c7
feat: snapshot. Attempt at applying snapshot spawn branch over new me…
jeffreyrainy Jul 27, 2021
daa7477
feat: snapshot. Reusing spawn entries.
jeffreyrainy Aug 2, 2021
9cb59e4
Merge branch 'develop' into experimental/snapshot-system-spawn3
jeffreyrainy Aug 2, 2021
62e87d5
Merge remote-tracking branch 'origin/develop' into experimental/snaps…
jeffreyrainy Aug 3, 2021
64bb7ec
feat: snapshot fix, prefab handling
jeffreyrainy Aug 3, 2021
847adcd
feat: snapshot. reduce log spam
jeffreyrainy Aug 3, 2021
bc49244
Merge branch 'develop' into experimental/snapshot-system-spawn3
jeffreyrainy Aug 3, 2021
4963d9c
Merge remote-tracking branch 'origin/develop' into experimental/snaps…
jeffreyrainy Aug 3, 2021
10ed439
snapshot: merge preparation. Removing old acks, removing unused varia…
jeffreyrainy Aug 3, 2021
9674acc
feat: snapshot. merge preparation. Removing old acks, removing unused…
jeffreyrainy Aug 3, 2021
bc4805d
Merge branch 'experimental/snapshot-prep2-spawn' of github.com:Unity-…
jeffreyrainy Aug 3, 2021
5c9a703
Merge branch 'develop' into experimental/snapshot-prep2-spawn
0xFA11 Aug 3, 2021
411ef16
Merge branch 'experimental/snapshot-prep2-spawn' into experimental/sn…
jeffreyrainy Aug 3, 2021
8201cd2
Merge branch 'experimental/snapshot-prep2-spawn' into experimental/sn…
jeffreyrainy Aug 3, 2021
7a597b3
Merge remote-tracking branch 'origin/develop' into experimental/snaps…
jeffreyrainy Aug 3, 2021
d2318bc
feat: snapshot. more reasonable sentinel checks around snapshot send/…
jeffreyrainy Aug 4, 2021
9f81229
Merge remote-tracking branch 'origin/develop' into experimental/snaps…
jeffreyrainy Aug 4, 2021
d0b6617
Merge remote-tracking branch 'origin/develop' into experimental/snaps…
jeffreyrainy Aug 4, 2021
43c26f6
feat: snapshot. small clenaup
jeffreyrainy Aug 4, 2021
4e32df9
feat: snapshot. Moving configuration items to NetworkConfig
jeffreyrainy Aug 4, 2021
4222365
Merge remote-tracking branch 'origin/develop' into experimental/snaps…
jeffreyrainy Aug 4, 2021
2bf493d
feat: snapshot. Adjusting to code standard
jeffreyrainy Aug 4, 2021
beac43f
feat: snapshot. Disabling snapshot for PR into develop as prototype
jeffreyrainy Aug 4, 2021
c7bb2ae
Merge remote-tracking branch 'origin/develop' into experimental/snaps…
jeffreyrainy Aug 5, 2021
9be6b97
Merge remote-tracking branch 'origin/develop' into experimental/snaps…
jeffreyrainy Aug 5, 2021
19718d2
Merge branch 'develop' into experimental/snapshot-system-spawn3-pr
jeffreyrainy Aug 5, 2021
f22b45c
Merge remote-tracking branch 'origin/develop' into experimental/snaps…
jeffreyrainy Aug 10, 2021
05813d0
Merge branch 'develop' into experimental/snapshot-system-spawn3-pr
jeffreyrainy Aug 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ public class NetworkConfig
/// </summary>
public bool EnableNetworkLogs = true;

// todo: transitional. For the next release, only Snapshot should remain
// The booleans allow iterative development and testing in the meantime
public bool UseSnapshotDelta = false;
public bool UseSnapshotSpawn = false;

public const int RttAverageSamples = 5; // number of RTT to keep an average of (plus one)
public const int RttWindowSize = 64; // number of slots to use for RTT computations (max number of in-flight packets)

private void Sort()
{
RegisteredScenes.Sort(StringComparer.Ordinal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,15 @@ private void NetworkVariableUpdate(ulong clientId, int behaviourIndex)
return;
}

if (NetworkManager.UseSnapshot)
if (NetworkManager.NetworkConfig.UseSnapshotDelta)
{
for (int k = 0; k < NetworkVariableFields.Count; k++)
{
NetworkManager.SnapshotSystem.Store(NetworkObjectId, behaviourIndex, k, NetworkVariableFields[k]);
}
}

if (NetworkManager.UseClassicDelta)
if (!NetworkManager.NetworkConfig.UseSnapshotDelta)
{
for (int j = 0; j < m_ChannelMappedNetworkVariableIndexes.Count; j++)
{
Expand Down
5 changes: 0 additions & 5 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ public class NetworkManager : MonoBehaviour, INetworkUpdateSystem, IProfilableTr
private static ProfilerMarker s_InvokeRpc = new ProfilerMarker($"{nameof(NetworkManager)}.{nameof(InvokeRpc)}");
#endif

// todo: transitional. For the next release, only Snapshot should remain
// The booleans allow iterative development and testing in the meantime
internal static bool UseClassicDelta = true;
internal static bool UseSnapshot = false;

private const double k_TimeSyncFrequency = 1.0d; // sync every second, TODO will be removed once timesync is done via snapshots

internal MessageQueueContainer MessageQueueContainer { get; private set; }
Expand Down
31 changes: 31 additions & 0 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,37 @@ private void SpawnInternal(bool destroyWithScene, ulong? ownerClientId, bool pla
}

NetworkManager.SpawnManager.SpawnNetworkObjectLocally(this, NetworkManager.SpawnManager.GetNetworkObjectId(), false, playerObject, ownerClientId, null, false, destroyWithScene);

if (NetworkManager.NetworkConfig.UseSnapshotSpawn)
{
SnapshotSpawnCommand command;
command.NetworkObjectId = NetworkObjectId;
command.OwnerClientId = OwnerClientId;
command.IsPlayerObject = IsPlayerObject;
command.IsSceneObject = (IsSceneObject == null) || IsSceneObject.Value;

ulong? parent = NetworkManager.SpawnManager.GetSpawnParentId(this);
if (parent != null)
{
command.ParentNetworkId = parent.Value;
}
else
{
// write own network id, when no parents. todo: optimize this.
command.ParentNetworkId = command.NetworkObjectId;
}

command.GlobalObjectIdHash = HostCheckForGlobalObjectIdHashOverride();
// todo: check if (IncludeTransformWhenSpawning == null || IncludeTransformWhenSpawning(clientId)) for any clientId
command.ObjectPosition = transform.position;
command.ObjectRotation = transform.rotation;
command.ObjectScale = transform.localScale;
command.TickWritten = 0; // will be reset in Spawn
command.TargetClientIds = default;

NetworkManager.SnapshotSystem.Spawn(command);
}

ulong ownerId = ownerClientId != null ? ownerClientId.Value : NetworkManager.ServerClientId;
for (int i = 0; i < NetworkManager.ConnectedClientsList.Count; i++)
{
Expand Down
27 changes: 12 additions & 15 deletions com.unity.multiplayer.mlapi/Runtime/Core/SnapshotRTT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace Unity.Netcode
{
internal class ConnectionRtt
{
internal const int RttSize = 5; // number of RTT to keep an average of (plus one)
internal const int RingSize = 64; // number of slots to use for RTT computations (max number of in-flight packets)

private double[] m_RttSendTimes; // times at which packet were sent for RTT computations
private int[] m_SendSequence; // tick, or other key, at which packets were sent (to allow matching)
private double[] m_MeasuredLatencies; // measured latencies (ring buffer)
Expand All @@ -26,17 +23,17 @@ public struct Rtt
}
public ConnectionRtt()
{
m_RttSendTimes = new double[RingSize];
m_SendSequence = new int[RingSize];
m_MeasuredLatencies = new double[RingSize];
m_RttSendTimes = new double[NetworkConfig.RttWindowSize];
m_SendSequence = new int[NetworkConfig.RttWindowSize];
m_MeasuredLatencies = new double[NetworkConfig.RttWindowSize];
}

/// <summary>
/// Returns the Round-trip-time computation for this client
/// </summary>
public Rtt GetRtt()
{
var ret = new Rtt(); // is this a memory alloc ? How do I get a stack alloc ?
var ret = new Rtt();
var index = m_LatenciesBegin;
double total = 0.0;
ret.BestSec = m_MeasuredLatencies[m_LatenciesBegin];
Expand All @@ -48,14 +45,14 @@ public Rtt GetRtt()
ret.SampleCount++;
ret.BestSec = Math.Min(ret.BestSec, m_MeasuredLatencies[index]);
ret.WorstSec = Math.Max(ret.WorstSec, m_MeasuredLatencies[index]);
index = (index + 1) % RttSize;
index = (index + 1) % NetworkConfig.RttAverageSamples;
}

if (ret.SampleCount != 0)
{
ret.AverageSec = total / ret.SampleCount;
// the latest RTT is one before m_LatenciesEnd
ret.LastSec = m_MeasuredLatencies[(m_LatenciesEnd + (RingSize - 1)) % RingSize];
ret.LastSec = m_MeasuredLatencies[(m_LatenciesEnd + (NetworkConfig.RttWindowSize - 1)) % NetworkConfig.RttWindowSize];
}
else
{
Expand All @@ -71,23 +68,23 @@ public Rtt GetRtt()

internal void NotifySend(int sequence, double timeSec)
{
m_RttSendTimes[sequence % RingSize] = timeSec;
m_SendSequence[sequence % RingSize] = sequence;
m_RttSendTimes[sequence % NetworkConfig.RttWindowSize] = timeSec;
m_SendSequence[sequence % NetworkConfig.RttWindowSize] = sequence;
}

internal void NotifyAck(int sequence, double timeSec)
{
// if the same slot was not used by a later send
if (m_SendSequence[sequence % RingSize] == sequence)
if (m_SendSequence[sequence % NetworkConfig.RttWindowSize] == sequence)
{
double latency = timeSec - m_RttSendTimes[sequence % RingSize];
double latency = timeSec - m_RttSendTimes[sequence % NetworkConfig.RttWindowSize];

m_MeasuredLatencies[m_LatenciesEnd] = latency;
m_LatenciesEnd = (m_LatenciesEnd + 1) % RttSize;
m_LatenciesEnd = (m_LatenciesEnd + 1) % NetworkConfig.RttAverageSamples;

if (m_LatenciesEnd == m_LatenciesBegin)
{
m_LatenciesBegin = (m_LatenciesBegin + 1) % RttSize;
m_LatenciesBegin = (m_LatenciesBegin + 1) % NetworkConfig.RttAverageSamples;
}
}
}
Expand Down
Loading