Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

LiteNetLib - another reliable udp library.

Discussion in 'Connected Games' 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:
    125
    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)
     
  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:
    125
    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:
    125
    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:
    125
    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:
    125
    @CallMeSpam there is no deep documentation (except XML comments to almost all methods) You can join discord and ask any questions :)
     
  12. Obsurveyor

    Obsurveyor

    Joined:
    Nov 22, 2012
    Posts:
    276
    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:
    125
    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:
    125
  15. Obsurveyor

    Obsurveyor

    Joined:
    Nov 22, 2012
    Posts:
    276
    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:
    125
    @Obsurveyor it must work on stable version too. (don't switch if you don't need)
     
  17. Obsurveyor

    Obsurveyor

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

    RevenantX

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

    Obsurveyor

    Joined:
    Nov 22, 2012
    Posts:
    276
    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:
    125
    @Obsurveyor if you read strings with NetDataReader you should write them with NetDataWriter
     
  21. Obsurveyor

    Obsurveyor

    Joined:
    Nov 22, 2012
    Posts:
    276
    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:
    125
    @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:
    64
    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:
    6
    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:
    125
    https://github.com/RevenantX/LiteNetLib/wiki/NetPacketProcessor-(NetSerializer)-usage