forked from jackokring/rub
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimpleipc.inc
198 lines (159 loc) · 5.04 KB
/
simpleipc.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{
This file is part of the Free Component library.
Copyright (c) 2005 by Michael Van Canneyt, member of
the Free Pascal development team
Unix implementation of one-way IPC between 2 processes
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.
**********************************************************************}
uses baseunix;
ResourceString
SErrFailedToCreatePipe = 'Failed to create named pipe: %s';
SErrFailedToRemovePipe = 'Failed to remove named pipe: %s';
{ ---------------------------------------------------------------------
TPipeClientComm
---------------------------------------------------------------------}
Type
TPipeClientComm = Class(TIPCClientComm)
Private
FFileName: String;
FStream: TFileStream;
Public
Constructor Create(AOWner : TSimpleIPCClient); override;
Procedure Connect; override;
Procedure Disconnect; override;
Procedure SendMessage(MsgType : TMessageType; AStream : TStream); override;
Function ServerRunning : Boolean; override;
Property FileName : String Read FFileName;
Property Stream : TFileStream Read FStream;
end;
constructor TPipeClientComm.Create(AOWner: TSimpleIPCClient);
Var
D : String;
begin
inherited Create(AOWner);
FFileName:=Owner.ServerID;
If (Owner.ServerInstance<>'') then
FFileName:=FFileName+'-'+Owner.ServerInstance;
D:='/tmp/'; // Change to something better later
FFileName:=D+FFileName;
end;
procedure TPipeClientComm.Connect;
begin
If Not ServerRunning then
Owner.DoError(SErrServerNotActive,[Owner.ServerID]);
// Use the sharedenynone line to allow more then one client
// communicating with one server at the same time
// see also mantis 15219
FStream:=TFileStream.Create(FFileName,fmOpenWrite+fmShareDenyNone);
// FStream:=TFileStream.Create(FFileName,fmOpenWrite);
end;
procedure TPipeClientComm.Disconnect;
begin
FreeAndNil(FStream);
end;
procedure TPipeClientComm.SendMessage(MsgType : TMessagetype; AStream: TStream);
Var
Hdr : TMsgHeader;
P,L,Count : Integer;
begin
Hdr.Version:=MsgVersion;
Hdr.msgType:=MsgType;
Hdr.MsgLen:=AStream.Size;
FStream.WriteBuffer(hdr,SizeOf(hdr));
FStream.CopyFrom(AStream,0);
end;
function TPipeClientComm.ServerRunning: Boolean;
begin
Result:=FileExists(FFileName);
end;
{ ---------------------------------------------------------------------
TPipeServerComm
---------------------------------------------------------------------}
Type
TPipeServerComm = Class(TIPCServerComm)
Private
FFileName: String;
FStream: TFileStream;
Public
Constructor Create(AOWner : TSimpleIPCServer); override;
Procedure StartServer; override;
Procedure StopServer; override;
Function PeekMessage(TimeOut : Integer) : Boolean; override;
Procedure ReadMessage ; override;
Function GetInstanceID : String;override;
Property FileName : String Read FFileName;
Property Stream : TFileStream Read FStream;
end;
constructor TPipeServerComm.Create(AOWner: TSimpleIPCServer);
Var
D : String;
begin
inherited Create(AOWner);
FFileName:=Owner.ServerID;
If Not Owner.Global then
FFileName:=FFileName+'-'+IntToStr(fpGetPID);
D:='/tmp/'; // Change to something better later
FFileName:=D+FFileName;
end;
procedure TPipeServerComm.StartServer;
begin
If not FileExists(FFileName) then
If (fpmkFifo(FFileName,438)<>0) then
Owner.DoError(SErrFailedToCreatePipe,[FFileName]);
FStream:=TFileStream.Create(FFileName,fmOpenReadWrite+fmShareDenyNone);
end;
procedure TPipeServerComm.StopServer;
begin
FreeAndNil(FStream);
if Not DeleteFile(FFileName) then
Owner.DoError(SErrFailedtoRemovePipe,[FFileName]);
end;
function TPipeServerComm.PeekMessage(TimeOut: Integer): Boolean;
Var
FDS : TFDSet;
begin
fpfd_zero(FDS);
fpfd_set(FStream.Handle,FDS);
Result:=fpSelect(FStream.Handle+1,@FDS,Nil,Nil,TimeOut)>0;
end;
procedure TPipeServerComm.ReadMessage;
Var
L,P,Count : Integer;
Hdr : TMsgHeader;
begin
FStream.ReadBuffer(Hdr,SizeOf(Hdr));
Owner.FMsgType:=Hdr.MsgType;
Count:=Hdr.MsgLen;
if count > 0 then
begin
Owner.FMsgData.Seek(0,soFrombeginning);
Owner.FMsgData.CopyFrom(FStream,Count);
end
else
Owner.FMsgData.Size := 0;
end;
function TPipeServerComm.GetInstanceID: String;
begin
Result:=IntToStr(fpGetPID);
end;
{ ---------------------------------------------------------------------
Set TSimpleIPCClient / TSimpleIPCServer defaults.
---------------------------------------------------------------------}
Function TSimpleIPCServer.CommClass : TIPCServerCommClass;
begin
if (DefaultIPCServerClass<>Nil) then
Result:=DefaultIPCServerClass
else
Result:=TPipeServerComm;
end;
function TSimpleIPCClient.CommClass: TIPCClientCommClass;
begin
if (DefaultIPCClientClass<>Nil) then
Result:=DefaultIPCClientClass
else
Result:=TPipeClientComm;
end;