blob: 66db240a011bf6acb3b099b2f05cc384eb26a028 [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;
194
195 FHandle : THandle;
196
197 protected
198 function AcceptImpl: ITransport; override;
Jens Geyerb64a7742013-01-23 20:58:47 +0100199 procedure CreateNamedPipe;
Roger Meier79655fb2012-10-20 20:59:41 +0000200
201 // INamedServerPipe
202 function Handle : THandle;
203
204 public
205 constructor Create( aPipename : string; aBufsize : Cardinal = 4096;
206 aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES);
207
208 procedure Close; override;
209 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000210
211
212implementation
213
214
Roger Meier79655fb2012-10-20 20:59:41 +0000215procedure ClosePipeHandle( var hPipe : THandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000216begin
Roger Meier79655fb2012-10-20 20:59:41 +0000217 if hPipe <> INVALID_HANDLE_VALUE
218 then try
219 CloseHandle( hPipe);
220 finally
221 hPipe := INVALID_HANDLE_VALUE;
222 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000223end;
224
225
Roger Meier79655fb2012-10-20 20:59:41 +0000226function DuplicatePipeHandle( const hSource : THandle) : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000227begin
Roger Meier79655fb2012-10-20 20:59:41 +0000228 if not DuplicateHandle( GetCurrentProcess, hSource,
229 GetCurrentProcess, @result,
230 0, FALSE, DUPLICATE_SAME_ACCESS)
231 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
232 'DuplicateHandle: '+SysErrorMessage(GetLastError));
Roger Meier3bef8c22012-10-06 06:58:00 +0000233end;
234
235
Roger Meier79655fb2012-10-20 20:59:41 +0000236
237{ TPipeStreamBaseImpl }
238
239
240constructor TPipeStreamBaseImpl.Create( const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT);
241begin
242 inherited Create;
243 FPipe := INVALID_HANDLE_VALUE;
244 FTimeout := aTimeOut;
245end;
246
247
248destructor TPipeStreamBaseImpl.Destroy;
Roger Meier3bef8c22012-10-06 06:58:00 +0000249begin
250 try
251 Close;
252 finally
253 inherited Destroy;
254 end;
255end;
256
257
Roger Meier79655fb2012-10-20 20:59:41 +0000258procedure TPipeStreamBaseImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000259begin
Roger Meier79655fb2012-10-20 20:59:41 +0000260 ClosePipeHandle( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000261end;
262
263
Roger Meier79655fb2012-10-20 20:59:41 +0000264procedure TPipeStreamBaseImpl.Flush;
Roger Meier3bef8c22012-10-06 06:58:00 +0000265begin
266 // nothing to do
267end;
268
269
Roger Meier79655fb2012-10-20 20:59:41 +0000270function TPipeStreamBaseImpl.IsOpen: Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000271begin
272 result := (FPipe <> INVALID_HANDLE_VALUE);
273end;
274
275
Roger Meier79655fb2012-10-20 20:59:41 +0000276procedure TPipeStreamBaseImpl.Write(const buffer: TBytes; offset, count: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000277var cbWritten : DWORD;
278begin
279 if not IsOpen
280 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
281 'Called write on non-open pipe');
282
283 if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil)
284 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
285 'Write to pipe failed');
286end;
287
288
Roger Meier79655fb2012-10-20 20:59:41 +0000289function TPipeStreamBaseImpl.Read( var buffer: TBytes; offset, count: Integer): Integer;
290var cbRead, dwErr : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000291 bytes, retries : LongInt;
292 bOk : Boolean;
293const INTERVAL = 10; // ms
294begin
295 if not IsOpen
296 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
297 'Called read on non-open pipe');
298
299 // MSDN: Handle can be a handle to a named pipe instance,
300 // or it can be a handle to the read end of an anonymous pipe,
301 // The handle must have GENERIC_READ access to the pipe.
302 if FTimeOut <> INFINITE then begin
303 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
304 while TRUE do begin
305 if IsOpen
306 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
307 and (bytes > 0)
308 then Break; // there are data
309
Roger Meier79655fb2012-10-20 20:59:41 +0000310 dwErr := GetLastError;
311 if (dwErr = ERROR_BROKEN_PIPE)
312 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
313 then begin
314 result := 0; // other side closed the pipe
315 Exit;
316 end;
317
Roger Meier3bef8c22012-10-06 06:58:00 +0000318 Dec( retries);
319 if retries > 0
320 then Sleep( INTERVAL)
321 else raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
322 'Pipe read timed out');
323 end;
324 end;
325
326 // read the data (or block INFINITE-ly)
327 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, nil);
328 if (not bOk) and (GetLastError() <> ERROR_MORE_DATA)
329 then result := 0 // No more data, possibly because client disconnected.
330 else result := cbRead;
331end;
332
333
Roger Meier79655fb2012-10-20 20:59:41 +0000334function TPipeStreamBaseImpl.ToArray: TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000335var bytes : LongInt;
336begin
337 SetLength( result, 0);
338 bytes := 0;
339
340 if IsOpen
341 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
342 and (bytes > 0)
343 then begin
344 SetLength( result, bytes);
345 Read( result, 0, bytes);
346 end;
347end;
348
349
Roger Meier79655fb2012-10-20 20:59:41 +0000350{ TNamedPipeStreamImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000351
352
Roger Meier79655fb2012-10-20 20:59:41 +0000353constructor TNamedPipeStreamImpl.Create( const aPipeName : string; const aShareMode: DWORD;
354 const aSecurityAttributes: PSecurityAttributes;
355 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000356begin
Roger Meier79655fb2012-10-20 20:59:41 +0000357 inherited Create( aTimeout);
358
359 FPipeName := aPipeName;
360 FShareMode := aShareMode;
361 FSecurityAttribs := aSecurityAttributes;
362
363 if Copy(FPipeName,1,2) <> '\\'
364 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
Roger Meier3bef8c22012-10-06 06:58:00 +0000365end;
366
367
Roger Meier79655fb2012-10-20 20:59:41 +0000368procedure TNamedPipeStreamImpl.Open;
369var hPipe : THandle;
370 dwMode : DWORD;
371begin
372 if IsOpen then Exit;
373
374 // open that thingy
375
376 if not WaitNamedPipe( PChar(FPipeName), FTimeout)
377 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
378 'Unable to open pipe, '+SysErrorMessage(GetLastError));
379
380 hPipe := CreateFile( PChar( FPipeName),
381 GENERIC_READ or GENERIC_WRITE,
382 FShareMode, // sharing
383 FSecurityAttribs, // security attributes
384 OPEN_EXISTING, // opens existing pipe
385 0, // default attributes
386 0); // no template file
387
388 if hPipe = INVALID_HANDLE_VALUE
389 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
390 'Unable to open pipe, '+SysErrorMessage(GetLastError));
391
392 // pipe connected; change to message-read mode.
393 dwMode := PIPE_READMODE_MESSAGE;
394 if not SetNamedPipeHandleState( hPipe, dwMode, nil, nil) then begin
395 Close;
396 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
397 'SetNamedPipeHandleState failed');
398 end;
399
400 // everything fine
401 FPipe := hPipe;
402end;
403
404
405{ THandlePipeStreamImpl }
406
407
408constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle; aOwnsHandle : Boolean);
409begin
410 inherited Create( DEFAULT_THRIFT_PIPE_TIMEOUT);
411
412 if aOwnsHandle
413 then FSrcHandle := aPipeHandle
414 else FSrcHandle := DuplicatePipeHandle( aPipeHandle);
415
416 Open;
417end;
418
419
420destructor THandlePipeStreamImpl.Destroy;
421begin
422 try
423 ClosePipeHandle( FSrcHandle);
424 finally
425 inherited Destroy;
426 end;
427end;
428
429
430procedure THandlePipeStreamImpl.Open;
431begin
432 if not IsOpen
433 then FPipe := DuplicatePipeHandle( FSrcHandle);
434end;
435
436
437{ TPipeTransportBaseImpl }
438
439
440function TPipeTransportBaseImpl.GetIsOpen: Boolean;
441begin
442 result := (FInputStream <> nil);
443end;
444
445
446procedure TPipeTransportBaseImpl.Open;
447begin
448 FInputStream.Open;
449 FOutputStream.Open;
450end;
451
452
453procedure TPipeTransportBaseImpl.Close;
454begin
455 FInputStream.Close;
456 FOutputStream.Close;
457end;
458
459
460{ TNamedPipeImpl }
461
462
Roger Meier3bef8c22012-10-06 06:58:00 +0000463constructor TNamedPipeImpl.Create( const aPipeName : string; const aShareMode: DWORD;
464 const aSecurityAttributes: PSecurityAttributes;
465 const aTimeOut : DWORD);
466// Named pipe constructor
467begin
Roger Meier79655fb2012-10-20 20:59:41 +0000468 inherited Create( nil, nil);
469 FInputStream := TNamedPipeStreamImpl.Create( aPipeName, aShareMode, aSecurityAttributes, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000470 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000471end;
472
473
474constructor TNamedPipeImpl.Create( aPipe : THandle; aOwnsHandle : Boolean);
475// Named pipe constructor
476begin
Roger Meier79655fb2012-10-20 20:59:41 +0000477 inherited Create( nil, nil);
478 FInputStream := THandlePipeStreamImpl.Create( aPipe, aOwnsHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000479 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000480end;
481
482
Roger Meier79655fb2012-10-20 20:59:41 +0000483{ TNamedPipeServerImpl }
484
485
486constructor TNamedPipeServerImpl.Create( aPipe : THandle; aOwnsHandle : Boolean);
487// Named pipe constructor
Roger Meier3bef8c22012-10-06 06:58:00 +0000488begin
Roger Meier79655fb2012-10-20 20:59:41 +0000489 FHandle := DuplicatePipeHandle( aPipe);
490 inherited Create( aPipe, aOwnsHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000491end;
492
493
Roger Meier79655fb2012-10-20 20:59:41 +0000494procedure TNamedPipeServerImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000495begin
Roger Meier79655fb2012-10-20 20:59:41 +0000496 FlushFileBuffers( FHandle);
497 DisconnectNamedPipe( FHandle); // force client off the pipe
498 ClosePipeHandle( FHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000499
Roger Meier79655fb2012-10-20 20:59:41 +0000500 inherited Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000501end;
502
503
504{ TAnonymousPipeImpl }
505
506
Roger Meier3bef8c22012-10-06 06:58:00 +0000507constructor TAnonymousPipeImpl.Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean);
508// Anonymous pipe constructor
509begin
Roger Meier79655fb2012-10-20 20:59:41 +0000510 inherited Create( nil, nil);
511 FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles);
512 FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles);
Roger Meier3bef8c22012-10-06 06:58:00 +0000513end;
514
515
Roger Meier79655fb2012-10-20 20:59:41 +0000516{ TServerPipeBaseImpl }
517
518
519procedure TServerPipeBaseImpl.Listen;
Roger Meier3bef8c22012-10-06 06:58:00 +0000520begin
Roger Meier79655fb2012-10-20 20:59:41 +0000521 // not much to do here
Roger Meier3bef8c22012-10-06 06:58:00 +0000522end;
523
524
Roger Meier79655fb2012-10-20 20:59:41 +0000525{ TAnonymousServerPipeImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000526
527
Roger Meier79655fb2012-10-20 20:59:41 +0000528constructor TAnonymousServerPipeImpl.Create( aBufsize : Cardinal);
Roger Meier3bef8c22012-10-06 06:58:00 +0000529// Anonymous pipe CTOR
530begin
531 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000532 FBufsize := aBufSize;
Roger Meier79655fb2012-10-20 20:59:41 +0000533 FReadHandle := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000534 FWriteHandle := INVALID_HANDLE_VALUE;
535 FClientAnonRead := INVALID_HANDLE_VALUE;
536 FClientAnonWrite := INVALID_HANDLE_VALUE;
537
538 // The anonymous pipe needs to be created first so that the server can
539 // pass the handles on to the client before the serve (acceptImpl)
540 // blocking call.
541 if not CreateAnonPipe
542 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
543 ClassName+'.Create() failed');
544end;
545
546
Roger Meier79655fb2012-10-20 20:59:41 +0000547function TAnonymousServerPipeImpl.AcceptImpl: ITransport;
Roger Meier3bef8c22012-10-06 06:58:00 +0000548var buf : Byte;
549 br : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000550begin
Roger Meier79655fb2012-10-20 20:59:41 +0000551 // This 0-byte read serves merely as a blocking call.
552 if not ReadFile( FReadHandle, buf, 0, br, nil)
553 and (GetLastError() <> ERROR_MORE_DATA)
554 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
555 'TServerPipe unable to initiate pipe communication');
556 result := TAnonymousPipeImpl.Create( FReadHandle, FWriteHandle, FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +0000557end;
558
559
Roger Meier79655fb2012-10-20 20:59:41 +0000560procedure TAnonymousServerPipeImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000561begin
Roger Meier79655fb2012-10-20 20:59:41 +0000562 ClosePipeHandle( FReadHandle);
563 ClosePipeHandle( FWriteHandle);
564 ClosePipeHandle( FClientAnonRead);
565 ClosePipeHandle( FClientAnonWrite);
Roger Meier3bef8c22012-10-06 06:58:00 +0000566end;
567
568
Roger Meier79655fb2012-10-20 20:59:41 +0000569function TAnonymousServerPipeImpl.ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000570begin
Roger Meier79655fb2012-10-20 20:59:41 +0000571 result := FReadHandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000572end;
573
574
Roger Meier79655fb2012-10-20 20:59:41 +0000575function TAnonymousServerPipeImpl.WriteHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000576begin
577 result := FWriteHandle;
578end;
579
580
Roger Meier79655fb2012-10-20 20:59:41 +0000581function TAnonymousServerPipeImpl.ClientAnonRead : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000582begin
583 result := FClientAnonRead;
584end;
585
586
Roger Meier79655fb2012-10-20 20:59:41 +0000587function TAnonymousServerPipeImpl.ClientAnonWrite : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000588begin
589 result := FClientAnonWrite;
590end;
591
592
Roger Meier79655fb2012-10-20 20:59:41 +0000593function TAnonymousServerPipeImpl.CreateAnonPipe : Boolean;
594var sd : PSECURITY_DESCRIPTOR;
595 sa : SECURITY_ATTRIBUTES; //TSecurityAttributes;
596 hCAR, hPipeW, hCAW, hPipe : THandle;
597begin
598 result := FALSE;
599
600 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
Jens Geyerb64a7742013-01-23 20:58:47 +0100601 try
602 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
603 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
Roger Meier79655fb2012-10-20 20:59:41 +0000604
Jens Geyerb64a7742013-01-23 20:58:47 +0100605 sa.nLength := sizeof( sa);
606 sa.lpSecurityDescriptor := sd;
607 sa.bInheritHandle := TRUE; //allow passing handle to child
Roger Meier79655fb2012-10-20 20:59:41 +0000608
Jens Geyerb64a7742013-01-23 20:58:47 +0100609 if not CreatePipe( hCAR, hPipeW, @sa, FBufSize) then begin //create stdin pipe
610 Console.WriteLine( 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
611 Exit;
612 end;
613
614 if not CreatePipe( hPipe, hCAW, @sa, FBufSize) then begin //create stdout pipe
615 Console.WriteLine( 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
616 CloseHandle( hCAR);
617 CloseHandle( hPipeW);
618 Exit;
619 end;
620
621 FClientAnonRead := hCAR;
622 FClientAnonWrite := hCAW;
623 FReadHandle := hPipe;
624 FWriteHandle := hPipeW;
625
626 result := TRUE;
627
628 finally
629 if sd <> nil then LocalFree( Cardinal(sd));
Roger Meier79655fb2012-10-20 20:59:41 +0000630 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000631end;
632
633
634{ TNamedServerPipeImpl }
635
636
637constructor TNamedServerPipeImpl.Create( aPipename : string; aBufsize, aMaxConns : Cardinal);
638// Named Pipe CTOR
639begin
640 inherited Create;
641 FPipeName := aPipename;
642 FBufsize := aBufSize;
643 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
644 FHandle := INVALID_HANDLE_VALUE;
645
646 if Copy(FPipeName,1,2) <> '\\'
647 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
648end;
649
650
651function TNamedServerPipeImpl.AcceptImpl: ITransport;
652var connectRet : Boolean;
653begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100654 CreateNamedPipe;
Roger Meier79655fb2012-10-20 20:59:41 +0000655
656 // Wait for the client to connect; if it succeeds, the
657 // function returns a nonzero value. If the function returns
658 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
659 if ConnectNamedPipe( FHandle,nil)
660 then connectRet := TRUE
661 else connectRet := (GetLastError() = ERROR_PIPE_CONNECTED);
662
663 if not connectRet then begin
664 Close;
665 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
666 'TServerPipe: client connection failed');
667 end;
668
669 result := TNamedPipeServerImpl.Create( FHandle, TRUE);
670end;
671
672
673procedure TNamedServerPipeImpl.Close;
674begin
675 if FHandle <> INVALID_HANDLE_VALUE
676 then try
677 FlushFileBuffers( FHandle);
678 DisconnectNamedPipe( FHandle);
679 finally
680 ClosePipeHandle( FHandle);
681 end;
682end;
683
684
685function TNamedServerPipeImpl.Handle : THandle;
686begin
687 result := FHandle;
688end;
689
690
Jens Geyerb64a7742013-01-23 20:58:47 +0100691procedure TNamedServerPipeImpl.CreateNamedPipe;
Roger Meier3bef8c22012-10-06 06:58:00 +0000692var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
693 everyone_sid : PSID;
694 ea : EXPLICIT_ACCESS;
695 acl : PACL;
696 sd : PSECURITY_DESCRIPTOR;
697 sa : SECURITY_ATTRIBUTES;
698 hPipe : THandle;
699const
700 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
701 SECURITY_WORLD_RID = $00000000;
702begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100703 sd := nil;
Roger Meier3bef8c22012-10-06 06:58:00 +0000704 everyone_sid := nil;
Jens Geyerb64a7742013-01-23 20:58:47 +0100705 try
706 // Windows - set security to allow non-elevated apps
707 // to access pipes created by elevated apps.
708 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
709 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000710
Jens Geyerb64a7742013-01-23 20:58:47 +0100711 ZeroMemory( @ea, SizeOf(ea));
712 ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
713 ea.grfAccessMode := SET_ACCESS;
714 ea.grfInheritance := NO_INHERITANCE;
715 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
716 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
717 ea.Trustee.ptstrName := PChar(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000718
Jens Geyerb64a7742013-01-23 20:58:47 +0100719 acl := nil;
720 SetEntriesInAcl( 1, @ea, nil, acl);
Roger Meier3bef8c22012-10-06 06:58:00 +0000721
Jens Geyerb64a7742013-01-23 20:58:47 +0100722 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
723 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
724 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
Roger Meier3bef8c22012-10-06 06:58:00 +0000725
Jens Geyerb64a7742013-01-23 20:58:47 +0100726 sa.nLength := SizeOf(sa);
727 sa.lpSecurityDescriptor := sd;
728 sa.bInheritHandle := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000729
Jens Geyerb64a7742013-01-23 20:58:47 +0100730 // Create an instance of the named pipe
731 hPipe := Windows.CreateNamedPipe( PChar( FPipeName), // pipe name
732 PIPE_ACCESS_DUPLEX, // read/write access
733 PIPE_TYPE_MESSAGE or // message type pipe
734 PIPE_READMODE_MESSAGE, // message-read mode
735 FMaxConns, // max. instances
736 FBufSize, // output buffer size
737 FBufSize, // input buffer size
738 0, // client time-out
739 @sa); // default security attribute
Roger Meier3bef8c22012-10-06 06:58:00 +0000740
Jens Geyerb64a7742013-01-23 20:58:47 +0100741 FHandle := hPipe;
742 if( FHandle = INVALID_HANDLE_VALUE)
743 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
744 'CreateNamedPipe() failed ' + IntToStr(GetLastError));
745
746 finally
747 if sd <> nil then LocalFree( Cardinal( sd));
748 if acl <> nil then LocalFree( Cardinal( acl));
749 if everyone_sid <> nil then FreeSid(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000750 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000751end;
752
753
Roger Meier3bef8c22012-10-06 06:58:00 +0000754
755end.
756
757
758