Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

LiteNetLib - another reliable udp library.

Discussion in 'Multiplayer' started by RevenantX, Jun 30, 2016.

  1. Eric-Gu

    Eric-Gu

    Joined:
    Feb 19, 2014
    Posts:
    28
  2. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    0.8 beta release
    https://github.com/RevenantX/LiteNetLib/releases/tag/v0.8.2-beta
    • Added new NetPacketProcessor for easy serializer usage
    • Added reliable disconnect with user data
    • Added connection requests with user data and OnConnectionRequest on remote side to check incoming data and decide to reject (with reason) or accept incoming peer.
    • Added possibility to bind to specific ip address (IPv4 and IPv6)
    • Added possibility to recycle incoming data (for low GC consumption)
    • Changed logging logic
    • Improved connection/disconnection logic
    • Improved speed
    • Updated .netcore and .netstandard requirements to 2.0
    • Tons of fixes and improvements (which I just forgot)
     
    ModLunar likes this.
  3. unity_tQoEL-bXycWSlg

    unity_tQoEL-bXycWSlg

    Joined:
    May 11, 2018
    Posts:
    4
    Hello sir,

    I want to know if the LiteNetLib library allows to connect a client in C#/Unity with an UDP server in C++.

    If this is possible, could you please give some indications about the walkthrough.

    In any case, thank you, for your work.
     
  4. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    If you need just connection - you can implement part of library in C++ (not hardest thing). But if you need reliable channel you will need to rewrite almost all library in C++)
     
  5. unity_tQoEL-bXycWSlg

    unity_tQoEL-bXycWSlg

    Joined:
    May 11, 2018
    Posts:
    4
    Thank for your fast reply.

    So, if i send asynchronously some UDP packet (with any application or language) on an IP adress and a port given, it's not possible to listen with liteNetLib library without create a connection previously.

    is it correct?

    Thank you very much.
     
  6. unity_tQoEL-bXycWSlg

    unity_tQoEL-bXycWSlg

    Joined:
    May 11, 2018
    Posts:
    4
    To add some precision.

    I try to connect a client created with LiteNetLib, and push some packets from the application "PacketSender", but without result.

    I have verified the port but even if i get a response after the call to the "connect" method, i never enter in the "OnNetworkReceive" method after send somepackets.

    Again, thank you very much to take time for reply.
     
  7. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    You can use Unconnected Messages for this. They very simple in structure - first byte = 11 other bytes - user data.
    (and call netManager.UnconnectedMessagesEnabled = true)
     
  8. unity_tQoEL-bXycWSlg

    unity_tQoEL-bXycWSlg

    Joined:
    May 11, 2018
    Posts:
    4
    thank you
     
  9. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    LiteNetLib 0.8.3 stable release :)
    https://github.com/RevenantX/LiteNetLib/releases/tag/v0.8.3
    Changelog:
    (differences from 0.7.7.3)
    • Added new NetPacketProcessor for easy serializer usage
    • Added reliable disconnect with user data
    • Added connection requests with user data and OnConnectionRequest on remote side to check incoming data and decide to reject (with reason) or accept incoming peer.
    • Added possibility to bind to specific ip address (IPv4 and IPv6)
    • Added possibility to recycle incoming data (for low GC consumption)
    • Added basic time sync things to NetPeer
    • Improved and enabled merging for all cases (setting removed) (it always reduces traffic)
    • Improved ping logic (more accurate and stable)
    • Changed logging logic
    • Improved connection/disconnection logic
    • Improved speed
    • Updated .netcore and .netstandard requirements to 2.0
    • Unity3d specific fixes
    • Tons of fixes and improvements (which I just forgot)
     
    Flavelius, MasoInar, Zoey_O and 2 others like this.
  10. CallMeSpam

    CallMeSpam

    Joined:
    Feb 10, 2018
    Posts:
    19
    Hi, just wanted to say that I like your networking package, thanks for making it available. The code and examples are clear enough that I was able to write some adapters to get it working with an existing project in a few hours, but there are a lot of pieces of code I don't understand. Is there any in-depth documentation, or tutorials on it's use?
     
  11. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    @CallMeSpam there is no deep documentation (except XML comments to almost all methods) You can join discord and ask any questions :)
     
    ModLunar likes this.
  12. Obsurveyor

    Obsurveyor

    Joined:
    Nov 22, 2012
    Posts:
    277
    Could I get some help with a simple Peer-to-Peer broadcast setup in Unity? I know that messages are getting sent(verified using a listener console program) but I'm not getting anything from a receiver in Unity.

    NetworkClient.cs:
    Code (CSharp):
    1. using System.Net;
    2. using System.Net.Sockets;
    3. using LiteNetLib;
    4. using Debug = UnityEngine.Debug;
    5.  
    6. public class NetworkClient : INetEventListener
    7. {
    8.     public void OnPeerConnected(NetPeer peer)
    9.     {
    10.         Debug.Log("OnPeerConnected");
    11.     }
    12.  
    13.     public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
    14.     {
    15.         Debug.Log("OnPeerDisconnected");
    16.     }
    17.  
    18.     public void OnNetworkError(IPEndPoint endPoint, SocketError socketError)
    19.     {
    20.         Debug.Log("OnPeerConnected");
    21.     }
    22.  
    23.     public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
    24.     {
    25.         Debug.Log(reader.GetString(100));
    26.     }
    27.  
    28.     public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType)
    29.     {
    30.         Debug.Log(reader.GetString(100));
    31.     }
    32.  
    33.     public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
    34.     {
    35.         Debug.Log("OnNetworkLatencyUpdate");
    36.     }
    37.  
    38.     public void OnConnectionRequest(ConnectionRequest request)
    39.     {
    40.         Debug.Log("OnConnectionRequest");
    41.     }
    42. }
    43.  
    Comms.cs:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using System.Net;
    4. using System.Text;
    5. using LiteNetLib;
    6.  
    7. public class Comms : MonoBehaviour
    8. {
    9.     private NetworkClient networkClient;
    10.     private NetManager networkManager;
    11.     private IPEndPoint ipEndPoint;
    12.  
    13.     void Start()
    14.     {
    15.         networkClient = new NetworkClient();
    16.         networkManager = new NetManager(networkClient);
    17.         networkManager.UnconnectedMessagesEnabled = true;
    18.         networkManager.DiscoveryEnabled = true;
    19.         networkManager.SendDiscoveryRequest(Encoding.ASCII.GetBytes("SendDiscoveryRequest"), 15000);
    20.         ipEndPoint = new IPEndPoint(IPAddress.Broadcast, 15000);
    21.  
    22.         if(!networkManager.Start(IPAddress.Any, IPAddress.IPv6None, 15000))
    23.         {
    24.             Debug.Log("Failed to start networkManager");
    25.         }
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         networkManager.PollEvents();
    31.     }
    32.  
    33.     void OnDestroy()
    34.     {
    35.         if(networkManager != null)
    36.         {
    37.             networkManager.Stop();
    38.         }
    39.     }
    40.  
    41.     public void Send1()
    42.     {
    43.         networkManager.SendUnconnectedMessage(Encoding.ASCII.GetBytes("Hello world!"), ipEndPoint);
    44.     }
    45.  
    46.     public void Send2()
    47.     {
    48.         networkManager.SendUnconnectedMessage(Encoding.ASCII.GetBytes("Hello there too!"), ipEndPoint);
    49.     }
    50. }
     
    Last edited: Mar 18, 2019
  13. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    To use broadcast - use SendDiscoveryRequest (and enable DiscoveryEnabled on receiving side in NetManager)
    And receive it in OnNetworkReceiveUnconnected
     
  14. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
  15. Obsurveyor

    Obsurveyor

    Joined:
    Nov 22, 2012
    Posts:
    277
    Added both to code in my post(edited to reflect) and still not getting anything.

    I'm using 0.8.3, do I need to switch to master?
     
  16. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    @Obsurveyor it must work on stable version too. (don't switch if you don't need)
     
  17. Obsurveyor

    Obsurveyor

    Joined:
    Nov 22, 2012
    Posts:
    277
    Well I still don't receive anything after adding the discovery stuff. :(
     
  18. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    @Obsurveyor try change firewall settings (it often blocks modern unity in windows by default)
     
  19. Obsurveyor

    Obsurveyor

    Joined:
    Nov 22, 2012
    Posts:
    277
    Well, I'm sorry about that usually Windows Firewall behaves well for me but changing the rule for the Unity editor did fix it(I don't know why it created it for Domain only, this is a standalone machine). However, I'm not getting my strings, just a blank line in the debug log. Is:

    Code (CSharp):
    1. networkManager.SendUnconnectedMessage(Encoding.ASCII.GetBytes("Hello world!"), ipEndPoint);
    invalid? Should I be using NetDataWriter instead?
     
  20. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    @Obsurveyor if you read strings with NetDataReader you should write them with NetDataWriter
     
  21. Obsurveyor

    Obsurveyor

    Joined:
    Nov 22, 2012
    Posts:
    277
    Great, thanks a lot, working now.
     
  22. ivan866

    ivan866

    Joined:
    Dec 28, 2018
    Posts:
    2
    You need to open the DLL file in DependencyWalker tool and see if it has mangled method names [looking like '@2112@asSHDSJDBH@actual-method-name']. If it does, you need to recompile this DLL with these flags
    > `-Wl,--kill-at
    or
    > `CFLAGS="-O -arch i386" CXXFLAGS="-O -arch i386" LDFLAGS="-arch i386"`
    and use Calling Convention either `cdecl` or `stdcall`.
    I do not know which one will do the trick.
     
  23. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    @ivan866 c# dll (assembly) and C++ dll are different things....
     
  24. jezun3twork

    jezun3twork

    Joined:
    Jun 6, 2019
    Posts:
    1
    Hi @RevenantX we are using litenetlib in our unity project, everything works great, we been using this library for like 3 months, but we are having issues to track network errors, im using the NetworkErrorEvent but this never gets called. I tried turning off the internet connection and keep sending messages, i also try using the Network Link Conditioner, with package loss. And i never gets any error event. Im missing something?
     
  25. JWLewis777

    JWLewis777

    Joined:
    May 17, 2015
    Posts:
    176
    Hello,
    I recently decided to get back into multiplayer. I previously had a working system using Unet with a relay server but decided that it wasn't the path I wanted to go. Unity also deprecating Unet made my system a lost cause.

    I wanted to check out LiteNetLib for the main reasons of how simple it is, how easy it looks to implement after checking out the samples and for a simple peer 2 peer system.

    After getting a chat system up and running and working great using the sample code, I wanted to change a few things and add the ability to input a ip address, port number and server key.

    Everything was looking great, but I started really getting confused on the order of events.

    In the client there is a _netClient.Start(), with options to _netClient.Start(IPV4 Address, IPV6 Address, Port)
    I wasnt sure if there is where I was supposed to supply the ip address and not having the IPV6 address made this unusable for me.

    So basically in the Unity sample, this is the order of events in the Client...
    Code (CSharp):
    1. void Start(){
    2.      _netClient.Start(); //Thought this is where I would specify my own IP but if I use that, it requires the IPV6 also
    3. }
    4.  
    5. void Update(){
    6.      _netClient.PollEvents();
    7.      peer = _netClient.FIrstPeer; //Trying to grab the first connected device?
    8.      if(peer != && peer.ConnectionState == ConnectionState.Connected){
    9.           //Doing nothing here
    10.      }
    11.      else{
    12.           _netClient.SendBroadcast(new byte[] {1}, portNum); //Not really sure whats happening here. If I havent connected, who am I broadcasting to? Is this a lan broadcast?
    13.      }
    14. }
    15.  
    16. public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType){
    17.      if(messageType == UnconnectedMessageType.BasicMessage && _netClient.PeersCount == 0 && reader.GetInt() == 1){
    18.      _netClient.Connect(remoteEndPoint, customKey); //So basically server responded to broadcast, sent this message, now client is connecting to that servers IP with a server key. We are now connected
    19. }
    20. }
    So this is as a simple of a break down as I can see using the unity sample but where would I specifiy my own IP address to connect to in this system?

    Thanks for any help!
    :)

    UPDATE:
    My dumb butt downloaded the dev version 0.9, once I downloaded 0.8 and put the LiteNetLib folder in the assets the way its supposed to be and removed the broadcast methods, everything worked great!

    Sorry for any confusion! :O
     
    Last edited: Jul 28, 2019
  26. xCeas

    xCeas

    Joined:
    Mar 31, 2013
    Posts:
    50
    Hi @RevenantX I really like your library and it works very well!
    I have some issues with the NetPacketProcessor, I'm sorry if I ask a question you've already answered before..

    What is the purpose of
    Code (CSharp):
    1. _packetProcessor.[B]SubscribeReusable[/B]<SomeType>(SomeMethod);
    If I remove the SubscribeReusable from my client code, the Deserialize process goes wrong for my packets.

    Thanks a lot
     
  27. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    https://github.com/RevenantX/LiteNetLib/wiki/NetPacketProcessor-(NetSerializer)-usage
     
  28. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    Jelmer123 and hungrybelome like this.
  29. GambitMonkey

    GambitMonkey

    Joined:
    Apr 5, 2016
    Posts:
    60
    RevenantX I was starting to look at moving to version 0.9 and it seems that the NetManager has lost the DiscoveryEnabled property. Is there a document that shows what has been deprecated in 0.9?

    Thank you and thank you for this wonderful library
     
  30. GambitMonkey

    GambitMonkey

    Joined:
    Apr 5, 2016
    Posts:
    60
    RevenantX It looks like DiscoveryEnabled was replaced with BroadcastReceiveEnabled. Is that correct?
     
  31. PixelizedPlayer

    PixelizedPlayer

    Joined:
    Feb 27, 2013
    Posts:
    540
  32. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    Yes. It just renamed.
     
  33. Ilia83

    Ilia83

    Joined:
    Oct 25, 2016
    Posts:
    3
    Hi, RevenantX.
    Will this library work with IL2CPP scripting backend?
     
  34. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    Hi. Sure :) It works with IL2CPP
     
    ModLunar likes this.
  35. GambitMonkey

    GambitMonkey

    Joined:
    Apr 5, 2016
    Posts:
    60
    @RevenantX Happy New Year to you sir. Over this holiday season I have started to deep dive into the PlayFab universe and I have a question for you if you do not care.

    I am working on QoS to their servers and they specify their requirements to do so as the following:
    1. Create a UDP socket.
    2. Send a single UDP datagram to port 3075 on the QoS server. The message content must start with 2 bytes of 255 (1111 1111 1111 1111).
    3. The server will reply with a single datagram, with the message contents having first 2 bytes "flipped" to 0 (0000 0000 0000 0000). The rest of the datagram contents will be copied from the initial ping.
    4. Measure the time between sending the UDP message and receiving a response
    https://api.playfab.com/docs/tutori...e-qos-beacons-to-measuplayer-latency-to-azure

    I am able to retrieve a listing of their QoS servers for each region and now I am just trying to figure out how to ping them using the above criteria and your library. I have it somewhat working with the System.Net.Sockets.UdpClient class, but since I am already using your library for my GameServer code just makes sense to me to stick with your library.

    Also I figure you have a much more elegant way to ping multiple servers and handle the receive callbacks.

    Thanks in advance and hope you are well and thank you for all that you do for the community.
     
  36. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    @docseuss just use simple UDP socket , or UDP client. LiteNetLib use internally special protocol that not compatible with this.
     
  37. GambitMonkey

    GambitMonkey

    Joined:
    Apr 5, 2016
    Posts:
    60
    @RevenantX Thank you for the reply and I guess I will keep going down the road I have been. Happy New Year to you
     
  38. john-gregory

    john-gregory

    Joined:
    Oct 28, 2013
    Posts:
    2
    Just a heads up. Using the latest build from GitHub, the individual data fragments are collated into a single NetPacket which has a maximum data size of 65535 (max(ushort)), so the maximum data message size is currently 65535, which includes packet type, and any field information in the NetworkMessage (including binary byte arrays and sizes). I'm working on a prototype, and had to chunk by binary sends into chunks on size 0xF000 (to allow space for a few other fields and packet type info).

    Regards,
    JPG
     
  39. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    Are you sure? Because i didn't changed anything. So if you cannot send data more than 65535 bytes this is bug. Can you send example?
     
  40. ReaperxOG

    ReaperxOG

    Joined:
    Nov 20, 2019
    Posts:
    2
    Hey @RevenantX !

    I have been following this project for awhile now, and am finally at a point where I need to decide on a net lib to use. I was just curious on how you feel this holds up against RakNet?
     
  41. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    Decide on features what you need. Compare and select.
     
  42. jhbyhuangzejie

    jhbyhuangzejie

    Joined:
    Mar 10, 2017
    Posts:
    7
    Hello request a problem, I send pictures of more than 10 m, converted into bytes writer. PutBytesWithLength () method, which can be one-time send it, I use DeliveryMethod ReliableOrdered
     
  43. jhbyhuangzejie

    jhbyhuangzejie

    Joined:
    Mar 10, 2017
    Posts:
    7
    Hello, is there a limit to the size of the split package?I have a picture of 10M to send. How do I send it?Thank you very much!
     
  44. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
  45. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    LiteNetLib 0.9.2.2 released
    https://github.com/RevenantX/LiteNetLib/releases/tag/v0.9.2.2
    (changes since 0.8.3)
    • Added multiple channels for Reliable and Sequenced methods (64 for each type of message) (configured by NetManager.ChannelsCount)
    • Added IPv6Enabled, StatsEnabled to NetManager - to allow enable/disable this features without recompiling and defines (IPV6_ENABLED, STATS_ENABLED)
    • Added packet layers that allows encrypt data or check checksums (Crc32C SSE optimized and XOR already included)
    • Added feature that allows trigger callback on reliable packets when packet delivered (#248)
    • Added RejectForce to reject connection requests without "reliable disconnect" (very handy in DDOS case)
    • Fixed some disconnection cases
    • Fixed ReliableSequenced channel
    • SendDiscoveryRequest renamed to SendBroadcast
    • More informative DisconnectReason in DisconnectInfo
    • Improved connection logic and speed
    • Better packets recycling (less pressure on GC)
    • Added DocFX documentation (https://revenantx.github.io/LiteNetLib/api/index.html)
    • Improved and optimized NetSerializer (~3x faster than previous version)
    • Minimum .netcore version increased to 2.1
    • Fixes for recent Unity3d (2018/2019)
    • Overall optimizations and stability improvements
     
    Last edited: May 12, 2020
  46. scheichs

    scheichs

    Joined:
    Sep 7, 2013
    Posts:
    73
    Hey Revenant! I'm loving this lib. Really great work! I don't know if I'm doing something wrong, but with the version 0.9.2.2 I get disconnect in the NatPuncherSample

    Code (CSharp):
    1. === HolePunch Test ===
    2. [MAIN] NTP time test offset: 00:00:01.5527040
    3. Press ESC to quit
    4. Wait peer created. i(192.168.0.11:52101) e([::1]:52101)
    5. Wait peer found, sending introduction...
    6. host - i(192.168.0.11:52101) e([::1]:52101)
    7. client - i(192.168.0.11:52102) e([::1]:52102)
    8. Success C1. Connecting to C2: 192.168.0.11:52102, connection created: True
    9. Success C1. Connecting to C2: [::1]:52102, connection created: True
    10. Success C2. Connecting to C1: 192.168.0.11:52101, connection created: True
    11. Success C2. Connecting to C1: [::1]:52101, connection created: False
    12. PeerConnected: [::1]:52101
    13. PeerConnected: [::1]:52102
    14. PeerDisconnected: ConnectionFailed
    15. PeerDisconnected: ConnectionFailed
    Can you maybe confirm that and do you have a suggestion how to solve it?
     
  47. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    @scheichs there is one bug with simultaneous connection from both sides. But here another thing. Peers have 2 IPs (IPv4 and IPv6) so you connected by IPv6 and "failed" by IPv4. You can disable one of protocols to get more predictable results
     
  48. scheichs

    scheichs

    Joined:
    Sep 7, 2013
    Posts:
    73
    Thanks for the quick response! Do you have another donation mehtod than bountysource? Maybe direct paypal donate? Otherwise I'm going to register bountysource...
     
    ModLunar likes this.
  49. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    144
    @scheichs Thanks. But now i have only bountysource option :\
     
  50. scheichs

    scheichs

    Joined:
    Sep 7, 2013
    Posts:
    73
    sure no prob. Will donate tomorrow!
     
    ModLunar likes this.
unityunity