Skip to content

Commit b89ccd3

Browse files
fix
Improving our defines that determine if metrics should be gathered. 1st pass attempt at unifying this process. The primary change is that if the __rpc_name_table is not built (yet) when an RPC is invoked that it will attempt to build it for that specific NetworkBehaviour prior to attempting to log RPC metrics.
1 parent 2ea2736 commit b89ccd3

File tree

7 files changed

+105
-82
lines changed

7 files changed

+105
-82
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public abstract class NetworkBehaviour : MonoBehaviour
4040
internal static readonly Dictionary<Type, Dictionary<uint, RpcReceiveHandler>> __rpc_func_table = new Dictionary<Type, Dictionary<uint, RpcReceiveHandler>>();
4141
internal static readonly Dictionary<Type, Dictionary<uint, RpcInvokePermission>> __rpc_permission_table = new Dictionary<Type, Dictionary<uint, RpcInvokePermission>>();
4242

43-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
43+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
4444
// RuntimeAccessModifiersILPP will make this `public`
4545
internal static readonly Dictionary<Type, Dictionary<uint, string>> __rpc_name_table = new Dictionary<Type, Dictionary<uint, string>>();
4646
#endif
@@ -142,16 +142,9 @@ internal void __endSendServerRpc(ref FastBufferWriter bufferWriter, uint rpcMeth
142142
}
143143

144144
bufferWriter.Dispose();
145-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
146-
if (__rpc_name_table[GetType()].TryGetValue(rpcMethodId, out var rpcMethodName))
147-
{
148-
networkManager.NetworkMetrics.TrackRpcSent(
149-
NetworkManager.ServerClientId,
150-
m_NetworkObject,
151-
rpcMethodName,
152-
__getTypeName(),
153-
rpcWriteSize);
154-
}
145+
146+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
147+
TrackRpcMetricsSend(ref serverRpcMessage, rpcMethodId, rpcWriteSize);
155148
#endif
156149
}
157150

@@ -275,7 +268,11 @@ internal void __endSendClientRpc(ref FastBufferWriter bufferWriter, uint rpcMeth
275268
}
276269

277270
bufferWriter.Dispose();
278-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
271+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
272+
if (!ValidateRpcMessageMetrics(GetType()))
273+
{
274+
return;
275+
}
279276
if (__rpc_name_table[GetType()].TryGetValue(rpcMethodId, out var rpcMethodName))
280277
{
281278
if (clientRpcParams.Send.TargetClientIds != null)
@@ -962,13 +959,91 @@ internal virtual void __initializeRpcs()
962959
internal void __registerRpc(uint hash, RpcReceiveHandler handler, string rpcMethodName, RpcInvokePermission permission)
963960
#pragma warning restore IDE1006 // restore naming rule violation check
964961
{
965-
__rpc_func_table[GetType()][hash] = handler;
966-
__rpc_permission_table[GetType()][hash] = permission;
967-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
968-
__rpc_name_table[GetType()][hash] = rpcMethodName;
962+
var rpcType = GetType();
963+
__rpc_func_table[rpcType][hash] = handler;
964+
__rpc_permission_table[rpcType][hash] = permission;
965+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
966+
__rpc_name_table[rpcType][hash] = rpcMethodName;
969967
#endif
970968
}
971969

970+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
971+
private bool ValidateRpcMessageMetrics(Type type)
972+
{
973+
if (m_NetworkManager == null)
974+
{
975+
Debug.LogError($"[{type.Name}] Attempting to invoking an RPC before {nameof(NetworkManager)} has been initialized within this {nameof(NetworkBehaviour)}!");
976+
// error and exit
977+
return false;
978+
}
979+
980+
if (!__rpc_name_table.ContainsKey(type))
981+
{
982+
__initializeRpcs();
983+
if (!__rpc_name_table.ContainsKey(type))
984+
{
985+
Debug.LogError($"[{nameof(TrackRpcMetricsSend)}] Rpc table does not contain an entry for {type.Name}! Failed to initialize RPCs for {type.Name}.");
986+
return false;
987+
}
988+
}
989+
return true;
990+
}
991+
992+
internal void TrackRpcMetricsSend(ref ServerRpcMessage message, uint rpcMethodId, int rpcWriteSize)
993+
{
994+
var type = GetType();
995+
if (!ValidateRpcMessageMetrics(type))
996+
{
997+
return;
998+
}
999+
if (__rpc_name_table[type].TryGetValue(rpcMethodId, out var rpcMethodName))
1000+
{
1001+
m_NetworkManager.NetworkMetrics.TrackRpcSent(
1002+
NetworkManager.ServerClientId,
1003+
m_NetworkObject,
1004+
rpcMethodName,
1005+
__getTypeName(),
1006+
rpcWriteSize);
1007+
}
1008+
}
1009+
1010+
internal void TrackRpcMetricsSend(ref RpcMessage message, int length)
1011+
{
1012+
var type = GetType();
1013+
if (!ValidateRpcMessageMetrics(type))
1014+
{
1015+
return;
1016+
}
1017+
if (__rpc_name_table[type].TryGetValue(message.Metadata.NetworkRpcMethodId, out var rpcMethodName))
1018+
{
1019+
m_NetworkManager.NetworkMetrics.TrackRpcSent(
1020+
m_NetworkManager.LocalClientId,
1021+
NetworkObject,
1022+
rpcMethodName,
1023+
__getTypeName(),
1024+
length);
1025+
}
1026+
}
1027+
1028+
internal void TrackRpcMetricsReceive(ref RpcMetadata metadata, ref NetworkContext context, int length)
1029+
{
1030+
var type = GetType();
1031+
if (!ValidateRpcMessageMetrics(type))
1032+
{
1033+
return;
1034+
}
1035+
if (__rpc_name_table[type].TryGetValue(metadata.NetworkRpcMethodId, out var rpcMethodName))
1036+
{
1037+
m_NetworkManager.NetworkMetrics.TrackRpcReceived(
1038+
context.SenderId,
1039+
NetworkObject,
1040+
rpcMethodName,
1041+
__getTypeName(),
1042+
length);
1043+
}
1044+
}
1045+
#endif
1046+
9721047
#pragma warning disable IDE1006 // disable naming rule violation check
9731048
// RuntimeAccessModifiersILPP will make this `protected`
9741049
// Using this method here because ILPP doesn't seem to let us do visibility modification on properties.

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class NetworkManager : MonoBehaviour, INetworkUpdateSystem
4949
// RuntimeAccessModifiersILPP will make this `public`
5050
internal static readonly Dictionary<uint, RpcReceiveHandler> __rpc_func_table = new Dictionary<uint, RpcReceiveHandler>();
5151

52-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
52+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
5353
// RuntimeAccessModifiersILPP will make this `public`
5454
internal static readonly Dictionary<uint, string> __rpc_name_table = new Dictionary<uint, string>();
5555
#endif

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/RpcMessages.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,8 @@ public static unsafe bool Deserialize(ref FastBufferReader reader, ref NetworkCo
4040
}
4141

4242
payload = new FastBufferReader(reader.GetUnsafePtrAtCurrentPosition(), Allocator.None, reader.Length - reader.Position);
43-
44-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
45-
if (NetworkBehaviour.__rpc_name_table[networkBehaviour.GetType()].TryGetValue(metadata.NetworkRpcMethodId, out var rpcMethodName))
46-
{
47-
networkManager.NetworkMetrics.TrackRpcReceived(
48-
context.SenderId,
49-
networkObject,
50-
rpcMethodName,
51-
networkBehaviour.__getTypeName(),
52-
reader.Length);
53-
}
43+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
44+
networkBehaviour.TrackRpcMetricsReceive(ref metadata, ref context, reader.Length);
5445
#endif
5546
return true;
5647
}

com.unity.netcode.gameobjects/Runtime/Messaging/RpcTargets/BaseRpcTarget.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,9 @@ protected void CheckLockBeforeDispose()
5252

5353
private protected void SendMessageToClient(NetworkBehaviour behaviour, ulong clientId, ref RpcMessage message, NetworkDelivery delivery)
5454
{
55-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
56-
var size =
57-
#endif
58-
behaviour.NetworkManager.MessageManager.SendMessage(ref message, delivery, clientId);
59-
60-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
61-
if (NetworkBehaviour.__rpc_name_table[behaviour.GetType()].TryGetValue(message.Metadata.NetworkRpcMethodId, out var rpcMethodName))
62-
{
63-
behaviour.NetworkManager.NetworkMetrics.TrackRpcSent(
64-
clientId,
65-
behaviour.NetworkObject,
66-
rpcMethodName,
67-
behaviour.__getTypeName(),
68-
size);
69-
}
55+
var size = behaviour.NetworkManager.MessageManager.SendMessage(ref message, delivery, clientId);
56+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
57+
behaviour.TrackRpcMetricsSend(ref message, size);
7058
#endif
7159
}
7260
}

com.unity.netcode.gameobjects/Runtime/Messaging/RpcTargets/LocalSendRpcTarget.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,8 @@ internal override void Send(NetworkBehaviour behaviour, ref RpcMessage message,
4646
message.Handle(ref context);
4747
length = tempBuffer.Length;
4848
}
49-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
50-
if (NetworkBehaviour.__rpc_name_table[behaviour.GetType()].TryGetValue(message.Metadata.NetworkRpcMethodId, out var rpcMethodName))
51-
{
52-
networkManager.NetworkMetrics.TrackRpcSent(
53-
networkManager.LocalClientId,
54-
behaviour.NetworkObject,
55-
rpcMethodName,
56-
behaviour.__getTypeName(),
57-
length);
58-
}
49+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
50+
behaviour.TrackRpcMetricsSend(ref message, length);
5951
#endif
6052
}
6153

com.unity.netcode.gameobjects/Runtime/Messaging/RpcTargets/ProxyRpcTargetGroup.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,11 @@ internal override void Send(NetworkBehaviour behaviour, ref RpcMessage message,
2323
return;
2424
}
2525
var proxyMessage = new ProxyMessage { Delivery = delivery, TargetClientIds = TargetClientIds.AsArray(), WrappedMessage = message };
26-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
27-
var size =
26+
var size = behaviour.NetworkManager.MessageManager.SendMessage(ref proxyMessage, delivery, NetworkManager.ServerClientId);
27+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
28+
behaviour.TrackRpcMetricsSend(ref message, size);
2829
#endif
29-
behaviour.NetworkManager.MessageManager.SendMessage(ref proxyMessage, delivery, NetworkManager.ServerClientId);
3030

31-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
32-
if (NetworkBehaviour.__rpc_name_table[behaviour.GetType()].TryGetValue(message.Metadata.NetworkRpcMethodId, out var rpcMethodName))
33-
{
34-
foreach (var clientId in TargetClientIds)
35-
{
36-
behaviour.NetworkManager.NetworkMetrics.TrackRpcSent(
37-
clientId,
38-
behaviour.NetworkObject,
39-
rpcMethodName,
40-
behaviour.__getTypeName(),
41-
size);
42-
}
43-
}
44-
#endif
4531
if (Ids.Contains(NetworkManager.ServerClientId))
4632
{
4733
m_ServerRpcTarget.Send(behaviour, ref message, delivery, rpcParams);

com.unity.netcode.gameobjects/Runtime/Messaging/RpcTargets/ServerRpcTarget.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,10 @@ internal override void Send(NetworkBehaviour behaviour, ref RpcMessage message,
4343
using var tempBuffer = new FastBufferReader(message.WriteBuffer, Allocator.None);
4444
message.ReadBuffer = tempBuffer;
4545
message.Handle(ref context);
46-
// If enabled, then add the RPC metrics for this
47-
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
48-
int length = tempBuffer.Length;
49-
if (NetworkBehaviour.__rpc_name_table[behaviour.GetType()].TryGetValue(message.Metadata.NetworkRpcMethodId, out var rpcMethodName))
50-
{
51-
m_NetworkManager.NetworkMetrics.TrackRpcSent(
52-
m_NetworkManager.LocalClientId,
53-
behaviour.NetworkObject,
54-
rpcMethodName,
55-
behaviour.__getTypeName(),
56-
length);
57-
}
46+
#if MULTIPLAYER_TOOLS && (DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE)
47+
behaviour.TrackRpcMetricsSend(ref message, tempBuffer.Length);
5848
#endif
49+
5950
}
6051
else // Otherwise, send a proxied message to the owner of the object
6152
{

0 commit comments

Comments
 (0)