void OnTimeSyncResponse(IInOctetStream stream) { //log.Trace("On Time Sync response"); if (state != ConnectionState.TimeSync) { if (useDebugLogging) { log.Warning("We are not in timesync state anymore"); } return; } var echoedTicks = stream.ReadUint64(); var latency = monotonicClock.NowMilliseconds() - (long)echoedTicks; // log.Trace($"Latency: {latency}"); var remoteTicks = stream.ReadUint64(); latencies.AddLatency((ushort)latency); ushort averageLatency; var isStable = latencies.StableLatency(out averageLatency); if (isStable) { var remoteTimeIsNow = remoteTicks + averageLatency / (ulong)2; //log.Trace($"We are stable! latency:{averageLatency}"); localMillisecondsToRemoteMilliseconds = (long)remoteTimeIsNow - monotonicClock.NowMilliseconds(); latencies = new LatencyCollection(); ConnectedAt = monotonicClock.NowMilliseconds(); SwitchState(ConnectionState.Connected, 100); } else { //log.Trace("Not stable yet, keep sending"); } }
public static PongResponseHeader Deserialize(IInOctetStream stream) { var elapsedMilliseconds = (long)stream.ReadUint64(); return(new PongResponseHeader(elapsedMilliseconds)); }