Named Pipe updates

This commit is contained in:
mysticbbs 2012-09-22 18:54:14 -04:00
parent 8164f855e8
commit 9764fb3e8e
3 changed files with 48 additions and 22 deletions

View File

@ -5,8 +5,8 @@ Unit m_Pipe;
Interface Interface
{$IFDEF UNIX} {$IFDEF UNIX}
Uses m_Pipe_Disk; Uses m_Pipe_Unix;
Type TPipe = Class(TPipeDisk); Type TPipe = Class(TPipeUnix);
{$ENDIF} {$ENDIF}
{$IFDEF WINDOWS} {$IFDEF WINDOWS}

View File

@ -5,6 +5,7 @@ Unit m_Pipe_Unix;
Interface Interface
Uses Uses
BaseUnix,
m_DateTime, m_DateTime,
m_FileIO, m_FileIO,
m_Strings; m_Strings;
@ -14,7 +15,7 @@ Type
PipeID : Word; PipeID : Word;
Connected : Boolean; Connected : Boolean;
IsClient : Boolean; IsClient : Boolean;
PipeDir : String; PipeHandle : THandle;
Constructor Create (Dir: String; Client: Boolean; ID: Word); Constructor Create (Dir: String; Client: Boolean; ID: Word);
Destructor Destroy; Override; Destructor Destroy; Override;
@ -25,7 +26,7 @@ Type
Function ConnectPipe (Secs: LongInt) : Boolean; Function ConnectPipe (Secs: LongInt) : Boolean;
// General functions // General functions
Procedure SendToPipe (Var Buf; Len: Longint); Procedure SendToPipe (Var Buf; Len: Longint);
Procedure ReadFromPipe (Var Buf; Len: LongInt; Var bRead: LongInt); Procedure ReadFromPipe (Var Buf; Len: LongInt; Var bRead: LongWord);
Procedure Disconnect; Procedure Disconnect;
End; End;
@ -35,8 +36,8 @@ Constructor TPipeUnix.Create (Dir: String; Client: Boolean; ID: Word);
Begin Begin
Connected := False; Connected := False;
IsClient := Client; IsClient := Client;
PipeDir := DirSlash(Dir);
PipeID := ID; PipeID := ID;
PipeHandle := -1;
End; End;
Destructor TPipeUnix.Destroy; Destructor TPipeUnix.Destroy;
@ -47,48 +48,72 @@ Begin
End; End;
Function TPipeUnix.CreatePipe : Boolean; Function TPipeUnix.CreatePipe : Boolean;
Var
PipeName : String;
Begin Begin
Result := False;
IsClient := False; IsClient := False;
PipeName := '/tmp/mystic_' + strI2S(PipeID);
Result := True; If Not FileExist(PipeName) Then
fpMkFIFO(PipeName, 438);
PipeHandle := fpOpen(PipeName, O_WRONLY, O_NONBLOCK);
Result := PipeHandle >= 0;
End; End;
Procedure TPipeUnix.SendToPipe (Var Buf; Len: LongInt); Procedure TPipeUnix.SendToPipe (Var Buf; Len: LongInt);
Begin Begin
If Not Connected Then Exit; If Not Connected Then Exit;
If fpWrite (PipeHandle, Buf, Len) < 0 Then
Disconnect;
End; End;
Procedure TPipeUnix.ReadFromPipe (Var Buf; Len: LongInt; Var bRead: LongInt); Procedure TPipeUnix.ReadFromPipe (Var Buf; Len: LongInt; Var bRead: LongWord);
Begin Begin
bRead := 0; bRead := 0;
If Not Connected Then Exit; If Not Connected Then Exit;
bRead := fpRead (PipeHandle, Buf, Len);
If bRead < 0 Then Disconnect;
End; End;
Function TPipeUnix.WaitForPipe (Secs: LongInt) : Boolean; Function TPipeUnix.WaitForPipe (Secs: LongInt) : Boolean;
Begin Begin
Result := Connected; Connected := PipeHandle > -1;
Result := Connected;
If Connected Then Exit;
Result := Connected;
End; End;
Function TPipeUnix.ConnectPipe (Secs: LongInt) : Boolean; Function TPipeUnix.ConnectPipe (Secs: LongInt) : Boolean;
Var
PipeName : String;
TimeOut : LongInt;
Begin Begin
Result := False; IsClient := True;
Connected := False;
IsClient := True; Disconnect;
PipeName := '/tmp/mystic_' + strI2S(PipeID);
TimeOut := TimerSet(Secs);
Repeat
PipeHandle := fpOpen(PipeName, O_RDONLY, O_NONBLOCK);
Connected := PipeHandle >= 0;
Until Connected or TimerUp(TimeOut);
Result := Connected; Result := Connected;
End; End;
Procedure TPipeUnix.Disconnect; Procedure TPipeUnix.Disconnect;
Begin Begin
If Not Connected Then Exit; If PipeHandle = -1 Then Exit;
Connected := False; fpClose (PipeHandle);
PipeHandle := -1;
Connected := False;
End; End;
End. End.

View File

@ -85,8 +85,8 @@ Begin
WriteFile (PipeHandle, Buf, Len, Written, NIL); WriteFile (PipeHandle, Buf, Len, Written, NIL);
// Connected := GetLastError = ERROR_SUCCESS; If Written <= 0 Then
Connected := Written > 0; Disconnect; // was ERROR_SUCCESS check
End; End;
Procedure TPipeWindows.ReadFromPipe (Var Buf; Len: LongInt; Var bRead: LongWord); Procedure TPipeWindows.ReadFromPipe (Var Buf; Len: LongInt; Var bRead: LongWord);
@ -97,7 +97,8 @@ Begin
ReadFile (PipeHandle, Buf, Len, bRead, NIL); ReadFile (PipeHandle, Buf, Len, bRead, NIL);
Connected := GetLastError = ERROR_SUCCESS; If GetLastError <> ERROR_SUCCESS Then
Disconnect;
End; End;
Function TPipeWindows.WaitForPipe (Secs: LongInt) : Boolean; Function TPipeWindows.WaitForPipe (Secs: LongInt) : Boolean;
@ -158,7 +159,7 @@ End;
Procedure TPipeWindows.Disconnect; Procedure TPipeWindows.Disconnect;
Begin Begin
If Not Connected Then Exit; If PipeHandle = -1 Then Exit;
DisconnectNamedPipe (PipeHandle); DisconnectNamedPipe (PipeHandle);
CloseHandle (PipeHandle); CloseHandle (PipeHandle);