@@ -10,7 +10,7 @@ namespace LiteNetLib
1010 /// <summary>
1111 /// More feature rich network manager with adjustable channels count
1212 /// </summary>
13- public class NetManager : LiteNetManager
13+ public class NetManager : LiteNetManager , IEnumerable < NetPeer >
1414 {
1515 private readonly INetEventListener _netEventListener ;
1616 private byte _channelsCount = 1 ;
@@ -30,6 +30,35 @@ public byte ChannelsCount
3030 }
3131 }
3232
33+ /// <summary>
34+ /// First peer. Useful for Client mode
35+ /// </summary>
36+ public new NetPeer FirstPeer => ( NetPeer ) _headPeer ;
37+
38+ /// <summary>
39+ /// Get copy of peers (without allocations)
40+ /// </summary>
41+ /// <param name="peers">List that will contain result</param>
42+ /// <param name="peerState">State of peers</param>
43+ public void GetPeers ( List < NetPeer > peers , ConnectionState peerState )
44+ {
45+ peers . Clear ( ) ;
46+ _peersLock . EnterReadLock ( ) ;
47+ for ( var netPeer = _headPeer ; netPeer != null ; netPeer = netPeer . NextPeer )
48+ {
49+ if ( ( netPeer . ConnectionState & peerState ) != 0 )
50+ peers . Add ( ( NetPeer ) netPeer ) ;
51+ }
52+ _peersLock . ExitReadLock ( ) ;
53+ }
54+
55+ /// <summary>
56+ /// Get copy of connected peers (without allocations)
57+ /// </summary>
58+ /// <param name="peers">List that will contain result</param>
59+ public void GetConnectedPeers ( List < NetPeer > peers ) =>
60+ GetPeers ( peers , ConnectionState . Connected ) ;
61+
3362 public NetManager ( INetEventListener listener , PacketLayerBase extraPacketLayer = null ) : base ( null , extraPacketLayer ) =>
3463 _netEventListener = listener ;
3564
@@ -249,8 +278,8 @@ public void SendToAll(byte[] data, int start, int length, byte channelNumber, De
249278 _peersLock . EnterReadLock ( ) ;
250279 for ( var netPeer = _headPeer ; netPeer != null ; netPeer = netPeer . NextPeer )
251280 {
252- if ( ! ReferenceEquals ( netPeer , excludePeer ) )
253- netPeer . Send ( data , start , length , channelNumber , options ) ;
281+ if ( netPeer != excludePeer )
282+ ( ( NetPeer ) netPeer ) . Send ( data , start , length , channelNumber , options ) ;
254283 }
255284 }
256285 finally
@@ -259,6 +288,28 @@ public void SendToAll(byte[] data, int start, int length, byte channelNumber, De
259288 }
260289 }
261290
291+ /// <summary>
292+ /// Send data to all connected peers
293+ /// </summary>
294+ /// <param name="data">Data</param>
295+ /// <param name="start">Start of data</param>
296+ /// <param name="length">Length of data</param>
297+ /// <param name="channelNumber">Number of channel (from 0 to channelsCount - 1)</param>
298+ /// <param name="options">Send options (reliable, unreliable, etc.)</param>
299+ public void SendToAll ( byte [ ] data , int start , int length , byte channelNumber , DeliveryMethod options )
300+ {
301+ try
302+ {
303+ _peersLock . EnterReadLock ( ) ;
304+ for ( var netPeer = _headPeer ; netPeer != null ; netPeer = netPeer . NextPeer )
305+ ( ( NetPeer ) netPeer ) . Send ( data , start , length , channelNumber , options ) ;
306+ }
307+ finally
308+ {
309+ _peersLock . ExitReadLock ( ) ;
310+ }
311+ }
312+
262313 /// <summary>
263314 /// Connect to remote host
264315 /// </summary>
@@ -310,5 +361,11 @@ public void SendToAll(byte[] data, int start, int length, byte channelNumber, De
310361 /// <exception cref="InvalidOperationException">Manager is not running. Call <see cref="LiteNetManager.Start()"/></exception>
311362 public new NetPeer Connect ( IPEndPoint target , ReadOnlySpan < byte > connectionData ) =>
312363 ( NetPeer ) base . Connect ( target , connectionData ) ;
364+
365+ public new NetPeerEnumerator < NetPeer > GetEnumerator ( ) =>
366+ new NetPeerEnumerator < NetPeer > ( ( NetPeer ) _headPeer ) ;
367+
368+ IEnumerator < NetPeer > IEnumerable < NetPeer > . GetEnumerator ( ) =>
369+ new NetPeerEnumerator < NetPeer > ( ( NetPeer ) _headPeer ) ;
313370 }
314371}
0 commit comments