-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDataPortPipes.pas
385 lines (344 loc) · 9.48 KB
/
DataPortPipes.pas
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
{
Data exchange through named pipes
Sergey Bodrov, 2012-2016
Data exchange through named pipes. Pipe name is platform-specific. On Windows,
'\\.\pipe\' prefix added automaticaly.
Pipe must be already exists, created by Linux 'mkfifo' command or some other program.
Methods:
* Open() - open pipe channel with specified name
}
unit DataPortPipes;
interface
uses SysUtils, Classes, DataPort, Pipes;
type
{ TPipesClient - pipes port reader/writer }
TPipesClient = class(TThread)
private
FInputPipeStream: TInputPipeStream;
FOutputPipeStream: TOutputPipeStream;
s: AnsiString;
sLastError: string;
FSafeMode: boolean;
FInputHandle: THandle;
FOutputHandle: THandle;
FOnIncomingMsgEvent: TMsgEvent;
FOnErrorEvent: TMsgEvent;
FOnConnectEvent: TNotifyEvent;
procedure SyncProc();
procedure SyncProcOnConnect();
protected
procedure Execute(); override;
public
InitStr: string;
CalledFromThread: boolean;
sToSend: AnsiString;
property SafeMode: boolean read FSafeMode write FSafeMode;
property InputHandle: THandle read FInputHandle write FInputHandle;
property OutputHandle: THandle read FOutputHandle write FOutputHandle;
property OnIncomingMsgEvent: TMsgEvent read FOnIncomingMsgEvent
write FOnIncomingMsgEvent;
property OnErrorEvent: TMsgEvent read FOnErrorEvent write FOnErrorEvent;
property OnConnectEvent: TNotifyEvent read FOnConnectEvent write FOnConnectEvent;
function SendString(const s: AnsiString): boolean;
procedure SendStream(st: TStream);
end;
{ TDataPortPipes - serial DataPort }
TDataPortPipes = class(TDataPort)
private
//slReadData: TStringList; // for storing every incoming data packet separately
sReadData: AnsiString;
lock: TMultiReadExclusiveWriteSynchronizer;
FInitStr: string;
FMinDataBytes: Integer;
FInputHandle: THandle;
FOutputHandle: THandle;
procedure OnIncomingMsgHandler(Sender: TObject; const AMsg: string);
procedure OnErrorHandler(Sender: TObject; const AMsg: string);
procedure OnConnectHandler(Sender: TObject);
public
PipesClient: TPipesClient;
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
{ Open pipe, InitStr = pipe name }
procedure Open(const AInitStr: string = ''); override;
procedure Close(); override;
function Push(const AData: AnsiString): boolean; override;
function Pull(size: Integer = MaxInt): AnsiString; override;
function Peek(size: Integer = MaxInt): AnsiString; override;
function PeekSize(): Cardinal; override;
published
property InputHandle: THandle read FInputHandle write FInputHandle;
property OutputHandle: THandle read FOutputHandle write FOutputHandle;
{ Minimum bytes in incoming buffer to trigger OnDataAppear }
property MinDataBytes: Integer read FMinDataBytes write FMinDataBytes;
property Active;
property OnDataAppear;
property OnError;
property OnOpen;
property OnClose;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('DataPort', [TDataPortPipes]);
end;
// === TPipesClient ===
procedure TPipesClient.SyncProc();
begin
if CalledFromThread then
Exit;
//if s:='' then Exit;
CalledFromThread := True;
if s <> '' then
begin
if Assigned(self.FOnIncomingMsgEvent) then
FOnIncomingMsgEvent(self, s);
s := '';
end;
if sLastError <> '' then
begin
if Assigned(self.FOnErrorEvent) then
FOnErrorEvent(self, sLastError);
self.Terminate();
end;
CalledFromThread := False;
end;
procedure TPipesClient.SyncProcOnConnect();
begin
if CalledFromThread then
Exit;
CalledFromThread := True;
if Assigned(self.FOnConnectEvent) then
self.FOnConnectEvent(self);
CalledFromThread := False;
end;
procedure TPipesClient.Execute();
var
buf: array[0..1023] of byte;
n: Integer;
ss: AnsiString;
begin
sLastError := '';
buf[0] := 0;
try
FInputPipeStream := TInputPipeStream.Create(FInputHandle);
FOutputPipeStream := TOutputPipeStream.Create(FOutputHandle);
Synchronize(SyncProcOnConnect);
while not Terminated do
begin
n := FInputPipeStream.Read(buf, Length(buf));
while n > 0 do
begin
SetString(ss, PAnsiChar(@buf), n);
s := s + ss;
n := FInputPipeStream.Read(buf, Length(buf));
end;
sLastError := '';
if (Length(s) > 0) or (Length(sLastError) > 0) then
Synchronize(SyncProc);
Sleep(1);
if sToSend <> '' then
begin
try
Self.FOutputPipeStream.WriteAnsiString(sToSend);
except
on E: Exception do
begin
sLastError := E.Message;
Synchronize(SyncProc);
end;
end;
sToSend := '';
end;
end;
finally
FreeAndNil(FOutputPipeStream);
FreeAndNil(FInputPipeStream);
end;
end;
function TPipesClient.SendString(const s: AnsiString): boolean;
begin
Result := False;
if not Assigned(Self.FOutputPipeStream) then
Exit;
if SafeMode then
self.sToSend := s
else
begin
try
Self.FOutputPipeStream.WriteAnsiString(s);
except
on E: Exception do
begin
sLastError := E.Message;
Synchronize(SyncProc);
Exit;
end;
end;
end;
Result := True;
end;
procedure TPipesClient.SendStream(st: TStream);
begin
if not Assigned(Self.FOutputPipeStream) then
Exit;
try
Self.FOutputPipeStream.CopyFrom(st, st.Size);
except
on E: Exception do
begin
sLastError := E.Message;
Synchronize(SyncProc);
end;
end;
end;
{ TDataPortPipes }
constructor TDataPortPipes.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
self.lock := TMultiReadExclusiveWriteSynchronizer.Create();
FMinDataBytes := 1;
FActive := False;
Self.sReadData := '';
Self.PipesClient := nil;
end;
function GetFirstWord(var s: string; delimiter: string = ' '): string;
var
i: Integer;
begin
Result := '';
i := Pos(delimiter, s);
if i > 0 then
begin
Result := Copy(s, 1, i - 1);
s := Copy(s, i + 1, maxint);
end
else
begin
Result := s;
s := '';
end;
end;
procedure TDataPortPipes.Open(const AInitStr: string = '');
var
s, ss: string;
begin
ss := AInitStr;
if ss = '' then
ss := FInitStr
else
FInitStr := ss;
if Assigned(self.PipesClient) then
begin
FreeAndNil(self.PipesClient);
end;
Self.PipesClient := TPipesClient.Create(True);
Self.PipesClient.OnIncomingMsgEvent := self.OnIncomingMsgHandler;
Self.PipesClient.OnErrorEvent := self.OnErrorHandler;
Self.PipesClient.OnConnectEvent := self.OnConnectHandler;
Self.PipesClient.SafeMode := True;
if ss <> '' then
begin
s := AInitStr;
{$IFDEF MSWINDOWS}
if Pos('\\.\pipe\', FInitStr) = 0 then
s := '\\.\pipe\' + FInitStr;
{$ENDIF}
PipesClient.InputHandle := FileOpen(s, fmOpenReadWrite or fmShareDenyNone);
PipesClient.OutputHandle := PipesClient.InputHandle;
end
else
begin
PipesClient.InputHandle := InputHandle;
PipesClient.OutputHandle := OutputHandle;
end;
Self.PipesClient.Suspended := False;
// don't inherits Open() - OnOpen event will be after successfull connection
end;
procedure TDataPortPipes.Close();
begin
if Assigned(self.PipesClient) then
begin
if self.PipesClient.CalledFromThread then
self.PipesClient.Terminate()
else
FreeAndNil(self.PipesClient);
end;
inherited Close();
end;
destructor TDataPortPipes.Destroy();
begin
if Assigned(self.PipesClient) then
begin
self.PipesClient.OnIncomingMsgEvent := nil;
self.PipesClient.OnErrorEvent := nil;
self.PipesClient.OnConnectEvent := nil;
FreeAndNil(self.PipesClient);
end;
FreeAndNil(self.lock);
inherited Destroy();
end;
procedure TDataPortPipes.OnIncomingMsgHandler(Sender: TObject; const AMsg: string);
begin
if AMsg <> '' then
begin
if lock.BeginWrite then
begin
sReadData := sReadData + AMsg;
lock.EndWrite;
if Assigned(FOnDataAppear) then
FOnDataAppear(self);
end;
end;
end;
procedure TDataPortPipes.OnErrorHandler(Sender: TObject; const AMsg: string);
begin
if Assigned(Self.FOnError) then
Self.FOnError(Self, AMsg);
self.FActive := False;
end;
function TDataPortPipes.Peek(size: Integer = MaxInt): AnsiString;
begin
lock.BeginRead();
Result := Copy(sReadData, 1, size);
lock.EndRead();
end;
function TDataPortPipes.PeekSize(): Cardinal;
begin
lock.BeginRead();
Result := Cardinal(Length(sReadData));
lock.EndRead();
end;
function TDataPortPipes.Pull(size: Integer = MaxInt): AnsiString;
begin
Result := '';
if lock.BeginWrite() then
begin
try
Result := Copy(sReadData, 1, size);
Delete(sReadData, 1, size);
finally
lock.EndWrite();
end;
end;
end;
function TDataPortPipes.Push(const AData: AnsiString): boolean;
begin
Result := False;
if Assigned(self.PipesClient) and lock.BeginWrite() then
begin
try
Result := self.PipesClient.SendString(AData);
finally
lock.EndWrite();
end;
end;
end;
procedure TDataPortPipes.OnConnectHandler(Sender: TObject);
begin
self.FActive := True;
if Assigned(OnOpen) then
OnOpen(Self);
end;
end.