fpasynch.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. {
  2. $Id$
  3. fpAsync: Asynchronous event management for Free Pascal
  4. Copyright (C) 2001-2002 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. EAsyncError = class(Exception)
  15. private
  16. FErrorCode: TAsyncResult;
  17. public
  18. constructor Create(AErrorCode: TAsyncResult);
  19. property ErrorCode: TAsyncResult read FErrorCode;
  20. end;
  21. TEventLoop = class
  22. private
  23. FData: TAsyncData;
  24. FFirstNotifyData: Pointer;
  25. function GetIsRunning: Boolean;
  26. procedure SetIsRunning(AIsRunning: Boolean);
  27. protected
  28. procedure CheckResult(AResultCode: TAsyncResult);
  29. public
  30. constructor Create;
  31. destructor Destroy; override;
  32. function Handle: TAsyncHandle;
  33. // Main loop control
  34. procedure Run;
  35. procedure Break;
  36. // Timer support
  37. function AddTimerCallback(AMSec: LongInt; APeriodic: Boolean;
  38. ACallback: TAsyncCallback; AUserData: Pointer): TAsyncTimer;
  39. procedure RemoveTimerCallback(ATimer: TAsyncTimer);
  40. function AddTimerNotify(AMSec: LongInt; APeriodic: Boolean;
  41. ANotify: TNotifyEvent; ASender: TObject): Pointer;
  42. procedure RemoveTimerNotify(AHandle: Pointer);
  43. // I/O notification support (for files, sockets etc.)
  44. procedure SetIOCallback(AHandle: Integer; ACallback: TAsyncCallback;
  45. AUserData: Pointer);
  46. procedure ClearIOCallback(AHandle: Integer);
  47. function SetIONotify(AHandle: Integer; ANotify: TNotifyEvent;
  48. ASender: TObject): Pointer;
  49. procedure ClearIONotify(AHandle: Pointer);
  50. procedure SetDataAvailableCallback(AHandle: Integer;
  51. ACallback: TAsyncCallback; AUserData: Pointer);
  52. procedure ClearDataAvailableCallback(AHandle: Integer);
  53. function SetDataAvailableNotify(AHandle: Integer; ANotify: TNotifyEvent;
  54. ASender: TObject): Pointer;
  55. procedure ClearDataAvailableNotify(AHandle: Pointer);
  56. procedure SetCanWriteCallback(AHandle: Integer; ACallback: TAsyncCallback;
  57. AUserData: Pointer);
  58. procedure ClearCanWriteCallback(AHandle: Integer);
  59. function SetCanWriteNotify(AHandle: Integer; ANotify: TNotifyEvent;
  60. ASender: TObject): Pointer;
  61. procedure ClearCanWriteNotify(AHandle: Pointer);
  62. class function TimerTicks: Int64;
  63. // Properties
  64. property IsRunning: Boolean read GetIsRunning write SetIsRunning;
  65. end;
  66. // -------------------------------------------------------------------
  67. // Asynchronous line reader
  68. // -------------------------------------------------------------------
  69. TLineNotify = procedure(const ALine: String) of object;
  70. TGenericLineReader = class
  71. protected
  72. RealBuffer, FBuffer: PChar;
  73. FBytesInBuffer: Integer;
  74. FOnLine: TLineNotify;
  75. function Read(var ABuffer; count: Integer): Integer; virtual; abstract;
  76. procedure NoData; virtual; abstract;
  77. public
  78. destructor Destroy; override;
  79. procedure Run; // Process as many lines as possible
  80. property Buffer: PChar read FBuffer;
  81. property BytesInBuffer: Integer read FBytesInBuffer;
  82. property OnLine: TLineNotify read FOnLine write FOnLine;
  83. end;
  84. TAsyncStreamLineReader = class(TGenericLineReader)
  85. protected
  86. FEventLoop: TEventLoop;
  87. FDataStream: TStream;
  88. FBlockingStream: THandleStream;
  89. FOnEOF: TNotifyEvent;
  90. NotifyHandle: Pointer;
  91. DoStopAndFree: Boolean;
  92. function Read(var ABuffer; count: Integer): Integer; override;
  93. procedure NoData; override;
  94. procedure StreamDataAvailable(UserData: TObject);
  95. public
  96. constructor Create(AEventLoop: TEventLoop; AStream: THandleStream);
  97. constructor Create(AEventLoop: TEventLoop; ADataStream: TStream;
  98. ABlockingStream: THandleStream);
  99. destructor Destroy; override;
  100. procedure StopAndFree; // Destroy instance after run
  101. property EventLoop: TEventLoop read FEventLoop;
  102. property DataStream: TStream read FDataStream;
  103. property BlockingStream: THandleStream read FBlockingStream;
  104. property OnEOF: TNotifyEvent read FOnEOF write FOnEOF;
  105. end;
  106. // -------------------------------------------------------------------
  107. // Asynchronous write buffers
  108. // -------------------------------------------------------------------
  109. TWriteBuffer = class(TStream)
  110. protected
  111. FBuffer: PChar;
  112. FBytesInBuffer: Integer;
  113. FOnBufferEmpty: TNotifyEvent;
  114. function Seek(Offset: LongInt; Origin: Word): LongInt; override;
  115. function Write(const ABuffer; Count: LongInt): LongInt; override;
  116. function DoRealWrite(const ABuffer; Count: Integer): Integer; virtual; abstract;
  117. procedure WritingFailed; virtual; abstract;
  118. procedure WantWrite; virtual; abstract;
  119. procedure BufferEmpty; virtual;
  120. public
  121. EndOfLineMarker: String;
  122. constructor Create;
  123. destructor Destroy; override;
  124. procedure WriteLine(const line: String);
  125. procedure Run; // Write as many data as possible
  126. property BytesInBuffer: Integer read FBytesInBuffer;
  127. property OnBufferEmpty: TNotifyEvent read FOnBufferEmpty write FOnBufferEmpty;
  128. end;
  129. TAsyncWriteStream = class(TWriteBuffer)
  130. protected
  131. FEventLoop: TEventLoop;
  132. FDataStream: TStream;
  133. FBlockingStream: THandleStream;
  134. NotifyHandle: Pointer;
  135. function DoRealWrite(const ABuffer; Count: Integer): Integer; override;
  136. procedure WritingFailed; override;
  137. procedure WantWrite; override;
  138. procedure BufferEmpty; override;
  139. procedure CanWrite(UserData: TObject);
  140. public
  141. constructor Create(AEventLoop: TEventLoop; AStream: THandleStream);
  142. constructor Create(AEventLoop: TEventLoop;
  143. ADataStream: TStream; ABlockingStream: THandleStream);
  144. destructor Destroy; override;
  145. property EventLoop: TEventLoop read FEventLoop;
  146. property DataStream: TStream read FDataStream;
  147. property BlockingStream: THandleStream read FBlockingStream;
  148. end;
  149. {
  150. $Log$
  151. Revision 1.3 2002-09-15 15:45:38 sg
  152. * Added stream line reader classes
  153. Revision 1.2 2002/09/07 15:42:57 peter
  154. * old logs removed and tabs fixed
  155. Revision 1.1 2002/01/29 17:55:02 peter
  156. * splitted to base and extra
  157. }