blob: c2696f416ab74e72e0b9ded0c629835e9cda38bd [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
Jens Geyer06045cf2013-03-27 20:26:25 +020026 Windows, SysUtils, Math, AccCtrl, AclAPI, SyncObjs,
Roger Meier3bef8c22012-10-06 06:58:00 +000027 Thrift.Transport,
Roger Meier3bef8c22012-10-06 06:58:00 +000028 Thrift.Stream;
29
30const
31 DEFAULT_THRIFT_PIPE_TIMEOUT = 5 * 1000; // ms
32
33
34type
Roger Meier79655fb2012-10-20 20:59:41 +000035 //--- Pipe Streams ---
Roger Meier3bef8c22012-10-06 06:58:00 +000036
37
Jens Geyer06045cf2013-03-27 20:26:25 +020038 TPipeStreamBase = class( TThriftStreamImpl)
Roger Meier79655fb2012-10-20 20:59:41 +000039 strict protected
40 FPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +000041 FTimeout : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +000042
Roger Meier3bef8c22012-10-06 06:58:00 +000043 procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override;
44 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override;
Roger Meier79655fb2012-10-20 20:59:41 +000045 //procedure Open; override; - see derived classes
Roger Meier3bef8c22012-10-06 06:58:00 +000046 procedure Close; override;
47 procedure Flush; override;
48
49 function IsOpen: Boolean; override;
50 function ToArray: TBytes; override;
51 public
Roger Meier79655fb2012-10-20 20:59:41 +000052 constructor Create( const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +000053 destructor Destroy; override;
54 end;
55
56
Jens Geyer06045cf2013-03-27 20:26:25 +020057 TNamedPipeStreamImpl = class sealed( TPipeStreamBase)
Roger Meier79655fb2012-10-20 20:59:41 +000058 private
59 FPipeName : string;
60 FShareMode : DWORD;
61 FSecurityAttribs : PSecurityAttributes;
Roger Meier3bef8c22012-10-06 06:58:00 +000062
Roger Meier79655fb2012-10-20 20:59:41 +000063 protected
64 procedure Open; override;
65
66 public
67 constructor Create( const aPipeName : string;
68 const aShareMode: DWORD = 0;
69 const aSecurityAttributes: PSecurityAttributes = nil;
70 const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); overload;
71 end;
72
73
Jens Geyer06045cf2013-03-27 20:26:25 +020074 THandlePipeStreamImpl = class sealed( TPipeStreamBase)
Roger Meier79655fb2012-10-20 20:59:41 +000075 private
76 FSrcHandle : THandle;
77
78 protected
79 procedure Open; override;
80
81 public
82 constructor Create( const aPipeHandle : THandle; aOwnsHandle : Boolean); overload;
83 destructor Destroy; override;
84 end;
85
86
87 //--- Pipe Transports ---
88
89
Jens Geyer06045cf2013-03-27 20:26:25 +020090 IPipeTransport = interface( IStreamTransport)
Roger Meier79655fb2012-10-20 20:59:41 +000091 ['{5E05CC85-434F-428F-BFB2-856A168B5558}']
92 end;
93
94
Jens Geyer06045cf2013-03-27 20:26:25 +020095 TPipeTransportBase = class( TStreamTransportImpl, IPipeTransport)
Roger Meier79655fb2012-10-20 20:59:41 +000096 public
97 // ITransport
98 function GetIsOpen: Boolean; override;
99 procedure Open; override;
100 procedure Close; override;
101 end;
102
103
Jens Geyer06045cf2013-03-27 20:26:25 +0200104 TNamedPipeTransportClientEndImpl = class( TPipeTransportBase)
Roger Meier79655fb2012-10-20 20:59:41 +0000105 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000106 // Named pipe constructors
107 constructor Create( aPipe : THandle; aOwnsHandle : Boolean); overload;
108 constructor Create( const aPipeName : string;
109 const aShareMode: DWORD = 0;
110 const aSecurityAttributes: PSecurityAttributes = nil;
111 const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000112 end;
113
114
Jens Geyer06045cf2013-03-27 20:26:25 +0200115 TNamedPipeTransportServerEndImpl = class( TNamedPipeTransportClientEndImpl)
Roger Meier79655fb2012-10-20 20:59:41 +0000116 strict private
117 FHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000118 public
Roger Meier79655fb2012-10-20 20:59:41 +0000119 // ITransport
120 procedure Close; override;
121 constructor Create( aPipe : THandle; aOwnsHandle : Boolean); reintroduce;
122 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000123
Roger Meier79655fb2012-10-20 20:59:41 +0000124
Jens Geyer06045cf2013-03-27 20:26:25 +0200125 TAnonymousPipeTransportImpl = class( TPipeTransportBase)
Roger Meier79655fb2012-10-20 20:59:41 +0000126 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000127 // Anonymous pipe constructor
128 constructor Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000129 end;
130
131
Roger Meier79655fb2012-10-20 20:59:41 +0000132 //--- Server Transports ---
133
134
Jens Geyer06045cf2013-03-27 20:26:25 +0200135 IAnonymousPipeServerTransport = interface( IServerTransport)
Roger Meier3bef8c22012-10-06 06:58:00 +0000136 ['{7AEE6793-47B9-4E49-981A-C39E9108E9AD}']
137 // Server side anonymous pipe ends
Roger Meier79655fb2012-10-20 20:59:41 +0000138 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000139 function WriteHandle : THandle;
140 // Client side anonymous pipe ends
141 function ClientAnonRead : THandle;
142 function ClientAnonWrite : THandle;
143 end;
144
145
Jens Geyer06045cf2013-03-27 20:26:25 +0200146 INamedPipeServerTransport = interface( IServerTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000147 ['{9DF9EE48-D065-40AF-8F67-D33037D3D960}']
148 function Handle : THandle;
149 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000150
Roger Meier79655fb2012-10-20 20:59:41 +0000151
Jens Geyer06045cf2013-03-27 20:26:25 +0200152 TPipeServerTransportBase = class( TServerTransportImpl)
153 protected
154 FStopServer : Boolean;
155 procedure InternalClose; virtual; abstract;
Roger Meier79655fb2012-10-20 20:59:41 +0000156 public
157 procedure Listen; override;
Jens Geyer06045cf2013-03-27 20:26:25 +0200158 procedure Close; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000159 end;
160
161
Jens Geyer06045cf2013-03-27 20:26:25 +0200162 TAnonymousPipeServerTransportImpl = class( TPipeServerTransportBase, IAnonymousPipeServerTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000163 private
164 FBufSize : DWORD;
165
166 // Server side anonymous pipe handles
167 FReadHandle,
Roger Meier3bef8c22012-10-06 06:58:00 +0000168 FWriteHandle : THandle;
169
170 //Client side anonymous pipe handles
171 FClientAnonRead,
172 FClientAnonWrite : THandle;
173
174 protected
Jens Geyer01640402013-09-25 21:12:21 +0200175 function Accept(const fnAccepting: TProc): ITransport; override;
Roger Meier3bef8c22012-10-06 06:58:00 +0000176
Roger Meier3bef8c22012-10-06 06:58:00 +0000177 function CreateAnonPipe : Boolean;
178
Jens Geyer06045cf2013-03-27 20:26:25 +0200179 // IAnonymousPipeServerTransport
Roger Meier79655fb2012-10-20 20:59:41 +0000180 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000181 function WriteHandle : THandle;
182 function ClientAnonRead : THandle;
183 function ClientAnonWrite : THandle;
184
Jens Geyer06045cf2013-03-27 20:26:25 +0200185 procedure InternalClose; override;
186
Roger Meier3bef8c22012-10-06 06:58:00 +0000187 public
Roger Meier79655fb2012-10-20 20:59:41 +0000188 constructor Create( aBufsize : Cardinal = 4096);
Roger Meier3bef8c22012-10-06 06:58:00 +0000189 end;
190
191
Jens Geyer06045cf2013-03-27 20:26:25 +0200192 TNamedPipeServerTransportImpl = class( TPipeServerTransportBase, INamedPipeServerTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000193 private
194 FPipeName : string;
195 FMaxConns : DWORD;
196 FBufSize : DWORD;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100197 FTimeout : DWORD;
Jens Geyer06045cf2013-03-27 20:26:25 +0200198 FHandle : THandle;
199 FConnected : Boolean;
Jens Geyer01640402013-09-25 21:12:21 +0200200
201
202 protected
203 function Accept(const fnAccepting: TProc): ITransport; override;
Jens Geyer06045cf2013-03-27 20:26:25 +0200204 function CreateNamedPipe : THandle;
205 function CreateTransportInstance : ITransport;
Roger Meier79655fb2012-10-20 20:59:41 +0000206
Jens Geyer06045cf2013-03-27 20:26:25 +0200207 // INamedPipeServerTransport
Roger Meier79655fb2012-10-20 20:59:41 +0000208 function Handle : THandle;
Jens Geyer06045cf2013-03-27 20:26:25 +0200209 procedure InternalClose; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000210
211 public
212 constructor Create( aPipename : string; aBufsize : Cardinal = 4096;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100213 aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES;
214 aTimeOut : Cardinal = 0);
Roger Meier79655fb2012-10-20 20:59:41 +0000215 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000216
217
218implementation
219
220
Roger Meier79655fb2012-10-20 20:59:41 +0000221procedure ClosePipeHandle( var hPipe : THandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000222begin
Roger Meier79655fb2012-10-20 20:59:41 +0000223 if hPipe <> INVALID_HANDLE_VALUE
224 then try
225 CloseHandle( hPipe);
226 finally
227 hPipe := INVALID_HANDLE_VALUE;
228 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000229end;
230
231
Roger Meier79655fb2012-10-20 20:59:41 +0000232function DuplicatePipeHandle( const hSource : THandle) : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000233begin
Roger Meier79655fb2012-10-20 20:59:41 +0000234 if not DuplicateHandle( GetCurrentProcess, hSource,
235 GetCurrentProcess, @result,
236 0, FALSE, DUPLICATE_SAME_ACCESS)
237 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
238 'DuplicateHandle: '+SysErrorMessage(GetLastError));
Roger Meier3bef8c22012-10-06 06:58:00 +0000239end;
240
241
Roger Meier79655fb2012-10-20 20:59:41 +0000242
Jens Geyer06045cf2013-03-27 20:26:25 +0200243{ TPipeStreamBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000244
245
Jens Geyer06045cf2013-03-27 20:26:25 +0200246constructor TPipeStreamBase.Create( const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT);
Roger Meier79655fb2012-10-20 20:59:41 +0000247begin
248 inherited Create;
249 FPipe := INVALID_HANDLE_VALUE;
250 FTimeout := aTimeOut;
251end;
252
253
Jens Geyer06045cf2013-03-27 20:26:25 +0200254destructor TPipeStreamBase.Destroy;
Roger Meier3bef8c22012-10-06 06:58:00 +0000255begin
256 try
257 Close;
258 finally
259 inherited Destroy;
260 end;
261end;
262
263
Jens Geyer06045cf2013-03-27 20:26:25 +0200264procedure TPipeStreamBase.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000265begin
Roger Meier79655fb2012-10-20 20:59:41 +0000266 ClosePipeHandle( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000267end;
268
269
Jens Geyer06045cf2013-03-27 20:26:25 +0200270procedure TPipeStreamBase.Flush;
Roger Meier3bef8c22012-10-06 06:58:00 +0000271begin
272 // nothing to do
273end;
274
275
Jens Geyer06045cf2013-03-27 20:26:25 +0200276function TPipeStreamBase.IsOpen: Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000277begin
278 result := (FPipe <> INVALID_HANDLE_VALUE);
279end;
280
281
Jens Geyer06045cf2013-03-27 20:26:25 +0200282procedure TPipeStreamBase.Write(const buffer: TBytes; offset, count: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000283var cbWritten : DWORD;
284begin
285 if not IsOpen
286 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
287 'Called write on non-open pipe');
288
289 if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil)
290 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
291 'Write to pipe failed');
292end;
293
294
Jens Geyer06045cf2013-03-27 20:26:25 +0200295function TPipeStreamBase.Read( var buffer: TBytes; offset, count: Integer): Integer;
Roger Meier79655fb2012-10-20 20:59:41 +0000296var cbRead, dwErr : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000297 bytes, retries : LongInt;
298 bOk : Boolean;
299const INTERVAL = 10; // ms
300begin
301 if not IsOpen
302 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
303 'Called read on non-open pipe');
304
305 // MSDN: Handle can be a handle to a named pipe instance,
306 // or it can be a handle to the read end of an anonymous pipe,
307 // The handle must have GENERIC_READ access to the pipe.
308 if FTimeOut <> INFINITE then begin
309 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
310 while TRUE do begin
311 if IsOpen
312 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
313 and (bytes > 0)
314 then Break; // there are data
315
Roger Meier79655fb2012-10-20 20:59:41 +0000316 dwErr := GetLastError;
Jens Geyer06045cf2013-03-27 20:26:25 +0200317 if (dwErr = ERROR_INVALID_HANDLE)
318 or (dwErr = ERROR_BROKEN_PIPE)
Roger Meier79655fb2012-10-20 20:59:41 +0000319 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
320 then begin
321 result := 0; // other side closed the pipe
322 Exit;
323 end;
324
Roger Meier3bef8c22012-10-06 06:58:00 +0000325 Dec( retries);
326 if retries > 0
327 then Sleep( INTERVAL)
328 else raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
329 'Pipe read timed out');
330 end;
331 end;
332
333 // read the data (or block INFINITE-ly)
334 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, nil);
335 if (not bOk) and (GetLastError() <> ERROR_MORE_DATA)
336 then result := 0 // No more data, possibly because client disconnected.
337 else result := cbRead;
338end;
339
340
Jens Geyer06045cf2013-03-27 20:26:25 +0200341function TPipeStreamBase.ToArray: TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000342var bytes : LongInt;
343begin
344 SetLength( result, 0);
345 bytes := 0;
346
347 if IsOpen
348 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
349 and (bytes > 0)
350 then begin
351 SetLength( result, bytes);
352 Read( result, 0, bytes);
353 end;
354end;
355
356
Roger Meier79655fb2012-10-20 20:59:41 +0000357{ TNamedPipeStreamImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000358
359
Roger Meier79655fb2012-10-20 20:59:41 +0000360constructor TNamedPipeStreamImpl.Create( const aPipeName : string; const aShareMode: DWORD;
361 const aSecurityAttributes: PSecurityAttributes;
362 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000363begin
Roger Meier79655fb2012-10-20 20:59:41 +0000364 inherited Create( aTimeout);
365
366 FPipeName := aPipeName;
367 FShareMode := aShareMode;
368 FSecurityAttribs := aSecurityAttributes;
369
370 if Copy(FPipeName,1,2) <> '\\'
371 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
Roger Meier3bef8c22012-10-06 06:58:00 +0000372end;
373
374
Roger Meier79655fb2012-10-20 20:59:41 +0000375procedure TNamedPipeStreamImpl.Open;
376var hPipe : THandle;
377 dwMode : DWORD;
378begin
379 if IsOpen then Exit;
380
381 // open that thingy
382
383 if not WaitNamedPipe( PChar(FPipeName), FTimeout)
384 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
385 'Unable to open pipe, '+SysErrorMessage(GetLastError));
386
387 hPipe := CreateFile( PChar( FPipeName),
388 GENERIC_READ or GENERIC_WRITE,
389 FShareMode, // sharing
390 FSecurityAttribs, // security attributes
391 OPEN_EXISTING, // opens existing pipe
392 0, // default attributes
393 0); // no template file
394
395 if hPipe = INVALID_HANDLE_VALUE
396 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
397 'Unable to open pipe, '+SysErrorMessage(GetLastError));
398
399 // pipe connected; change to message-read mode.
400 dwMode := PIPE_READMODE_MESSAGE;
401 if not SetNamedPipeHandleState( hPipe, dwMode, nil, nil) then begin
402 Close;
403 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
404 'SetNamedPipeHandleState failed');
405 end;
406
407 // everything fine
408 FPipe := hPipe;
409end;
410
411
412{ THandlePipeStreamImpl }
413
414
415constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle; aOwnsHandle : Boolean);
416begin
417 inherited Create( DEFAULT_THRIFT_PIPE_TIMEOUT);
418
419 if aOwnsHandle
420 then FSrcHandle := aPipeHandle
421 else FSrcHandle := DuplicatePipeHandle( aPipeHandle);
422
423 Open;
424end;
425
426
427destructor THandlePipeStreamImpl.Destroy;
428begin
429 try
430 ClosePipeHandle( FSrcHandle);
431 finally
432 inherited Destroy;
433 end;
434end;
435
436
437procedure THandlePipeStreamImpl.Open;
438begin
439 if not IsOpen
440 then FPipe := DuplicatePipeHandle( FSrcHandle);
441end;
442
443
Jens Geyer06045cf2013-03-27 20:26:25 +0200444{ TPipeTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000445
446
Jens Geyer06045cf2013-03-27 20:26:25 +0200447function TPipeTransportBase.GetIsOpen: Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000448begin
Jens Geyer0b20cc82013-03-07 20:47:01 +0100449 result := (FInputStream <> nil) and (FInputStream.IsOpen)
450 and (FOutputStream <> nil) and (FOutputStream.IsOpen);
Roger Meier79655fb2012-10-20 20:59:41 +0000451end;
452
453
Jens Geyer06045cf2013-03-27 20:26:25 +0200454procedure TPipeTransportBase.Open;
Roger Meier79655fb2012-10-20 20:59:41 +0000455begin
456 FInputStream.Open;
457 FOutputStream.Open;
458end;
459
460
Jens Geyer06045cf2013-03-27 20:26:25 +0200461procedure TPipeTransportBase.Close;
Roger Meier79655fb2012-10-20 20:59:41 +0000462begin
463 FInputStream.Close;
464 FOutputStream.Close;
465end;
466
467
Jens Geyer06045cf2013-03-27 20:26:25 +0200468{ TNamedPipeTransportClientEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000469
470
Jens Geyer06045cf2013-03-27 20:26:25 +0200471constructor TNamedPipeTransportClientEndImpl.Create( const aPipeName : string; const aShareMode: DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000472 const aSecurityAttributes: PSecurityAttributes;
473 const aTimeOut : DWORD);
474// Named pipe constructor
475begin
Roger Meier79655fb2012-10-20 20:59:41 +0000476 inherited Create( nil, nil);
477 FInputStream := TNamedPipeStreamImpl.Create( aPipeName, aShareMode, aSecurityAttributes, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000478 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000479end;
480
481
Jens Geyer06045cf2013-03-27 20:26:25 +0200482constructor TNamedPipeTransportClientEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean);
Roger Meier3bef8c22012-10-06 06:58:00 +0000483// Named pipe constructor
484begin
Roger Meier79655fb2012-10-20 20:59:41 +0000485 inherited Create( nil, nil);
486 FInputStream := THandlePipeStreamImpl.Create( aPipe, aOwnsHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000487 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000488end;
489
490
Jens Geyer06045cf2013-03-27 20:26:25 +0200491{ TNamedPipeTransportServerEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000492
493
Jens Geyer06045cf2013-03-27 20:26:25 +0200494constructor TNamedPipeTransportServerEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean);
Roger Meier79655fb2012-10-20 20:59:41 +0000495// Named pipe constructor
Roger Meier3bef8c22012-10-06 06:58:00 +0000496begin
Roger Meier79655fb2012-10-20 20:59:41 +0000497 FHandle := DuplicatePipeHandle( aPipe);
498 inherited Create( aPipe, aOwnsHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000499end;
500
501
Jens Geyer06045cf2013-03-27 20:26:25 +0200502procedure TNamedPipeTransportServerEndImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000503begin
Roger Meier79655fb2012-10-20 20:59:41 +0000504 FlushFileBuffers( FHandle);
505 DisconnectNamedPipe( FHandle); // force client off the pipe
506 ClosePipeHandle( FHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000507
Roger Meier79655fb2012-10-20 20:59:41 +0000508 inherited Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000509end;
510
511
Jens Geyer06045cf2013-03-27 20:26:25 +0200512{ TAnonymousPipeTransportImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000513
514
Jens Geyer06045cf2013-03-27 20:26:25 +0200515constructor TAnonymousPipeTransportImpl.Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean);
Roger Meier3bef8c22012-10-06 06:58:00 +0000516// Anonymous pipe constructor
517begin
Roger Meier79655fb2012-10-20 20:59:41 +0000518 inherited Create( nil, nil);
519 FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles);
520 FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles);
Roger Meier3bef8c22012-10-06 06:58:00 +0000521end;
522
523
Jens Geyer06045cf2013-03-27 20:26:25 +0200524{ TPipeServerTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000525
526
Jens Geyer06045cf2013-03-27 20:26:25 +0200527procedure TPipeServerTransportBase.Listen;
Roger Meier3bef8c22012-10-06 06:58:00 +0000528begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200529 FStopServer := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000530end;
531
532
Jens Geyer06045cf2013-03-27 20:26:25 +0200533procedure TPipeServerTransportBase.Close;
534begin
535 FStopServer := TRUE;
536 InternalClose;
537end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000538
539
Jens Geyer06045cf2013-03-27 20:26:25 +0200540{ TAnonymousPipeServerTransportImpl }
541
542
543constructor TAnonymousPipeServerTransportImpl.Create( aBufsize : Cardinal);
Roger Meier3bef8c22012-10-06 06:58:00 +0000544// Anonymous pipe CTOR
545begin
546 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000547 FBufsize := aBufSize;
Roger Meier79655fb2012-10-20 20:59:41 +0000548 FReadHandle := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000549 FWriteHandle := INVALID_HANDLE_VALUE;
550 FClientAnonRead := INVALID_HANDLE_VALUE;
551 FClientAnonWrite := INVALID_HANDLE_VALUE;
552
553 // The anonymous pipe needs to be created first so that the server can
554 // pass the handles on to the client before the serve (acceptImpl)
555 // blocking call.
556 if not CreateAnonPipe
557 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
558 ClassName+'.Create() failed');
559end;
560
561
Jens Geyer01640402013-09-25 21:12:21 +0200562function TAnonymousPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Roger Meier3bef8c22012-10-06 06:58:00 +0000563var buf : Byte;
564 br : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000565begin
Jens Geyer01640402013-09-25 21:12:21 +0200566 if Assigned(fnAccepting)
567 then fnAccepting();
568
Roger Meier79655fb2012-10-20 20:59:41 +0000569 // This 0-byte read serves merely as a blocking call.
570 if not ReadFile( FReadHandle, buf, 0, br, nil)
571 and (GetLastError() <> ERROR_MORE_DATA)
572 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
573 'TServerPipe unable to initiate pipe communication');
Jens Geyer06045cf2013-03-27 20:26:25 +0200574
575 // create the transport impl
576 result := TAnonymousPipeTransportImpl.Create( FReadHandle, FWriteHandle, FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +0000577end;
578
579
Jens Geyer06045cf2013-03-27 20:26:25 +0200580procedure TAnonymousPipeServerTransportImpl.InternalClose;
Roger Meier3bef8c22012-10-06 06:58:00 +0000581begin
Roger Meier79655fb2012-10-20 20:59:41 +0000582 ClosePipeHandle( FReadHandle);
583 ClosePipeHandle( FWriteHandle);
584 ClosePipeHandle( FClientAnonRead);
585 ClosePipeHandle( FClientAnonWrite);
Roger Meier3bef8c22012-10-06 06:58:00 +0000586end;
587
588
Jens Geyer06045cf2013-03-27 20:26:25 +0200589function TAnonymousPipeServerTransportImpl.ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000590begin
Roger Meier79655fb2012-10-20 20:59:41 +0000591 result := FReadHandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000592end;
593
594
Jens Geyer06045cf2013-03-27 20:26:25 +0200595function TAnonymousPipeServerTransportImpl.WriteHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000596begin
597 result := FWriteHandle;
598end;
599
600
Jens Geyer06045cf2013-03-27 20:26:25 +0200601function TAnonymousPipeServerTransportImpl.ClientAnonRead : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000602begin
603 result := FClientAnonRead;
604end;
605
606
Jens Geyer06045cf2013-03-27 20:26:25 +0200607function TAnonymousPipeServerTransportImpl.ClientAnonWrite : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000608begin
609 result := FClientAnonWrite;
610end;
611
612
Jens Geyer06045cf2013-03-27 20:26:25 +0200613function TAnonymousPipeServerTransportImpl.CreateAnonPipe : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000614var sd : PSECURITY_DESCRIPTOR;
615 sa : SECURITY_ATTRIBUTES; //TSecurityAttributes;
616 hCAR, hPipeW, hCAW, hPipe : THandle;
617begin
618 result := FALSE;
619
620 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
Jens Geyerb64a7742013-01-23 20:58:47 +0100621 try
622 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
623 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
Roger Meier79655fb2012-10-20 20:59:41 +0000624
Jens Geyerb64a7742013-01-23 20:58:47 +0100625 sa.nLength := sizeof( sa);
626 sa.lpSecurityDescriptor := sd;
627 sa.bInheritHandle := TRUE; //allow passing handle to child
Roger Meier79655fb2012-10-20 20:59:41 +0000628
Jens Geyerb64a7742013-01-23 20:58:47 +0100629 if not CreatePipe( hCAR, hPipeW, @sa, FBufSize) then begin //create stdin pipe
Jens Geyer06045cf2013-03-27 20:26:25 +0200630 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
631 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100632 Exit;
633 end;
634
635 if not CreatePipe( hPipe, hCAW, @sa, FBufSize) then begin //create stdout pipe
Jens Geyerb64a7742013-01-23 20:58:47 +0100636 CloseHandle( hCAR);
637 CloseHandle( hPipeW);
Jens Geyer06045cf2013-03-27 20:26:25 +0200638 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
639 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100640 Exit;
641 end;
642
643 FClientAnonRead := hCAR;
644 FClientAnonWrite := hCAW;
645 FReadHandle := hPipe;
646 FWriteHandle := hPipeW;
647
648 result := TRUE;
649
650 finally
651 if sd <> nil then LocalFree( Cardinal(sd));
Roger Meier79655fb2012-10-20 20:59:41 +0000652 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000653end;
654
655
Jens Geyer06045cf2013-03-27 20:26:25 +0200656{ TNamedPipeServerTransportImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000657
658
Jens Geyer06045cf2013-03-27 20:26:25 +0200659constructor TNamedPipeServerTransportImpl.Create( aPipename : string; aBufsize, aMaxConns, aTimeOut : Cardinal);
Roger Meier79655fb2012-10-20 20:59:41 +0000660// Named Pipe CTOR
661begin
662 inherited Create;
Jens Geyer06045cf2013-03-27 20:26:25 +0200663 FPipeName := aPipename;
664 FBufsize := aBufSize;
665 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
666 FHandle := INVALID_HANDLE_VALUE;
667 FTimeout := aTimeOut;
668 FConnected := FALSE;
Roger Meier79655fb2012-10-20 20:59:41 +0000669
670 if Copy(FPipeName,1,2) <> '\\'
671 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
672end;
673
674
Jens Geyer01640402013-09-25 21:12:21 +0200675function TNamedPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Jens Geyer06045cf2013-03-27 20:26:25 +0200676var dwError, dwWait, dwDummy : DWORD;
677 overlapped : TOverlapped;
Jens Geyer01640402013-09-25 21:12:21 +0200678 event : TEvent;
679begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200680 FillChar( overlapped, SizeOf(overlapped), 0);
681 event := TEvent.Create( nil, TRUE, FALSE, ''); // always ManualReset, see MSDN
682 try
Jens Geyer01640402013-09-25 21:12:21 +0200683 overlapped.hEvent := event.Handle;
684
685 ASSERT( not FConnected);
686 while not FConnected do begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200687 InternalClose;
688 if FStopServer then Abort;
689 CreateNamedPipe;
Roger Meier79655fb2012-10-20 20:59:41 +0000690
Jens Geyer01640402013-09-25 21:12:21 +0200691 if Assigned(fnAccepting)
692 then fnAccepting();
693
Jens Geyer06045cf2013-03-27 20:26:25 +0200694 // Wait for the client to connect; if it succeeds, the
695 // function returns a nonzero value. If the function returns
696 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
Jens Geyer01640402013-09-25 21:12:21 +0200697 if ConnectNamedPipe( Handle, @overlapped) then begin
698 FConnected := TRUE;
699 Break;
700 end;
701
702 // ConnectNamedPipe() returns FALSE for OverlappedIO, even if connected.
703 // We have to check GetLastError() explicitly to find out
704 dwError := GetLastError;
705 case dwError of
706 ERROR_PIPE_CONNECTED : begin
707 FConnected := not FStopServer; // special case: pipe immediately connected
708 end;
709
710 ERROR_IO_PENDING : begin
711 repeat
712 dwWait := WaitForSingleObject( overlapped.hEvent, DEFAULT_THRIFT_PIPE_TIMEOUT);
713 until (dwWait <> WAIT_TIMEOUT) or FStopServer;
714 FConnected := (dwWait = WAIT_OBJECT_0)
715 and GetOverlappedResult( Handle, overlapped, dwDummy, TRUE)
716 and not FStopServer;
717 end;
718
719 else
720 InternalClose;
721 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
722 'Client connection failed');
723 end;
724 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000725
Jens Geyer06045cf2013-03-27 20:26:25 +0200726 // create the transport impl
727 result := CreateTransportInstance;
Roger Meier79655fb2012-10-20 20:59:41 +0000728
Roger Meier79655fb2012-10-20 20:59:41 +0000729 finally
Jens Geyer01640402013-09-25 21:12:21 +0200730 event.Free;
Roger Meier79655fb2012-10-20 20:59:41 +0000731 end;
732end;
733
734
Jens Geyer06045cf2013-03-27 20:26:25 +0200735function TNamedPipeServerTransportImpl.CreateTransportInstance : ITransport;
736// create the transport impl
737var hPipe : THandle;
Roger Meier79655fb2012-10-20 20:59:41 +0000738begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200739 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
740 try
741 FConnected := FALSE;
742 result := TNamedPipeTransportServerEndImpl.Create( hPipe, TRUE);
743 except
Jens Geyer01640402013-09-25 21:12:21 +0200744 ClosePipeHandle(hPipe);
745 raise;
746 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000747end;
748
749
Jens Geyer06045cf2013-03-27 20:26:25 +0200750procedure TNamedPipeServerTransportImpl.InternalClose;
751var hPipe : THandle;
752begin
753 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
754 if hPipe = INVALID_HANDLE_VALUE then Exit;
755
756 try
757 if FConnected
758 then FlushFileBuffers( hPipe)
759 else CancelIo( hPipe);
760 DisconnectNamedPipe( hPipe);
761 finally
762 ClosePipeHandle( hPipe);
763 FConnected := FALSE;
764 end;
765end;
766
767
768function TNamedPipeServerTransportImpl.Handle : THandle;
769begin
770 {$IFDEF WIN64}
771 result := THandle( InterlockedExchangeAdd64( Integer(FHandle), 0));
772 {$ELSE}
773 result := THandle( InterlockedExchangeAdd( Integer(FHandle), 0));
774 {$ENDIF}
775end;
776
777
778function TNamedPipeServerTransportImpl.CreateNamedPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000779var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
780 everyone_sid : PSID;
781 ea : EXPLICIT_ACCESS;
782 acl : PACL;
783 sd : PSECURITY_DESCRIPTOR;
784 sa : SECURITY_ATTRIBUTES;
Roger Meier3bef8c22012-10-06 06:58:00 +0000785const
786 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
787 SECURITY_WORLD_RID = $00000000;
788begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100789 sd := nil;
Roger Meier3bef8c22012-10-06 06:58:00 +0000790 everyone_sid := nil;
Jens Geyerb64a7742013-01-23 20:58:47 +0100791 try
Jens Geyer06045cf2013-03-27 20:26:25 +0200792 ASSERT( (FHandle = INVALID_HANDLE_VALUE) and not FConnected);
793
Jens Geyerb64a7742013-01-23 20:58:47 +0100794 // Windows - set security to allow non-elevated apps
795 // to access pipes created by elevated apps.
796 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
797 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000798
Jens Geyerb64a7742013-01-23 20:58:47 +0100799 ZeroMemory( @ea, SizeOf(ea));
800 ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
801 ea.grfAccessMode := SET_ACCESS;
802 ea.grfInheritance := NO_INHERITANCE;
803 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
804 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
805 ea.Trustee.ptstrName := PChar(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000806
Jens Geyerb64a7742013-01-23 20:58:47 +0100807 acl := nil;
808 SetEntriesInAcl( 1, @ea, nil, acl);
Roger Meier3bef8c22012-10-06 06:58:00 +0000809
Jens Geyerb64a7742013-01-23 20:58:47 +0100810 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
811 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
812 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
Roger Meier3bef8c22012-10-06 06:58:00 +0000813
Jens Geyerb64a7742013-01-23 20:58:47 +0100814 sa.nLength := SizeOf(sa);
815 sa.lpSecurityDescriptor := sd;
816 sa.bInheritHandle := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000817
Jens Geyerb64a7742013-01-23 20:58:47 +0100818 // Create an instance of the named pipe
Jens Geyer06045cf2013-03-27 20:26:25 +0200819 result := Windows.CreateNamedPipe( PChar( FPipeName), // pipe name
820 PIPE_ACCESS_DUPLEX or // read/write access
821 FILE_FLAG_OVERLAPPED, // async mode
822 PIPE_TYPE_MESSAGE or // message type pipe
823 PIPE_READMODE_MESSAGE, // message-read mode
824 FMaxConns, // max. instances
825 FBufSize, // output buffer size
826 FBufSize, // input buffer size
827 FTimeout, // time-out, see MSDN
828 @sa); // default security attribute
Roger Meier3bef8c22012-10-06 06:58:00 +0000829
Jens Geyer06045cf2013-03-27 20:26:25 +0200830 if( result <> INVALID_HANDLE_VALUE)
831 then InterlockedExchangePointer( Pointer(FHandle), Pointer(result))
832 else raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
Jens Geyerb64a7742013-01-23 20:58:47 +0100833 'CreateNamedPipe() failed ' + IntToStr(GetLastError));
834
835 finally
836 if sd <> nil then LocalFree( Cardinal( sd));
837 if acl <> nil then LocalFree( Cardinal( acl));
838 if everyone_sid <> nil then FreeSid(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000839 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000840end;
841
842
Roger Meier3bef8c22012-10-06 06:58:00 +0000843
844end.
845
846
847