Skip to content

Commit 0db3230

Browse files
committed
Removed allocations in observer-aware send methods
1 parent cce06fc commit 0db3230

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Send.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,19 @@ internal static void Send(List<uint> clientIds, string messageType, string chann
224224
}
225225
}
226226

227+
private static List<uint> failedObservers = new List<uint>();
227228
//RETURNS THE CLIENTIDS WHICH WAS NOT BEING OBSERVED
228-
internal static List<uint> Send(string messageType, string channelName, byte[] data, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
229+
internal static ref List<uint> Send(string messageType, string channelName, byte[] data, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
229230
{
231+
failedObservers.Clear();
230232
if (netManager.connectedClients.Count == 0)
231-
return null;
233+
return ref failedObservers;
232234
if (netManager.NetworkConfig.EncryptedChannels.Contains(channelName))
233235
{
234236
Debug.LogWarning("MLAPI: Cannot send messages over encrypted channel to multiple clients.");
235-
return null;
237+
return ref failedObservers;
236238
}
237239

238-
List<uint> nonObservedIds = new List<uint>();
239-
240240
using (BitWriter writer = new BitWriter())
241241
{
242242
writer.WriteUShort(MessageManager.messageTypes[messageType]);
@@ -272,7 +272,7 @@ internal static List<uint> Send(string messageType, string channelName, byte[] d
272272
//If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue
273273
if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(pair.Key))
274274
{
275-
nonObservedIds.Add(pair.Key);
275+
failedObservers.Add(pair.Key);
276276
continue;
277277
}
278278

@@ -281,21 +281,20 @@ internal static List<uint> Send(string messageType, string channelName, byte[] d
281281
byte error;
282282
netManager.NetworkConfig.NetworkTransport.QueueMessageForSending(targetClientId, ref FinalMessageBuffer, (int)writer.GetFinalizeSize(), channel, false, out error);
283283
}
284-
return nonObservedIds;
284+
return ref failedObservers;
285285
}
286286
}
287287

288288
//RETURNS THE CLIENTIDS WHICH WAS NOT BEING OBSERVED
289-
internal static List<uint> Send(string messageType, string channelName, byte[] data, uint clientIdToIgnore, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
289+
internal static ref List<uint> Send(string messageType, string channelName, byte[] data, uint clientIdToIgnore, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
290290
{
291+
failedObservers.Clear();
291292
if (netManager.NetworkConfig.EncryptedChannels.Contains(channelName))
292293
{
293294
Debug.LogWarning("MLAPI: Cannot send messages over encrypted channel to multiple clients.");
294-
return null;
295+
return ref failedObservers;
295296
}
296297

297-
List<uint> nonObservedIds = new List<uint>();
298-
299298
using (BitWriter writer = new BitWriter())
300299
{
301300
writer.WriteUShort(MessageManager.messageTypes[messageType]);
@@ -334,7 +333,7 @@ internal static List<uint> Send(string messageType, string channelName, byte[] d
334333
//If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue
335334
if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(pair.Key))
336335
{
337-
nonObservedIds.Add(pair.Key);
336+
failedObservers.Add(pair.Key);
338337
continue;
339338
}
340339

@@ -343,7 +342,7 @@ internal static List<uint> Send(string messageType, string channelName, byte[] d
343342
byte error;
344343
netManager.NetworkConfig.NetworkTransport.QueueMessageForSending(targetClientId, ref FinalMessageBuffer, (int)writer.GetFinalizeSize(), channel, false, out error);
345344
}
346-
return nonObservedIds;
345+
return ref failedObservers;
347346
}
348347
}
349348
}

0 commit comments

Comments
 (0)