IdEcho.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 2004.02.03 5:45:06 PM czhower
  18. Name changes
  19. Rev 1.7 1/21/2004 3:27:46 PM JPMugaas
  20. InitComponent
  21. Rev 1.6 1/3/2004 12:59:52 PM JPMugaas
  22. These should now compile with Kudzu's change in IdCoreGlobal.
  23. Rev 1.5 2003.11.29 10:18:52 AM czhower
  24. Updated for core change to InputBuffer.
  25. Rev 1.4 3/6/2003 5:08:48 PM SGrobety
  26. Updated the read buffer methodes to fit the new core (InputBuffer ->
  27. InputBufferAsString + call to CheckForDataOnSource)
  28. Rev 1.3 2/24/2003 08:41:28 PM JPMugaas
  29. Should compile with new code.
  30. Rev 1.2 12/8/2002 07:26:34 PM JPMugaas
  31. Added published host and port properties.
  32. Rev 1.1 12/6/2002 05:29:32 PM JPMugaas
  33. Now decend from TIdTCPClientCustom instead of TIdTCPClient.
  34. Rev 1.0 11/14/2002 02:19:24 PM JPMugaas
  35. }
  36. unit IdEcho;
  37. {*******************************************************}
  38. { }
  39. { Indy Echo Client TIdEcho }
  40. { }
  41. { Copyright (C) 2000 Winshoes Working Group }
  42. { Original author J. Peter Mugaas }
  43. { 2000-April-24 }
  44. { }
  45. {*******************************************************}
  46. interface
  47. {$i IdCompilerDefines.inc}
  48. uses
  49. IdGlobal,
  50. IdAssignedNumbers,
  51. IdTCPClient;
  52. type
  53. TIdEcho = class(TIdTCPClient)
  54. protected
  55. FEchoTime: UInt32;
  56. procedure InitComponent; override;
  57. public
  58. {This sends Text to the peer and returns the reply from the peer}
  59. function Echo(const AText: String): String;
  60. {Time taken to send and receive data}
  61. property EchoTime: UInt32 read FEchoTime;
  62. published
  63. property Port default IdPORT_ECHO;
  64. end;
  65. implementation
  66. uses
  67. {$IFDEF USE_VCL_POSIX}
  68. {$IFDEF OSX}
  69. Macapi.CoreServices,
  70. {$ENDIF}
  71. {$ENDIF}
  72. IdComponent,
  73. IdTCPConnection,
  74. IdIOHandler;
  75. { TIdEcho }
  76. procedure TIdEcho.InitComponent;
  77. begin
  78. inherited InitComponent;
  79. Port := IdPORT_ECHO;
  80. end;
  81. function TIdEcho.Echo(const AText: String): String;
  82. var
  83. LEncoding: IIdTextEncoding;
  84. LBuffer: TIdBytes;
  85. LLen: Integer;
  86. StartTime: TIdTicks;
  87. begin
  88. LEncoding := IndyTextEncoding(
  89. {$IFDEF STRING_IS_UNICODE}
  90. encUTF16LE
  91. {$ELSE}
  92. encOSDefault
  93. {$ENDIF}
  94. );
  95. {Send time monitoring}
  96. LBuffer := ToBytes(AText, LEncoding);
  97. LLen := Length(LBuffer);
  98. {Send time monitoring}
  99. StartTime := Ticks64;
  100. IOHandler.Write(LBuffer);
  101. IOHandler.ReadBytes(LBuffer, LLen, False);
  102. {This is just in case the TickCount rolled back to zero}
  103. FEchoTime := GetElapsedTicks(StartTime);
  104. Result := BytesToString(LBuffer, LEncoding);
  105. end;
  106. end.