Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  3. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

LiteNetLib - another reliable udp library.

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

  1. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    61
    NAT punch is pretty simple thing. It used in 90% for Peer 2 Peer connections.
    LiteNetLib uses algorithm very similar to STUN (https://en.wikipedia.org/wiki/STUN) but with different protocol.
    You must have one server(master server) with "white" ip address. This server can be behind NAT but with forwared ports.
    Server example: https://github.com/RevenantX/LiteNetLib/blob/master/LibSample/HolePunchServerTest.cs
    Code (CSharp):
    1. private NetManager _puncher; - server.
    Code (CSharp):
    1. void INatPunchListener.OnNatIntroductionRequest(NetEndPoint localEndPoint, NetEndPoint remoteEndPoint, string token)
    NAT punch works almost in all situations but there some exceptions - when 2 clients behind symmetric NAT and in some mobile networks.
    I will create better and clear example for NAT punch.
     
    Skylander17 likes this.
  2. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    61
    when client connects to server - you will receive NetPeer class in "OnPeerConnected" method - that will be your peer. Also you can store Server "peer" (really this is just connection betwen) by calling client.GetFirstPeer() after client.Connect(...);
    Code (CSharp):
    1. class SomeClass
    2. {
    3.     private NetPeer _serverConnection;
    4.     private NetManager _client;
    5.     public void Run()
    6.     {
    7.         _client = new NetManager(...);
    8.         _client.Connect("mysupergame.com"); //or any external ip
    9.         _serverConnection = _client.GetFirstPeer();
    10.     }
    11. }
     
  3. Reddogey

    Reddogey

    Joined:
    Feb 19, 2016
    Posts:
    9
    Thank you! Should the server just have a list of peer connections to keep track of them and send the messages accordingly?
     
  4. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    61
    Server(any NetManager) already have list of peers:
    Code (CSharp):
    1. public NetPeer[] GetPeers()
    2. //and faster method
    3. public void GetPeersNonAlloc(List<NetPeer> peers)
    NetManager has methods like
    Code (CSharp):
    1. public void SendToAll(NetDataWriter writer, SendOptions options)
    2. public void SendToAll(byte[] data, SendOptions options)
    3. public void SendToAll(byte[] data, int start, int length, SendOptions options)
    4. public void SendToAll(NetDataWriter writer, SendOptions options, NetPeer excludePeer)
    5. ... etc
    or you can store your NetPeers in some other place and send data directly to peers (clients)
     
  5. Reddogey

    Reddogey

    Joined:
    Feb 19, 2016
    Posts:
    9
    Lots of good info, the chm file wasn’t working for me when I tried it. Also, this dll doesn’t happen to work in c++ does it?
     
  6. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    61
    No. This DLL only for C# (.net, mono, dotnetcore, xamarin)
     
  7. Reddogey

    Reddogey

    Joined:
    Feb 19, 2016
    Posts:
    9
    Hey, I took the script from the example that was working and the dll moved it over to my unity project but now I am getting a few errors. It recognizes the DLL is there. What did I do wrong? upload_2017-12-4_18-1-26.png
     
    Last edited: Dec 5, 2017
  8. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    61
    Show your script. And use library sources (not DLL) in unity3d (best will be with UNITY compilation symbol defined)
     
  9. Reddogey

    Reddogey

    Joined:
    Feb 19, 2016
    Posts:
    9
    I am sorry, what do you mean by library sources. Here is my script.

    Code (CSharp):
    1. using UnityEngine;
    2. using LiteNetLib;
    3. using LiteNetLib.Utils;
    4.  
    5. public class GameClient : MonoBehaviour, INetEventListener
    6. {
    7.     private NetManager _netClient;
    8.  
    9.     [SerializeField] private GameObject _clientBall;
    10.     [SerializeField] private GameObject _clientBallInterpolated;
    11.  
    12.     private float _newBallPosX;
    13.     private float _oldBallPosX;
    14.     private float _lerpTime;
    15.  
    16.     void Start()
    17.     {
    18.         _netClient = new NetManager(this, "sample_app");
    19.         _netClient.Start();
    20.         _netClient.UpdateTime = 15;
    21.         _netClient.Connect("127.0.0.1", 5000);
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         _netClient.PollEvents();
    27.  
    28.         var peer = _netClient.GetFirstPeer();
    29.         if (peer != null && peer.ConnectionState == ConnectionState.Connected)
    30.         {
    31.             //Fixed delta set to 0.05
    32.             var pos = _clientBallInterpolated.transform.position;
    33.             pos.x = Mathf.Lerp(_oldBallPosX, _newBallPosX, _lerpTime);
    34.             _clientBallInterpolated.transform.position = pos;
    35.  
    36.             //Basic lerp
    37.             _lerpTime += Time.deltaTime / Time.fixedDeltaTime;
    38.  
    39.         }
    40.         else
    41.         {
    42.             _netClient.SendDiscoveryRequest(new byte[] {1}, 5000);
    43.         }
    44.     }
    45.  
    46.     void OnDestroy()
    47.     {
    48.         if (_netClient != null)
    49.             _netClient.Stop();
    50.     }
    51.  
    52.     public void OnPeerConnected(NetPeer peer)
    53.     {
    54.         Debug.Log("[CLIENT] We connected to " + peer.EndPoint);
    55.  
    56.  
    57.     }
    58.  
    59.     public void OnNetworkError(NetEndPoint endPoint, int socketErrorCode)
    60.     {
    61.         Debug.Log("[CLIENT] We received error " + socketErrorCode);
    62.     }
    63.  
    64.     public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
    65.     {
    66.         _newBallPosX = reader.GetFloat();
    67.  
    68.         var pos = _clientBall.transform.position;
    69.  
    70.         _oldBallPosX = pos.x;
    71.         pos.x = _newBallPosX;
    72.  
    73.         _clientBall.transform.position = pos;
    74.  
    75.         _lerpTime = 0f;
    76.     }
    77.  
    78.     public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader,
    79.         UnconnectedMessageType messageType)
    80.     {
    81.         if (messageType == UnconnectedMessageType.DiscoveryResponse && _netClient.PeersCount == 0)
    82.         {
    83.             Debug.Log("[CLIENT] Received discovery response. Connecting to: " + remoteEndPoint);
    84.             _netClient.Connect(remoteEndPoint);
    85.         }
    86.     }
    87.  
    88.     public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
    89.     {
    90.     }
    91.  
    92.     public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
    93.     {
    94.         Debug.Log("[CLIENT] We disconnected because " + disconnectInfo.Reason);
    95.     }
    96. }
     
    Last edited: Dec 5, 2017
  10. Reddogey

    Reddogey

    Joined:
    Feb 19, 2016
    Posts:
    9
    So I did what I think you said and I just added the scripts instead of the dll and still had the same problem with the same errors.

    upload_2017-12-4_21-45-10.png
     
  11. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    61
    Hmm. This is very strange. What version of Unity3d are you using? Can you send to me archived project?
    Maybe you already have NetManager class and you get conflicts?
     
  12. Reddogey

    Reddogey

    Joined:
    Feb 19, 2016
    Posts:
    9
    I totally forgot that I had another NetManager class in another folder..... So that fixes that rookie mistake. Do you still think I should use the source files more than the DLL, or should the DLL be fine?
     
  13. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    61
    Better will be source. (especially if you target mobiles) because of some epic unity3d network bugs.
     
  14. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    61
    New update! LiteNetLib 0.7.7
    • Removed unnecessary flow nodes
    • Improved sending speed
    • Thread safe NetPeer.Send and NetManager.PollEvents
    • Packet loss statistics
    • Added NetDataReader clone method.
    • Optimizations
    https://github.com/RevenantX/LiteNetLib/releases/tag/0.7.7

    And now in master branch next LiteNetLib version that has neat new features, fixes (and new bugs :) )
    • Reliable disconnect
    • Connect with user data and connection approval system
    • Refactored NetSerializer (documenation isn't updated at this time)
    • Connection logic stability
    • Unity improved defines (for source version)
    • Support to bind socket to specific IPAddress
    Some of this features can be unstable. So i will be very grateful if you help)
     
    nxrighthere likes this.
  15. BlankMauser

    BlankMauser

    Joined:
    Dec 10, 2013
    Posts:
    88
    I am trying to mess with the Unity3D Sample and im getting some compiler errors:
    [​IMG]

    Do I need to update to a specific version of Unity3D even though these don't seem Unity API related?
     
  16. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    61
    i forgot to update library in UnitySample. This was fixed.
     
    BlankMauser likes this.
  17. alexr1221

    alexr1221

    Joined:
    Mar 5, 2016
    Posts:
    25
    Thank you for this amazing project! I'll try to add features and optimizations whenever I can. Maybe I'll try to add a data compressor (huffman)
     
  18. RevenantX

    RevenantX

    Joined:
    Jul 17, 2012
    Posts:
    61
    @alexr1221 thanks!

    > data compressor (huffman)

    Thanks but i don't need compressor) I leave compressor selection to users (and if they need this)
     
    Last edited: Jan 18, 2018 at 9:09 AM
  19. alexr1221

    alexr1221

    Joined:
    Mar 5, 2016
    Posts:
    25
    Ok no problems!