IdTimeUDPServer.pas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.6 2/10/2005 2:24:38 PM JPMugaas
  18. Minor Restructures for some new UnixTime Service components.
  19. Rev 1.5 2004.02.03 5:44:36 PM czhower
  20. Name changes
  21. Rev 1.4 1/21/2004 4:21:02 PM JPMugaas
  22. InitComponent
  23. Rev 1.3 10/22/2003 08:48:06 PM JPMugaas
  24. Minor code cleanup.
  25. Rev 1.1 2003.10.12 6:36:46 PM czhower
  26. Now compiles.
  27. Rev 1.0 11/13/2002 08:03:28 AM JPMugaas
  28. }
  29. unit IdTimeUDPServer;
  30. interface
  31. {$i IdCompilerDefines.inc}
  32. uses
  33. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  34. Classes,
  35. {$ENDIF}
  36. IdAssignedNumbers, IdGlobal, IdSocketHandle, IdUDPServer;
  37. type
  38. TIdCustomTimeUDPServer = class(TIdUDPServer)
  39. protected
  40. FBaseDate : TDateTime;
  41. procedure DoUDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle); override;
  42. procedure InitComponent; override;
  43. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  44. public
  45. constructor Create(AOwner: TComponent); reintroduce; overload;
  46. {$ENDIF}
  47. end;
  48. TIdTimeUDPServer = class(TIdCustomTimeUDPServer)
  49. published
  50. property DefaultPort default IdPORT_TIME;
  51. {This property is used to set the Date the Time server bases it's
  52. calculations from. If both the server and client are based from the same
  53. date which is higher than the original date, you can extend it beyond the
  54. year 2035}
  55. property BaseDate : TDateTime read FBaseDate write FBaseDate;
  56. end;
  57. implementation
  58. uses
  59. {$IFDEF USE_VCL_POSIX}
  60. Posix.SysTime,
  61. {$ENDIF}
  62. IdGlobalProtocols, IdStack, SysUtils;
  63. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  64. constructor TIdCustomTimeUDPServer.Create(AOwner: TComponent);
  65. begin
  66. inherited Create(AOwner);
  67. end;
  68. {$ENDIF}
  69. procedure TIdCustomTimeUDPServer.InitComponent;
  70. begin
  71. inherited;
  72. DefaultPort := IdPORT_TIME;
  73. {This indicates that the default date is Jan 1, 1900 which was specified
  74. by RFC 868.}
  75. FBaseDate := TIME_BASEDATE;
  76. end;
  77. procedure TIdCustomTimeUDPServer.DoUDPRead(AThread: TIdUDPListenerThread;
  78. const AData: TIdBytes; ABinding: TIdSocketHandle);
  79. var
  80. LTime : UInt32;
  81. begin
  82. inherited DoUDPRead(AThread, AData, ABinding);
  83. LTime := Trunc(Extended(LocalTimeToUTCTime(Now) - Int(FBaseDate)) * 24 * 60 * 60);
  84. LTime := GStack.HostToNetwork(LTime);
  85. ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, ToBytes(LTime), ABinding.IPVersion);
  86. end;
  87. end.