mysticbbs/mdl/m_pipe_unix.pas

135 lines
2.7 KiB
ObjectPascal
Raw Normal View History

2012-09-06 01:33:44 -07:00
Unit m_Pipe_Unix;
{$I M_OPS.PAS}
Interface
Uses
2012-09-22 15:54:14 -07:00
BaseUnix,
2012-09-06 01:33:44 -07:00
m_DateTime,
m_FileIO,
m_Strings;
Type
TPipeUnix = Class
PipeID : Word;
Connected : Boolean;
IsClient : Boolean;
2012-09-22 15:54:14 -07:00
PipeHandle : THandle;
2012-09-06 01:33:44 -07:00
Constructor Create (Dir: String; Client: Boolean; ID: Word);
Destructor Destroy; Override;
// Server functions
Function CreatePipe : Boolean;
Function WaitForPipe (Secs: LongInt) : Boolean;
// Client functions
Function ConnectPipe (Secs: LongInt) : Boolean;
// General functions
Procedure SendToPipe (Var Buf; Len: Longint);
Procedure ReadFromPipe (Var Buf; Len: LongInt; Var bRead: LongInt);
2012-09-06 01:33:44 -07:00
Procedure Disconnect;
2012-09-23 12:09:22 -07:00
Function DataWaiting : Boolean;
2012-09-06 01:33:44 -07:00
End;
Implementation
Constructor TPipeUnix.Create (Dir: String; Client: Boolean; ID: Word);
Begin
Connected := False;
IsClient := Client;
PipeID := ID;
2012-09-22 15:54:14 -07:00
PipeHandle := -1;
2012-09-06 01:33:44 -07:00
End;
Destructor TPipeUnix.Destroy;
Begin
If Connected Then Disconnect;
Inherited Destroy;
End;
2012-09-23 12:09:22 -07:00
Function TPipeUnix.DataWaiting : Boolean;
Var
FDSin : TFDSet;
Begin
Result := False;
If PipeHandle = -1 Then Exit;
fpFD_Zero (FDSIN);
fpFD_Set (PipeHandle, FDSIN);
Result := fpSelect(PipeHandle + 1, @FDSIN, NIL, NIL, 0) > 0;
End;
2012-09-06 01:33:44 -07:00
Function TPipeUnix.CreatePipe : Boolean;
2012-09-22 15:54:14 -07:00
Var
PipeName : String;
2012-09-06 01:33:44 -07:00
Begin
IsClient := False;
2012-09-22 15:54:14 -07:00
PipeName := '/tmp/mystic_' + strI2S(PipeID);
2012-09-06 01:33:44 -07:00
2012-09-22 15:54:14 -07:00
If Not FileExist(PipeName) Then
fpMkFIFO(PipeName, 438);
PipeHandle := fpOpen(PipeName, O_RDWR, O_NONBLOCK);
2012-09-22 15:54:14 -07:00
Result := PipeHandle >= 0;
2012-09-06 01:33:44 -07:00
End;
Procedure TPipeUnix.SendToPipe (Var Buf; Len: LongInt);
Begin
If Not Connected Then Exit;
2012-09-22 15:54:14 -07:00
If fpWrite (PipeHandle, Buf, Len) < 0 Then
Disconnect;
2012-09-06 01:33:44 -07:00
End;
Procedure TPipeUnix.ReadFromPipe (Var Buf; Len: LongInt; Var bRead: LongInt);
2012-09-06 01:33:44 -07:00
Begin
bRead := 0;
If Not Connected Then Exit;
2012-09-22 15:54:14 -07:00
bRead := fpRead (PipeHandle, Buf, Len);
If bRead < 0 Then Disconnect;
2012-09-06 01:33:44 -07:00
End;
Function TPipeUnix.WaitForPipe (Secs: LongInt) : Boolean;
Begin
2012-09-22 15:54:14 -07:00
Connected := PipeHandle > -1;
Result := Connected;
2012-09-06 01:33:44 -07:00
End;
Function TPipeUnix.ConnectPipe (Secs: LongInt) : Boolean;
2012-09-22 15:54:14 -07:00
Var
PipeName : String;
TimeOut : LongInt;
2012-09-06 01:33:44 -07:00
Begin
2012-09-22 15:54:14 -07:00
IsClient := True;
Disconnect;
PipeName := '/tmp/mystic_' + strI2S(PipeID);
TimeOut := TimerSet(Secs);
Repeat
PipeHandle := fpOpen(PipeName, O_RDWR, O_NONBLOCK);
2012-09-22 15:54:14 -07:00
Connected := PipeHandle >= 0;
Until Connected or TimerUp(TimeOut);
2012-09-06 01:33:44 -07:00
Result := Connected;
End;
Procedure TPipeUnix.Disconnect;
Begin
2012-09-22 15:54:14 -07:00
If PipeHandle = -1 Then Exit;
fpClose (PipeHandle);
2012-09-06 01:33:44 -07:00
2012-09-22 15:54:14 -07:00
PipeHandle := -1;
Connected := False;
2012-09-06 01:33:44 -07:00
End;
End.