diff --git a/source/Runtime/Server/SimpleWebServer.cs b/source/Runtime/Server/SimpleWebServer.cs index 8201367..a626fd0 100644 --- a/source/Runtime/Server/SimpleWebServer.cs +++ b/source/Runtime/Server/SimpleWebServer.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace JamesFrowen.SimpleWeb @@ -38,6 +39,11 @@ public void Stop() Active = false; } + /// + /// Sends to a list of connections, use version to avoid foreach allocation + /// + /// + /// public void SendAll(List connectionIds, ArraySegment source) { ArrayBuffer buffer = bufferPool.Take(source.Count); @@ -49,6 +55,38 @@ public void SendAll(List connectionIds, ArraySegment source) server.Send(id, buffer); } + /// + /// Sends to a list of connections, use version when you are using a non-list collection (will allocate in foreach) + /// + /// + /// + public void SendAll(ICollection connectionIds, ArraySegment source) + { + ArrayBuffer buffer = bufferPool.Take(source.Count); + buffer.CopyFrom(source); + buffer.SetReleasesRequired(connectionIds.Count); + + // make copy of array before for each, data sent to each client is the same + foreach (int id in connectionIds) + server.Send(id, buffer); + } + + /// + /// Sends to a list of connections, use version in cases where you want to use LINQ to get connections (will allocate from LINQ functions and foreach) + /// + /// + /// + public void SendAll(IEnumerable connectionIds, ArraySegment source) + { + ArrayBuffer buffer = bufferPool.Take(source.Count); + buffer.CopyFrom(source); + buffer.SetReleasesRequired(connectionIds.Count()); + + // make copy of array before for each, data sent to each client is the same + foreach (int id in connectionIds) + server.Send(id, buffer); + } + public void SendOne(int connectionId, ArraySegment source) { ArrayBuffer buffer = bufferPool.Take(source.Count);