LiteNetLib - another reliable udp library.

Hi!. I want to introduce my “Reliable UDP” network library.

Discord

Documentation
https://revenantx.github.io/LiteNetLib/index.html

Features

  • - Lightweight*
  • - Small CPU and RAM usage*
  • - Small packet size overhead ( 1 byte for unreliable, 3 bytes for reliable packets )*
  • - Simple connection handling*
  • - Peer to peer connections*
  • - Helper classes for sending and reading messages*
  • - Different send mechanics*
  • - Reliable with order*
  • - Reliable without order*
  • - Ordered but unreliable with duplication prevention*
  • - Simple UDP packets without order and reliability*
  • - Fast packet serializer*
  • - Automatic small packets merging*
  • - Automatic fragmentation of reliable packets*
  • - Automatic MTU detection*
  • - UDP NAT hole punching*
  • - NTP time requests*
  • - Packet loss and latency simulation*
  • - IPv6 support (dual mode)*
  • - Connection statisitcs (need DEBUG or STATS_ENABLED flag)*
  • - Multicasting (for discovering hosts in local network)*
  • - Unity support*
  • - Supported platforms:*
  • - Windows/Mac/Linux (.NET Framework, Mono, .NET Core)*
  • - Android (Unity)*
  • - iOS (Unity)*
  • - UWP Windows 10 including phones*
  • - Lumin OS (Magic Leap)*

It used in some game projects already. And works very stable and fast.
You can use it without Unity3d (just Mono or .NET) that sometimes very useful for servers. It doesn’t use LINQ or Reflection (except in NetSerializer). And doesn’t use native code.

17 Likes

I like how simple this looks. I’ve used a number of other libraries (including lidgren multiple times) in Unity, but my choices are limited this time since this project needs to have no paid assets in it. I was going to go with Lidgren again, but I’ll give this a shot first.

1 Like

Can you provide any comparison/benchmark with Lidgren Network? It can be very interesting to compare them.
I will give it a shot if it results more stable and fast than Lidgren lib!

2 Likes

I created this library for my FPS multiplayer game because lidgren-network have bugs in Reliable channel (drop packets and reordering)
About performance: In last update i improved and tested transfer speed. Result - 100 mbytes per second on local socket with ReliableInOrder packets.

NTP Time Requests

Do you possibly have an example of this?

Sure. First strings of code.
https://github.com/RevenantX/LiteNetLib/blob/master/LibSample/Program.cs

Hey, thanks! That works, but is this SNTP? Trying to get several clients to tightly sync.

Oops - nevermind. I see from the NtpSyncModule class that it is true NTP.

Thank you.

Yes. It not so accurate.

From RFC :slight_smile:
An SNTP client implementing the on-wire protocol has a single server
and no dependent clients. It can operate with any subset of the NTP
on-wire protocol, the simplest approach using only the transmit
timestamp of the server packet and ignoring all other fields.
However, the additional complexity to implement the full on-wire
protocol is minimal so that a full implementation is encouraged.

That exactly what i do. Only timestamp is used. But it works with any servers)

Some updates since “June” post:
New version 0.5.3.

1 Like

August-September updates.

  • Connection statisitcs (received/sent packets/bytes count)
  • Multicasting (for discovering servers in local network)
  • Better latency simulation (you can set MinLatency)
  • Better information about disconnects (string replaced to informative Enum)
  • And as always - optimizations :slight_smile:

These features in master branch.

4 Likes

Hey can we get some documentation ? And maybe some more examples ?

RevenantX, I am attempting to use LiteNetLib to communicate between a server built in Unity and a stand alone server written in C#. I’ve been able to communicate between two Unity instances using LiteNetLib but cannot get a Unity instance to connect to a stand alone LiteNetLib server. Using the code shown below on the server the only response in the console dialog is “Database Server status true port 5000”. It appears that there is no data being received from the Unity client. I’ve tried disabling firewalls etc without improvement. The same client that I’m using connects to another Unity instance just fine. Any suggestions would be greatly appreciated.

using System;
using System.Threading;
using LiteNetLib;
using LiteNetLib.Utils;

class Program
{
    static void Main(string[] args)
    {
        int port = 5000;
       TestNet tn;

        EventBasedNetListener listener = new EventBasedNetListener();
        NetServer server = new NetServer(listener, 10, "LNDB Server");
        bool serverStat = server.Start(port);

        server.DiscoveryEnabled = true;
        server.UnconnectedMessagesEnabled = true;
        
        tn = new TestNet(server);

        Console.WriteLine("Database Server  status" + serverStat + " port " + port);

        listener.PeerConnectedEvent += peer =>
        {
            Console.WriteLine("We got connection: {0}", peer.EndPoint); // Show peer ip
            NetDataWriter writer = new NetDataWriter();                 // Create writer class
            writer.Put(1);                                              // Put some string
            peer.Send(writer, SendOptions.ReliableOrdered);             // Send with reliability

            listener.NetworkReceiveEvent += tn.OnServerReceive;        // Connect the OnNetworkReceive callback
        };

        listener.NetworkReceiveUnconnectedEvent += tn.OnNetworkReceiveUnconnected;

        tn.Poller();
    }
}

class TestNet
{
    private NetServer _netServer;

    public TestNet(NetServer ns)
    {
        _netServer = ns;
    }

    public void Poller()
    {
        while (true)
        {
            _netServer.PollEvents();
            Thread.Sleep(15);
        }
    }
    public void OnServerReceive(NetPeer peer, NetDataReader reader)
    {
        Console.WriteLine("[SERVER] Received data");
    }

    public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader, UnconnectedMessageType messageType)
    {
        Console.WriteLine("[SERVER] Received discovery request");

        if (messageType == UnconnectedMessageType.DiscoveryRequest)
        {
            _netServer.SendDiscoveryResponse(new byte[] { 1 }, remoteEndPoint);
        }
    }

}

Hi!. Can you show client connection code? Are you using the same “ConnectionKey” - “LNDB Server”?

Yes, same ConnectionKey. Code from Client:

using UnityEngine;
using LiteNetLib;
using LiteNetLib.Utils;

public class GameClient : MonoBehaviour, INetEventListener
{
    private NetClient _netClient;

    void Start ()
    {
        _netClient = new NetClient(this, "LNDB Server");
        _netClient.Start();
        _netClient.UpdateTime = 15;
    }

    void Update ()
    {
       _netClient.PollEvents();

        if (_netClient.IsConnected)
        {
            Debug.Log("Client is connected!");
        }
        else
        {
            Debug.Log("Sending discovery packet to endpoint");
            _netClient.SendDiscoveryRequest(new byte[] { 1 }, 5000);
        }
    }

    void OnDestroy()
    {
        if(_netClient != null)
            _netClient.Stop();
    }

    public void OnPeerConnected(NetPeer peer)
    {
        Debug.Log("[CLIENT] We connected to " + peer.EndPoint);
    }

    public void OnPeerDisconnected(NetPeer peer, DisconnectReason reason, int socketErrorCode)
    {
        Debug.Log("[CLIENT] We disconnected because " + reason);
    }

    public void OnNetworkError(NetEndPoint endPoint, int socketErrorCode)
    {
        Debug.Log("[CLIENT] We received error " + socketErrorCode);
    }

    public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
    {

    }

    public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader, UnconnectedMessageType messageType)
    {
        if (messageType == UnconnectedMessageType.DiscoveryResponse && _netClient.Peer == null)
        {
            Debug.Log("[CLIENT] Received discovery response. Connecting to: " + remoteEndPoint);
            _netClient.Connect(remoteEndPoint);
        }
    }

    public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
    {
       
    }
}

Do you use same versions of LiteNetLib on client and server? (in repo UnityExample there is outdated lib, use from Release pages)
P.S. And better set UpdateTime before Start() call

1 Like

I want to say yes, should all be from tag 0.6.1, but I’ll make sure all is the same. And I’ll try UpdateTime as suggested.

Hm… Server instance in your local network?
P.S. I just checked your code locally. It works.

RevenantX, I copied over the dll from the stand alone and that worked. As I write this I realize that I didn’t try the UpdateTime but will add that too. Thanks for suggesting to look at the library versions.

Looks like a great library. Looking forward to working with it.