IdTimeUDPServer.pas 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10393: IdTimeUDPServer.pas
  11. {
  12. { Rev 1.0 2002.11.12 10:57:42 PM czhower
  13. }
  14. unit IdTimeUDPServer;
  15. interface
  16. uses IdAssignedNumbers, IdSocketHandle, IdUDPBase, IdUDPServer, Classes;
  17. type
  18. TIdTimeUDPServer = class(TIdUDPServer)
  19. protected
  20. FBaseDate : TDateTime;
  21. procedure DoUDPRead(AData: TStream; ABinding: TIdSocketHandle); override;
  22. public
  23. constructor Create(axOwner: TComponent); override;
  24. published
  25. property DefaultPort default IdPORT_TIME;
  26. {This property is used to set the Date the Time server bases it's
  27. calculations from. If both the server and client are based from the same
  28. date which is higher than the original date, you can extend it beyond the
  29. year 2035}
  30. property BaseDate : TDateTime read FBaseDate write FBaseDate;
  31. end;
  32. implementation
  33. uses IdGlobal, IdStack, SysUtils;
  34. const
  35. {This indicates that the default date is Jan 1, 1900 which was specified
  36. by RFC 868.}
  37. TIMEUDP_DEFBASEDATE = 2;
  38. { TIdTimeUDPServer }
  39. constructor TIdTimeUDPServer.Create(axOwner: TComponent);
  40. begin
  41. inherited Create(axOwner);
  42. DefaultPort := IdPORT_TIME;
  43. {This indicates that the default date is Jan 1, 1900 which was specified
  44. by RFC 868.}
  45. FBaseDate := TIMEUDP_DEFBASEDATE;
  46. end;
  47. procedure TIdTimeUDPServer.DoUDPRead(AData: TStream;
  48. ABinding: TIdSocketHandle);
  49. var s : String;
  50. LTime : Cardinal;
  51. begin
  52. inherited DoUDPRead(AData, ABinding);
  53. SetLength(s, AData.Size);
  54. AData.Read(s[1], AData.Size);
  55. LTime := Trunc(extended(Now + IdGlobal.TimeZoneBias - Int(FBaseDate)) * 24 * 60 * 60);
  56. LTime := GStack.WSHToNl(LTime);
  57. SendBuffer(ABinding.PeerIP,ABinding.PeerPort,LTime,SizeOf(LTime));
  58. end;
  59. end.