Ejemplo n.º 1
0
        public static NtpResponse ParseBytes(byte[] bytes)
        {
            if (bytes.Length != 48)
            {
                return(null);
            }

            var response = new NtpResponse();

            response.TransmitTimestampUtc  = response.GetDateTime(bytes, TransmitTimestampOffset);
            response.ReceiveTimestampUtc   = response.GetDateTime(bytes, ReceiveTimestampOffset);
            response.OriginateTimestampUtc = response.GetDateTime(bytes, OriginateTimestampOffset);
            response.ReferenceTimestampUtc = response.GetDateTime(bytes, ReferenceTimestampOffset);
            response.RootDispersion        = BitConverter.ToInt32(new byte[] { bytes[8], bytes[9], bytes[10], bytes[11] }, 0);
            response.RootDelay             = BitConverter.ToInt32(new byte[] { bytes[4], bytes[5], bytes[6], bytes[7] }, 0);
            response.PrecisionLog2Seconds  = Convert.ToInt32(bytes[3]);
            response.PrecisionLog2Seconds  = Convert.ToInt32(bytes[2]);
            response.Stratum             = response.GetStratum(bytes[1]);
            response.ReferenceIdentifier = response.GetReferenceIdentifier(response.Stratum, new byte[] { bytes[12], bytes[13], bytes[14], bytes[15] });
            response.LeapIndicator       = response.GetLeapIndictator(bytes[0]);
            response.VersionNumber       = response.GetVersionNumber(bytes[0]);
            response.Mode = response.GetMode(bytes[0]);

            return(response);
        }
Ejemplo n.º 2
0
        public byte[] GetOriginateTimestamp()
        {
            var time                = OriginateTimestampUtc;
            var totalTicks          = (time - Base).Ticks;
            var totalSeconds        = (uint)(totalTicks / 10000000);
            var leftOverPicoseconds = (uint)(((totalTicks % 10000000) * 100000) / 232);

            totalSeconds        = NtpResponse.SwapEndianness(totalSeconds);
            leftOverPicoseconds = NtpResponse.SwapEndianness(leftOverPicoseconds);
            var result = new byte[8];

            BitConverter.GetBytes(totalSeconds).CopyTo(result, 0);
            BitConverter.GetBytes(leftOverPicoseconds).CopyTo(result, 4);
            return(result);
        }