fpasynch.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. {
  2. $Id$
  3. fpAsync: Asynchronous event management for Free Pascal
  4. Copyright (C) 2001 by
  5. Areca Systems GmbH / Sebastian Guenther, [email protected]
  6. Common interface
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. }
  13. type
  14. TEventLoop = class
  15. private
  16. FData: TAsyncData;
  17. FFirstNotifyData: Pointer;
  18. function GetIsRunning: Boolean;
  19. procedure SetIsRunning(AIsRunning: Boolean);
  20. public
  21. constructor Create;
  22. destructor Destroy; override;
  23. function Handle: TAsyncHandle;
  24. // Main loop control
  25. procedure Run;
  26. procedure Break;
  27. // Timer support
  28. function AddTimerCallback(AMSec: LongInt; APeriodic: Boolean;
  29. ACallback: TAsyncCallback; AUserData: Pointer): TAsyncTimer;
  30. procedure RemoveTimerCallback(ATimer: TAsyncTimer);
  31. function AddTimerNotify(AMSec: LongInt; APeriodic: Boolean;
  32. ANotify: TNotifyEvent; ASender: TObject): Pointer;
  33. procedure RemoveTimerNotify(AHandle: Pointer);
  34. // I/O notification support (for files, sockets etc.)
  35. procedure SetIOCallback(AHandle: Integer; ACallback: TAsyncCallback;
  36. AUserData: Pointer);
  37. procedure ClearIOCallback(AHandle: Integer);
  38. function SetIONotify(AHandle: Integer; ANotify: TNotifyEvent;
  39. ASender: TObject): Pointer;
  40. procedure ClearIONotify(AHandle: Pointer);
  41. procedure SetDataAvailableCallback(AHandle: Integer;
  42. ACallback: TAsyncCallback; AUserData: Pointer);
  43. procedure ClearDataAvailableCallback(AHandle: Integer);
  44. function SetDataAvailableNotify(AHandle: Integer; ANotify: TNotifyEvent;
  45. ASender: TObject): Pointer;
  46. procedure ClearDataAvailableNotify(AHandle: Pointer);
  47. procedure SetCanWriteCallback(AHandle: Integer; ACallback: TAsyncCallback;
  48. AUserData: Pointer);
  49. procedure ClearCanWriteCallback(AHandle: Integer);
  50. function SetCanWriteNotify(AHandle: Integer; ANotify: TNotifyEvent;
  51. ASender: TObject): Pointer;
  52. procedure ClearCanWriteNotify(AHandle: Pointer);
  53. class function TimerTicks: Int64;
  54. // Properties
  55. property IsRunning: Boolean read GetIsRunning write SetIsRunning;
  56. end;
  57. // -------------------------------------------------------------------
  58. // Asynchronous write buffers
  59. // -------------------------------------------------------------------
  60. TWriteBuffer = class(TStream)
  61. protected
  62. FBuffer: PChar;
  63. FBytesInBuffer: Integer;
  64. FOnBufferEmpty: TNotifyEvent;
  65. function Seek(Offset: LongInt; Origin: Word): LongInt; override;
  66. function Write(const ABuffer; Count: LongInt): LongInt; override;
  67. function DoRealWrite(const ABuffer; Count: Integer): Integer; virtual; abstract;
  68. procedure WritingFailed; virtual; abstract;
  69. procedure WantWrite; virtual; abstract;
  70. procedure BufferEmpty; virtual;
  71. public
  72. EndOfLineMarker: String;
  73. constructor Create;
  74. destructor Destroy; override;
  75. procedure WriteLine(const line: String);
  76. procedure Run; // Write as many data as possible
  77. property BytesInBuffer: Integer read FBytesInBuffer;
  78. property OnBufferEmpty: TNotifyEvent read FOnBufferEmpty write FOnBufferEmpty;
  79. end;
  80. TAsyncWriteStream = class(TWriteBuffer)
  81. protected
  82. FManager: TEventLoop;
  83. FDataStream: TStream;
  84. FBlockingStream: THandleStream;
  85. FNotifyHandle: Pointer;
  86. function DoRealWrite(const ABuffer; Count: Integer): Integer; override;
  87. procedure WritingFailed; override;
  88. procedure WantWrite; override;
  89. procedure BufferEmpty; override;
  90. procedure CanWrite(UserData: TObject);
  91. public
  92. constructor Create(AManager: TEventLoop; AStream: THandleStream);
  93. constructor Create(AManager: TEventLoop;
  94. ADataStream: TStream; ABlockingStream: THandleStream);
  95. destructor Destroy; override;
  96. property DataStream: TStream read FDataStream;
  97. property BlockingStream: THandleStream read FBlockingStream;
  98. end;
  99. {
  100. $Log$
  101. Revision 1.2 2001-12-11 17:45:28 marco
  102. * was only commited to fixes.
  103. Revision 1.1.2.2 2001/11/16 12:51:41 sg
  104. * Now different handlers for available data and space in write buffer can
  105. be set
  106. * LOTS of bugfixes in the implementation
  107. * fpAsync now has a write buffer class (a read buffer class for reading
  108. line by line will be included in the next release)
  109. Revision 1.1.2.1 2001/09/08 15:43:24 sg
  110. * First public version
  111. }