### Basic LiteNetLib Server Setup Source: https://github.com/revenantx/litenetlib/blob/master/docs/index.html Initializes an EventBasedNetListener and NetManager, starts the server on a specified port, and begins polling for events. Handles incoming connection requests by accepting them if the peer count is below the maximum, otherwise rejecting them. Upon a successful peer connection, it sends a 'Hello client!' message. ```csharp EventBasedNetListener listener = new EventBasedNetListener(); NetManager server = new NetManager(listener); server.Start(9050 /* port */); listener.ConnectionRequestEvent += request => { if(server.PeersCount < 10 /* max connections */) request.AcceptIfKey("SomeConnectionKey"); else request.Reject(); }; listener.PeerConnectedEvent += peer => { Console.WriteLine("We got connection: {0}", peer.EndPoint); // Show peer ip NetDataWriter writer = new NetDataWriter(); // Create writer class writer.Put("Hello client!"); // Put some string peer.Send(writer, DeliveryMethod.ReliableOrdered); // Send with reliability }; while (!Console.KeyAvailable) { server.PollEvents(); Thread.Sleep(15); } server.Stop(); ``` -------------------------------- ### Start Methods Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetManager.html Methods for starting the LiteNetManager with different configurations. ```APIDOC ## Start ### Description Starts the LiteNetManager. Various overloads allow for different startup configurations. ### Method `Start()` `Start(IPAddress localAddress, IPAddress publicAddress, int port)` `Start(string localAddress, string publicAddress, int port)` `Start(int port)` ``` -------------------------------- ### Start LiteNetLib Server Source: https://github.com/revenantx/litenetlib/blob/master/README.md Initializes and starts a LiteNetLib server. Handles incoming connection requests, limits connections, and manages peer connections. Use this to set up a basic server endpoint. ```csharp var listener = new EventBasedNetListener(); var server = new NetManager(listener); server.Start(9050 /* port */); listener.ConnectionRequestEvent += request => { if(server.ConnectedPeersCount < 10 /* max connections */) request.AcceptIfKey("SomeConnectionKey"); else request.Reject(); }; listener.PeerConnectedEvent += peer => { Console.WriteLine("We got connection: {0}", peer); // Show peer IP var writer = new NetDataWriter(); // Create writer class writer.Put("Hello client!"); // Put some string peer.Send(writer, DeliveryMethod.ReliableOrdered); // Send with reliability }; while (!Console.KeyAvailable) { server.PollEvents(); Thread.Sleep(15); } server.Stop(); ``` -------------------------------- ### Start Network Manager Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.html Methods for starting the LiteNetManager, allowing it to listen on a specified port and optionally bind to specific IP addresses. ```APIDOC ## Start() ### Description Start logic thread and listening on available port. ### Method Signature public bool Start() ### Returns - **bool** - True if the manager started successfully, false otherwise. ``` ```APIDOC ## Start(int port) ### Description Start logic thread and listening on the selected port. ### Method Signature public bool Start(int port) ### Parameters #### Parameters - **port** (int) - The port number to listen on. ### Returns - **bool** - True if the manager started successfully, false otherwise. ``` ```APIDOC ## Start(IPAddress addressIPv4, IPAddress addressIPv6, int port) ### Description Start logic thread and listening on the selected port, binding to specific IPv4 and IPv6 addresses. ### Method Signature public bool Start(IPAddress addressIPv4, IPAddress addressIPv6, int port) ### Parameters #### Parameters - **addressIPv4** (IPAddress) - The IPv4 address to bind to. - **addressIPv6** (IPAddress) - The IPv6 address to bind to. - **port** (int) - The port number to listen on. ### Returns - **bool** - True if the manager started successfully, false otherwise. ``` ```APIDOC ## Start(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool manualMode) ### Description Start logic thread and listening on the selected port with manual mode option. When `manualMode` is true, internal background threads are disabled, requiring manual calls to `PollEvents()` and `ManualUpdate(float)`. ### Method Signature public bool Start(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool manualMode) ### Parameters #### Parameters - **addressIPv4** (IPAddress) - The IPv4 address to bind to. - **addressIPv6** (IPAddress) - The IPv6 address to bind to. - **port** (int) - The port number to listen on. - **manualMode** (bool) - If true, disables internal background threads. You must manually call `PollEvents()` and `ManualUpdate(float)`. ### Returns - **bool** - True if the manager started successfully, false otherwise. ``` ```APIDOC ## Start(string addressIPv4, string addressIPv6, int port) ### Description Start logic thread and listening on the selected port, using string representations for IP addresses. ### Method Signature public bool Start(string addressIPv4, string addressIPv6, int port) ### Parameters #### Parameters - **addressIPv4** (string) - The IPv4 address string to bind to. - **addressIPv6** (string) - The IPv6 address string to bind to. - **port** (int) - The port number to listen on. ### Returns - **bool** - True if the manager started successfully, false otherwise. ``` -------------------------------- ### StartInManualMode Methods Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetManager.html Methods for starting the LiteNetManager in manual mode. ```APIDOC ## StartInManualMode ### Description Starts the LiteNetManager in manual mode, requiring manual event polling. ### Method `StartInManualMode(IPAddress localAddress, IPAddress publicAddress, int port)` `StartInManualMode(string localAddress, string publicAddress, int port)` `StartInManualMode(int port)` ``` -------------------------------- ### LiteNetLib Server Usage Source: https://github.com/revenantx/litenetlib/blob/master/docfx_project/index.md Example of setting up and running a LiteNetLib server. It listens on a specified port, accepts connections with a key, and sends messages to connected peers. Connection limits are enforced. ```csharp EventBasedNetListener listener = new EventBasedNetListener(); NetManager server = new NetManager(listener); server.Start(9050 /* port */); listener.ConnectionRequestEvent += request => { if(server.PeersCount < 10 /* max connections */) request.AcceptIfKey("SomeConnectionKey"); else request.Reject(); }; listener.PeerConnectedEvent += peer => { Console.WriteLine("We got connection: {0}", peer.EndPoint); // Show peer ip NetDataWriter writer = new NetDataWriter(); // Create writer class writer.Put("Hello client!"); // Put some string peer.Send(writer, DeliveryMethod.ReliableOrdered); // Send with reliability }; while (!Console.KeyAvailable) { server.PollEvents(); Thread.Sleep(15); } server.Stop(); ``` -------------------------------- ### LiteNetLib Client Usage Source: https://github.com/revenantx/litenetlib/blob/master/docfx_project/index.md Example of setting up and running a LiteNetLib client. It connects to a server, sends messages, and handles incoming data. Ensure the listener is properly configured for network events. ```csharp EventBasedNetListener listener = new EventBasedNetListener(); NetManager client = new NetManager(listener); client.Start(); client.Connect("localhost" /* host ip or name */, 9050 /* port */, "SomeConnectionKey" /* text key or NetDataWriter */); listener.NetworkReceiveEvent += (fromPeer, dataReader, deliveryMethod) => { Console.WriteLine("We got: {0}", dataReader.GetString(100 /* max length of string */)); dataReader.Recycle(); }; while (!Console.KeyAvailable) { client.PollEvents(); Thread.Sleep(15); } client.Stop(); ``` -------------------------------- ### LiteNetLib Client Connection Example Source: https://github.com/revenantx/litenetlib/blob/master/README.md Connects a client to a server using LiteNetLib. Requires a NetManager and an EventBasedNetListener. Ensure the server is running and accessible on the specified host and port. ```csharp var listener = new EventBasedNetListener(); var client = new NetManager(listener); client.Start(); client.Connect("localhost" /* host IP or name */, 9050 /* port */, "SomeConnectionKey" /* text key or NetDataWriter */); listener.NetworkReceiveEvent += (fromPeer, dataReader, channel, deliveryMethod) => { Console.WriteLine("We got: {0}", dataReader.GetString(100 /* max length of string */)); dataReader.Recycle(); }; while (!Console.KeyAvailable) { client.PollEvents(); Thread.Sleep(15); } client.Stop(); ``` -------------------------------- ### Client-Side Packet Sending Source: https://github.com/revenantx/litenetlib/wiki/NetPacketProcessor-(NetSerializer)-usage Example of a client sending a SamplePacket over the network using NetPacketProcessor. ```csharp //First side class SomeClientListener : INetEventListener { private readonly NetPacketProcessor _netPacketProcessor = new NetPacketProcessor(); ... public void OnPeerConnected(NetPeer peer) { SamplePacket sp = new SamplePacket { SomeFloat = 3.42f, SomeIntArray = new[] {6, 5, 4}, SomeString = "Test String", } peer.Send(_netPacketProcessor.Write(sp), DeliveryMethod.ReliableOrdered); //or you can use _netPacketProcessor.Send(peer, sp, DeliveryMethod.ReliableOrdered); } } ``` -------------------------------- ### GetPeers(List, ConnectionState) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.html Get a copy of peers matching a specific connection state without allocations. ```APIDOC ## GetPeers(List, ConnectionState) ### Description Get copy of peers (without allocations). ### Method public void GetPeers(List peers, ConnectionState peerState) ### Parameters #### Path Parameters - **peers** (List) - Description: List that will contain result - **peerState** (ConnectionState) - Description: State of peers ``` -------------------------------- ### RejectForce(byte[] rejectData, int start, int length) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteConnectionRequest.html Rejects the connection immediately without reliability, allowing specification of a data segment. Minimizes resource usage by not creating an internal peer. ```APIDOC ## RejectForce(byte[] rejectData, int start, int length) ### Description Rejects the connection immediately without reliability. Minimizes resource usage by not creating an internal peer. ### Method Signature public void RejectForce(byte[] rejectData, int start, int length) ### Parameters #### Path Parameters - **rejectData** (byte[]) - Required - Data to send with the rejection. - **start** (int) - Required - Offset in the `rejectData` array. - **length** (int) - Required - Length of the data to be sent. ``` -------------------------------- ### StartInManualMode Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.html Starts the LiteNetManager in manual mode, allowing for custom packet handling and updates. This is useful for single-threaded server applications. ```APIDOC ## StartInManualMode(string, string, int) ### Description Starts in manual mode and listening on selected port. In this mode, you should use `ManualReceive` (without `PollEvents`) for receiving packets and `ManualUpdate(...)` for updating and sending packets. This mode is useful mostly for single-threaded servers. ### Declaration ```csharp public bool StartInManualMode(string addressIPv4, string addressIPv6, int port) ``` ### Parameters #### Path Parameters - **addressIPv4** (string) - Required - Binds to a specific IPv4 address. - **addressIPv6** (string) - Required - Binds to a specific IPv6 address. - **port** (int) - Required - The port to listen on. ### Returns - **bool** - True if the manager started successfully, false otherwise. ``` -------------------------------- ### StartInManualMode Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.html Starts the LiteNetManager in manual mode, requiring manual handling of packet reception and updates. This is useful for single-threaded applications. ```APIDOC ## StartInManualMode(int port) ### Description Start in manual mode and listening on the selected port. In this mode, you should use `ManualReceive` (without `PollEvents`) for receiving packets and `ManualUpdate(...)` for updates and sending packets. This mode is useful mostly for single-threaded servers. ### Method Signature public bool StartInManualMode(int port) ### Parameters #### Parameters - **port** (int) - The port number to listen on. ### Returns - **bool** - True if the manager started successfully, false otherwise. ``` ```APIDOC ## StartInManualMode(IPAddress addressIPv4, IPAddress addressIPv6, int port) ### Description Start in manual mode and listening on the selected port, binding to specific IP addresses. In this mode, you should use `ManualReceive` (without `PollEvents`) for receiving packets and `ManualUpdate(...)` for updates and sending packets. This mode is useful mostly for single-threaded servers. ### Method Signature public bool StartInManualMode(IPAddress addressIPv4, IPAddress addressIPv6, int port) ### Parameters #### Parameters - **addressIPv4** (IPAddress) - The IPv4 address to bind to. - **addressIPv6** (IPAddress) - The IPv6 address to bind to. - **port** (int) - The port number to listen on. ### Returns - **bool** - True if the manager started successfully, false otherwise. ``` -------------------------------- ### Get(out Guid) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a Guid from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out Guid) ### Description Reads a Guid and assigns it to `result`. ### Method `public void Get(out Guid result)` ### Parameters #### Path Parameters - **result** (Guid) - Description: The Guid to be read. ``` -------------------------------- ### GetGuid() Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a 16-byte GUID from the data reader. ```APIDOC ## GetGuid() ### Description Reads a 16-byte [Guid](https://learn.microsoft.com/dotnet/api/system.guid). ### Declaration public Guid GetGuid() ### Returns Type Description [Guid](https://learn.microsoft.com/dotnet/api/system.guid) The deserialized [Guid](https://learn.microsoft.com/dotnet/api/system.guid). ``` -------------------------------- ### LiteNetLib Client Connection and Event Handling Source: https://github.com/revenantx/litenetlib/blob/master/docs/index.html This snippet demonstrates how to set up a LiteNetLib client to connect to a server, handle incoming network data, and manage the client's lifecycle. Ensure the listener is properly configured to process network events. ```csharp EventBasedNetListener listener = new EventBasedNetListener(); NetManager client = new NetManager(listener); client.Start(); client.Connect("localhost" /* host ip or name */, 9050 /* port */, "SomeConnectionKey" /* text key or NetDataWriter */); listener.NetworkReceiveEvent += (fromPeer, dataReader, deliveryMethod) => { Console.WriteLine("We got: {0}", dataReader.GetString(100 /* max length of string */)); dataReader.Recycle(); }; while (!Console.KeyAvailable) { client.PollEvents(); Thread.Sleep(15); } client.Stop(); ``` -------------------------------- ### GetPeerById(int) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.html Gets a peer by its unique ID. ```APIDOC ## GetPeerById(int) ### Description Gets peer by peer id. ### Method public LiteNetPeer GetPeerById(int id) ### Parameters #### Path Parameters - **id** (int) - Description: id of peer ### Returns - **LiteNetPeer** - Peer if peer with id exist, otherwise null ``` -------------------------------- ### Current Property Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.NetPeerEnumerator.html Gets the current LiteNetPeer in the enumeration. ```APIDOC ## Current Property ### Description Gets the element in the collection at the current position of the enumerator. ### Property Value - **LiteNetPeer** - The current LiteNetPeer being enumerated. ``` -------------------------------- ### Constructors Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetPacketProcessor.html Initializes a new instance of the NetPacketProcessor class. ```APIDOC ## NetPacketProcessor() ### Description Initializes a new instance of the NetPacketProcessor class with default settings. ### Syntax ```csharp public NetPacketProcessor() ``` ``` ```APIDOC ## NetPacketProcessor(int maxStringLength) ### Description Initializes a new instance of the NetPacketProcessor class with a specified maximum string length for serialization. ### Parameters - **maxStringLength** (int) - The maximum length for strings to be serialized. ``` -------------------------------- ### Get() where T : struct, INetSerializable Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Deserializes a struct that implements INetSerializable. ```APIDOC ## Get() where T : struct, INetSerializable ### Description Deserializes a struct that implements `INetSerializable`. ### Type Parameters - `T`: A struct type implementing `INetSerializable`. ### Returns - `T`: The deserialized struct. ``` -------------------------------- ### NatPunchModule Send NAT Introduce Request (string, int) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NatPunchModule.html Sends a request to the Master Server to introduce this peer to another peer using a hostname and port. ```APIDOC ## SendNatIntroduceRequest(string, int, string) ### Description Sends a request to the Master Server to introduce this peer to another peer. ### Method `public void SendNatIntroduceRequest(string host, int port, string additionalInfo)` ### Parameters - **host** (string) - The hostname or IP of the Master Server. - **port** (int) - The port of the Master Server. - **additionalInfo** (string) - Custom token to identify the connection or room. ``` -------------------------------- ### GetBytesSegment(int count) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Gets an ArraySegment of bytes from the current position. ```APIDOC ## GetBytesSegment(int count) ### Description Gets an [ArraySegment](https://learn.microsoft.com/dotnet/api/system.arraysegment-1) of [byte](https://learn.microsoft.com/dotnet/api/system.byte)s from the current position. ### Method Signature ```csharp public ArraySegment GetBytesSegment(int count) ``` ### Parameters #### count - **Type**: `int` - **Description**: The number of bytes to include in the segment. ### Returns - **Type**: `ArraySegment` - **Description**: An [ArraySegment](https://learn.microsoft.com/dotnet/api/system.arraysegment-1) wrapping the internal buffer. ``` -------------------------------- ### Constructors Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Layers.XorEncryptLayer.html Initializes a new instance of the XorEncryptLayer class. You can initialize it with a default key or provide a custom key as a byte array or a string. ```APIDOC ## Constructors ### XorEncryptLayer() Initializes a new instance of the XorEncryptLayer class with a default key. ### XorEncryptLayer(byte[] key) Initializes a new instance of the XorEncryptLayer class with the specified byte array key. - **key** (byte[]): The encryption key as a byte array. ### XorEncryptLayer(string key) Initializes a new instance of the XorEncryptLayer class with the specified string key. - **key** (string): The encryption key as a string. ``` -------------------------------- ### Current Property Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.NetPeerEnumerator-1.html Gets the element in the collection at the current position of the enumerator. ```APIDOC ## Current Property ### Description Gets the element in the collection at the current position of the enumerator. ### Property Value - **T** - The element in the collection at the current position of the enumerator. ``` -------------------------------- ### Connect(string, int, string) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.html Connects to a remote host using an address, port, and a connection key. Returns a LiteNetPeer object. ```APIDOC ## Connect(string, int, string) ### Description Connect to remote host using an address, port, and a connection key. ### Method Connect ### Parameters #### Path Parameters - **address** (string) - Required - Server IP or hostname - **port** (int) - Required - Server Port - **key** (string) - Required - Connection key ### Returns #### Success Response - **LiteNetPeer** - New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting ### Exceptions - **InvalidOperationException** - Manager is not running. Call Start() ``` -------------------------------- ### GetConnectedPeers(List) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.html Get a copy of all connected peers without allocations. ```APIDOC ## GetConnectedPeers(List) ### Description Get copy of connected peers (without allocations). ### Method public void GetConnectedPeers(List peers) ### Parameters #### Path Parameters - **peers** (List) - Description: List that will contain result ``` -------------------------------- ### Get(out ulong) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a ulong from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out ulong) ### Description Reads a ulong and assigns it to `result`. ### Method `public void Get(out ulong result)` ### Parameters #### Path Parameters - **result** (ulong) - Description: The ulong to be read. ``` -------------------------------- ### Create NtpRequest Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NtpRequest.html Demonstrates the different ways to create an NtpRequest object using the static `Create` methods. ```APIDOC ## Create NtpRequest ### Description These static methods are used to create and initialize an `NtpRequest` object, which prepares it for sending an NTP request to a specified server. ### Methods: #### Create(IPAddress ipAddress, Action onRequestComplete) ##### Parameters: - **ipAddress** (System.Net.IPAddress) - Required - NTP Server address. - **onRequestComplete** (System.Action) - Required - Callback function that will be invoked with the NTP response or null on error. This callback is executed on a separate thread. ##### Returns: - [NtpRequest](LiteNetLib.Utils.NtpRequest.html) #### Create(IPEndPoint endPoint, Action onRequestComplete) ##### Parameters: - **endPoint** (System.Net.IPEndPoint) - Required - NTP Server address. - **onRequestComplete** (System.Action) - Required - Callback function that will be invoked with the NTP response or null on error. This callback is executed on a separate thread. ##### Returns: - [NtpRequest](LiteNetLib.Utils.NtpRequest.html) #### Create(String ntpServerAddress, Action onRequestComplete) ##### Parameters: - **ntpServerAddress** (System.String) - Required - NTP Server address (uses default port 123). - **onRequestComplete** (System.Action) - Required - Callback function that will be invoked with the NTP response or null on error. This callback is executed on a separate thread. ##### Returns: - [NtpRequest](LiteNetLib.Utils.NtpRequest.html) #### Create(String ntpServerAddress, int port, Action onRequestComplete) ##### Parameters: - **ntpServerAddress** (System.String) - Required - NTP Server address. - **port** (System.Int32) - Required - The port number to connect to. - **onRequestComplete** (System.Action) - Required - Callback function that will be invoked with the NTP response or null on error. This callback is executed on a separate thread. ##### Returns: - [NtpRequest](LiteNetLib.Utils.NtpRequest.html) ### Example Usage: ```csharp // Using IPAddress IPAddress ip = IPAddress.Parse("1.pool.ntp.org"); NtpRequest req1 = NtpRequest.Create(ip, MyCallback); // Using IPEndPoint IPEndPoint ep = new IPEndPoint(ip, 123); NtpRequest req2 = NtpRequest.Create(ep, MyCallback); // Using server address string (default port) NtpRequest req3 = NtpRequest.Create("pool.ntp.org", MyCallback); // Using server address string and specific port NtpRequest req4 = NtpRequest.Create("pool.ntp.org", 123, MyCallback); void MyCallback(NtpPacket packet) { // Handle response or error } ``` ``` -------------------------------- ### Get(out uint) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a uint from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out uint) ### Description Reads a uint and assigns it to `result`. ### Method `public void Get(out uint result)` ### Parameters #### Path Parameters - **result** (uint) - Description: The uint to be read. ``` -------------------------------- ### Connect(string, int, string) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetManager.html Connects to a remote host specified by address and port, using a connection key. ```APIDOC ## Connect(string, int, string) ### Description Connect to remote host with a connection key. ### Parameters - **address** (string) - Server IP or hostname. - **port** (int) - Server Port. - **key** (string) - Connection key. ### Returns - **NetPeer** - New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting. ### Exceptions - **InvalidOperationException** - If the manager is not running. Call Start(). ``` -------------------------------- ### Connect(string, int, NetDataWriter) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.html Connects to a remote host using an address, port, and additional connection data. Returns a LiteNetPeer object. ```APIDOC ## Connect(string, int, NetDataWriter) ### Description Connect to remote host using an address, port, and additional connection data. ### Method Connect ### Parameters #### Path Parameters - **address** (string) - Required - Server IP or hostname - **port** (int) - Required - Server Port - **connectionData** (NetDataWriter) - Required - Additional data for remote peer ### Returns #### Success Response - **LiteNetPeer** - New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting ### Exceptions - **InvalidOperationException** - Manager is not running. Call Start() ``` -------------------------------- ### Get(out ushort) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a ushort from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out ushort) ### Description Reads a ushort and assigns it to `result`. ### Method `public void Get(out ushort result)` ### Parameters #### Path Parameters - **result** (ushort) - Description: The ushort to be read. ``` -------------------------------- ### NatPunchModule NAT Introduction Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NatPunchModule.html Sends NAT introduction packets to both the host and the client to facilitate punchthrough. This is typically called by a mediator, such as a master server. ```APIDOC ## NatIntroduce(IPEndPoint, IPEndPoint, IPEndPoint, IPEndPoint, string) ### Description Sends NAT introduction packets to both the host and the client to facilitate punchthrough. ### Method `public void NatIntroduce(IPEndPoint hostInternal, IPEndPoint hostExternal, IPEndPoint clientInternal, IPEndPoint clientExternal, string additionalInfo)` ### Parameters - **hostInternal** (IPEndPoint) - Internal (LAN) endpoint of the host. - **hostExternal** (IPEndPoint) - External (WAN) endpoint of the host. - **clientInternal** (IPEndPoint) - Internal (LAN) endpoint of the client. - **clientExternal** (IPEndPoint) - External (WAN) endpoint of the client. - **additionalInfo** (string) - Custom token or data to include in the introduction. ### Remarks This is typically called by a mediator (e.g. a master server). ``` -------------------------------- ### Get(out string) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a string from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out string) ### Description Reads a string and assigns it to `result`. ### Method `public void Get(out string result)` ### Parameters #### Path Parameters - **result** (string) - Description: The string to be read. ``` -------------------------------- ### Get(out float) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a float from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out float) ### Description Reads a float and assigns it to `result`. ### Method `public void Get(out float result)` ### Parameters #### Path Parameters - **result** (float) - Description: The float to be read. ``` -------------------------------- ### NatPunchModule Send NAT Introduce Request (IPEndPoint) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NatPunchModule.html Sends a request to the Master Server to introduce this peer to another peer using an IPEndPoint for the Master Server. ```APIDOC ## SendNatIntroduceRequest(IPEndPoint, string) ### Description Sends a request to the Master Server to introduce this peer to another peer. ### Method `public void SendNatIntroduceRequest(IPEndPoint masterServerEndPoint, string additionalInfo)` ### Parameters - **masterServerEndPoint** (IPEndPoint) - The endpoint of the Master Server. - **additionalInfo** (string) - Custom token to identify the connection or room. ``` -------------------------------- ### Get(out sbyte) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads an sbyte from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out sbyte) ### Description Reads an sbyte and assigns it to `result`. ### Method `public void Get(out sbyte result)` ### Parameters #### Path Parameters - **result** (sbyte) - Description: The sbyte to be read. ``` -------------------------------- ### NtpPacket.FromServerResponse Method Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NtpPacket.html Initializes an NtpPacket from data received from an NTP server. ```APIDOC ## NtpPacket.FromServerResponse(byte[], DateTime) ### Description Initializes packet from data received from a server. ### Method `public static NtpPacket FromServerResponse(byte[] bytes, DateTime destinationTimestamp)` ### Parameters #### Path Parameters - **bytes** (byte[]) - Required - Data received from the server. - **destinationTimestamp** (DateTime) - Required - Utc time of reception of response SNTP packet on the client. ### Returns - **NtpPacket** - The initialized NtpPacket object. ``` -------------------------------- ### Get(out IPEndPoint) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Deserializes an IPEndPoint from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out IPEndPoint) ### Description Deserializes an IPEndPoint and assigns it to the `result` parameter. ### Method `public void Get(out IPEndPoint result)` ### Parameters #### Path Parameters - **result** (IPEndPoint) - Description: The deserialized IPEndPoint output. ``` -------------------------------- ### NetDataWriter Constructors Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataWriter.html Initializes a new instance of the NetDataWriter class. You can create a writer with default settings, with auto-resizing enabled, or with a specified initial capacity. ```APIDOC ## NetDataWriter() ### Description Initializes a new instance of the NetDataWriter class with default settings. ### Syntax ```csharp public NetDataWriter() ``` ``` ```APIDOC ## NetDataWriter(bool autoResize) ### Description Initializes a new instance of the NetDataWriter class with the specified auto-resizing behavior. ### Syntax ```csharp public NetDataWriter(bool autoResize) ``` ### Parameters * **autoResize** (bool) - Determines if the writer should automatically resize its internal buffer when it becomes full. ``` ```APIDOC ## NetDataWriter(bool autoResize, int initialSize) ### Description Initializes a new instance of the NetDataWriter class with the specified auto-resizing behavior and initial buffer size. ### Syntax ```csharp public NetDataWriter(bool autoResize, int initialSize) ``` ### Parameters * **autoResize** (bool) - Determines if the writer should automatically resize its internal buffer when it becomes full. * **initialSize** (int) - The initial size of the internal buffer. ``` -------------------------------- ### Get(out long) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a long from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out long) ### Description Reads a long and assigns it to `result`. ### Method `public void Get(out long result)` ### Parameters #### Path Parameters - **result** (long) - Description: The long to be read. ``` -------------------------------- ### Accept Connection If Key Matches Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.ConnectionRequest.html Accepts the connection only if the provided key matches the data sent with the connection request. If the key does not match or the data is invalid, the connection is automatically rejected. ```APIDOC ## AcceptIfKey(string key) ### Description Accepts the connection if the first string in the [Data](LiteNetLib.LiteConnectionRequest.html#LiteNetLib_LiteConnectionRequest_Data) matches the provided key. ### Method AcceptIfKey ### Parameters #### Path Parameters - **key** (string) - Required - The required string key to match. ### Returns - **NetPeer** - A new [LiteNetPeer](LiteNetLib.LiteNetPeer.html) if the key matches and connection is accepted; otherwise, `null`. ### Remarks This is a helper method for simple password/key validation. If the key does not match or data is invalid, the connection is automatically rejected. ``` -------------------------------- ### Get(out int) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads an int from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out int) ### Description Reads an int and assigns it to `result`. ### Method `public void Get(out int result)` ### Parameters #### Path Parameters - **result** (int) - Description: The int to be read. ``` -------------------------------- ### NatIntroductionRequest Event Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.EventBasedNatPunchListener.html This event is triggered when a NAT introduction request is received. You can subscribe to this event to handle incoming requests. ```APIDOC ## Event: NatIntroductionRequest ### Description Event triggered when a NAT introduction request is received. ### Declaration ```csharp public event EventBasedNatPunchListener.OnNatIntroductionRequest NatIntroductionRequest ``` ### Event Type [EventBasedNatPunchListener](LiteNetLib.EventBasedNatPunchListener.html).[OnNatIntroductionRequest](LiteNetLib.EventBasedNatPunchListener.html) ``` -------------------------------- ### Get(out short) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a short from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out short) ### Description Reads a short and assigns it to `result`. ### Method `public void Get(out short result)` ### Parameters #### Path Parameters - **result** (short) - Description: The short to be read. ``` -------------------------------- ### Connect(IPEndPoint, string) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetManager.html Connects to a remote host using an IPEndPoint and a connection key. Returns a LiteNetPeer object representing the connection. ```APIDOC ## Connect(IPEndPoint, string) ### Description Connect to remote host using an IP endpoint and a connection key. ### Method Connect ### Parameters #### Path Parameters - **target** (IPEndPoint) - Required - Server end point (ip and port) - **key** (string) - Required - Connection key ### Returns #### Success Response - **LiteNetPeer** - New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting ### Exceptions - **InvalidOperationException** - Manager is not running. Call Start() ``` -------------------------------- ### Get(out double) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a double from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out double) ### Description Reads a double and assigns it to `result`. ### Method `public void Get(out double result)` ### Parameters #### Path Parameters - **result** (double) - Description: The double to be read. ``` -------------------------------- ### Connect(string, int, NetDataWriter) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetManager.html Connects to a remote host specified by address and port, with additional connection data. ```APIDOC ## Connect(string, int, NetDataWriter) ### Description Connect to remote host with additional data. ### Parameters - **address** (string) - Server IP or hostname. - **port** (int) - Server Port. - **connectionData** (NetDataWriter) - Additional data for remote peer. ### Returns - **NetPeer** - New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting. ### Exceptions - **InvalidOperationException** - If the manager is not running. Call Start(). ``` -------------------------------- ### Get(out char) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a char from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out char) ### Description Reads a char and assigns it to `result`. ### Method `public void Get(out char result)` ### Parameters #### Path Parameters - **result** (char) - Description: The char to be read. ``` -------------------------------- ### MakeEndPoint Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetUtils.html Creates an IPEndPoint from a host string and a port number. ```APIDOC ## MakeEndPoint(string, int) ### Description Creates an [IPEndPoint](https://learn.microsoft.com/dotnet/api/system.net.ipendpoint) from a host string and a port. ### Method `public static IPEndPoint MakeEndPoint(string hostStr, int port)` ### Parameters #### Path Parameters - **hostStr** (string) - Description: The host name or IP address string to resolve. - **port** (int) - Description: The port number for the endpoint. ### Returns - **IPEndPoint** - A new [IPEndPoint](https://learn.microsoft.com/dotnet/api/system.net.ipendpoint) instance. ``` -------------------------------- ### Get(out T) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Deserializes a struct that implements INetSerializable. The struct is passed by reference and will be populated with the deserialized data. ```APIDOC ## Get(out T) ### Description Deserializes a struct that implements INetSerializable. ### Method Signature ```csharp public void Get(out T result) where T : struct, INetSerializable ``` ### Parameters #### Output Parameters - **result** (T) - The deserialized struct output. This parameter must be a struct type implementing INetSerializable. ``` -------------------------------- ### Client Sending Packet with NetSerializer Source: https://github.com/revenantx/litenetlib/blob/master/docs/articles/netserializerusage.html Demonstrates how a client serializes a packet using NetSerializer and sends it to the server upon connection. Requires NetPacketProcessor and an INetEventListener implementation. ```csharp // Client class SomeClientListener : INetEventListener { private readonly NetPacketProcessor _netPacketProcessor = new NetPacketProcessor(); ... public void OnPeerConnected(NetPeer peer) { // After connection is established you will have the server as a NetPeer SamplePacket packet = new SamplePacket { SomeFloat = 3.42f, SomeIntArray = new[] {6, 5, 4}, SomeString = "Test String", } // Serialize the packet with NetSerializer and send it to the peer (server) peer.Send(_netPacketProcessor.Write(packet), DeliveryMethod.ReliableOrdered); //You can also use _netPacketProcessor.Send(peer, packet, DeliveryMethod.ReliableOrdered); } } ``` -------------------------------- ### SendToAll Methods Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetManager.html Methods for sending data to all connected peers. ```APIDOC ## SendToAll ### Description Sends data to all connected peers. ### Method `SendToAll(byte[] data, int offset, int length, DeliveryMethod method, LiteNetPeer excludePeer)` `SendToAll(ReadOnlySpan data, DeliveryMethod method)` `SendToAll(ReadOnlySpan data, DeliveryMethod method, LiteNetPeer excludePeer)` ``` -------------------------------- ### GetRemainingBytesSegment() Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Gets an ArraySegment containing all remaining bytes from the current position to the end of the data. ```APIDOC ## GetRemainingBytesSegment() ### Description Gets an ArraySegment containing all remaining bytes. ### Returns - `ArraySegment`: An ArraySegment from the current position to the end of the data. ``` -------------------------------- ### FirstPeer Property Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetManager.html Gets the first connected peer. This property is particularly useful in client mode. ```APIDOC ## FirstPeer Property ### Description Gets the first peer. Useful for Client mode. ### Property Value - **NetPeer** - The first connected NetPeer. ``` -------------------------------- ### NtpRequest Usage Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NtpRequest.html This snippet demonstrates the basic usage of the NtpRequest class, including creating a request, sending it, and handling the response. ```APIDOC ## NtpRequest Usage Example ### Description This example shows how to create an NtpRequest, send it to an NTP server, and process the response. ### Steps: 1. **Create NtpRequest**: Use one of the static `Create` methods to instantiate an `NtpRequest` object. You can provide an IP address, an IPEndPoint, or a server address string, along with a callback action to handle the response. 2. **Send Request**: Call the `Send()` method on the `NtpRequest` object to send the request to the NTP server. 3. **Handle Response**: The callback action provided during creation will be invoked when a response is received or if an error occurs. The callback receives an `NtpPacket` object, which will be null if an error occurred. 4. **Close Socket**: After receiving the response or if a timeout occurs, call the `Close()` method to release the socket resources. It's important to do this after the response is handled to avoid missing it. ### Example Code: ```csharp // Example using Create(string, Action) string ntpServer = "pool.ntp.org"; NtpRequest request = NtpRequest.Create(ntpServer, (packet) => { if (packet != null) { Console.WriteLine($"NTP Response: Offset = {packet.NtpTimeOffset}"); } else { Console.WriteLine("NTP Request failed."); } request.Close(); // Close socket after handling response }); request.Send(); ``` ### Methods Used: - `NtpRequest.Create(string ntpServerAddress, Action onRequestComplete)` - `NtpRequest.Send()` - `NtpRequest.Close()` ### Notes: - The callback action is executed on a different thread. - Ensure `Close()` is called to prevent resource leaks. ``` -------------------------------- ### NetDataReader Constructors Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Initializes a new instance of the NetDataReader class with various options for data sources. ```APIDOC ## NetDataReader Constructors ### NetDataReader() Initializes a new instance of the NetDataReader class with an empty buffer. ### NetDataReader(NetDataWriter writer) Initializes a new instance of the NetDataReader class from a NetDataWriter. ### NetDataReader(byte[] source) Initializes a new instance of the NetDataReader class from a byte array. ### NetDataReader(byte[] source, int offset, int maxSize) Initializes a new instance of the NetDataReader class from a byte array with a specified offset and maximum size. ``` -------------------------------- ### GetMaxSinglePacketSize(DeliveryMethod) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.LiteNetPeer.html Gets the maximum size of a packet that will not be fragmented based on the specified delivery method. ```APIDOC ## GetMaxSinglePacketSize(DeliveryMethod) ### Description Gets the maximum size of a packet that will not be fragmented. ### Method public int GetMaxSinglePacketSize(DeliveryMethod options) ### Parameters #### Path Parameters - **options** (DeliveryMethod) - The delivery method for which to determine the maximum packet size. ``` -------------------------------- ### Get(Func constructor) where T : class, INetSerializable Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Deserializes a class that implements INetSerializable using a provided constructor. ```APIDOC ## Get(Func constructor) where T : class, INetSerializable ### Description Deserializes a class that implements `INetSerializable` using a provided constructor. ### Type Parameters - `T`: A class type implementing `INetSerializable`. ### Parameters - `constructor` (Func) - The constructor function to create an instance of `T`. ``` -------------------------------- ### Connect(IPEndPoint, string) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetManager.html Connects to a remote host using a connection key. ```APIDOC ## Connect(IPEndPoint, string) ### Description Connect to remote host with a connection key. ### Parameters - **target** (IPEndPoint) - Server end point (ip and port). - **key** (string) - Connection key. ### Returns - **NetPeer** - New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting. ### Exceptions - **InvalidOperationException** - If the manager is not running. Call Start(). ``` -------------------------------- ### Send() Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NtpRequest.html Sends the prepared NTP request to the server. ```APIDOC ## Send() ### Description Sends the NTP request to the configured server. If the request is successful, the callback provided during `NtpRequest` creation will be invoked with the `NtpPacket`. If an error occurs during sending or receiving, the callback will be invoked with a `null` parameter. ### Method `public void Send()` ### Usage: ```csharp // Assuming 'request' is an initialized NtpRequest object request.Send(); ``` ### Notes: - This method should be called after the `NtpRequest` object has been successfully created using one of the `Create` methods. ``` -------------------------------- ### Get(out string, int) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.Utils.NetDataReader.html Reads a string with a specified maximum length from the data stream and assigns it to the result parameter. ```APIDOC ## Get(out string, int) ### Description Reads a string with a length limit and assigns it to `result`. ### Method `public void Get(out string result, int maxLength)` ### Parameters #### Path Parameters - **result** (string) - Description: The string to be read. - **maxLength** (int) - Description: The maximum length of the string to read. ``` -------------------------------- ### NetPeerEnumerator Constructor Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetManager.NetPeerEnumerator.html Initializes a new instance of the NetPeerEnumerator struct with a specified NetPeer. ```APIDOC ## NetPeerEnumerator(NetPeer) ### Description Initializes a new instance of the NetPeerEnumerator struct. ### Parameters - **p** (NetPeer) - The NetPeer to initialize the enumerator with. ``` -------------------------------- ### ChannelsCount Property Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetManager.html Gets or sets the number of QoS channels available for messages. The value must be between 1 and 64. ```APIDOC ## ChannelsCount Property ### Description Gets or sets the QoS channel count per message type. The value must be between 1 and 64 channels. ### Property Value - **byte** - The number of channels. ``` -------------------------------- ### TooBigPacketException Constructor Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.TooBigPacketException.html Initializes a new instance of the TooBigPacketException class with a specified error message. ```APIDOC ## TooBigPacketException(string message) ### Description Initializes a new instance of the TooBigPacketException class with a specified error message. ### Parameters #### Parameters - **message** (string) - Required - The message that describes the error. ### Syntax ```csharp public TooBigPacketException(string message) ``` ``` -------------------------------- ### Connect(IPEndPoint, NetDataWriter) Source: https://github.com/revenantx/litenetlib/blob/master/docs/api/LiteNetLib.NetManager.html Connects to a remote host with additional connection data. ```APIDOC ## Connect(IPEndPoint, NetDataWriter) ### Description Connect to remote host with additional data. ### Parameters - **target** (IPEndPoint) - Server end point (ip and port). - **connectionData** (NetDataWriter) - Additional data for remote peer. ### Returns - **NetPeer** - New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting. ### Exceptions - **InvalidOperationException** - If the manager is not running. Call Start(). ```