Search Unity

UdpClient Send Receive message, not fixed rate

Discussion in 'Multiplayer' started by Boardley, Nov 26, 2014.

  1. Boardley

    Boardley

    Joined:
    Nov 14, 2014
    Posts:
    14
    Hi there,
    bored by Unity Network's not fixed update rate, i tried to write a simple script with UdpClient, it sends and receives everything properly (I am not just talking about local host, but on the net, one standalone here, and one standalone on another pc (under the same router)).
    It is behaving same as Unity Network.
    sometimes it is going correctly, sometimes it is lagging,

    I tried lag compensation techniques but, didn't had that result.
    I would like to first fix the update rate (more constant then possible), then develop my lag compensation.

    Where is the problem? Unity Network, or UdpClient Communication, are both behaving same, so I guess there is something to do with the Router, is there anyway to fix the router update rate?
    I am not sending a lot of bytes, just 3 float of the vector of position of 1 transform.

    If there exists something to set router to update constantly would be awesome.
    Thanks.
    patrick
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    If you have to rely on router fixes, you won't have a lot of success with your network solution.
    Debugging this is probably tricky. I can't really help, aside from some tips:
    Get Wireshark and log the actual packages on client and server side. You can try to analyze if it's network lag or your own that's causing delays.
    Look out for any blocking methods (which must be put in a thread to not block the Unity main thread) and keep in mind that some things might simply be broken and not work as expected (but working on some platform/Editor).
     
    Boardley likes this.
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Constant update rates do not exist in networking. Its always possible that network congestion throws off the timing or 'shifts back packages' in time.I would recommend to look up the old Unity Networking example bundle as it had a great implementation of authorative server implementation with client side prediction that can handle lag up to 100ms with relative ease. Alternatively look up Valves Network article on client side prediction which is the paper that forms the basis of most modern day multiplayer action game implementations.

    If you want to roll your own solution then my recommendation is to immediately drop UDP sockets and go with Lidgren instead. It bases on UDP sockets but resolves several issues related to packet fragmentation etc that you otherwise might be hit by very hard. There are also libraries offered in the Unity asset store that base on Lidgren but add additional features to let you focus on creating your game like Tasharen Network.

    Your game currently seems to make the crucial error of assuming that 'if client 1 updates every 16ms, then all other clients running at the same rate will automatically be in sync', but that ignores some crucial issues in the networking environment, namely that the package send time is neither a multiply of this 16ms and even worse, it is not 0ms to be in sync.
    So what you really need to do first is to develop the core that ensures that the send time does not prevent the actions from being in sync.
    Valves solution and the one implementated in the original Unity Networking bundle is that it takes the user input and stores it in a command queue with a timestamp (basing on the network time) and send the input to the authorative server immediately. 100ms (configurable) later it will then apply this user input locally (thats the client side prediction). At the same time the server will apply this user input on his side too (which controls the physics and real gmae state) and have the position sync back to all users (either through a manual send or through NetworkView + Transform serialization) including the original sender of the input. the client will then interpolate towards this 'real state' it receives from the server (thats equal for all) to mix in his client side prediction.


    As you dropped me a PM regarding this with Android on the plate too though I would recommend to think about Photon Cloud, as an Android device on mobile phone lines will never be able to be the server.
     
    Boardley likes this.
  4. Boardley

    Boardley

    Joined:
    Nov 14, 2014
    Posts:
    14
    Thanks all, awesome posts, a lot in depth, really interesting.
    #region talking about my network
    I am going to download Wireshark, and learn it.
    I have just simple code for sending and receiving based on this thread.
    I didn't putted hands on wireshark, but the debug log says that the message arrives instantly when sent. This was the first test i did.
    So i have improved it little bit, to have something similar to networkView in my UdpNetwork. It syncronizes the position only of a cube moved by user input.
    It is better then Unity Network, but it looks as the cache of the router are full something like that, it goes smooth WITHOUT ANY INTERPOLATION (LAG COMPENSATION TECHNIQUE) for few frames, and then it goes to sleep for [0.5-0.8]s 3 times.
    so this is a similar graph of the result (1 axis only: update times)
    Code (csharp):
    1. ...............         .          .         ................         .          .         ................         .          .         .
    I will do Lag Compensation, I will work on that, but i can do that only after this is fixed. I can not have a network which is 0.5 or 0.8 in the past. the beautifull thing is that if goes perfectly smooth without any interpolation for few time, then it lags 3 or 4 times, then it regoes perfectly. it is a loop.
    #endregion
    #region talking about other networks

    I had a lag compensation for Unity Network, but it was not as smooth i wanted, it was still bumpy.
    I was sending pos, rot, tried also with sending rigidbody vel, and angular vel, but i didn't got the result.
    I will see Lidgren, but it looks i will have to redo all my UdpNetwork =(, it does not matter, what it matter is the result.
    #endregion

    About Photon Cloud,
    Ya i also see it, and liked it much. But i am needing something offline, under wifi network (router or wifi direct) without any Internet Connection.

    I am going to make a video to show what i am having.
     
  5. Boardley

    Boardley

    Joined:
    Nov 14, 2014
    Posts:
    14