IdTCPStream.pas 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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: 10369: IdTCPStream.pas
  11. {
  12. { Rev 1.0 2002.11.12 10:55:28 PM czhower
  13. }
  14. unit IdTCPStream;
  15. interface
  16. uses
  17. Classes,
  18. IdTCPConnection;
  19. type
  20. TIdTCPStream = class(TStream)
  21. protected
  22. FConnection: TIdTCPConnection;
  23. FWriteBuffering: Boolean;
  24. FWriteThreshold: Integer;
  25. public
  26. constructor Create(AConnection: TIdTCPConnection; const AWriteThreshold: Integer = 0); reintroduce;
  27. destructor Destroy; override;
  28. function Read(var ABuffer; ACount: Longint): Longint; override;
  29. function Write(const ABuffer; ACount: Longint): Longint; override;
  30. function Seek(AOffset: Longint; AOrigin: Word): Longint; override;
  31. //
  32. property Connection: TIdTCPConnection read FConnection;
  33. end;
  34. implementation
  35. { TIdTCPStream }
  36. constructor TIdTCPStream.Create(AConnection: TIdTCPConnection; const AWriteThreshold: Integer = 0);
  37. begin
  38. inherited Create;
  39. FConnection := AConnection;
  40. FWriteThreshold := AWriteThreshold;
  41. end;
  42. destructor TIdTCPStream.Destroy;
  43. begin
  44. if FWriteBuffering then begin
  45. Connection.CloseWriteBuffer;
  46. end;
  47. inherited Destroy;
  48. end;
  49. function TIdTCPStream.Read(var ABuffer; ACount: Integer): Longint;
  50. begin
  51. Connection.ReadBuffer(ABuffer, ACount);
  52. Result := ACount;
  53. end;
  54. function TIdTCPStream.Seek(AOffset: Integer; AOrigin: Word): Longint;
  55. begin
  56. Result := -1;
  57. end;
  58. type
  59. TIdTCPConnectionAccess = class(TIdTCPConnection)
  60. end;
  61. function TIdTCPStream.Write(const ABuffer; ACount: Integer): Longint;
  62. begin
  63. if (not FWriteBuffering) and (FWriteThreshold > 0) and
  64. (TIdTCPConnectionAccess(Connection).FWriteBuffer = nil) then
  65. begin
  66. Connection.OpenWriteBuffer(FWriteThreshold);
  67. FWriteBuffering := True;
  68. end;
  69. Connection.WriteBuffer(ABuffer, ACount);
  70. Result := ACount;
  71. end;
  72. end.