Skip to content

Commit 8e31242

Browse files
committed
perf: Bulk serialized client rpcs
1 parent 42e661f commit 8e31242

File tree

2 files changed

+57
-49
lines changed

2 files changed

+57
-49
lines changed

MLAPI/Core/NetworkedBehaviour.cs

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,48 +1258,20 @@ internal void SendClientRPCPerformance(ulong hash, List<ulong> clientIds, Strea
12581258

12591259
stream.CopyFrom(messageStream);
12601260

1261-
if (clientIds == null)
1261+
if (IsHost)
12621262
{
1263-
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
1263+
if (this.NetworkedObject.observers.Contains(NetworkingManager.Singleton.LocalClientId))
12641264
{
1265-
if (!this.NetworkedObject.observers.Contains(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId))
1266-
{
1267-
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) NetworkLog.LogWarning("Silently suppressed ClientRPC because a target in the bulk list was not an observer");
1268-
continue;
1269-
}
1270-
1271-
if (IsHost && NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == NetworkingManager.Singleton.LocalClientId)
1272-
{
1273-
messageStream.Position = 0;
1274-
InvokeClientRPCLocal(hash, NetworkingManager.Singleton.LocalClientId, messageStream);
1275-
}
1276-
else
1277-
{
1278-
InternalMessageSender.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null);
1279-
}
1265+
messageStream.Position = 0;
1266+
InvokeClientRPCLocal(hash, NetworkingManager.Singleton.LocalClientId, messageStream);
12801267
}
1281-
}
1282-
else
1283-
{
1284-
for (int i = 0; i < clientIds.Count; i++)
1268+
else
12851269
{
1286-
if (!this.NetworkedObject.observers.Contains(clientIds[i]))
1287-
{
1288-
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) NetworkLog.LogWarning("Cannot send ClientRPC to client without visibility to the object");
1289-
continue;
1290-
}
1291-
1292-
if (IsHost && clientIds[i] == NetworkingManager.Singleton.LocalClientId)
1293-
{
1294-
messageStream.Position = 0;
1295-
InvokeClientRPCLocal(hash, NetworkingManager.Singleton.LocalClientId, messageStream);
1296-
}
1297-
else
1298-
{
1299-
InternalMessageSender.Send(clientIds[i], MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null);
1300-
}
1270+
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) NetworkLog.LogWarning("Silently suppressed ClientRPC because a connected client was not an observer");
13011271
}
13021272
}
1273+
1274+
InternalMessageSender.Send(MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, clientIds, stream, security, this.NetworkedObject);
13031275
}
13041276
}
13051277
}
@@ -1324,28 +1296,20 @@ internal void SendClientRPCPerformance(ulong hash, Stream messageStream, ulong c
13241296
stream.CopyFrom(messageStream);
13251297

13261298

1327-
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
1299+
if (IsHost && NetworkingManager.Singleton.LocalClientId != clientIdToIgnore)
13281300
{
1329-
if (NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == clientIdToIgnore)
1330-
continue;
1331-
1332-
if (!this.NetworkedObject.observers.Contains(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId))
1333-
{
1334-
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) NetworkLog.LogWarning("Silently suppressed ClientRPC because a connected client was not an observer");
1335-
continue;
1336-
}
1337-
1338-
1339-
if (IsHost && NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == NetworkingManager.Singleton.LocalClientId)
1301+
if (this.NetworkedObject.observers.Contains(NetworkingManager.Singleton.LocalClientId))
13401302
{
13411303
messageStream.Position = 0;
13421304
InvokeClientRPCLocal(hash, NetworkingManager.Singleton.LocalClientId, messageStream);
13431305
}
13441306
else
13451307
{
1346-
InternalMessageSender.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null);
1308+
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) NetworkLog.LogWarning("Silently suppressed ClientRPC because a connected client was not an observer");
13471309
}
13481310
}
1311+
1312+
InternalMessageSender.Send(MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, clientIdToIgnore, stream, security, this.NetworkedObject);
13491313
}
13501314
}
13511315
}

MLAPI/Messaging/InternalMessageSender.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using MLAPI.Configuration;
34
using MLAPI.Internal;
45
using MLAPI.Logging;
@@ -70,6 +71,49 @@ internal static void Send(byte messageType, string channelName, BitStream messag
7071
}
7172
}
7273

74+
internal static void Send(byte messageType, string channelName, List<ulong> clientIds, BitStream messageStream, SecuritySendFlags flags, NetworkedObject targetObject)
75+
{
76+
if (clientIds == null)
77+
{
78+
Send(messageType, channelName, messageStream, flags, targetObject);
79+
return;
80+
}
81+
82+
bool encrypted = ((flags & SecuritySendFlags.Encrypted) == SecuritySendFlags.Encrypted) && NetworkingManager.Singleton.NetworkConfig.EnableEncryption;
83+
bool authenticated = ((flags & SecuritySendFlags.Authenticated) == SecuritySendFlags.Authenticated) && NetworkingManager.Singleton.NetworkConfig.EnableEncryption;
84+
85+
if (authenticated || encrypted)
86+
{
87+
for (int i = 0; i < clientIds.Count; i++)
88+
{
89+
Send(clientIds[i], messageType, channelName, messageStream, flags, targetObject);
90+
}
91+
}
92+
else
93+
{
94+
messageStream.PadStream();
95+
96+
using (BitStream stream = MessagePacker.WrapMessage(messageType, 0, messageStream, flags))
97+
{
98+
NetworkProfiler.StartEvent(TickType.Send, (uint)stream.Length, channelName, MLAPIConstants.MESSAGE_NAMES[messageType]);
99+
for (int i = 0; i < clientIds.Count; i++)
100+
{
101+
if (NetworkingManager.Singleton.IsServer && clientIds[i] == NetworkingManager.Singleton.ServerClientId)
102+
continue;
103+
104+
if (targetObject != null && !targetObject.observers.Contains(clientIds[i]))
105+
{
106+
if (LogHelper.CurrentLogLevel <= LogLevel.Developer) LogHelper.LogWarning("Silently suppressed send(all) call because it was directed to an object without visibility");
107+
continue;
108+
}
109+
110+
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(clientIds[i], new ArraySegment<byte>(stream.GetBuffer(), 0, (int)stream.Length), channelName);
111+
}
112+
NetworkProfiler.EndEvent();
113+
}
114+
}
115+
}
116+
73117
internal static void Send(byte messageType, string channelName, ulong clientIdToIgnore, BitStream messageStream, SecuritySendFlags flags, NetworkedObject targetObject)
74118
{
75119
bool encrypted = ((flags & SecuritySendFlags.Encrypted) == SecuritySendFlags.Encrypted) && NetworkingManager.Singleton.NetworkConfig.EnableEncryption;

0 commit comments

Comments
 (0)