示例#1
0
        public void VersionNumberTest()
        {
            NtpData validate = new NtpData(new byte[] { 0x23 });

            Assert.AreEqual(4, validate.VersionNumber);
            Assert.AreEqual(NtpMode.Client, validate.Mode);

            NtpData target   = new NtpData();
            byte    expected = 4;

            target.VersionNumber = expected;
            Assert.AreEqual(expected, target.VersionNumber);

            target.Mode = NtpMode.Client;
            Assert.AreEqual <NtpMode>(NtpMode.Client, target.Mode);
            Assert.AreEqual(expected, target.VersionNumber, "Version number changed by setting mode");

            Assert.AreEqual(0x23, target.ToArray()[0]);
        }
示例#2
0
        /// <summary>
        /// Called when a packet is received.
        /// </summary>
        /// <param name="state">The state.</param>
        private void OnReceive(IAsyncResult state)
        {
            EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

            if (PortOpen)
            {
                try
                {
                    MemoryStream receiveState = (MemoryStream)(state.AsyncState);

                    if (receiveState != null)
                    {
                        EndReceiveFrom(state, ref remoteEndPoint);

                        LastPacket = DateTime.UtcNow;

                        if (NewPacket != null)
                        {
                            //Read the Header
                            NtpData packet = NtpData.ReadPacket(receiveState);

                            NewPacketEventArgs args = new NewPacketEventArgs(packet)
                            {
                                ReceivedTime = (DateTime)LastPacket
                            };
                            args.SourceEndPoint = (IPEndPoint)remoteEndPoint;

                            NewPacket(this, args);
                        }
                    }
                }
                catch (Exception ex)
                {
                    OnUnhandledException(ex);
                }
                finally
                {
                    //Attempt to receive another packet.
                    StartReceive();
                }
            }
        }
示例#3
0
        /// <summary>
        /// Настройка NTP конфигурации
        /// </summary>
        public static async Task <string> SetNtpFromBody([FromBody] NtpData data)
        {
            _logger.Info("[SetNtpFromBody] Method started");
            try
            {
                using var xmlData = Converters.ToStringContent(data, "NTPServer");
                var response = await WebClient.Client.PutAsync("System/time/NtpServers/1", xmlData).Result.Content.ReadAsStringAsync();

                var jsonResponse = Converters.XmlToJson(response);
                var responseData = JsonConvert.DeserializeObject <CamResponses>(jsonResponse);

                _logger.Info("[SetNtpFromBody] Method has complete");
                return($"{responseData.responseStatus.StatusString,-10} Request url {responseData.responseStatus.RequestUrl}");
            }
            catch (Exception e)
            {
                _logger.Error($"[SetNtpFromBody] Method failed. \nError message: {e.Message}");
                return(e.Message);
            }
        }
示例#4
0
        public void RootDispersionTest()
        {
            NtpData target = new NtpData();
            double  actual = 250;

            target.RootDispersion = actual;
            Assert.AreEqual <double>(actual, target.RootDispersion);
            actual = -250;
            target.RootDispersion = actual;
            Assert.AreEqual <double>(actual, target.RootDispersion);
            actual = -31.25d;
            target.RootDispersion = actual;
            Assert.AreEqual <double>(actual, target.RootDispersion);
            actual = 3.90625;
            target.RootDispersion = actual;
            Assert.AreEqual <double>(actual, target.RootDispersion);
            actual = 8000;
            target.RootDispersion = actual;
            Assert.AreEqual <double>(actual, target.RootDispersion);
            actual = -8250;
            target.RootDispersion = actual;
            Assert.AreEqual <double>(actual, target.RootDispersion);
        }
示例#5
0
 /// <summary>
 /// Sends the specified packet to a target.
 /// </summary>
 /// <param name="target">The target.</param>
 /// <param name="packet">The packet.</param>
 public void Send(IPEndPoint target, NtpData packet)
 {
     byte[] data = packet.ToArray();
     BeginSendTo(data, 0, (int)data.Length, SocketFlags.None, target, null, null);
 }
示例#6
0
 /// <summary>
 /// Sends the specified packet.
 /// </summary>
 /// <param name="packet">The packet.</param>
 public void Send(NtpData packet)
 {
     Send(new IPEndPoint(MulticastGroup, Port), packet);
 }
示例#7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NewPacketEventArgs"/> class.
 /// </summary>
 /// <param name="packet">The packet.</param>
 public NewPacketEventArgs(NtpData packet)
 {
     Packet = packet;
 }