ChatterBox.Client.Presentation.Shared.Services.NtpService.GetNetworkTime C# (CSharp) Method

GetNetworkTime() public method

Retrieve the current network time from ntp server "time.windows.com".
public GetNetworkTime ( string ntpServer ) : void
ntpServer string
return void
        public async void GetNetworkTime(string ntpServer)
        {

            averageNtpRTT = 0; //reset

            currentNtpQueryCount = 0; //reset
            minNtpRTT = -1; //reset

            //NTP uses UDP
            ntpSocket = new Windows.Networking.Sockets.DatagramSocket();
            ntpSocket.MessageReceived += OnNTPTimeReceived;


            if (ntpQueryTimer == null)
            {
                ntpQueryTimer = new Windows.UI.Xaml.DispatcherTimer();
                ntpQueryTimer.Tick += NTPQueryTimeout;
                ntpQueryTimer.Interval = new TimeSpan(0, 0, 5); //5 seconds
            }

            if (ntpRTTIntervalTimer == null)
            {
                ntpRTTIntervalTimer = new Windows.UI.Xaml.DispatcherTimer();
                ntpRTTIntervalTimer.Tick += SendNTPQuery;
                ntpRTTIntervalTimer.Interval = new TimeSpan(0, 0, 0, 0, 200); //200ms

            }

            ntpQueryTimer.Start();

            try
            {
                //The UDP port number assigned to NTP is 123
                await ntpSocket.ConnectAsync(new Windows.Networking.HostName(ntpServer), "123");
                ntpRTTIntervalTimer.Start();

            }
            catch (Exception e)
            {
                Debug.WriteLine($"NtpSync: Exception when connect socket: {e.Message}");
                ntpResponseMonitor.Stop();
                ReportNtpSyncStatus(false);
            }

        }