Skip to content
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

feat: distributed authority #2852

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 5 additions & 7 deletions .yamato/project.metafile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
validation_editor: 2021.3
mobile_validation_editor: 2021.3
validation_editor: 2022.3
mobile_validation_editor: 2022.3

# Platforms that will be tested. The first entry in this array will also
# be used for validation
Expand Down Expand Up @@ -42,8 +42,7 @@ projects:
- name: com.unity.netcode.gameobjects
path: com.unity.netcode.gameobjects
test_editors:
- 2021.3
- 2022.2
- 2022.3
- 2023.1
- trunk
- name: minimalproject
Expand All @@ -55,15 +54,14 @@ projects:
- name: com.unity.netcode.gameobjects
path: com.unity.netcode.gameobjects
test_editors:
- 2021.3
- 2022.3
- name: testproject-tools-integration
path: testproject-tools-integration
validate: false
publish: false
has_tests: true
test_editors:
- 2021.3
- 2022.2
- 2022.3
- trunk

# Package dependencies
Expand Down
10 changes: 4 additions & 6 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
MIT License
Unity Companion License (UCL License)

Copyright (c) 2021 Unity Technologies
com.unity.netcode.gameobjects copyright © 2021-2024 Unity Technologies
Licensed under the Unity Companion License for Unity-dependent projects (see https://unity3d.com/legal/licenses/unity_companion_license).
Unless expressly provided otherwise, the Software under this license is made available strictly on an “AS IS” BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. Please review the license for details on these and other terms and conditions.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ internal struct NetworkTransformMessage : INetworkMessage
public int Version => 0;
public ulong NetworkObjectId;
public int NetworkBehaviourId;
#if NGO_DAMODE
// This is only used when serializing but not serialized
public bool DistributedAuthorityMode;
// Might get removed
public ulong[] TargetIds;

private int GetTargetIdLength()
{
if (TargetIds != null)
{
return TargetIds.Length;
}
return 0;
}
#endif
public NetworkTransform.NetworkTransformState State;

private NetworkTransform m_ReceiverNetworkTransform;
Expand All @@ -32,6 +47,22 @@ public void Serialize(FastBufferWriter writer, int targetVersion)
BytePacker.WriteValueBitPacked(writer, NetworkObjectId);
BytePacker.WriteValueBitPacked(writer, NetworkBehaviourId);
writer.WriteNetworkSerializable(State);
#if NGO_DAMODE
if (DistributedAuthorityMode)
{
var length = GetTargetIdLength();
BytePacker.WriteValuePacked(writer, length);
// If no target ids, then just exit early (DAHost specific)
if (length == 0)
{
return;
}
foreach (var target in TargetIds)
{
BytePacker.WriteValuePacked(writer, target);
}
}
#endif
}
}

Expand All @@ -45,39 +76,101 @@ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int
}
var currentPosition = reader.Position;
ByteUnpacker.ReadValueBitPacked(reader, out NetworkObjectId);
if (!networkManager.SpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId))
var isSpawnedLocally = networkManager.SpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId);
#if NGO_DAMODE
// Only defer if the NetworkObject is not spawned yet and the local NetworkManager is not running as a DAHost.
if (!isSpawnedLocally && !networkManager.DAHost)
#else
if (!isSpawnedLocally)
#endif
{
networkManager.DeferredMessageManager.DeferMessage(IDeferredNetworkMessageManager.TriggerType.OnSpawn, NetworkObjectId, reader, ref context);
return false;
}

// While the below check and assignment might seem out of place, this is specific to running in DAHost mode when a NetworkObject is
// hidden from the DAHost but is visible to other clients. Since the DAHost needs to forward updates to the clients, we ignore processing
// this message locally
var networkObject = (NetworkObject)null;
var isServerAuthoritative = false;
var ownerAuthoritativeServerSide = false;

// Get the behaviour index
ByteUnpacker.ReadValueBitPacked(reader, out NetworkBehaviourId);

// Deserialize the state
reader.ReadNetworkSerializable(out State);
reader.ReadNetworkSerializableInPlace(ref State);

var networkObject = networkManager.SpawnManager.SpawnedObjects[NetworkObjectId];
#if NGO_DAMODE
if (networkManager.DistributedAuthorityMode)
{
var targetCount = 0;
ByteUnpacker.ReadValueBitPacked(reader, out targetCount);
if (targetCount > 0)
{
TargetIds = new ulong[targetCount];
}
var targetId = (ulong)0;
for (int i = 0; i < targetCount; i++)
{
ByteUnpacker.ReadValueBitPacked(reader, out targetId);
TargetIds[i] = targetId;
}
}
#endif

// Get the target NetworkTransform
m_ReceiverNetworkTransform = networkObject.ChildNetworkBehaviours[NetworkBehaviourId] as NetworkTransform;
if (isSpawnedLocally)
{
networkObject = networkManager.SpawnManager.SpawnedObjects[NetworkObjectId];
// Get the target NetworkTransform
m_ReceiverNetworkTransform = networkObject.ChildNetworkBehaviours[NetworkBehaviourId] as NetworkTransform;
isServerAuthoritative = m_ReceiverNetworkTransform.IsServerAuthoritative();
ownerAuthoritativeServerSide = !isServerAuthoritative && networkManager.IsServer;
}
#if NGO_DAMODE
else
{
// If we are the DAHost and the NetworkObject is hidden from the host we still need to forward this message
ownerAuthoritativeServerSide = networkManager.DAHost && !isSpawnedLocally;
}
#endif

var isServerAuthoritative = m_ReceiverNetworkTransform.IsServerAuthoritative();
var ownerAuthoritativeServerSide = !isServerAuthoritative && networkManager.IsServer;
if (ownerAuthoritativeServerSide)
{
var ownerClientId = networkObject.OwnerClientId;
if (ownerClientId == NetworkManager.ServerClientId)
var ownerClientId = (ulong)0;

if (networkObject != null)
{
// Ownership must have changed, ignore any additional pending messages that might have
// come from a previous owner client.
return true;
ownerClientId = networkObject.OwnerClientId;
if (ownerClientId == NetworkManager.ServerClientId)
{
// Ownership must have changed, ignore any additional pending messages that might have
// come from a previous owner client.
return true;
}
}
#if NGO_DAMODE
else if (networkManager.DAHost)
{
// Specific to distributed authority mode, the only sender of state updates will be the owner
ownerClientId = context.SenderId;
}
#endif

var networkDelivery = State.IsReliableStateUpdate() ? NetworkDelivery.ReliableSequenced : NetworkDelivery.UnreliableSequenced;

// Forward the state update if there are any remote clients to foward it to
if (networkManager.ConnectionManager.ConnectedClientsList.Count > (networkManager.IsHost ? 2 : 1))
{
#if NGO_DAMODE
var clientCount = networkManager.DistributedAuthorityMode ? GetTargetIdLength() : networkManager.ConnectionManager.ConnectedClientsList.Count;
if (clientCount == 0)
{
return true;
}
#else
var clientCount = networkManager.ConnectionManager.ConnectedClientsList.Count;
#endif
// This is only to copy the existing and already serialized struct for forwarding purposes only.
// This will not include any changes made to this struct at this particular stage of processing the message.
var currentMessage = this;
Expand All @@ -86,15 +179,29 @@ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int
// Rewind the new reader to the beginning of the message's payload
currentMessage.m_CurrentReader.Seek(currentPosition);
// Forward the message to all connected clients that are observers of the associated NetworkObject
var clientCount = networkManager.ConnectionManager.ConnectedClientsList.Count;

for (int i = 0; i < clientCount; i++)
{
#if NGO_DAMODE
var clientId = networkManager.DistributedAuthorityMode ? TargetIds[i] : networkManager.ConnectionManager.ConnectedClientsList[i].ClientId;
if (NetworkManager.ServerClientId == clientId || (!isServerAuthoritative && clientId == ownerClientId) ||
(!networkManager.DistributedAuthorityMode && !networkObject.Observers.Contains(clientId)))
#else
var clientId = networkManager.ConnectionManager.ConnectedClientsList[i].ClientId;
if (NetworkManager.ServerClientId == clientId || (!isServerAuthoritative && clientId == ownerClientId) || !networkObject.Observers.Contains(clientId))
#endif
{
continue;
}
networkManager.MessageManager.SendMessage(ref currentMessage, networkDelivery, clientId);

if (clientId > 1)
{
networkManager.MessageManager.SendMessage(ref currentMessage, networkDelivery, clientId);
}
else
{
networkManager.MessageManager.SendMessage(ref currentMessage, networkDelivery, clientId);
}
}
// Dispose of the reader used for forwarding
currentMessage.m_CurrentReader.Dispose();
Expand All @@ -105,6 +212,17 @@ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int

public void Handle(ref NetworkContext context)
{
#if NGO_DAMODE
var networkManager = context.SystemOwner as NetworkManager;
// Only if the local NetworkManager instance is running as the DAHost we just exit if there is no local
// NetworkTransform component to apply the state update to (i.e. it is hidden from the DAHost and it
// just forwarded the state update to any other connected client)
if (networkManager.DAHost && m_ReceiverNetworkTransform == null)
{
return;
}
#endif

if (m_ReceiverNetworkTransform == null)
{
Debug.LogError($"[{nameof(NetworkTransformMessage)}][Dropped] Reciever {nameof(NetworkTransform)} was not set!");
Expand Down
Loading
Loading