Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
22 changes: 20 additions & 2 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ internal void __sendServerRpc(FastBufferWriter writer, uint rpcMethodId, ServerR
if (IsHost || IsServer)
{
using var tempBuffer = new FastBufferReader(writer, Allocator.Temp);
message.Handle(tempBuffer, NetworkManager, NetworkManager.ServerClientId);
var context = new NetworkContext
{
SenderId = NetworkManager.ServerClientId,
Timestamp = Time.realtimeSinceStartup,
SystemOwner = NetworkManager,
// header information isn't valid since it's not a real message.
// Passing false to canDefer prevents it being accessed.
Header = new MessageHeader()
};
message.Handle(tempBuffer, context, NetworkManager, NetworkManager.ServerClientId, false);
rpcMessageSize = tempBuffer.Length;
}
else
Expand Down Expand Up @@ -172,7 +181,16 @@ internal unsafe void __sendClientRpc(FastBufferWriter writer, uint rpcMethodId,
if (shouldSendToHost)
{
using var tempBuffer = new FastBufferReader(writer, Allocator.Temp);
message.Handle(tempBuffer, NetworkManager, NetworkManager.ServerClientId);
var context = new NetworkContext
{
SenderId = NetworkManager.ServerClientId,
Timestamp = Time.realtimeSinceStartup,
SystemOwner = NetworkManager,
// header information isn't valid since it's not a real message.
// Passing false to canDefer prevents it being accessed.
Header = new MessageHeader()
};
message.Handle(tempBuffer, context, NetworkManager, NetworkManager.ServerClientId, false);
messageSize = tempBuffer.Length;
}

Expand Down
47 changes: 24 additions & 23 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal static string PrefabDebugHelper(NetworkPrefab networkPrefab)
internal SnapshotSystem SnapshotSystem { get; private set; }
internal NetworkBehaviourUpdater BehaviourUpdater { get; private set; }

private MessagingSystem m_MessagingSystem;
internal MessagingSystem MessagingSystem { get; private set; }

private NetworkPrefabHandler m_PrefabHandler;

Expand Down Expand Up @@ -244,7 +244,7 @@ public ulong LocalClientId
internal set
{
m_LocalClientId = value;
m_MessagingSystem.SetLocalClientId(value);
MessagingSystem.SetLocalClientId(value);
}
}

Expand Down Expand Up @@ -497,13 +497,13 @@ private void Initialize(bool server)
this.RegisterNetworkUpdate(NetworkUpdateStage.EarlyUpdate);
this.RegisterNetworkUpdate(NetworkUpdateStage.PostLateUpdate);

m_MessagingSystem = new MessagingSystem(new NetworkManagerMessageSender(this), this, ulong.MaxValue);
MessagingSystem = new MessagingSystem(new NetworkManagerMessageSender(this), this, ulong.MaxValue);

m_MessagingSystem.Hook(new NetworkManagerHooks(this));
MessagingSystem.Hook(new NetworkManagerHooks(this));
#if DEVELOPMENT_BUILD || UNITY_EDITOR
m_MessagingSystem.Hook(new ProfilingHooks());
MessagingSystem.Hook(new ProfilingHooks());
#endif
m_MessagingSystem.Hook(new MetricHooks(this));
MessagingSystem.Hook(new MetricHooks(this));

LocalClientId = ulong.MaxValue;

Expand Down Expand Up @@ -845,7 +845,7 @@ public SocketTasks StartClient()
}

Initialize(false);
m_MessagingSystem.ClientConnected(ServerClientId);
MessagingSystem.ClientConnected(ServerClientId);

var socketTasks = NetworkConfig.NetworkTransport.StartClient();

Expand Down Expand Up @@ -891,7 +891,7 @@ public SocketTasks StartHost()
Initialize(true);

var socketTasks = NetworkConfig.NetworkTransport.StartServer();
m_MessagingSystem.ClientConnected(ServerClientId);
MessagingSystem.ClientConnected(ServerClientId);
LocalClientId = ServerClientId;

IsServer = true;
Expand Down Expand Up @@ -1000,9 +1000,9 @@ public void Shutdown()
if (IsServer)
{
// make sure all messages are flushed before transport disconnect clients
if (m_MessagingSystem != null)
if (MessagingSystem != null)
{
m_MessagingSystem.ProcessSendQueues();
MessagingSystem.ProcessSendQueues();
}

var disconnectedIds = new HashSet<ulong>();
Expand Down Expand Up @@ -1063,10 +1063,10 @@ public void Shutdown()
NetworkTickSystem = null;
}

if (m_MessagingSystem != null)
if (MessagingSystem != null)
{
m_MessagingSystem.Dispose();
m_MessagingSystem = null;
MessagingSystem.Dispose();
MessagingSystem = null;
}

NetworkConfig.NetworkTransport.OnTransportEvent -= HandleRawTransportPoll;
Expand Down Expand Up @@ -1142,7 +1142,7 @@ private void OnNetworkEarlyUpdate()
// Only do another iteration if: there are no more messages AND (there is no limit to max events or we have processed less than the maximum)
} while (IsListening && networkEvent != NetworkEvent.Nothing);

m_MessagingSystem.ProcessIncomingMessageQueue();
MessagingSystem.ProcessIncomingMessageQueue();

#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_TransportPoll.End();
Expand All @@ -1164,7 +1164,8 @@ private void OnNetworkPreUpdate()

private void OnNetworkPostLateUpdate()
{
m_MessagingSystem.ProcessSendQueues();
MessagingSystem.ProcessSendQueues();
SpawnManager.CleanupStaleTriggers();
}

/// <summary>
Expand Down Expand Up @@ -1228,7 +1229,7 @@ private void HandleRawTransportPoll(NetworkEvent networkEvent, ulong clientId, A
#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_TransportConnect.Begin();
#endif
m_MessagingSystem.ClientConnected(clientId);
MessagingSystem.ClientConnected(clientId);
if (IsServer)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
Expand Down Expand Up @@ -1320,9 +1321,9 @@ public unsafe int SendMessage<TMessageType, TClientIdListType>(in TMessageType m
{
return 0;
}
return m_MessagingSystem.SendMessage(message, delivery, nonServerIds, newIdx);
return MessagingSystem.SendMessage(message, delivery, nonServerIds, newIdx);
}
return m_MessagingSystem.SendMessage(message, delivery, clientIds);
return MessagingSystem.SendMessage(message, delivery, clientIds);
}

public unsafe int SendMessage<T>(in T message, NetworkDelivery delivery,
Expand All @@ -1348,10 +1349,10 @@ public unsafe int SendMessage<T>(in T message, NetworkDelivery delivery,
{
return 0;
}
return m_MessagingSystem.SendMessage(message, delivery, nonServerIds, newIdx);
return MessagingSystem.SendMessage(message, delivery, nonServerIds, newIdx);
}

return m_MessagingSystem.SendMessage(message, delivery, clientIds, numClientIds);
return MessagingSystem.SendMessage(message, delivery, clientIds, numClientIds);
}

internal unsafe int SendMessage<T>(in T message, NetworkDelivery delivery, in NativeArray<ulong> clientIds)
Expand All @@ -1368,7 +1369,7 @@ public int SendMessage<T>(in T message, NetworkDelivery delivery, ulong clientId
{
return 0;
}
return m_MessagingSystem.SendMessage(message, delivery, clientId);
return MessagingSystem.SendMessage(message, delivery, clientId);
}

internal void HandleIncomingData(ulong clientId, ArraySegment<byte> payload, float receiveTime)
Expand All @@ -1377,7 +1378,7 @@ internal void HandleIncomingData(ulong clientId, ArraySegment<byte> payload, flo
s_HandleIncomingData.Begin();
#endif

m_MessagingSystem.HandleIncomingData(clientId, payload, receiveTime);
MessagingSystem.HandleIncomingData(clientId, payload, receiveTime);

#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_HandleIncomingData.End();
Expand Down Expand Up @@ -1473,7 +1474,7 @@ private void OnClientDisconnectFromServer(ulong clientId)

m_ConnectedClients.Remove(clientId);
}
m_MessagingSystem.ClientDisconnected(clientId);
MessagingSystem.ClientDisconnected(clientId);
}

private void SyncTime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ public static void Receive(FastBufferReader reader, in NetworkContext context)
return;
}
reader.ReadValueSafe(out ChangeOwnershipMessage message);
message.Handle(context.SenderId, networkManager, reader.Length);
message.Handle(reader, context, context.SenderId, networkManager, reader.Length);
}

public void Handle(ulong senderId, NetworkManager networkManager, int messageSize)
public void Handle(FastBufferReader reader, in NetworkContext context, ulong senderId, NetworkManager networkManager, int messageSize)
{
if (!networkManager.SpawnManager.SpawnedObjects.TryGetValue(NetworkObjectId, out var networkObject))
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"Trying to handle owner change but {nameof(NetworkObject)} #{NetworkObjectId} does not exist in {nameof(NetworkSpawnManager.SpawnedObjects)} anymore!");
}

networkManager.SpawnManager.TriggerOnSpawn(NetworkObjectId, reader, context);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ public static void Receive(FastBufferReader reader, in NetworkContext context)
}
reader.ReadValue(out message.NetworkObjectId);
reader.ReadValue(out message.NetworkBehaviourIndex);
message.Handle(context.SenderId, reader, networkManager);
message.Handle(context.SenderId, reader, context, networkManager);
}

public void Handle(ulong senderId, FastBufferReader reader, NetworkManager networkManager)
public void Handle(ulong senderId, FastBufferReader reader, in NetworkContext context, NetworkManager networkManager)
{
if (networkManager.SpawnManager.SpawnedObjects.TryGetValue(NetworkObjectId, out NetworkObject networkObject))
{
Expand Down Expand Up @@ -221,12 +221,9 @@ public void Handle(ulong senderId, FastBufferReader reader, NetworkManager netwo
}
}
}
else if (networkManager.IsServer)
else
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"Network variable delta message received for a non-existent object with {nameof(NetworkObjectId)}: {NetworkObjectId}. This delta was lost.");
}
networkManager.SpawnManager.TriggerOnSpawn(NetworkObjectId, reader, context);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ public static void Receive(FastBufferReader reader, in NetworkContext context)
}
}

message.Handle(networkManager);
message.Handle(reader, context, networkManager);
}

public void Handle(NetworkManager networkManager)
public void Handle(FastBufferReader reader, in NetworkContext context, NetworkManager networkManager)
{
if (networkManager.SpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId))
{
var networkObject = networkManager.SpawnManager.SpawnedObjects[NetworkObjectId];
networkObject.SetNetworkParenting(IsReparented, LatestParent);
networkObject.ApplyNetworkParenting();
}
else if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
else
{
NetworkLog.LogWarning($"Read {nameof(ParentSyncMessage)} for {nameof(NetworkObject)} #{NetworkObjectId} but could not find it in the {nameof(networkManager.SpawnManager.SpawnedObjects)}");
networkManager.SpawnManager.TriggerOnSpawn(NetworkObjectId, reader, context);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ public static void Receive(FastBufferReader reader, in NetworkContext context)
throw new OverflowException("Not enough space in the buffer to read RPC data.");
}
reader.ReadValue(out message.Header);
message.Handle(reader, (NetworkManager)context.SystemOwner, context.SenderId);
message.Handle(reader, context, (NetworkManager)context.SystemOwner, context.SenderId, true);
}

public void Handle(FastBufferReader reader, NetworkManager networkManager, ulong senderId)
public void Handle(FastBufferReader reader, in NetworkContext context, NetworkManager networkManager, ulong senderId, bool canDefer)
{
if (NetworkManager.__rpc_func_table.ContainsKey(Header.NetworkMethodId))
{
if (!networkManager.SpawnManager.SpawnedObjects.ContainsKey(Header.NetworkObjectId))
{
if (canDefer)
{
networkManager.SpawnManager.TriggerOnSpawn(Header.NetworkObjectId, reader, context);
}
else
{
NetworkLog.LogError($"Tried to invoke an RPC on a non-existent {nameof(NetworkObject)} with {nameof(canDefer)}=false");
}
return;
}

Expand Down
Loading