IdTimeUDPServer.pas 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. Classes,
  34. IdAssignedNumbers, IdGlobal, IdSocketHandle, IdUDPServer;
  35. type
  36. TIdCustomTimeUDPServer = class(TIdUDPServer)
  37. protected
  38. FBaseDate : TDateTime;
  39. procedure DoUDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle); override;
  40. public
  41. constructor Create(AOwner: TComponent); override;
  42. end;
  43. TIdTimeUDPServer = class(TIdCustomTimeUDPServer)
  44. published
  45. property DefaultPort default IdPORT_TIME;
  46. {This property is used to set the Date the Time server bases it's
  47. calculations from. If both the server and client are based from the same
  48. date which is higher than the original date, you can extend it beyond the
  49. year 2035}
  50. property BaseDate : TDateTime read FBaseDate write FBaseDate;
  51. end;
  52. implementation
  53. uses
  54. {$IFDEF USE_VCL_POSIX}
  55. Posix.SysTime,
  56. {$ENDIF}
  57. IdGlobalProtocols, IdStack, SysUtils;
  58. constructor TIdCustomTimeUDPServer.Create(AOwner: TComponent);
  59. begin
  60. inherited Create(AOwner);
  61. DefaultPort := IdPORT_TIME;
  62. {This indicates that the default date is Jan 1, 1900 which was specified
  63. by RFC 868.}
  64. FBaseDate := TIME_BASEDATE;
  65. end;
  66. procedure TIdCustomTimeUDPServer.DoUDPRead(AThread: TIdUDPListenerThread;
  67. const AData: TIdBytes; ABinding: TIdSocketHandle);
  68. var
  69. LTime : UInt32;
  70. begin
  71. inherited DoUDPRead(AThread, AData, ABinding);
  72. LTime := Trunc(Extended(LocalTimeToUTCTime(Now) - Int(FBaseDate)) * 24 * 60 * 60);
  73. LTime := GStack.HostToNetwork(LTime);
  74. ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, ToBytes(LTime), ABinding.IPVersion);
  75. end;
  76. end.