/// <summary> /// Check server time offset, /// and sync it with client time if it is more than 5 seconds out /// </summary> async Task SyncServerTime(BrainHatServerStatus connectionStatus) { // sync time on the server if it is more than 5 seconds off if (connectionStatus.OffsetTime.TotalSeconds > 5 && connectionStatus.IpAddress.Length > 0) { Log?.Invoke(this, new LogEventArgs(this, "CreateNewHatClient", "Server time is more than 5 seconds behind system time, setting server time.", LogLevel.INFO)); var sw = new Stopwatch(); sw.Start(); var response = await Tcpip.GetTcpResponseAsync(connectionStatus.IpAddress, BrainHatNetworkAddresses.ServerPort, "ping"); if (response.Contains("ACK")) { sw.Stop(); response = await Tcpip.GetTcpResponseAsync(connectionStatus.IpAddress, BrainHatNetworkAddresses.ServerPort, $"settime?time={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + (sw.Elapsed.Milliseconds / 2)}"); if (response == null || !response.Contains("ACK")) { Log?.Invoke(this, new LogEventArgs(this, "CreateNewHatClient", "Failed to set server time.", LogLevel.ERROR)); } else { SyncedTime = true; } } else { Log?.Invoke(this, new LogEventArgs(this, "CreateNewHatClient", "Failed to get server ping.", LogLevel.ERROR)); } } }
public BrainHatStatusEventArgs(BrainHatServerStatus status) { Status = status; }