//-------------------------------------------------------------------- // NtpBase - header // Copyright (C) Microsoft Corporation, 1999 // // Created by: Louis Thomas (louisth), 4-16-99 // // The basic message structure, definitions, and helper functions // #ifndef NTPBASE_H #define NTPBASE_H //-------------------------------------------------------------------- // Time formats // a clock reading, little-endian, in (10^-7)s struct NtTimeEpoch { unsigned __int64 qw; void dump(void); }; // a signed time offset, little-endian, in (10^-7)s struct NtTimeOffset { signed __int64 qw; void dump(void); }; // a length of time, little-endian, in (10^-7)s struct NtTimePeriod { unsigned __int64 qw; void dump(void); }; // a clock reading, big-endian, in (2^-32)s struct NtpTimeEpoch { unsigned __int64 qw; }; // a signed time offset, big-endian, in (2^-16)s struct NtpTimeOffset { signed __int32 dw; }; // a length of time, big-endian, in (2^-16)s struct NtpTimePeriod { unsigned __int32 dw; }; extern const NtTimeEpoch gc_teNtpZero; // convenient 'zero' extern const NtpTimeEpoch gc_teZero; // convenient 'zero' extern const NtTimePeriod gc_tpZero; // convenient 'zero' extern const NtTimeOffset gc_toZero; // convenient 'zero' //-------------------------------------------------------------------- // helpful conversion functions NtTimeEpoch NtTimeEpochFromNtpTimeEpoch(NtpTimeEpoch te); NtpTimeEpoch NtpTimeEpochFromNtTimeEpoch(NtTimeEpoch te); NtTimePeriod NtTimePeriodFromNtpTimePeriod(NtpTimePeriod tp); NtpTimePeriod NtpTimePeriodFromNtTimePeriod(NtTimePeriod tp); NtTimeOffset NtTimeOffsetFromNtpTimeOffset(NtpTimeOffset to); NtpTimeOffset NtpTimeOffsetFromNtTimeOffset(NtTimeOffset to); //-------------------------------------------------------------------- // Math operators static inline NtTimeOffset operator -(const NtTimeOffset toRight) { NtTimeOffset toRet; toRet.qw=-toRight.qw; return toRet; } static inline NtTimeOffset operator -(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) { NtTimeOffset toRet; toRet.qw=teLeft.qw-teRight.qw; return toRet; } static inline NtTimeOffset operator -(const NtTimeOffset toLeft, const NtTimeOffset toRight) { NtTimeOffset toRet; toRet.qw=toLeft.qw-toRight.qw; return toRet; } static inline NtTimeOffset operator +(const NtTimeOffset toLeft, const NtTimeOffset toRight) { NtTimeOffset toRet; toRet.qw=toLeft.qw+toRight.qw; return toRet; } static inline NtTimeOffset & operator /=(NtTimeOffset &toLeft, const int nDiv) { toLeft.qw/=nDiv; return toLeft; } static inline NtTimeOffset & operator -=(NtTimeOffset &toLeft, const NtTimeOffset toRight) { toLeft.qw-=toRight.qw; return toLeft; } static inline NtTimeOffset & operator +=(NtTimeOffset &toLeft, const NtTimeOffset toRight) { toLeft.qw-=toRight.qw; return toLeft; } static inline NtTimeEpoch operator +(const NtTimeEpoch teLeft, const NtTimePeriod tpRight) { NtTimeEpoch teRet; teRet.qw=teLeft.qw+tpRight.qw; return teRet; } static inline NtTimePeriod operator *(const NtTimePeriod tpLeft, const unsigned __int64 qwMult) { NtTimePeriod tpRet; tpRet.qw=tpLeft.qw*qwMult; return tpRet; } static inline NtTimePeriod & operator *=(NtTimePeriod &tpLeft, const unsigned __int64 qwMult) { tpLeft.qw*=qwMult; return tpLeft; } static inline NtTimePeriod operator /(const NtTimePeriod tpLeft, const int nDiv) { NtTimePeriod tpRet; tpRet.qw=tpLeft.qw/nDiv; return tpRet; } static inline NtTimePeriod & operator +=(NtTimePeriod &tpLeft, const NtTimePeriod tpRight) { tpLeft.qw+=tpRight.qw; return tpLeft; } static inline NtTimePeriod operator +(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) { NtTimePeriod tpRet; tpRet.qw=tpLeft.qw+tpRight.qw; return tpRet; } static inline NtTimePeriod & operator -=(NtTimePeriod &tpLeft, const NtTimePeriod tpRight) { tpLeft.qw-=tpRight.qw; return tpLeft; } static inline NtTimePeriod operator -(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) { NtTimePeriod tpRet; tpRet.qw=tpLeft.qw-tpRight.qw; return tpRet; } static inline bool operator <(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) { return teLeft.qw(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) { return teLeft.qw>teRight.qw; } static inline bool operator >=(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) { return teLeft.qw>=teRight.qw; } static inline bool operator ==(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) { return teLeft.qw==teRight.qw; } static inline bool operator !=(const NtTimeEpoch teLeft, const NtTimeEpoch teRight) { return teLeft.qw!=teRight.qw; } static inline bool operator <(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) { return tpLeft.qw(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) { return tpLeft.qw>tpRight.qw; } static inline bool operator >=(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) { return tpLeft.qw>=tpRight.qw; } static inline bool operator ==(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) { return tpLeft.qw==tpRight.qw; } static inline bool operator !=(const NtTimePeriod tpLeft, const NtTimePeriod tpRight) { return tpLeft.qw!=tpRight.qw; } static inline bool operator <(const NtTimeOffset toLeft, const NtTimeOffset toRight) { return toLeft.qw(const NtTimeOffset toLeft, const NtTimeOffset toRight) { return toLeft.qw>toRight.qw; } static inline bool operator >=(const NtTimeOffset toLeft, const NtTimeOffset toRight) { return toLeft.qw>=toRight.qw; } static inline bool operator ==(const NtTimeOffset toLeft, const NtTimeOffset toRight) { return toLeft.qw==toRight.qw; } static inline bool operator !=(const NtTimeOffset toLeft, const NtTimeOffset toRight) { return toLeft.qw!=toRight.qw; } static inline bool operator ==(const NtpTimeEpoch teLeft, const NtpTimeEpoch teRight) { return teLeft.qw==teRight.qw; } static inline bool operator !=(const NtpTimeEpoch teLeft, const NtpTimeEpoch teRight) { return teLeft.qw!=teRight.qw; } static inline NtTimePeriod abs(const NtTimeOffset to) { NtTimePeriod tpRet; tpRet.qw=((to.qw<0)?((unsigned __int64)(-to.qw)):((unsigned __int64)(to.qw))); return tpRet; } //-------------------------------------------------------------------- static inline NtTimePeriod minimum(NtTimePeriod tpLeft, NtTimePeriod tpRight) { return ((tpLeft