namespace Sandbox.Network; internal partial class NetworkSystem { /// /// Send a message to all connections. You can optionally pass in a filter to determine who actually receives the message. /// /// The message to send. /// The minumum state the connection must be to receive the message. /// If specified, the connection must pass this filter to receive the message. /// Network message flags that may dictate how the message is sent. public void Broadcast( ByteStream msg, Connection.ChannelState minimumState = Connection.ChannelState.Snapshot, Connection.Filter? filter = null, NetFlags flags = NetFlags.Reliable ) { IEnumerable availableConnections = Networking.IsHost ? _connections : Connection.All; if ( Connection is not null ) { availableConnections = availableConnections .Append( Connection ) .Distinct(); } foreach ( var c in availableConnections ) { if ( c == Connection.Local ) continue; if ( c.State < minimumState ) continue; if ( filter.HasValue && !filter.Value.IsRecipient( c ) ) continue; c.SendRawMessage( msg, flags ); } } /// /// Broadcast a packed message to all connections. /// internal void Broadcast( T msg, Connection.ChannelState minimumState = Connection.ChannelState.Snapshot, Connection.Filter? filter = null, NetFlags flags = NetFlags.Reliable ) { var bs = ByteStream.Create( 32 ); bs.Write( InternalMessageType.Packed ); TypeLibrary.ToBytes( msg, ref bs ); Broadcast( bs, minimumState ); bs.Dispose(); } /// /// Get a list of connections that meet a specific criteria. /// /// The minumum state the connection must be to receive the message. /// If specified, the connection must pass this filter to receive the message. public IEnumerable GetFilteredConnections( Connection.ChannelState minimumState = Connection.ChannelState.Snapshot, Connection.Filter? filter = null ) { IEnumerable availableConnections = Networking.IsHost ? _connections : Connection.All; if ( Connection is not null ) { availableConnections = availableConnections .Append( Connection ) .Distinct(); } foreach ( var c in availableConnections ) { if ( c == Connection.Local ) continue; if ( c.State < minimumState ) continue; if ( filter.HasValue && !filter.Value.IsRecipient( c ) ) continue; yield return c; } } }