123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- {
- $Id$
- fpAsync: Asynchronous event management for Free Pascal
- Copyright (C) 2001-2002 by
- Areca Systems GmbH / Sebastian Guenther, [email protected]
- Common interface
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- }
- type
- EAsyncError = class(Exception)
- private
- FErrorCode: TAsyncResult;
- public
- constructor Create(AErrorCode: TAsyncResult);
- property ErrorCode: TAsyncResult read FErrorCode;
- end;
- TEventLoop = class
- private
- FData: TAsyncData;
- FFirstNotifyData: Pointer;
- function GetIsRunning: Boolean;
- procedure SetIsRunning(AIsRunning: Boolean);
- protected
- procedure CheckResult(AResultCode: TAsyncResult);
- public
- constructor Create;
- destructor Destroy; override;
- function Handle: TAsyncHandle;
- // Main loop control
- procedure Run;
- procedure Break;
- // Timer support
- function AddTimerCallback(AMSec: LongInt; APeriodic: Boolean;
- ACallback: TAsyncCallback; AUserData: Pointer): TAsyncTimer;
- procedure RemoveTimerCallback(ATimer: TAsyncTimer);
- function AddTimerNotify(AMSec: LongInt; APeriodic: Boolean;
- ANotify: TNotifyEvent; ASender: TObject): Pointer;
- procedure RemoveTimerNotify(AHandle: Pointer);
- // I/O notification support (for files, sockets etc.)
- procedure SetIOCallback(AHandle: Integer; ACallback: TAsyncCallback;
- AUserData: Pointer);
- procedure ClearIOCallback(AHandle: Integer);
- function SetIONotify(AHandle: Integer; ANotify: TNotifyEvent;
- ASender: TObject): Pointer;
- procedure ClearIONotify(AHandle: Pointer);
- procedure SetDataAvailableCallback(AHandle: Integer;
- ACallback: TAsyncCallback; AUserData: Pointer);
- procedure ClearDataAvailableCallback(AHandle: Integer);
- function SetDataAvailableNotify(AHandle: Integer; ANotify: TNotifyEvent;
- ASender: TObject): Pointer;
- procedure ClearDataAvailableNotify(AHandle: Pointer);
- procedure SetCanWriteCallback(AHandle: Integer; ACallback: TAsyncCallback;
- AUserData: Pointer);
- procedure ClearCanWriteCallback(AHandle: Integer);
- function SetCanWriteNotify(AHandle: Integer; ANotify: TNotifyEvent;
- ASender: TObject): Pointer;
- procedure ClearCanWriteNotify(AHandle: Pointer);
- class function TimerTicks: Int64;
- // Properties
- property IsRunning: Boolean read GetIsRunning write SetIsRunning;
- end;
- // -------------------------------------------------------------------
- // Asynchronous line reader
- // -------------------------------------------------------------------
- TLineNotify = procedure(const ALine: String) of object;
- TGenericLineReader = class
- protected
- RealBuffer, FBuffer: PChar;
- FBytesInBuffer: Integer;
- FOnLine: TLineNotify;
- function Read(var ABuffer; count: Integer): Integer; virtual; abstract;
- procedure NoData; virtual; abstract;
- public
- destructor Destroy; override;
- procedure Run; // Process as many lines as possible
- property Buffer: PChar read FBuffer;
- property BytesInBuffer: Integer read FBytesInBuffer;
- property OnLine: TLineNotify read FOnLine write FOnLine;
- end;
- TAsyncStreamLineReader = class(TGenericLineReader)
- protected
- FEventLoop: TEventLoop;
- FDataStream: TStream;
- FBlockingStream: THandleStream;
- FOnEOF: TNotifyEvent;
- NotifyHandle: Pointer;
- DoStopAndFree: Boolean;
- function Read(var ABuffer; count: Integer): Integer; override;
- procedure NoData; override;
- procedure StreamDataAvailable(UserData: TObject);
- public
- constructor Create(AEventLoop: TEventLoop; AStream: THandleStream);
- constructor Create(AEventLoop: TEventLoop; ADataStream: TStream;
- ABlockingStream: THandleStream);
- destructor Destroy; override;
- procedure StopAndFree; // Destroy instance after run
- property EventLoop: TEventLoop read FEventLoop;
- property DataStream: TStream read FDataStream;
- property BlockingStream: THandleStream read FBlockingStream;
- property OnEOF: TNotifyEvent read FOnEOF write FOnEOF;
- end;
- // -------------------------------------------------------------------
- // Asynchronous write buffers
- // -------------------------------------------------------------------
- TWriteBuffer = class(TStream)
- protected
- FBuffer: PChar;
- FBytesInBuffer: Integer;
- FOnBufferEmpty: TNotifyEvent;
- function Seek(Offset: LongInt; Origin: Word): LongInt; override;
- function Write(const ABuffer; Count: LongInt): LongInt; override;
- function DoRealWrite(const ABuffer; Count: Integer): Integer; virtual; abstract;
- procedure WritingFailed; virtual; abstract;
- procedure WantWrite; virtual; abstract;
- procedure BufferEmpty; virtual;
- public
- EndOfLineMarker: String;
- constructor Create;
- destructor Destroy; override;
- procedure WriteLine(const line: String);
- procedure Run; // Write as many data as possible
- property BytesInBuffer: Integer read FBytesInBuffer;
- property OnBufferEmpty: TNotifyEvent read FOnBufferEmpty write FOnBufferEmpty;
- end;
- TAsyncWriteStream = class(TWriteBuffer)
- protected
- FEventLoop: TEventLoop;
- FDataStream: TStream;
- FBlockingStream: THandleStream;
- NotifyHandle: Pointer;
- function DoRealWrite(const ABuffer; Count: Integer): Integer; override;
- procedure WritingFailed; override;
- procedure WantWrite; override;
- procedure BufferEmpty; override;
- procedure CanWrite(UserData: TObject);
- public
- constructor Create(AEventLoop: TEventLoop; AStream: THandleStream);
- constructor Create(AEventLoop: TEventLoop;
- ADataStream: TStream; ABlockingStream: THandleStream);
- destructor Destroy; override;
- property EventLoop: TEventLoop read FEventLoop;
- property DataStream: TStream read FDataStream;
- property BlockingStream: THandleStream read FBlockingStream;
- end;
- {
- $Log$
- Revision 1.3 2002-09-15 15:45:38 sg
- * Added stream line reader classes
- Revision 1.2 2002/09/07 15:42:57 peter
- * old logs removed and tabs fixed
- Revision 1.1 2002/01/29 17:55:02 peter
- * splitted to base and extra
- }
|