/// <summary>Attempts to calculate what the remote offset should be based on the current ping</summary> private void CalculateRemoteOffset() { // Check if we got a fixed offset if (RemoteTickOffset > 0) { MDLog.Trace(LOG_CAT, $"Ping offset is set to be static at {RemoteTickOffset}"); CurrentRemoteTickOffsetTarget = RemoteTickOffset + OffsetBuffer; return; } // Check if we got any ping int HighestPing = (int)Mathf.Ceil(GameSynchronizer.GetMaxPlayerPing() * RemoteTickPingModifier); if (HighestPing == 0) { CurrentRemoteTickOffsetTarget = MinimumOffset + OffsetBuffer; MDLog.Trace(LOG_CAT, $"We got no ping setting offset to minimum offset of {MinimumOffset}"); return; } // Calculate offset based on ping int newOffset = (int)Mathf.Ceil(HighestPing / TICK_INTERVAL_MILLISECONDS); // If it is less than minimum set our target to minimum if (newOffset <= MinimumOffset) { CurrentRemoteTickOffsetTarget = MinimumOffset + OffsetBuffer; MDLog.Trace(LOG_CAT, $"Ping offset of {newOffset} is less than our minimum offset of {MinimumOffset}"); return; } // Calculate the difference between new and old float difference = (float)(newOffset - CurrentRemoteTickOffset) / newOffset; difference = Mathf.Abs(difference); // Is the difference larger than our allowed tolerance? if (Mathf.Abs(difference) >= RemoteTickPingTolerance) { MDLog.Trace(LOG_CAT, $"Ping difference is too large adjust remote tick offset target from {CurrentRemoteTickOffsetTarget} to {newOffset}"); // We need to adjust the remote tick offset CurrentRemoteTickOffsetTarget = newOffset + OffsetBuffer; } }
/// <summary> /// Called when the ping timer times out, sends a ping request to the given client. /// </summary> public void OnPingTimerTimeout() { // Check if network is still active if (!MDStatics.IsNetworkActive()) { MDLog.Trace(LOG_CAT, $"Network is no longer active"); return; } // Send ping request if (GameSynchronizer.GameClock == null) { GameSynchronizer.RpcId(PeerId, MDGameSynchronizer.METHOD_REQUEST_PING, OS.GetTicksMsec()); } else { int maxPlayerPing = GameSynchronizer.GetMaxPlayerPing() + (int)Ping; uint estimate = GameSynchronizer.GetPlayerTicksMsec(PeerId) + (uint)Ping; GameSynchronizer.RpcId(PeerId, MDGameSynchronizer.METHOD_REQUEST_PING, OS.GetTicksMsec(), estimate, GameSynchronizer.GameClock.GetTickAtTimeOffset(Ping), maxPlayerPing); } }