Class NetManager
Main class for all network operations. Can be used as client and/or server.
Inheritance
Namespace: LiteNetLib
Assembly: LiteNetLib.dll
Syntax
public class NetManager : INetSocketListener, IEnumerable<NetPeer>, IEnumerable
Constructors
NetManager(INetEventListener, PacketLayerBase)
NetManager constructor
Declaration
public NetManager(INetEventListener listener, PacketLayerBase extraPacketLayer = null)
Parameters
Type | Name | Description |
---|---|---|
INetEventListener | listener | Network events listener (also can implement IDeliveryEventListener) |
PacketLayerBase | extraPacketLayer | Extra processing of packages, like CRC checksum or encryption. All connected NetManagers must have same layer. |
Fields
AutoRecycle
Automatically recycle NetPacketReader after OnReceive event
Declaration
public bool AutoRecycle
Field Value
Type | Description |
---|---|
System.Boolean |
BroadcastReceiveEnabled
Allows receive broadcast packets
Declaration
public bool BroadcastReceiveEnabled
Field Value
Type | Description |
---|---|
System.Boolean |
DisconnectTimeout
If NetManager doesn't receive any packet from remote peer during this time then connection will be closed (including library internal keepalive packets)
Declaration
public int DisconnectTimeout
Field Value
Type | Description |
---|---|
System.Int32 |
EnableStatistics
Toggles the collection of network statistics for the instance and all known peers
Declaration
public bool EnableStatistics
Field Value
Type | Description |
---|---|
System.Boolean |
IPv6Enabled
IPv6 support
Declaration
public IPv6Mode IPv6Enabled
Field Value
Type | Description |
---|---|
IPv6Mode |
MaxConnectAttempts
Maximum connection attempts before client stops and call disconnect event.
Declaration
public int MaxConnectAttempts
Field Value
Type | Description |
---|---|
System.Int32 |
NatPunchEnabled
Enable nat punch messages
Declaration
public bool NatPunchEnabled
Field Value
Type | Description |
---|---|
System.Boolean |
NatPunchModule
NatPunchModule for NAT hole punching operations
Declaration
public readonly NatPunchModule NatPunchModule
Field Value
Type | Description |
---|---|
NatPunchModule |
PingInterval
Interval for latency detection and checking connection
Declaration
public int PingInterval
Field Value
Type | Description |
---|---|
System.Int32 |
ReconnectDelay
Delay between initial connection attempts
Declaration
public int ReconnectDelay
Field Value
Type | Description |
---|---|
System.Int32 |
ReuseAddress
Enables socket option "ReuseAddress" for specific purposes
Declaration
public bool ReuseAddress
Field Value
Type | Description |
---|---|
System.Boolean |
SimulateLatency
Simulate latency by holding packets for random time. (Works only in DEBUG mode)
Declaration
public bool SimulateLatency
Field Value
Type | Description |
---|---|
System.Boolean |
SimulatePacketLoss
Simulate packet loss by dropping random amount of packets. (Works only in DEBUG mode)
Declaration
public bool SimulatePacketLoss
Field Value
Type | Description |
---|---|
System.Boolean |
SimulationMaxLatency
Maximum simulated latency
Declaration
public int SimulationMaxLatency
Field Value
Type | Description |
---|---|
System.Int32 |
SimulationMinLatency
Minimum simulated latency
Declaration
public int SimulationMinLatency
Field Value
Type | Description |
---|---|
System.Int32 |
SimulationPacketLossChance
Chance of packet loss when simulation enabled. value in percents (1 - 100).
Declaration
public int SimulationPacketLossChance
Field Value
Type | Description |
---|---|
System.Int32 |
Statistics
Statistics of all connections
Declaration
public readonly NetStatistics Statistics
Field Value
Type | Description |
---|---|
NetStatistics |
UnconnectedMessagesEnabled
Enable messages receiving without connection. (with SendUnconnectedMessage method)
Declaration
public bool UnconnectedMessagesEnabled
Field Value
Type | Description |
---|---|
System.Boolean |
UnsyncedDeliveryEvent
If true - delivery event will be called from "receive" thread otherwise on PollEvents call
Declaration
public bool UnsyncedDeliveryEvent
Field Value
Type | Description |
---|---|
System.Boolean |
UnsyncedEvents
Experimental feature. Events automatically will be called without PollEvents method from another thread
Declaration
public bool UnsyncedEvents
Field Value
Type | Description |
---|---|
System.Boolean |
UpdateTime
Library logic update and send period in milliseconds
Declaration
public int UpdateTime
Field Value
Type | Description |
---|---|
System.Int32 |
Properties
ChannelsCount
QoS channel count per message type (value must be between 1 and 64 channels)
Declaration
public byte ChannelsCount { get; set; }
Property Value
Type | Description |
---|---|
System.Byte |
ConnectedPeerList
Returns connected peers list (with internal cached list)
Declaration
public List<NetPeer> ConnectedPeerList { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.List<NetPeer> |
ConnectedPeersCount
Returns connected peers count
Declaration
public int ConnectedPeersCount { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
ExtraPacketSizeForLayer
Declaration
public int ExtraPacketSizeForLayer { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
FirstPeer
First peer. Useful for Client mode
Declaration
public NetPeer FirstPeer { get; }
Property Value
Type | Description |
---|---|
NetPeer |
IsRunning
Returns true if socket listening and update thread is running
Declaration
public bool IsRunning { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
LocalPort
Local EndPoint (host and port)
Declaration
public int LocalPort { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Methods
Connect(IPEndPoint, NetDataWriter)
Connect to remote host
Declaration
public NetPeer Connect(IPEndPoint target, NetDataWriter connectionData)
Parameters
Type | Name | Description |
---|---|---|
System.Net.IPEndPoint | target | Server end point (ip and port) |
NetDataWriter | connectionData | Additional data for remote peer |
Returns
Type | Description |
---|---|
NetPeer | New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting |
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Manager is not running. Call Start() |
Connect(IPEndPoint, String)
Connect to remote host
Declaration
public NetPeer Connect(IPEndPoint target, string key)
Parameters
Type | Name | Description |
---|---|---|
System.Net.IPEndPoint | target | Server end point (ip and port) |
System.String | key | Connection key |
Returns
Type | Description |
---|---|
NetPeer | New NetPeer if new connection, Old NetPeer if already connected |
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Manager is not running. Call Start() |
Connect(String, Int32, NetDataWriter)
Connect to remote host
Declaration
public NetPeer Connect(string address, int port, NetDataWriter connectionData)
Parameters
Type | Name | Description |
---|---|---|
System.String | address | Server IP or hostname |
System.Int32 | port | Server Port |
NetDataWriter | connectionData | Additional data for remote peer |
Returns
Type | Description |
---|---|
NetPeer | New NetPeer if new connection, Old NetPeer if already connected |
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Manager is not running. Call Start() |
Connect(String, Int32, String)
Connect to remote host
Declaration
public NetPeer Connect(string address, int port, string key)
Parameters
Type | Name | Description |
---|---|---|
System.String | address | Server IP or hostname |
System.Int32 | port | Server Port |
System.String | key | Connection key |
Returns
Type | Description |
---|---|
NetPeer | New NetPeer if new connection, Old NetPeer if already connected |
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Manager is not running. Call Start() |
DisconnectAll()
Disconnect all peers without any additional data
Declaration
public void DisconnectAll()
DisconnectAll(Byte[], Int32, Int32)
Disconnect all peers with shutdown message
Declaration
public void DisconnectAll(byte[] data, int start, int count)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data to send (must be less or equal MTU) |
System.Int32 | start | Data start |
System.Int32 | count | Data count |
DisconnectPeer(NetPeer)
Disconnect peer from server
Declaration
public void DisconnectPeer(NetPeer peer)
Parameters
Type | Name | Description |
---|---|---|
NetPeer | peer | peer to disconnect |
DisconnectPeer(NetPeer, NetDataWriter)
Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
Declaration
public void DisconnectPeer(NetPeer peer, NetDataWriter writer)
Parameters
Type | Name | Description |
---|---|---|
NetPeer | peer | peer to disconnect |
NetDataWriter | writer | additional data |
DisconnectPeer(NetPeer, Byte[])
Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
Declaration
public void DisconnectPeer(NetPeer peer, byte[] data)
Parameters
Type | Name | Description |
---|---|---|
NetPeer | peer | peer to disconnect |
System.Byte[] | data | additional data |
DisconnectPeer(NetPeer, Byte[], Int32, Int32)
Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
Declaration
public void DisconnectPeer(NetPeer peer, byte[] data, int start, int count)
Parameters
Type | Name | Description |
---|---|---|
NetPeer | peer | peer to disconnect |
System.Byte[] | data | additional data |
System.Int32 | start | data start |
System.Int32 | count | data length |
DisconnectPeerForce(NetPeer)
Immediately disconnect peer from server without additional data
Declaration
public void DisconnectPeerForce(NetPeer peer)
Parameters
Type | Name | Description |
---|---|---|
NetPeer | peer | peer to disconnect |
Flush()
Flush all queued packets of all peers
Declaration
public void Flush()
GetEnumerator()
Declaration
public NetManager.NetPeerEnumerator GetEnumerator()
Returns
Type | Description |
---|---|
NetManager.NetPeerEnumerator |
GetPeerById(Int32)
Gets peer by peer id
Declaration
public NetPeer GetPeerById(int id)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | id | id of peer |
Returns
Type | Description |
---|---|
NetPeer | Peer if peer with id exist, otherwise null |
GetPeersCount(ConnectionState)
Return peers count with connection state
Declaration
public int GetPeersCount(ConnectionState peerState)
Parameters
Type | Name | Description |
---|---|---|
ConnectionState | peerState | peer connection state (you can use as bit flags) |
Returns
Type | Description |
---|---|
System.Int32 | peers count |
GetPeersNonAlloc(List<NetPeer>, ConnectionState)
Get copy of peers (without allocations)
Declaration
public void GetPeersNonAlloc(List<NetPeer> peers, ConnectionState peerState)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.List<NetPeer> | peers | List that will contain result |
ConnectionState | peerState | State of peers |
PollEvents()
Receive all pending events. Call this in game update code
Declaration
public void PollEvents()
SendBroadcast(NetDataWriter, Int32)
Declaration
public bool SendBroadcast(NetDataWriter writer, int port)
Parameters
Type | Name | Description |
---|---|---|
NetDataWriter | writer | |
System.Int32 | port |
Returns
Type | Description |
---|---|
System.Boolean |
SendBroadcast(Byte[], Int32)
Declaration
public bool SendBroadcast(byte[] data, int port)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | |
System.Int32 | port |
Returns
Type | Description |
---|---|
System.Boolean |
SendBroadcast(Byte[], Int32, Int32, Int32)
Declaration
public bool SendBroadcast(byte[] data, int start, int length, int port)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | |
System.Int32 | start | |
System.Int32 | length | |
System.Int32 | port |
Returns
Type | Description |
---|---|
System.Boolean |
SendToAll(NetDataWriter, DeliveryMethod)
Send data to all connected peers (channel - 0)
Declaration
public void SendToAll(NetDataWriter writer, DeliveryMethod options)
Parameters
Type | Name | Description |
---|---|---|
NetDataWriter | writer | DataWriter with data |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
SendToAll(NetDataWriter, DeliveryMethod, NetPeer)
Send data to all connected peers (channel - 0)
Declaration
public void SendToAll(NetDataWriter writer, DeliveryMethod options, NetPeer excludePeer)
Parameters
Type | Name | Description |
---|---|---|
NetDataWriter | writer | DataWriter with data |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
NetPeer | excludePeer | Excluded peer |
SendToAll(NetDataWriter, Byte, DeliveryMethod)
Send data to all connected peers
Declaration
public void SendToAll(NetDataWriter writer, byte channelNumber, DeliveryMethod options)
Parameters
Type | Name | Description |
---|---|---|
NetDataWriter | writer | DataWriter with data |
System.Byte | channelNumber | Number of channel (from 0 to channelsCount - 1) |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
SendToAll(NetDataWriter, Byte, DeliveryMethod, NetPeer)
Send data to all connected peers
Declaration
public void SendToAll(NetDataWriter writer, byte channelNumber, DeliveryMethod options, NetPeer excludePeer)
Parameters
Type | Name | Description |
---|---|---|
NetDataWriter | writer | DataWriter with data |
System.Byte | channelNumber | Number of channel (from 0 to channelsCount - 1) |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
NetPeer | excludePeer | Excluded peer |
SendToAll(Byte[], DeliveryMethod)
Send data to all connected peers (channel - 0)
Declaration
public void SendToAll(byte[] data, DeliveryMethod options)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
SendToAll(Byte[], DeliveryMethod, NetPeer)
Send data to all connected peers (channel - 0)
Declaration
public void SendToAll(byte[] data, DeliveryMethod options, NetPeer excludePeer)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
NetPeer | excludePeer | Excluded peer |
SendToAll(Byte[], Byte, DeliveryMethod)
Send data to all connected peers
Declaration
public void SendToAll(byte[] data, byte channelNumber, DeliveryMethod options)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data |
System.Byte | channelNumber | Number of channel (from 0 to channelsCount - 1) |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
SendToAll(Byte[], Byte, DeliveryMethod, NetPeer)
Send data to all connected peers
Declaration
public void SendToAll(byte[] data, byte channelNumber, DeliveryMethod options, NetPeer excludePeer)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data |
System.Byte | channelNumber | Number of channel (from 0 to channelsCount - 1) |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
NetPeer | excludePeer | Excluded peer |
SendToAll(Byte[], Int32, Int32, DeliveryMethod)
Send data to all connected peers (channel - 0)
Declaration
public void SendToAll(byte[] data, int start, int length, DeliveryMethod options)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data |
System.Int32 | start | Start of data |
System.Int32 | length | Length of data |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
SendToAll(Byte[], Int32, Int32, DeliveryMethod, NetPeer)
Send data to all connected peers (channel - 0)
Declaration
public void SendToAll(byte[] data, int start, int length, DeliveryMethod options, NetPeer excludePeer)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data |
System.Int32 | start | Start of data |
System.Int32 | length | Length of data |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
NetPeer | excludePeer | Excluded peer |
SendToAll(Byte[], Int32, Int32, Byte, DeliveryMethod)
Send data to all connected peers
Declaration
public void SendToAll(byte[] data, int start, int length, byte channelNumber, DeliveryMethod options)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data |
System.Int32 | start | Start of data |
System.Int32 | length | Length of data |
System.Byte | channelNumber | Number of channel (from 0 to channelsCount - 1) |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
SendToAll(Byte[], Int32, Int32, Byte, DeliveryMethod, NetPeer)
Send data to all connected peers
Declaration
public void SendToAll(byte[] data, int start, int length, byte channelNumber, DeliveryMethod options, NetPeer excludePeer)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data |
System.Int32 | start | Start of data |
System.Int32 | length | Length of data |
System.Byte | channelNumber | Number of channel (from 0 to channelsCount - 1) |
DeliveryMethod | options | Send options (reliable, unreliable, etc.) |
NetPeer | excludePeer | Excluded peer |
SendUnconnectedMessage(NetDataWriter, IPEndPoint)
Send message without connection
Declaration
public bool SendUnconnectedMessage(NetDataWriter writer, IPEndPoint remoteEndPoint)
Parameters
Type | Name | Description |
---|---|---|
NetDataWriter | writer | Data serializer |
System.Net.IPEndPoint | remoteEndPoint | Packet destination |
Returns
Type | Description |
---|---|
System.Boolean | Operation result |
SendUnconnectedMessage(Byte[], Int32, Int32, IPEndPoint)
Send message without connection
Declaration
public bool SendUnconnectedMessage(byte[] message, int start, int length, IPEndPoint remoteEndPoint)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | message | Raw data |
System.Int32 | start | data start |
System.Int32 | length | data length |
System.Net.IPEndPoint | remoteEndPoint | Packet destination |
Returns
Type | Description |
---|---|
System.Boolean | Operation result |
SendUnconnectedMessage(Byte[], IPEndPoint)
Send message without connection
Declaration
public bool SendUnconnectedMessage(byte[] message, IPEndPoint remoteEndPoint)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | message | Raw data |
System.Net.IPEndPoint | remoteEndPoint | Packet destination |
Returns
Type | Description |
---|---|
System.Boolean | Operation result |
Start()
Start logic thread and listening on available port
Declaration
public bool Start()
Returns
Type | Description |
---|---|
System.Boolean |
Start(Int32)
Start logic thread and listening on selected port
Declaration
public bool Start(int port)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | port | port to listen |
Returns
Type | Description |
---|---|
System.Boolean |
Start(IPAddress, IPAddress, Int32)
Start logic thread and listening on selected port
Declaration
public bool Start(IPAddress addressIPv4, IPAddress addressIPv6, int port)
Parameters
Type | Name | Description |
---|---|---|
System.Net.IPAddress | addressIPv4 | bind to specific ipv4 address |
System.Net.IPAddress | addressIPv6 | bind to specific ipv6 address |
System.Int32 | port | port to listen |
Returns
Type | Description |
---|---|
System.Boolean |
Start(String, String, Int32)
Start logic thread and listening on selected port
Declaration
public bool Start(string addressIPv4, string addressIPv6, int port)
Parameters
Type | Name | Description |
---|---|---|
System.String | addressIPv4 | bind to specific ipv4 address |
System.String | addressIPv6 | bind to specific ipv6 address |
System.Int32 | port | port to listen |
Returns
Type | Description |
---|---|
System.Boolean |
Stop()
Force closes connection and stop all threads.
Declaration
public void Stop()
Stop(Boolean)
Force closes connection and stop all threads.
Declaration
public void Stop(bool sendDisconnectMessages)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | sendDisconnectMessages | Send disconnect messages |
Explicit Interface Implementations
IEnumerable<NetPeer>.GetEnumerator()
Declaration
IEnumerator<NetPeer> IEnumerable<NetPeer>.GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerator<NetPeer> |
IEnumerable.GetEnumerator()
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.IEnumerator |