IdTCPStream.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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(TStream)
  54. protected
  55. FConnection: TIdTCPConnection;
  56. FWriteThreshold: Integer;
  57. FWriteBuffering: Boolean;
  58. procedure SetSize(const NewSize: Int64); override;
  59. public
  60. constructor Create(AConnection: TIdTCPConnection; const AWriteThreshold: Integer = 0); reintroduce;
  61. destructor Destroy; override;
  62. function Read(var Buffer; Count: Longint): Longint; override;
  63. function Write(const Buffer; Count: Longint): Longint; override;
  64. function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
  65. property Connection: TIdTCPConnection read FConnection;
  66. end;
  67. implementation
  68. uses
  69. SysUtils;
  70. constructor TIdTCPStream.Create(AConnection: TIdTCPConnection; const AWriteThreshold: Integer = 0);
  71. begin
  72. inherited Create;
  73. FConnection := AConnection;
  74. FWriteThreshold := AWriteThreshold;
  75. end;
  76. destructor TIdTCPStream.Destroy;
  77. begin
  78. if FWriteBuffering then begin
  79. Connection.IOHandler.WriteBufferClose;
  80. end;
  81. inherited Destroy;
  82. end;
  83. function TIdTCPStream.Read(var Buffer; Count: Longint): Longint;
  84. var
  85. LStream: TIdMemoryBufferStream;
  86. begin
  87. if Count > 0 then
  88. begin
  89. LStream := TIdMemoryBufferStream.Create(@Buffer, Count);
  90. try
  91. Connection.IOHandler.ReadStream(LStream, Count, False);
  92. finally
  93. LStream.Free;
  94. end;
  95. end;
  96. Result := Count;
  97. end;
  98. function TIdTCPStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
  99. begin
  100. Result := 0;
  101. end;
  102. procedure TIdTCPStream.SetSize(const NewSize: Int64);
  103. begin
  104. //
  105. end;
  106. function TIdTCPStream.Write(const Buffer; Count: Longint): Longint;
  107. var
  108. LStream: TStream;
  109. begin
  110. if (not FWriteBuffering) and (FWriteThreshold > 0) and (not Connection.IOHandler.WriteBufferingActive) then begin
  111. Connection.IOHandler.WriteBufferOpen(FWriteThreshold);
  112. FWriteBuffering := True;
  113. end;
  114. if Count > 0 then
  115. begin
  116. LStream := TIdReadOnlyMemoryBufferStream.Create(@Buffer, Count);
  117. try
  118. Connection.IOHandler.Write(LStream, Count, False);
  119. finally
  120. LStream.Free;
  121. end;
  122. end;
  123. Result := Count;
  124. end;
  125. end.