IdTimeServer.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.8 2/10/2005 2:24:42 PM JPMugaas
  18. Minor Restructures for some new UnixTime Service components.
  19. Rev 1.7 12/2/2004 4:24:00 PM JPMugaas
  20. Adjusted for changes in Core.
  21. Rev 1.6 2004.02.03 5:44:34 PM czhower
  22. Name changes
  23. Rev 1.5 1/21/2004 4:20:58 PM JPMugaas
  24. InitComponent
  25. Rev 1.4 2003.10.12 6:36:44 PM czhower
  26. Now compiles.
  27. Rev 1.3 2/24/2003 10:37:04 PM JPMugaas
  28. Should compile. TODO: Figure out what to do with TIdTime and the timeout
  29. feature.
  30. Rev 1.2 1/17/2003 07:11:08 PM JPMugaas
  31. Now compiles under new framework.
  32. Rev 1.1 1/8/2003 05:54:00 PM JPMugaas
  33. Switched stuff to IdContext.
  34. Rev 1.0 11/13/2002 08:03:20 AM JPMugaas
  35. }
  36. unit IdTimeServer;
  37. interface
  38. {$i IdCompilerDefines.inc}
  39. {
  40. 2000-3-May J. Peter Mugaas
  41. -Added BaseDate to the date the calculations are based on can be
  42. adjusted to work after the year 2035
  43. 2000-30-April J. Peter Mugaas
  44. -Adjusted the formula for the integer so that the Time is now
  45. always based on Universal Time (also known as Greenwhich Mean
  46. -Time Replaced the old forumala used to calculate the time with
  47. a new one suggested by Jim Gunkel. This forumala is more
  48. accurate than the old one.
  49. 2000-24-April J. Peter Mugaaas
  50. -This now uses the Internet Byte order functions
  51. 2000-22-Apr J Peter Mugass
  52. -Ported to Indy
  53. -Fixed a problem where the server was not returning anything
  54. 2000-13-Jan MTL
  55. -Moved to new Palette Scheme (Winshoes Servers)
  56. 1999-13-Apr
  57. -Final Version
  58. Original Author: Ozz Nixon
  59. -Based on RFC 868
  60. }
  61. uses
  62. Classes,
  63. IdAssignedNumbers,
  64. IdContext,
  65. IdCustomTCPServer;
  66. Type
  67. TIdCustomTimeServer = class(TIdCustomTCPServer)
  68. protected
  69. FBaseDate : TDateTime;
  70. //
  71. function DoExecute(AContext: TIdContext): Boolean; override;
  72. public
  73. constructor Create(AOwner: TComponent); reintroduce; overload;
  74. published
  75. end;
  76. TIdTimeServer = class(TIdCustomTimeServer)
  77. published
  78. property DefaultPort default IdPORT_TIME;
  79. {This property is used to set the Date the Time server bases it's
  80. calculations from. If both the server and client are based from the same
  81. date which is higher than the original date, you can extend it beyond the
  82. year 2035}
  83. property BaseDate : TDateTime read FBaseDate write FBaseDate;
  84. end;
  85. implementation
  86. uses
  87. {$IFDEF USE_VCL_POSIX}
  88. Posix.Time,
  89. {$ENDIF}
  90. IdGlobal, //here to facilitate inlining
  91. IdGlobalProtocols,
  92. SysUtils;
  93. constructor TIdCustomTimeServer.Create(AOwner: TComponent);
  94. begin
  95. inherited Create(AOwner);
  96. DefaultPort := 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 TIdCustomTimeServer.DoExecute(AContext: TIdContext): Boolean;
  102. begin
  103. Result := true;
  104. AContext.Connection.IOHandler.Write(UInt32(Trunc(Extended(LocalTimeToUTCTime(Now) - Int(FBaseDate)) * 24 * 60 * 60)));
  105. AContext.Connection.Disconnect;
  106. end;
  107. end.