IdTCPStream.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.13 27.08.2004 21:58:22 Andreas Hausladen
  18. Speed optimization ("const" for string parameters)
  19. Rev 1.12 29/05/2004 21:17:48 CCostelloe
  20. ReadLnSplit added, needed for binary attachments
  21. Rev 1.11 28/05/2004 20:30:12 CCostelloe
  22. Bug fix
  23. Rev 1.10 2004.05.21 8:22:16 PM czhower
  24. Added ReadLn
  25. Rev 1.9 2004.05.20 1:40:00 PM czhower
  26. Last of the IdStream updates
  27. Rev 1.8 2004.03.07 11:48:46 AM czhower
  28. Flushbuffer fix + other minor ones found
  29. Rev 1.7 2004.02.03 4:16:58 PM czhower
  30. For unit name changes.
  31. Rev 1.6 5/12/2003 9:17:58 AM GGrieve
  32. remove dead code
  33. Rev 1.5 5/12/2003 12:32:14 AM GGrieve
  34. Refactor to work under DotNet
  35. Rev 1.4 10/10/2003 11:04:24 PM BGooijen
  36. DotNet
  37. Rev 1.3 9/10/2003 1:50:50 PM SGrobety
  38. DotNet
  39. Rev 1.2 2003.10.01 11:16:38 AM czhower
  40. .Net
  41. Rev 1.1 2003.10.01 1:37:36 AM czhower
  42. .Net
  43. Rev 1.0 11/13/2002 09:01:04 AM JPMugaas
  44. }
  45. unit IdTCPStream;
  46. interface
  47. {$I IdCompilerDefines.inc}
  48. //TODO: This should be renamed to IdStreamTCP for consistency, and class too
  49. uses
  50. Classes,
  51. IdGlobal, IdTCPConnection;
  52. type
  53. TIdTCPStream = class(TIdBaseStream)
  54. protected
  55. FConnection: TIdTCPConnection;
  56. FWriteThreshold: Integer;
  57. FWriteBuffering: Boolean;
  58. function IdRead(var VBuffer: TIdBytes; AOffset, ACount: Longint): Longint; override;
  59. function IdWrite(const ABuffer: TIdBytes; AOffset, ACount: Longint): Longint; override;
  60. function IdSeek(const AOffset: Int64; AOrigin: TSeekOrigin): Int64; override;
  61. procedure IdSetSize(ASize: Int64); override;
  62. public
  63. constructor Create(AConnection: TIdTCPConnection; const AWriteThreshold: Integer = 0); reintroduce;
  64. destructor Destroy; override;
  65. property Connection: TIdTCPConnection read FConnection;
  66. end;
  67. implementation
  68. uses
  69. IdException,
  70. SysUtils;
  71. constructor TIdTCPStream.Create(AConnection: TIdTCPConnection; const AWriteThreshold: Integer = 0);
  72. begin
  73. inherited Create;
  74. FConnection := AConnection;
  75. FWriteThreshold := AWriteThreshold;
  76. end;
  77. destructor TIdTCPStream.Destroy;
  78. begin
  79. if FWriteBuffering then begin
  80. Connection.IOHandler.WriteBufferClose;
  81. end;
  82. inherited Destroy;
  83. end;
  84. function TIdTCPStream.IdRead(var VBuffer: TIdBytes; AOffset, ACount: Longint): Longint;
  85. begin
  86. if AOffset <> 0 then begin
  87. ToDo('IdRead() method of TIdTCPStream class does not support seeking'); {do not localized}
  88. end;
  89. Connection.IOHandler.ReadBytes(VBuffer, ACount, False);
  90. Result := ACount;
  91. end;
  92. function TIdTCPStream.IdSeek(const AOffset: Int64; AOrigin: TSeekOrigin): Int64;
  93. begin
  94. Result := 0;
  95. end;
  96. procedure TIdTCPStream.IdSetSize(ASize: Int64);
  97. begin
  98. //
  99. end;
  100. function TIdTCPStream.IdWrite(const ABuffer: TIdBytes; AOffset, ACount: Longint): Longint;
  101. begin
  102. if (not FWriteBuffering) and (FWriteThreshold > 0) and (not Connection.IOHandler.WriteBufferingActive) then begin
  103. Connection.IOHandler.WriteBufferOpen(FWriteThreshold);
  104. FWriteBuffering := True;
  105. end;
  106. Connection.IOHandler.Write(ABuffer, ACount, AOffset);
  107. Result := ACount;
  108. end;
  109. end.