NetworkTime.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using Stopwatch = System.Diagnostics.Stopwatch;
  3. namespace Mirror
  4. {
  5. /// <summary>Synchronizes server time to clients.</summary>
  6. public static class NetworkTime
  7. {
  8. /// <summary>Ping message frequency, used to calculate network time and RTT</summary>
  9. public static float PingFrequency = 2.0f;
  10. /// <summary>Average out the last few results from Ping</summary>
  11. public static int PingWindowSize = 10;
  12. static double lastPingTime;
  13. // Date and time when the application started
  14. // TODO Unity 2020 / 2021 supposedly has double Time.time now?
  15. static readonly Stopwatch stopwatch = new Stopwatch();
  16. static NetworkTime()
  17. {
  18. stopwatch.Start();
  19. }
  20. static ExponentialMovingAverage _rtt = new ExponentialMovingAverage(10);
  21. static ExponentialMovingAverage _offset = new ExponentialMovingAverage(10);
  22. // the true offset guaranteed to be in this range
  23. static double offsetMin = double.MinValue;
  24. static double offsetMax = double.MaxValue;
  25. /// <summary>Returns double precision clock time _in this system_, unaffected by the network.</summary>
  26. // useful until we have Unity's 'double' Time.time
  27. //
  28. // CAREFUL: unlike Time.time, this is not a FRAME time.
  29. // it changes during the frame too.
  30. public static double localTime => stopwatch.Elapsed.TotalSeconds;
  31. /// <summary>The time in seconds since the server started.</summary>
  32. //
  33. // I measured the accuracy of float and I got this:
  34. // for the same day, accuracy is better than 1 ms
  35. // after 1 day, accuracy goes down to 7 ms
  36. // after 10 days, accuracy is 61 ms
  37. // after 30 days , accuracy is 238 ms
  38. // after 60 days, accuracy is 454 ms
  39. // in other words, if the server is running for 2 months,
  40. // and you cast down to float, then the time will jump in 0.4s intervals.
  41. //
  42. // TODO consider using Unbatcher's remoteTime for NetworkTime
  43. public static double time => localTime - _offset.Value;
  44. /// <summary>Time measurement variance. The higher, the less accurate the time is.</summary>
  45. // TODO does this need to be public? user should only need NetworkTime.time
  46. public static double timeVariance => _offset.Var;
  47. /// <summary>Time standard deviation. The highe, the less accurate the time is.</summary>
  48. // TODO does this need to be public? user should only need NetworkTime.time
  49. public static double timeStandardDeviation => Math.Sqrt(timeVariance);
  50. /// <summary>Clock difference in seconds between the client and the server. Always 0 on server.</summary>
  51. public static double offset => _offset.Value;
  52. /// <summary>Round trip time (in seconds) that it takes a message to go client->server->client.</summary>
  53. public static double rtt => _rtt.Value;
  54. /// <summary>Round trip time variance. The higher, the less accurate the rtt is.</summary>
  55. // TODO does this need to be public? user should only need NetworkTime.time
  56. public static double rttVariance => _rtt.Var;
  57. /// <summary>Round trip time standard deviation. The higher, the less accurate the rtt is.</summary>
  58. // TODO does this need to be public? user should only need NetworkTime.time
  59. public static double rttStandardDeviation => Math.Sqrt(rttVariance);
  60. public static void Reset()
  61. {
  62. stopwatch.Restart();
  63. _rtt = new ExponentialMovingAverage(PingWindowSize);
  64. _offset = new ExponentialMovingAverage(PingWindowSize);
  65. offsetMin = double.MinValue;
  66. offsetMax = double.MaxValue;
  67. lastPingTime = 0;
  68. }
  69. internal static void UpdateClient()
  70. {
  71. // localTime (double) instead of Time.time for accuracy over days
  72. if (localTime - lastPingTime >= PingFrequency)
  73. {
  74. NetworkPingMessage pingMessage = new NetworkPingMessage(localTime);
  75. NetworkClient.Send(pingMessage, Channels.Unreliable);
  76. lastPingTime = localTime;
  77. }
  78. }
  79. // executed at the server when we receive a ping message
  80. // reply with a pong containing the time from the client
  81. // and time from the server
  82. internal static void OnServerPing(NetworkConnection conn, NetworkPingMessage message)
  83. {
  84. // Debug.Log($"OnPingServerMessage conn:{conn}");
  85. NetworkPongMessage pongMessage = new NetworkPongMessage
  86. {
  87. clientTime = message.clientTime,
  88. serverTime = localTime
  89. };
  90. conn.Send(pongMessage, Channels.Unreliable);
  91. }
  92. // Executed at the client when we receive a Pong message
  93. // find out how long it took since we sent the Ping
  94. // and update time offset
  95. internal static void OnClientPong(NetworkPongMessage message)
  96. {
  97. double now = localTime;
  98. // how long did this message take to come back
  99. double newRtt = now - message.clientTime;
  100. _rtt.Add(newRtt);
  101. // the difference in time between the client and the server
  102. // but subtract half of the rtt to compensate for latency
  103. // half of rtt is the best approximation we have
  104. double newOffset = now - newRtt * 0.5f - message.serverTime;
  105. double newOffsetMin = now - newRtt - message.serverTime;
  106. double newOffsetMax = now - message.serverTime;
  107. offsetMin = Math.Max(offsetMin, newOffsetMin);
  108. offsetMax = Math.Min(offsetMax, newOffsetMax);
  109. if (_offset.Value < offsetMin || _offset.Value > offsetMax)
  110. {
  111. // the old offset was offrange, throw it away and use new one
  112. _offset = new ExponentialMovingAverage(PingWindowSize);
  113. _offset.Add(newOffset);
  114. }
  115. else if (newOffset >= offsetMin || newOffset <= offsetMax)
  116. {
  117. // new offset looks reasonable, add to the average
  118. _offset.Add(newOffset);
  119. }
  120. }
  121. }
  122. }