blob: 54e00a45bdd2f5547ef365d825be6c501464be21 [file] [log] [blame]
Roger Meier3bef8c22012-10-06 06:58:00 +00001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19unit Thrift.Transport.Pipes;
20
21{$WARN SYMBOL_PLATFORM OFF}
22
23interface
24
25uses
26 Windows, SysUtils, Math, AccCtrl, AclAPI,
27 Thrift.Transport,
28 Thrift.Console,
29 Thrift.Stream;
30
31const
32 DEFAULT_THRIFT_PIPE_TIMEOUT = 5 * 1000; // ms
33
34
35type
Roger Meier79655fb2012-10-20 20:59:41 +000036 //--- Pipe Streams ---
Roger Meier3bef8c22012-10-06 06:58:00 +000037
38
Roger Meier79655fb2012-10-20 20:59:41 +000039 TPipeStreamBaseImpl = class( TThriftStreamImpl)
40 strict protected
41 FPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +000042 FTimeout : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +000043
Roger Meier3bef8c22012-10-06 06:58:00 +000044 procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override;
45 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override;
Roger Meier79655fb2012-10-20 20:59:41 +000046 //procedure Open; override; - see derived classes
Roger Meier3bef8c22012-10-06 06:58:00 +000047 procedure Close; override;
48 procedure Flush; override;
49
50 function IsOpen: Boolean; override;
51 function ToArray: TBytes; override;
52 public
Roger Meier79655fb2012-10-20 20:59:41 +000053 constructor Create( const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +000054 destructor Destroy; override;
55 end;
56
57
Roger Meier79655fb2012-10-20 20:59:41 +000058 TNamedPipeStreamImpl = class sealed( TPipeStreamBaseImpl)
59 private
60 FPipeName : string;
61 FShareMode : DWORD;
62 FSecurityAttribs : PSecurityAttributes;
Roger Meier3bef8c22012-10-06 06:58:00 +000063
Roger Meier79655fb2012-10-20 20:59:41 +000064 protected
65 procedure Open; override;
66
67 public
68 constructor Create( const aPipeName : string;
69 const aShareMode: DWORD = 0;
70 const aSecurityAttributes: PSecurityAttributes = nil;
71 const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); overload;
72 end;
73
74
75 THandlePipeStreamImpl = class sealed( TPipeStreamBaseImpl)
76 private
77 FSrcHandle : THandle;
78
79 protected
80 procedure Open; override;
81
82 public
83 constructor Create( const aPipeHandle : THandle; aOwnsHandle : Boolean); overload;
84 destructor Destroy; override;
85 end;
86
87
88 //--- Pipe Transports ---
89
90
91 IPipe = interface( IStreamTransport)
92 ['{5E05CC85-434F-428F-BFB2-856A168B5558}']
93 end;
94
95
96 TPipeTransportBaseImpl = class( TStreamTransportImpl, IPipe)
97 public
98 // ITransport
99 function GetIsOpen: Boolean; override;
100 procedure Open; override;
101 procedure Close; override;
102 end;
103
104
105 TNamedPipeImpl = class( TPipeTransportBaseImpl)
106 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000107 // Named pipe constructors
108 constructor Create( aPipe : THandle; aOwnsHandle : Boolean); overload;
109 constructor Create( const aPipeName : string;
110 const aShareMode: DWORD = 0;
111 const aSecurityAttributes: PSecurityAttributes = nil;
112 const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000113 end;
114
115
Roger Meier79655fb2012-10-20 20:59:41 +0000116 TNamedPipeServerImpl = class( TNamedPipeImpl)
117 strict private
118 FHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000119 public
Roger Meier79655fb2012-10-20 20:59:41 +0000120 // ITransport
121 procedure Close; override;
122 constructor Create( aPipe : THandle; aOwnsHandle : Boolean); reintroduce;
123 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000124
Roger Meier79655fb2012-10-20 20:59:41 +0000125
126 TAnonymousPipeImpl = class( TPipeTransportBaseImpl)
127 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000128 // Anonymous pipe constructor
129 constructor Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000130 end;
131
132
Roger Meier79655fb2012-10-20 20:59:41 +0000133 //--- Server Transports ---
134
135
136 IAnonymousServerPipe = interface( IServerTransport)
Roger Meier3bef8c22012-10-06 06:58:00 +0000137 ['{7AEE6793-47B9-4E49-981A-C39E9108E9AD}']
138 // Server side anonymous pipe ends
Roger Meier79655fb2012-10-20 20:59:41 +0000139 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000140 function WriteHandle : THandle;
141 // Client side anonymous pipe ends
142 function ClientAnonRead : THandle;
143 function ClientAnonWrite : THandle;
144 end;
145
146
Roger Meier79655fb2012-10-20 20:59:41 +0000147 INamedServerPipe = interface( IServerTransport)
148 ['{9DF9EE48-D065-40AF-8F67-D33037D3D960}']
149 function Handle : THandle;
150 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000151
Roger Meier79655fb2012-10-20 20:59:41 +0000152
153 TServerPipeBaseImpl = class( TServerTransportImpl)
154 public
155 procedure Listen; override;
156 end;
157
158
159 TAnonymousServerPipeImpl = class( TServerPipeBaseImpl, IAnonymousServerPipe)
160 private
161 FBufSize : DWORD;
162
163 // Server side anonymous pipe handles
164 FReadHandle,
Roger Meier3bef8c22012-10-06 06:58:00 +0000165 FWriteHandle : THandle;
166
167 //Client side anonymous pipe handles
168 FClientAnonRead,
169 FClientAnonWrite : THandle;
170
171 protected
172 function AcceptImpl: ITransport; override;
173
Roger Meier3bef8c22012-10-06 06:58:00 +0000174 function CreateAnonPipe : Boolean;
175
Roger Meier79655fb2012-10-20 20:59:41 +0000176 // IAnonymousServerPipe
177 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000178 function WriteHandle : THandle;
179 function ClientAnonRead : THandle;
180 function ClientAnonWrite : THandle;
181
182 public
Roger Meier79655fb2012-10-20 20:59:41 +0000183 constructor Create( aBufsize : Cardinal = 4096);
Roger Meier3bef8c22012-10-06 06:58:00 +0000184
Roger Meier3bef8c22012-10-06 06:58:00 +0000185 procedure Close; override;
186 end;
187
188
Roger Meier79655fb2012-10-20 20:59:41 +0000189 TNamedServerPipeImpl = class( TServerPipeBaseImpl, INamedServerPipe)
190 private
191 FPipeName : string;
192 FMaxConns : DWORD;
193 FBufSize : DWORD;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100194 FTimeout : DWORD;
Roger Meier79655fb2012-10-20 20:59:41 +0000195
196 FHandle : THandle;
197
198 protected
199 function AcceptImpl: ITransport; override;
Jens Geyerb64a7742013-01-23 20:58:47 +0100200 procedure CreateNamedPipe;
Roger Meier79655fb2012-10-20 20:59:41 +0000201
202 // INamedServerPipe
203 function Handle : THandle;
204
205 public
206 constructor Create( aPipename : string; aBufsize : Cardinal = 4096;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100207 aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES;
208 aTimeOut : Cardinal = 0);
Roger Meier79655fb2012-10-20 20:59:41 +0000209
210 procedure Close; override;
211 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000212
213
214implementation
215
216
Roger Meier79655fb2012-10-20 20:59:41 +0000217procedure ClosePipeHandle( var hPipe : THandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000218begin
Roger Meier79655fb2012-10-20 20:59:41 +0000219 if hPipe <> INVALID_HANDLE_VALUE
220 then try
221 CloseHandle( hPipe);
222 finally
223 hPipe := INVALID_HANDLE_VALUE;
224 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000225end;
226
227
Roger Meier79655fb2012-10-20 20:59:41 +0000228function DuplicatePipeHandle( const hSource : THandle) : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000229begin
Roger Meier79655fb2012-10-20 20:59:41 +0000230 if not DuplicateHandle( GetCurrentProcess, hSource,
231 GetCurrentProcess, @result,
232 0, FALSE, DUPLICATE_SAME_ACCESS)
233 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
234 'DuplicateHandle: '+SysErrorMessage(GetLastError));
Roger Meier3bef8c22012-10-06 06:58:00 +0000235end;
236
237
Roger Meier79655fb2012-10-20 20:59:41 +0000238
239{ TPipeStreamBaseImpl }
240
241
242constructor TPipeStreamBaseImpl.Create( const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT);
243begin
244 inherited Create;
245 FPipe := INVALID_HANDLE_VALUE;
246 FTimeout := aTimeOut;
247end;
248
249
250destructor TPipeStreamBaseImpl.Destroy;
Roger Meier3bef8c22012-10-06 06:58:00 +0000251begin
252 try
253 Close;
254 finally
255 inherited Destroy;
256 end;
257end;
258
259
Roger Meier79655fb2012-10-20 20:59:41 +0000260procedure TPipeStreamBaseImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000261begin
Roger Meier79655fb2012-10-20 20:59:41 +0000262 ClosePipeHandle( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000263end;
264
265
Roger Meier79655fb2012-10-20 20:59:41 +0000266procedure TPipeStreamBaseImpl.Flush;
Roger Meier3bef8c22012-10-06 06:58:00 +0000267begin
268 // nothing to do
269end;
270
271
Roger Meier79655fb2012-10-20 20:59:41 +0000272function TPipeStreamBaseImpl.IsOpen: Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000273begin
274 result := (FPipe <> INVALID_HANDLE_VALUE);
275end;
276
277
Roger Meier79655fb2012-10-20 20:59:41 +0000278procedure TPipeStreamBaseImpl.Write(const buffer: TBytes; offset, count: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000279var cbWritten : DWORD;
280begin
281 if not IsOpen
282 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
283 'Called write on non-open pipe');
284
285 if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil)
286 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
287 'Write to pipe failed');
288end;
289
290
Roger Meier79655fb2012-10-20 20:59:41 +0000291function TPipeStreamBaseImpl.Read( var buffer: TBytes; offset, count: Integer): Integer;
292var cbRead, dwErr : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000293 bytes, retries : LongInt;
294 bOk : Boolean;
295const INTERVAL = 10; // ms
296begin
297 if not IsOpen
298 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
299 'Called read on non-open pipe');
300
301 // MSDN: Handle can be a handle to a named pipe instance,
302 // or it can be a handle to the read end of an anonymous pipe,
303 // The handle must have GENERIC_READ access to the pipe.
304 if FTimeOut <> INFINITE then begin
305 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
306 while TRUE do begin
307 if IsOpen
308 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
309 and (bytes > 0)
310 then Break; // there are data
311
Roger Meier79655fb2012-10-20 20:59:41 +0000312 dwErr := GetLastError;
313 if (dwErr = ERROR_BROKEN_PIPE)
314 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
315 then begin
316 result := 0; // other side closed the pipe
317 Exit;
318 end;
319
Roger Meier3bef8c22012-10-06 06:58:00 +0000320 Dec( retries);
321 if retries > 0
322 then Sleep( INTERVAL)
323 else raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
324 'Pipe read timed out');
325 end;
326 end;
327
328 // read the data (or block INFINITE-ly)
329 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, nil);
330 if (not bOk) and (GetLastError() <> ERROR_MORE_DATA)
331 then result := 0 // No more data, possibly because client disconnected.
332 else result := cbRead;
333end;
334
335
Roger Meier79655fb2012-10-20 20:59:41 +0000336function TPipeStreamBaseImpl.ToArray: TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000337var bytes : LongInt;
338begin
339 SetLength( result, 0);
340 bytes := 0;
341
342 if IsOpen
343 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
344 and (bytes > 0)
345 then begin
346 SetLength( result, bytes);
347 Read( result, 0, bytes);
348 end;
349end;
350
351
Roger Meier79655fb2012-10-20 20:59:41 +0000352{ TNamedPipeStreamImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000353
354
Roger Meier79655fb2012-10-20 20:59:41 +0000355constructor TNamedPipeStreamImpl.Create( const aPipeName : string; const aShareMode: DWORD;
356 const aSecurityAttributes: PSecurityAttributes;
357 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000358begin
Roger Meier79655fb2012-10-20 20:59:41 +0000359 inherited Create( aTimeout);
360
361 FPipeName := aPipeName;
362 FShareMode := aShareMode;
363 FSecurityAttribs := aSecurityAttributes;
364
365 if Copy(FPipeName,1,2) <> '\\'
366 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
Roger Meier3bef8c22012-10-06 06:58:00 +0000367end;
368
369
Roger Meier79655fb2012-10-20 20:59:41 +0000370procedure TNamedPipeStreamImpl.Open;
371var hPipe : THandle;
372 dwMode : DWORD;
373begin
374 if IsOpen then Exit;
375
376 // open that thingy
377
378 if not WaitNamedPipe( PChar(FPipeName), FTimeout)
379 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
380 'Unable to open pipe, '+SysErrorMessage(GetLastError));
381
382 hPipe := CreateFile( PChar( FPipeName),
383 GENERIC_READ or GENERIC_WRITE,
384 FShareMode, // sharing
385 FSecurityAttribs, // security attributes
386 OPEN_EXISTING, // opens existing pipe
387 0, // default attributes
388 0); // no template file
389
390 if hPipe = INVALID_HANDLE_VALUE
391 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
392 'Unable to open pipe, '+SysErrorMessage(GetLastError));
393
394 // pipe connected; change to message-read mode.
395 dwMode := PIPE_READMODE_MESSAGE;
396 if not SetNamedPipeHandleState( hPipe, dwMode, nil, nil) then begin
397 Close;
398 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
399 'SetNamedPipeHandleState failed');
400 end;
401
402 // everything fine
403 FPipe := hPipe;
404end;
405
406
407{ THandlePipeStreamImpl }
408
409
410constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle; aOwnsHandle : Boolean);
411begin
412 inherited Create( DEFAULT_THRIFT_PIPE_TIMEOUT);
413
414 if aOwnsHandle
415 then FSrcHandle := aPipeHandle
416 else FSrcHandle := DuplicatePipeHandle( aPipeHandle);
417
418 Open;
419end;
420
421
422destructor THandlePipeStreamImpl.Destroy;
423begin
424 try
425 ClosePipeHandle( FSrcHandle);
426 finally
427 inherited Destroy;
428 end;
429end;
430
431
432procedure THandlePipeStreamImpl.Open;
433begin
434 if not IsOpen
435 then FPipe := DuplicatePipeHandle( FSrcHandle);
436end;
437
438
439{ TPipeTransportBaseImpl }
440
441
442function TPipeTransportBaseImpl.GetIsOpen: Boolean;
443begin
Jens Geyer0b20cc82013-03-07 20:47:01 +0100444 result := (FInputStream <> nil) and (FInputStream.IsOpen)
445 and (FOutputStream <> nil) and (FOutputStream.IsOpen);
Roger Meier79655fb2012-10-20 20:59:41 +0000446end;
447
448
449procedure TPipeTransportBaseImpl.Open;
450begin
451 FInputStream.Open;
452 FOutputStream.Open;
453end;
454
455
456procedure TPipeTransportBaseImpl.Close;
457begin
458 FInputStream.Close;
459 FOutputStream.Close;
460end;
461
462
463{ TNamedPipeImpl }
464
465
Roger Meier3bef8c22012-10-06 06:58:00 +0000466constructor TNamedPipeImpl.Create( const aPipeName : string; const aShareMode: DWORD;
467 const aSecurityAttributes: PSecurityAttributes;
468 const aTimeOut : DWORD);
469// Named pipe constructor
470begin
Roger Meier79655fb2012-10-20 20:59:41 +0000471 inherited Create( nil, nil);
472 FInputStream := TNamedPipeStreamImpl.Create( aPipeName, aShareMode, aSecurityAttributes, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000473 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000474end;
475
476
477constructor TNamedPipeImpl.Create( aPipe : THandle; aOwnsHandle : Boolean);
478// Named pipe constructor
479begin
Roger Meier79655fb2012-10-20 20:59:41 +0000480 inherited Create( nil, nil);
481 FInputStream := THandlePipeStreamImpl.Create( aPipe, aOwnsHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000482 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000483end;
484
485
Roger Meier79655fb2012-10-20 20:59:41 +0000486{ TNamedPipeServerImpl }
487
488
489constructor TNamedPipeServerImpl.Create( aPipe : THandle; aOwnsHandle : Boolean);
490// Named pipe constructor
Roger Meier3bef8c22012-10-06 06:58:00 +0000491begin
Roger Meier79655fb2012-10-20 20:59:41 +0000492 FHandle := DuplicatePipeHandle( aPipe);
493 inherited Create( aPipe, aOwnsHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000494end;
495
496
Roger Meier79655fb2012-10-20 20:59:41 +0000497procedure TNamedPipeServerImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000498begin
Roger Meier79655fb2012-10-20 20:59:41 +0000499 FlushFileBuffers( FHandle);
500 DisconnectNamedPipe( FHandle); // force client off the pipe
501 ClosePipeHandle( FHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000502
Roger Meier79655fb2012-10-20 20:59:41 +0000503 inherited Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000504end;
505
506
507{ TAnonymousPipeImpl }
508
509
Roger Meier3bef8c22012-10-06 06:58:00 +0000510constructor TAnonymousPipeImpl.Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean);
511// Anonymous pipe constructor
512begin
Roger Meier79655fb2012-10-20 20:59:41 +0000513 inherited Create( nil, nil);
514 FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles);
515 FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles);
Roger Meier3bef8c22012-10-06 06:58:00 +0000516end;
517
518
Roger Meier79655fb2012-10-20 20:59:41 +0000519{ TServerPipeBaseImpl }
520
521
522procedure TServerPipeBaseImpl.Listen;
Roger Meier3bef8c22012-10-06 06:58:00 +0000523begin
Roger Meier79655fb2012-10-20 20:59:41 +0000524 // not much to do here
Roger Meier3bef8c22012-10-06 06:58:00 +0000525end;
526
527
Roger Meier79655fb2012-10-20 20:59:41 +0000528{ TAnonymousServerPipeImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000529
530
Roger Meier79655fb2012-10-20 20:59:41 +0000531constructor TAnonymousServerPipeImpl.Create( aBufsize : Cardinal);
Roger Meier3bef8c22012-10-06 06:58:00 +0000532// Anonymous pipe CTOR
533begin
534 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000535 FBufsize := aBufSize;
Roger Meier79655fb2012-10-20 20:59:41 +0000536 FReadHandle := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000537 FWriteHandle := INVALID_HANDLE_VALUE;
538 FClientAnonRead := INVALID_HANDLE_VALUE;
539 FClientAnonWrite := INVALID_HANDLE_VALUE;
540
541 // The anonymous pipe needs to be created first so that the server can
542 // pass the handles on to the client before the serve (acceptImpl)
543 // blocking call.
544 if not CreateAnonPipe
545 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
546 ClassName+'.Create() failed');
547end;
548
549
Roger Meier79655fb2012-10-20 20:59:41 +0000550function TAnonymousServerPipeImpl.AcceptImpl: ITransport;
Roger Meier3bef8c22012-10-06 06:58:00 +0000551var buf : Byte;
552 br : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000553begin
Roger Meier79655fb2012-10-20 20:59:41 +0000554 // This 0-byte read serves merely as a blocking call.
555 if not ReadFile( FReadHandle, buf, 0, br, nil)
556 and (GetLastError() <> ERROR_MORE_DATA)
557 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
558 'TServerPipe unable to initiate pipe communication');
559 result := TAnonymousPipeImpl.Create( FReadHandle, FWriteHandle, FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +0000560end;
561
562
Roger Meier79655fb2012-10-20 20:59:41 +0000563procedure TAnonymousServerPipeImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000564begin
Roger Meier79655fb2012-10-20 20:59:41 +0000565 ClosePipeHandle( FReadHandle);
566 ClosePipeHandle( FWriteHandle);
567 ClosePipeHandle( FClientAnonRead);
568 ClosePipeHandle( FClientAnonWrite);
Roger Meier3bef8c22012-10-06 06:58:00 +0000569end;
570
571
Roger Meier79655fb2012-10-20 20:59:41 +0000572function TAnonymousServerPipeImpl.ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000573begin
Roger Meier79655fb2012-10-20 20:59:41 +0000574 result := FReadHandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000575end;
576
577
Roger Meier79655fb2012-10-20 20:59:41 +0000578function TAnonymousServerPipeImpl.WriteHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000579begin
580 result := FWriteHandle;
581end;
582
583
Roger Meier79655fb2012-10-20 20:59:41 +0000584function TAnonymousServerPipeImpl.ClientAnonRead : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000585begin
586 result := FClientAnonRead;
587end;
588
589
Roger Meier79655fb2012-10-20 20:59:41 +0000590function TAnonymousServerPipeImpl.ClientAnonWrite : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000591begin
592 result := FClientAnonWrite;
593end;
594
595
Roger Meier79655fb2012-10-20 20:59:41 +0000596function TAnonymousServerPipeImpl.CreateAnonPipe : Boolean;
597var sd : PSECURITY_DESCRIPTOR;
598 sa : SECURITY_ATTRIBUTES; //TSecurityAttributes;
599 hCAR, hPipeW, hCAW, hPipe : THandle;
600begin
601 result := FALSE;
602
603 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
Jens Geyerb64a7742013-01-23 20:58:47 +0100604 try
605 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
606 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
Roger Meier79655fb2012-10-20 20:59:41 +0000607
Jens Geyerb64a7742013-01-23 20:58:47 +0100608 sa.nLength := sizeof( sa);
609 sa.lpSecurityDescriptor := sd;
610 sa.bInheritHandle := TRUE; //allow passing handle to child
Roger Meier79655fb2012-10-20 20:59:41 +0000611
Jens Geyerb64a7742013-01-23 20:58:47 +0100612 if not CreatePipe( hCAR, hPipeW, @sa, FBufSize) then begin //create stdin pipe
613 Console.WriteLine( 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
614 Exit;
615 end;
616
617 if not CreatePipe( hPipe, hCAW, @sa, FBufSize) then begin //create stdout pipe
618 Console.WriteLine( 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
619 CloseHandle( hCAR);
620 CloseHandle( hPipeW);
621 Exit;
622 end;
623
624 FClientAnonRead := hCAR;
625 FClientAnonWrite := hCAW;
626 FReadHandle := hPipe;
627 FWriteHandle := hPipeW;
628
629 result := TRUE;
630
631 finally
632 if sd <> nil then LocalFree( Cardinal(sd));
Roger Meier79655fb2012-10-20 20:59:41 +0000633 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000634end;
635
636
637{ TNamedServerPipeImpl }
638
639
Jens Geyer0b20cc82013-03-07 20:47:01 +0100640constructor TNamedServerPipeImpl.Create( aPipename : string; aBufsize, aMaxConns, aTimeOut : Cardinal);
Roger Meier79655fb2012-10-20 20:59:41 +0000641// Named Pipe CTOR
642begin
643 inherited Create;
644 FPipeName := aPipename;
645 FBufsize := aBufSize;
646 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
647 FHandle := INVALID_HANDLE_VALUE;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100648 FTimeout := aTimeOut;
Roger Meier79655fb2012-10-20 20:59:41 +0000649
650 if Copy(FPipeName,1,2) <> '\\'
651 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
652end;
653
654
655function TNamedServerPipeImpl.AcceptImpl: ITransport;
656var connectRet : Boolean;
657begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100658 CreateNamedPipe;
Roger Meier79655fb2012-10-20 20:59:41 +0000659
660 // Wait for the client to connect; if it succeeds, the
661 // function returns a nonzero value. If the function returns
662 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
663 if ConnectNamedPipe( FHandle,nil)
664 then connectRet := TRUE
665 else connectRet := (GetLastError() = ERROR_PIPE_CONNECTED);
666
667 if not connectRet then begin
668 Close;
669 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
670 'TServerPipe: client connection failed');
671 end;
672
673 result := TNamedPipeServerImpl.Create( FHandle, TRUE);
674end;
675
676
677procedure TNamedServerPipeImpl.Close;
678begin
679 if FHandle <> INVALID_HANDLE_VALUE
680 then try
681 FlushFileBuffers( FHandle);
682 DisconnectNamedPipe( FHandle);
683 finally
684 ClosePipeHandle( FHandle);
685 end;
686end;
687
688
689function TNamedServerPipeImpl.Handle : THandle;
690begin
691 result := FHandle;
692end;
693
694
Jens Geyerb64a7742013-01-23 20:58:47 +0100695procedure TNamedServerPipeImpl.CreateNamedPipe;
Roger Meier3bef8c22012-10-06 06:58:00 +0000696var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
697 everyone_sid : PSID;
698 ea : EXPLICIT_ACCESS;
699 acl : PACL;
700 sd : PSECURITY_DESCRIPTOR;
701 sa : SECURITY_ATTRIBUTES;
702 hPipe : THandle;
703const
704 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
705 SECURITY_WORLD_RID = $00000000;
706begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100707 sd := nil;
Roger Meier3bef8c22012-10-06 06:58:00 +0000708 everyone_sid := nil;
Jens Geyerb64a7742013-01-23 20:58:47 +0100709 try
710 // Windows - set security to allow non-elevated apps
711 // to access pipes created by elevated apps.
712 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
713 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000714
Jens Geyerb64a7742013-01-23 20:58:47 +0100715 ZeroMemory( @ea, SizeOf(ea));
716 ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
717 ea.grfAccessMode := SET_ACCESS;
718 ea.grfInheritance := NO_INHERITANCE;
719 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
720 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
721 ea.Trustee.ptstrName := PChar(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000722
Jens Geyerb64a7742013-01-23 20:58:47 +0100723 acl := nil;
724 SetEntriesInAcl( 1, @ea, nil, acl);
Roger Meier3bef8c22012-10-06 06:58:00 +0000725
Jens Geyerb64a7742013-01-23 20:58:47 +0100726 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
727 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
728 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
Roger Meier3bef8c22012-10-06 06:58:00 +0000729
Jens Geyerb64a7742013-01-23 20:58:47 +0100730 sa.nLength := SizeOf(sa);
731 sa.lpSecurityDescriptor := sd;
732 sa.bInheritHandle := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000733
Jens Geyerb64a7742013-01-23 20:58:47 +0100734 // Create an instance of the named pipe
735 hPipe := Windows.CreateNamedPipe( PChar( FPipeName), // pipe name
736 PIPE_ACCESS_DUPLEX, // read/write access
737 PIPE_TYPE_MESSAGE or // message type pipe
738 PIPE_READMODE_MESSAGE, // message-read mode
739 FMaxConns, // max. instances
740 FBufSize, // output buffer size
741 FBufSize, // input buffer size
Jens Geyer0b20cc82013-03-07 20:47:01 +0100742 FTimeout, // time-out, see MSDN
Jens Geyerb64a7742013-01-23 20:58:47 +0100743 @sa); // default security attribute
Roger Meier3bef8c22012-10-06 06:58:00 +0000744
Jens Geyerb64a7742013-01-23 20:58:47 +0100745 FHandle := hPipe;
746 if( FHandle = INVALID_HANDLE_VALUE)
747 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
748 'CreateNamedPipe() failed ' + IntToStr(GetLastError));
749
750 finally
751 if sd <> nil then LocalFree( Cardinal( sd));
752 if acl <> nil then LocalFree( Cardinal( acl));
753 if everyone_sid <> nil then FreeSid(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000754 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000755end;
756
757
Roger Meier3bef8c22012-10-06 06:58:00 +0000758
759end.
760
761
762