Search Unity

Question Getting round-trip time/current packet delay estimate

Discussion in 'Unity Transport' started by CosmoM, Feb 8, 2022.

  1. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Is there any way to get an estimate of the current RTT or packet delay? I know that
    ClientSimulatorParameters.PacketDelayMs on the UnityTransport component gets the packet delay set artificially, and that plus twice the tick rate would be the estimate of the RTT, but what about in a real-world scenario?
     
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    You can get an estimate of the RTT for traffic sent on pipelines with a
    ReliableSequencedPipelineStage
    . Here's an example of code fetching the RTT information from the pipeline's internal buffers.

    For non-reliable traffic there is no way to get RTT information currently (and it's not planned in the short term). We'd expect users to build that themselves by sending custom messages or integrating timestamps in other request/response traffic.
     
    CosmoM likes this.
  3. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Thanks for that! I did try to build it in myself but there doesn't seem to be a reliable time to use as timestamp. The documentation mentions that a client's localTime/serverTime and that of the server are offset such that e.g. a message sent from the client at its localTime arrives at the server's serverTime, but I've tested this and it's nowhere near correct. Instead it seems there's a 70ms delay built in that does not depend on latency. Is there another way to use timestamps that gives a delay time for a message? I've thought about using ticks but (a) I'm not sure those are synchronized and (b) that doesn't give a high temporal resolution.
     
  4. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Ping is just a measurement of two-way delay. So basically you send a message with some identifier and mark the time at which you sent it (you can also store the timestamp in the packet). The time can be taken from any local source (could be
    Time.realtimeSinceStartup
    ). The remote peer then resends you the same message and you compare the time you sent it and received and that's your RTT.

    Boss Room implements something similar with RPCs. Here's the code.
     
    CosmoM likes this.
  5. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Ah, I was hoping that there was a way to do it in a single communication (i.e. server receives a message and knows how long ago it was sent, in realtime). Thanks for the link to the relevant Boss Room code, I'll do something similar then!
     
  6. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    You'd need to have the clocks of both client and server be synchronized if you wanted to measure one-way delay like that. That's a much more difficult problem to solve.