IdTimeUDP.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.7 2/10/2005 2:24:38 PM JPMugaas
  18. Minor Restructures for some new UnixTime Service components.
  19. Rev 1.6 2004.02.03 5:44:36 PM czhower
  20. Name changes
  21. Rev 1.5 1/21/2004 4:21:00 PM JPMugaas
  22. InitComponent
  23. Rev 1.4 1/3/2004 1:00:06 PM JPMugaas
  24. These should now compile with Kudzu's change in IdCoreGlobal.
  25. Rev 1.3 10/26/2003 5:16:52 PM BGooijen
  26. Works now, times in GetTickDiff were in wrong order
  27. Rev 1.2 10/22/2003 04:54:26 PM JPMugaas
  28. Attempted to get these to work.
  29. Rev 1.1 2003.10.12 6:36:46 PM czhower
  30. Now compiles.
  31. Rev 1.0 11/13/2002 08:03:24 AM JPMugaas
  32. }
  33. unit IdTimeUDP;
  34. interface
  35. {$i IdCompilerDefines.inc}
  36. uses
  37. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  38. Classes,
  39. {$ENDIF}
  40. IdGlobal,
  41. IdAssignedNumbers, IdUDPBase, IdGlobalProtocols, IdUDPClient;
  42. type
  43. TIdCustomTimeUDP = class(TIdUDPClient)
  44. protected
  45. FBaseDate: TDateTime;
  46. FRoundTripDelay: UInt32;
  47. //
  48. function GetDateTimeCard: UInt32;
  49. function GetDateTime: TDateTime;
  50. procedure InitComponent; override;
  51. public
  52. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  53. constructor Create(AOwner: TComponent); reintroduce; overload;
  54. {$ENDIF}
  55. {This synchronizes the local clock with the Time Server}
  56. function SyncTime: Boolean;
  57. {This is the number of seconds since 12:00 AM, 1900 - Jan-1}
  58. property DateTimeCard: UInt32 read GetDateTimeCard;
  59. {This is the current time according to the server. TimeZone and Time used
  60. to receive the data are accounted for}
  61. property DateTime: TDateTime read GetDateTime;
  62. {This is the time it took to receive the Time from the server. There is no
  63. need to use this to calculate the current time when using DateTime property
  64. as we have done that here}
  65. property RoundTripDelay: UInt32 read FRoundTripDelay;
  66. published
  67. end;
  68. TIdTimeUDP = class(TIdCustomTimeUDP)
  69. published
  70. {This property is used to set the Date that the Time server bases its
  71. calculations from. If both the server and client are based from the same
  72. date which is higher than the original date, you can extend it beyond the
  73. year 2035}
  74. property BaseDate: TDateTime read FBaseDate write FBaseDate;
  75. property Port default IdPORT_TIME;
  76. end;
  77. implementation
  78. uses
  79. {$IFDEF USE_VCL_POSIX}
  80. {$IFDEF OSX}
  81. Macapi.CoreServices,
  82. {$ENDIF}
  83. Posix.SysTime,
  84. {$ENDIF}
  85. IdStack, SysUtils; //Sysutils added to facilitate inlining.
  86. { TIdCustomTimeUDP }
  87. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  88. constructor TIdCustomTimeUDP.Create(AOwner: TComponent);
  89. begin
  90. inherited Create(AOwner);
  91. end;
  92. {$ENDIF}
  93. procedure TIdCustomTimeUDP.InitComponent;
  94. begin
  95. inherited;
  96. Port := IdPORT_TIME;
  97. {This indicates that the default date is Jan 1, 1900 which was specified
  98. by RFC 868.}
  99. FBaseDate := TIME_BASEDATE;
  100. end;
  101. function TIdCustomTimeUDP.GetDateTime: TDateTime;
  102. var
  103. BufCard: UInt32;
  104. begin
  105. BufCard := GetDateTimeCard;
  106. if BufCard <> 0 then begin
  107. {The formula is The Time cardinal we receive divided by (24 * 60*60 for days + RoundTrip divided by one-thousand since this is based on seconds
  108. - the Time Zone difference}
  109. Result := UTCTimeToLocalTime( ((BufCard + (FRoundTripDelay div 1000))/ (24 * 60 * 60) ) + Int(fBaseDate) );
  110. end else begin
  111. { Somehow, I really doubt we are ever going to really get a time such as
  112. 12/30/1899 12:00 am so use that as a failure test}
  113. Result := 0;
  114. end;
  115. end;
  116. function TIdCustomTimeUDP.GetDateTimeCard: UInt32;
  117. var
  118. LTimeBeforeRetrieve: TIdTicks;
  119. LBuffer : TIdBytes;
  120. begin
  121. //Important - This must send an empty UDP Datagram
  122. Send(''); {Do not Localize}
  123. LTimeBeforeRetrieve := Ticks64;
  124. SetLength(LBuffer,4);
  125. ReceiveBuffer(LBuffer);
  126. Result := BytesToUInt32(LBuffer);
  127. Result := GStack.NetworkToHost(Result);
  128. {Theoritically, it should take about 1/2 of the time to receive the data
  129. but in practice, it could be any portion depending upon network conditions. This is also
  130. as per RFC standard}
  131. {This is just in case the TickCount rolled back to zero}
  132. FRoundTripDelay := GetElapsedTicks(LTimeBeforeRetrieve) div 2;
  133. end;
  134. function TIdCustomTimeUDP.SyncTime: Boolean;
  135. var
  136. LBufTime: TDateTime;
  137. begin
  138. LBufTime := DateTime;
  139. Result := LBufTime <> 0;
  140. if Result then begin
  141. Result := IndySetLocalTime(LBufTime);
  142. end;
  143. end;
  144. end.