My Project
Loading...
Searching...
No Matches
Classes | Public Member Functions | Public Attributes | Properties | List of all members
LiteNetLib.NetManager Class Reference

Main class for all network operations. Can be used as client and/or server. More...

Inheritance diagram for LiteNetLib.NetManager:

Classes

struct  NetPeerEnumerator
 

Public Member Functions

NetPeer GetPeerById (int id)
 Gets peer by peer id.
 
 NetManager (INetEventListener listener, PacketLayerBase extraPacketLayer=null)
 NetManager constructor.
 
void ManualUpdate (int elapsedMilliseconds)
 Update and send logic. Use this only when NetManager started in manual mode.
 
void ManualReceive ()
 Receive logic. It will call Receive events immediately without need to use PollEvents or UnsyncedEvents Use this only when NetManager started in manual mode.
 
void SendToAll (NetDataWriter writer, DeliveryMethod options)
 Send data to all connected peers (channel - 0)
 
void SendToAll (byte[] data, DeliveryMethod options)
 Send data to all connected peers (channel - 0)
 
void SendToAll (byte[] data, int start, int length, DeliveryMethod options)
 Send data to all connected peers (channel - 0)
 
void SendToAll (NetDataWriter writer, byte channelNumber, DeliveryMethod options)
 Send data to all connected peers.
 
void SendToAll (byte[] data, byte channelNumber, DeliveryMethod options)
 Send data to all connected peers.
 
void SendToAll (byte[] data, int start, int length, byte channelNumber, DeliveryMethod options)
 Send data to all connected peers.
 
void SendToAll (NetDataWriter writer, DeliveryMethod options, NetPeer excludePeer)
 Send data to all connected peers (channel - 0)
 
void SendToAll (byte[] data, DeliveryMethod options, NetPeer excludePeer)
 Send data to all connected peers (channel - 0)
 
void SendToAll (byte[] data, int start, int length, DeliveryMethod options, NetPeer excludePeer)
 Send data to all connected peers (channel - 0)
 
void SendToAll (NetDataWriter writer, byte channelNumber, DeliveryMethod options, NetPeer excludePeer)
 Send data to all connected peers.
 
void SendToAll (byte[] data, byte channelNumber, DeliveryMethod options, NetPeer excludePeer)
 Send data to all connected peers.
 
void SendToAll (byte[] data, int start, int length, byte channelNumber, DeliveryMethod options, NetPeer excludePeer)
 Send data to all connected peers.
 
bool Start ()
 Start logic thread and listening on available port.
 
bool Start (IPAddress addressIPv4, IPAddress addressIPv6, int port)
 Start logic thread and listening on selected port.
 
bool Start (string addressIPv4, string addressIPv6, int port)
 Start logic thread and listening on selected port.
 
bool Start (int port)
 Start logic thread and listening on selected port.
 
bool StartInManualMode (IPAddress addressIPv4, IPAddress addressIPv6, int port)
 Start in manual mode and listening on selected port In this mode you should use ManualReceive (without PollEvents) for receive packets and ManualUpdate(...) for update and send packets This mode useful mostly for single-threaded servers.
 
bool StartInManualMode (string addressIPv4, string addressIPv6, int port)
 Start in manual mode and listening on selected port In this mode you should use ManualReceive (without PollEvents) for receive packets and ManualUpdate(...) for update and send packets This mode useful mostly for single-threaded servers.
 
bool StartInManualMode (int port)
 Start in manual mode and listening on selected port In this mode you should use ManualReceive (without PollEvents) for receive packets and ManualUpdate(...) for update and send packets This mode useful mostly for single-threaded servers.
 
bool SendUnconnectedMessage (byte[] message, IPEndPoint remoteEndPoint)
 Send message without connection.
 
bool SendUnconnectedMessage (NetDataWriter writer, IPEndPoint remoteEndPoint)
 Send message without connection.
 
bool SendUnconnectedMessage (byte[] message, int start, int length, IPEndPoint remoteEndPoint)
 Send message without connection.
 
bool SendBroadcast (NetDataWriter writer, int port)
 
bool SendBroadcast (byte[] data, int port)
 
bool SendBroadcast (byte[] data, int start, int length, int port)
 
void TriggerUpdate ()
 Triggers update and send logic immediately (works asynchronously)
 
void PollEvents ()
 Receive all pending events. Call this in game update code.
 
NetPeer Connect (string address, int port, string key)
 Connect to remote host.
 
NetPeer Connect (string address, int port, NetDataWriter connectionData)
 Connect to remote host.
 
NetPeer Connect (IPEndPoint target, string key)
 Connect to remote host.
 
NetPeer Connect (IPEndPoint target, NetDataWriter connectionData)
 Connect to remote host.
 
void Stop ()
 Force closes connection and stop all threads.
 
void Stop (bool sendDisconnectMessages)
 Force closes connection and stop all threads.
 
int GetPeersCount (ConnectionState peerState)
 Return peers count with connection state.
 
void GetPeersNonAlloc (List< NetPeer > peers, ConnectionState peerState)
 Get copy of peers (without allocations)
 
void DisconnectAll ()
 Disconnect all peers without any additional data.
 
void DisconnectAll (byte[] data, int start, int count)
 Disconnect all peers with shutdown message.
 
void DisconnectPeerForce (NetPeer peer)
 Immediately disconnect peer from server without additional data.
 
void DisconnectPeer (NetPeer peer)
 Disconnect peer from server.
 
void DisconnectPeer (NetPeer peer, byte[] data)
 Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
 
void DisconnectPeer (NetPeer peer, NetDataWriter writer)
 Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
 
void DisconnectPeer (NetPeer peer, byte[] data, int start, int count)
 Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
 
void CreateNtpRequest (IPEndPoint endPoint)
 Create the requests for NTP server.
 
void CreateNtpRequest (string ntpServerAddress, int port)
 Create the requests for NTP server.
 
void CreateNtpRequest (string ntpServerAddress)
 Create the requests for NTP server (default port)
 
NetPeerEnumerator GetEnumerator ()
 

Public Attributes

bool UnconnectedMessagesEnabled = false
 Enable messages receiving without connection. (with SendUnconnectedMessage method)
 
bool NatPunchEnabled = false
 Enable nat punch messages.
 
int UpdateTime = 15
 Library logic update and send period in milliseconds.
 
int PingInterval = 1000
 Interval for latency detection and checking connection.
 
int DisconnectTimeout = 5000
 If NetManager doesn't receive any packet from remote peer during this time then connection will be closed (including library internal keepalive packets)
 
bool SimulatePacketLoss = false
 Simulate packet loss by dropping random amount of packets. (Works only in DEBUG mode)
 
bool SimulateLatency = false
 Simulate latency by holding packets for random time. (Works only in DEBUG mode)
 
int SimulationPacketLossChance = 10
 Chance of packet loss when simulation enabled. value in percents (1 - 100).
 
int SimulationMinLatency = 30
 Minimum simulated latency.
 
int SimulationMaxLatency = 100
 Maximum simulated latency.
 
bool UnsyncedEvents = false
 Events automatically will be called without PollEvents method from another thread.
 
bool UnsyncedReceiveEvent = false
 If true - receive event will be called from "receive" thread immediately otherwise on PollEvents call.
 
bool UnsyncedDeliveryEvent = false
 If true - delivery event will be called from "receive" thread immediately otherwise on PollEvents call.
 
bool BroadcastReceiveEnabled = false
 Allows receive broadcast packets.
 
int ReconnectDelay = 500
 Delay between initial connection attempts.
 
int MaxConnectAttempts = 10
 Maximum connection attempts before client stops and call disconnect event.
 
bool ReuseAddress = false
 Enables socket option "ReuseAddress" for specific purposes.
 
readonly NetStatistics Statistics
 Statistics of all connections.
 
bool EnableStatistics = false
 Toggles the collection of network statistics for the instance and all known peers.
 
readonly NatPunchModule NatPunchModule
 NatPunchModule for NAT hole punching operations.
 
bool AutoRecycle
 Automatically recycle NetPacketReader after OnReceive event.
 
IPv6Mode IPv6Enabled = IPv6Mode.SeparateSocket
 IPv6 support.
 
int MtuOverride = 0
 Override MTU for all new peers registered in this NetManager, will ignores MTU Discovery!
 
bool UseSafeMtu = false
 Sets initial MTU to lowest possible value according to RFC1191 (576 bytes)
 
bool DisconnectOnUnreachable = false
 Disconnect peers if HostUnreachable or NetworkUnreachable spawned (old behaviour 0.9.x was true)
 

Properties

bool IsRunning [get]
 Returns true if socket listening and update thread is running.
 
int LocalPort [get]
 Local EndPoint (host and port)
 
NetPeer FirstPeer [get]
 First peer. Useful for Client mode.
 
byte ChannelsCount [get, set]
 QoS channel count per message type (value must be between 1 and 64 channels)
 
List< NetPeerConnectedPeerList [get]
 Returns connected peers list (with internal cached list)
 
int ConnectedPeersCount [get]
 Returns connected peers count.
 
int ExtraPacketSizeForLayer [get]
 

Detailed Description

Main class for all network operations. Can be used as client and/or server.

Constructor & Destructor Documentation

◆ NetManager()

LiteNetLib.NetManager.NetManager ( INetEventListener  listener,
PacketLayerBase  extraPacketLayer = null 
)
inline

NetManager constructor.

Parameters
listenerNetwork events listener (also can implement IDeliveryEventListener)
extraPacketLayerExtra processing of packages, like CRC checksum or encryption. All connected NetManagers must have same layer.

Member Function Documentation

◆ Connect() [1/4]

NetPeer LiteNetLib.NetManager.Connect ( IPEndPoint  target,
NetDataWriter  connectionData 
)
inline

Connect to remote host.

Parameters
targetServer end point (ip and port)
connectionDataAdditional data for remote peer
Returns
New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting
Exceptions
InvalidOperationExceptionManager is not running. Call Start()

◆ Connect() [2/4]

NetPeer LiteNetLib.NetManager.Connect ( IPEndPoint  target,
string  key 
)
inline

Connect to remote host.

Parameters
targetServer end point (ip and port)
keyConnection key
Returns
New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting
Exceptions
InvalidOperationExceptionManager is not running. Call Start()

◆ Connect() [3/4]

NetPeer LiteNetLib.NetManager.Connect ( string  address,
int  port,
NetDataWriter  connectionData 
)
inline

Connect to remote host.

Parameters
addressServer IP or hostname
portServer Port
connectionDataAdditional data for remote peer
Returns
New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting
Exceptions
InvalidOperationExceptionManager is not running. Call Start()

◆ Connect() [4/4]

NetPeer LiteNetLib.NetManager.Connect ( string  address,
int  port,
string  key 
)
inline

Connect to remote host.

Parameters
addressServer IP or hostname
portServer Port
keyConnection key
Returns
New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting
Exceptions
InvalidOperationExceptionManager is not running. Call Start()

◆ CreateNtpRequest() [1/3]

void LiteNetLib.NetManager.CreateNtpRequest ( IPEndPoint  endPoint)
inline

Create the requests for NTP server.

Parameters
endPointNTP Server address.

◆ CreateNtpRequest() [2/3]

void LiteNetLib.NetManager.CreateNtpRequest ( string  ntpServerAddress)
inline

Create the requests for NTP server (default port)

Parameters
ntpServerAddressNTP Server address.

◆ CreateNtpRequest() [3/3]

void LiteNetLib.NetManager.CreateNtpRequest ( string  ntpServerAddress,
int  port 
)
inline

Create the requests for NTP server.

Parameters
ntpServerAddressNTP Server address.
portport

◆ DisconnectAll()

void LiteNetLib.NetManager.DisconnectAll ( byte[]  data,
int  start,
int  count 
)
inline

Disconnect all peers with shutdown message.

Parameters
dataData to send (must be less or equal MTU)
startData start
countData count

◆ DisconnectPeer() [1/4]

void LiteNetLib.NetManager.DisconnectPeer ( NetPeer  peer)
inline

Disconnect peer from server.

Parameters
peerpeer to disconnect

◆ DisconnectPeer() [2/4]

void LiteNetLib.NetManager.DisconnectPeer ( NetPeer  peer,
byte[]  data 
)
inline

Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)

Parameters
peerpeer to disconnect
dataadditional data

◆ DisconnectPeer() [3/4]

void LiteNetLib.NetManager.DisconnectPeer ( NetPeer  peer,
byte[]  data,
int  start,
int  count 
)
inline

Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)

Parameters
peerpeer to disconnect
dataadditional data
startdata start
countdata length

◆ DisconnectPeer() [4/4]

void LiteNetLib.NetManager.DisconnectPeer ( NetPeer  peer,
NetDataWriter  writer 
)
inline

Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)

Parameters
peerpeer to disconnect
writeradditional data

◆ DisconnectPeerForce()

void LiteNetLib.NetManager.DisconnectPeerForce ( NetPeer  peer)
inline

Immediately disconnect peer from server without additional data.

Parameters
peerpeer to disconnect

◆ GetPeerById()

NetPeer LiteNetLib.NetManager.GetPeerById ( int  id)
inline

Gets peer by peer id.

Parameters
idid of peer
Returns
Peer if peer with id exist, otherwise null

◆ GetPeersCount()

int LiteNetLib.NetManager.GetPeersCount ( ConnectionState  peerState)
inline

Return peers count with connection state.

Parameters
peerStatepeer connection state (you can use as bit flags)
Returns
peers count

◆ GetPeersNonAlloc()

void LiteNetLib.NetManager.GetPeersNonAlloc ( List< NetPeer peers,
ConnectionState  peerState 
)
inline

Get copy of peers (without allocations)

Parameters
peersList that will contain result
peerStateState of peers

◆ ManualUpdate()

void LiteNetLib.NetManager.ManualUpdate ( int  elapsedMilliseconds)
inline

Update and send logic. Use this only when NetManager started in manual mode.

Parameters
elapsedMillisecondselapsed milliseconds since last update call

◆ SendToAll() [1/12]

void LiteNetLib.NetManager.SendToAll ( byte[]  data,
byte  channelNumber,
DeliveryMethod  options 
)
inline

Send data to all connected peers.

Parameters
dataData
channelNumberNumber of channel (from 0 to channelsCount - 1)
optionsSend options (reliable, unreliable, etc.)

◆ SendToAll() [2/12]

void LiteNetLib.NetManager.SendToAll ( byte[]  data,
byte  channelNumber,
DeliveryMethod  options,
NetPeer  excludePeer 
)
inline

Send data to all connected peers.

Parameters
dataData
channelNumberNumber of channel (from 0 to channelsCount - 1)
optionsSend options (reliable, unreliable, etc.)
excludePeerExcluded peer

◆ SendToAll() [3/12]

void LiteNetLib.NetManager.SendToAll ( byte[]  data,
DeliveryMethod  options 
)
inline

Send data to all connected peers (channel - 0)

Parameters
dataData
optionsSend options (reliable, unreliable, etc.)

◆ SendToAll() [4/12]

void LiteNetLib.NetManager.SendToAll ( byte[]  data,
DeliveryMethod  options,
NetPeer  excludePeer 
)
inline

Send data to all connected peers (channel - 0)

Parameters
dataData
optionsSend options (reliable, unreliable, etc.)
excludePeerExcluded peer

◆ SendToAll() [5/12]

void LiteNetLib.NetManager.SendToAll ( byte[]  data,
int  start,
int  length,
byte  channelNumber,
DeliveryMethod  options 
)
inline

Send data to all connected peers.

Parameters
dataData
startStart of data
lengthLength of data
channelNumberNumber of channel (from 0 to channelsCount - 1)
optionsSend options (reliable, unreliable, etc.)

◆ SendToAll() [6/12]

void LiteNetLib.NetManager.SendToAll ( byte[]  data,
int  start,
int  length,
byte  channelNumber,
DeliveryMethod  options,
NetPeer  excludePeer 
)
inline

Send data to all connected peers.

Parameters
dataData
startStart of data
lengthLength of data
channelNumberNumber of channel (from 0 to channelsCount - 1)
optionsSend options (reliable, unreliable, etc.)
excludePeerExcluded peer

◆ SendToAll() [7/12]

void LiteNetLib.NetManager.SendToAll ( byte[]  data,
int  start,
int  length,
DeliveryMethod  options 
)
inline

Send data to all connected peers (channel - 0)

Parameters
dataData
startStart of data
lengthLength of data
optionsSend options (reliable, unreliable, etc.)

◆ SendToAll() [8/12]

void LiteNetLib.NetManager.SendToAll ( byte[]  data,
int  start,
int  length,
DeliveryMethod  options,
NetPeer  excludePeer 
)
inline

Send data to all connected peers (channel - 0)

Parameters
dataData
startStart of data
lengthLength of data
optionsSend options (reliable, unreliable, etc.)
excludePeerExcluded peer

◆ SendToAll() [9/12]

void LiteNetLib.NetManager.SendToAll ( NetDataWriter  writer,
byte  channelNumber,
DeliveryMethod  options 
)
inline

Send data to all connected peers.

Parameters
writerDataWriter with data
channelNumberNumber of channel (from 0 to channelsCount - 1)
optionsSend options (reliable, unreliable, etc.)

◆ SendToAll() [10/12]

void LiteNetLib.NetManager.SendToAll ( NetDataWriter  writer,
byte  channelNumber,
DeliveryMethod  options,
NetPeer  excludePeer 
)
inline

Send data to all connected peers.

Parameters
writerDataWriter with data
channelNumberNumber of channel (from 0 to channelsCount - 1)
optionsSend options (reliable, unreliable, etc.)
excludePeerExcluded peer

◆ SendToAll() [11/12]

void LiteNetLib.NetManager.SendToAll ( NetDataWriter  writer,
DeliveryMethod  options 
)
inline

Send data to all connected peers (channel - 0)

Parameters
writerDataWriter with data
optionsSend options (reliable, unreliable, etc.)

◆ SendToAll() [12/12]

void LiteNetLib.NetManager.SendToAll ( NetDataWriter  writer,
DeliveryMethod  options,
NetPeer  excludePeer 
)
inline

Send data to all connected peers (channel - 0)

Parameters
writerDataWriter with data
optionsSend options (reliable, unreliable, etc.)
excludePeerExcluded peer

◆ SendUnconnectedMessage() [1/3]

bool LiteNetLib.NetManager.SendUnconnectedMessage ( byte[]  message,
int  start,
int  length,
IPEndPoint  remoteEndPoint 
)
inline

Send message without connection.

Parameters
messageRaw data
startdata start
lengthdata length
remoteEndPointPacket destination
Returns
Operation result

◆ SendUnconnectedMessage() [2/3]

bool LiteNetLib.NetManager.SendUnconnectedMessage ( byte[]  message,
IPEndPoint  remoteEndPoint 
)
inline

Send message without connection.

Parameters
messageRaw data
remoteEndPointPacket destination
Returns
Operation result

◆ SendUnconnectedMessage() [3/3]

bool LiteNetLib.NetManager.SendUnconnectedMessage ( NetDataWriter  writer,
IPEndPoint  remoteEndPoint 
)
inline

Send message without connection.

Parameters
writerData serializer
remoteEndPointPacket destination
Returns
Operation result

◆ Start() [1/3]

bool LiteNetLib.NetManager.Start ( int  port)
inline

Start logic thread and listening on selected port.

Parameters
portport to listen

◆ Start() [2/3]

bool LiteNetLib.NetManager.Start ( IPAddress  addressIPv4,
IPAddress  addressIPv6,
int  port 
)
inline

Start logic thread and listening on selected port.

Parameters
addressIPv4bind to specific ipv4 address
addressIPv6bind to specific ipv6 address
portport to listen

◆ Start() [3/3]

bool LiteNetLib.NetManager.Start ( string  addressIPv4,
string  addressIPv6,
int  port 
)
inline

Start logic thread and listening on selected port.

Parameters
addressIPv4bind to specific ipv4 address
addressIPv6bind to specific ipv6 address
portport to listen

◆ StartInManualMode() [1/3]

bool LiteNetLib.NetManager.StartInManualMode ( int  port)
inline

Start in manual mode and listening on selected port In this mode you should use ManualReceive (without PollEvents) for receive packets and ManualUpdate(...) for update and send packets This mode useful mostly for single-threaded servers.

Parameters
portport to listen

◆ StartInManualMode() [2/3]

bool LiteNetLib.NetManager.StartInManualMode ( IPAddress  addressIPv4,
IPAddress  addressIPv6,
int  port 
)
inline

Start in manual mode and listening on selected port In this mode you should use ManualReceive (without PollEvents) for receive packets and ManualUpdate(...) for update and send packets This mode useful mostly for single-threaded servers.

Parameters
addressIPv4bind to specific ipv4 address
addressIPv6bind to specific ipv6 address
portport to listen

◆ StartInManualMode() [3/3]

bool LiteNetLib.NetManager.StartInManualMode ( string  addressIPv4,
string  addressIPv6,
int  port 
)
inline

Start in manual mode and listening on selected port In this mode you should use ManualReceive (without PollEvents) for receive packets and ManualUpdate(...) for update and send packets This mode useful mostly for single-threaded servers.

Parameters
addressIPv4bind to specific ipv4 address
addressIPv6bind to specific ipv6 address
portport to listen

◆ Stop()

void LiteNetLib.NetManager.Stop ( bool  sendDisconnectMessages)
inline

Force closes connection and stop all threads.

Parameters
sendDisconnectMessagesSend disconnect messages

The documentation for this class was generated from the following file: