blob: a11bdc15e887b76a4a3d3c00fe36bc7f870f9035 [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}
Jens Geyer9f7f11e2016-04-14 21:37:11 +020022{$I Thrift.Defines.inc}
Roger Meier3bef8c22012-10-06 06:58:00 +000023
24interface
25
26uses
Jens Geyer9f7f11e2016-04-14 21:37:11 +020027 {$IFDEF OLD_UNIT_NAMES}
Jens Geyer06045cf2013-03-27 20:26:25 +020028 Windows, SysUtils, Math, AccCtrl, AclAPI, SyncObjs,
Jens Geyer9f7f11e2016-04-14 21:37:11 +020029 {$ELSE}
Nick4f5229e2016-04-14 16:43:22 +030030 Winapi.Windows, System.SysUtils, System.Math, Winapi.AccCtrl, Winapi.AclAPI, System.SyncObjs,
Jens Geyer9f7f11e2016-04-14 21:37:11 +020031 {$ENDIF}
Roger Meier3bef8c22012-10-06 06:58:00 +000032 Thrift.Transport,
Jens Geyere9651362014-03-20 22:46:17 +020033 Thrift.Utils,
Roger Meier3bef8c22012-10-06 06:58:00 +000034 Thrift.Stream;
35
36const
Jens Geyer3e8d9272014-09-14 20:10:40 +020037 DEFAULT_THRIFT_PIPE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT deprecated 'use DEFAULT_THRIFT_TIMEOUT';
Roger Meier3bef8c22012-10-06 06:58:00 +000038
39
Jens Geyere9651362014-03-20 22:46:17 +020040
Roger Meier3bef8c22012-10-06 06:58:00 +000041type
Roger Meier79655fb2012-10-20 20:59:41 +000042 //--- Pipe Streams ---
Roger Meier3bef8c22012-10-06 06:58:00 +000043
44
Jens Geyer06045cf2013-03-27 20:26:25 +020045 TPipeStreamBase = class( TThriftStreamImpl)
Roger Meier79655fb2012-10-20 20:59:41 +000046 strict protected
47 FPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +000048 FTimeout : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +020049 FOverlapped : Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +000050
Roger Meier3bef8c22012-10-06 06:58:00 +000051 procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override;
52 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override;
Roger Meier79655fb2012-10-20 20:59:41 +000053 //procedure Open; override; - see derived classes
Roger Meier3bef8c22012-10-06 06:58:00 +000054 procedure Close; override;
55 procedure Flush; override;
56
Jens Geyere9651362014-03-20 22:46:17 +020057 function ReadDirect( var buffer: TBytes; offset: Integer; count: Integer): Integer;
58 function ReadOverlapped( var buffer: TBytes; offset: Integer; count: Integer): Integer;
59 procedure WriteDirect( const buffer: TBytes; offset: Integer; count: Integer);
60 procedure WriteOverlapped( const buffer: TBytes; offset: Integer; count: Integer);
61
Roger Meier3bef8c22012-10-06 06:58:00 +000062 function IsOpen: Boolean; override;
63 function ToArray: TBytes; override;
64 public
Jens Geyer3e8d9272014-09-14 20:10:40 +020065 constructor Create( aEnableOverlapped : Boolean; const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +000066 destructor Destroy; override;
67 end;
68
69
Jens Geyer06045cf2013-03-27 20:26:25 +020070 TNamedPipeStreamImpl = class sealed( TPipeStreamBase)
Jens Geyere9651362014-03-20 22:46:17 +020071 strict private
Roger Meier79655fb2012-10-20 20:59:41 +000072 FPipeName : string;
73 FShareMode : DWORD;
74 FSecurityAttribs : PSecurityAttributes;
Roger Meier3bef8c22012-10-06 06:58:00 +000075
Jens Geyere9651362014-03-20 22:46:17 +020076 strict protected
Roger Meier79655fb2012-10-20 20:59:41 +000077 procedure Open; override;
78
79 public
80 constructor Create( const aPipeName : string;
Jens Geyere9651362014-03-20 22:46:17 +020081 const aEnableOverlapped : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +000082 const aShareMode: DWORD = 0;
83 const aSecurityAttributes: PSecurityAttributes = nil;
Jens Geyer3e8d9272014-09-14 20:10:40 +020084 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload;
Roger Meier79655fb2012-10-20 20:59:41 +000085 end;
86
87
Jens Geyer06045cf2013-03-27 20:26:25 +020088 THandlePipeStreamImpl = class sealed( TPipeStreamBase)
Jens Geyere9651362014-03-20 22:46:17 +020089 strict private
Roger Meier79655fb2012-10-20 20:59:41 +000090 FSrcHandle : THandle;
91
Jens Geyere9651362014-03-20 22:46:17 +020092 strict protected
Roger Meier79655fb2012-10-20 20:59:41 +000093 procedure Open; override;
94
95 public
Jens Geyere9651362014-03-20 22:46:17 +020096 constructor Create( const aPipeHandle : THandle;
97 const aOwnsHandle, aEnableOverlapped : Boolean;
Jens Geyer3e8d9272014-09-14 20:10:40 +020098 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload;
Roger Meier79655fb2012-10-20 20:59:41 +000099 destructor Destroy; override;
100 end;
101
102
103 //--- Pipe Transports ---
104
105
Jens Geyer06045cf2013-03-27 20:26:25 +0200106 IPipeTransport = interface( IStreamTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000107 ['{5E05CC85-434F-428F-BFB2-856A168B5558}']
108 end;
109
110
Jens Geyer06045cf2013-03-27 20:26:25 +0200111 TPipeTransportBase = class( TStreamTransportImpl, IPipeTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000112 public
113 // ITransport
114 function GetIsOpen: Boolean; override;
115 procedure Open; override;
116 procedure Close; override;
117 end;
118
119
Jens Geyer06045cf2013-03-27 20:26:25 +0200120 TNamedPipeTransportClientEndImpl = class( TPipeTransportBase)
Roger Meier79655fb2012-10-20 20:59:41 +0000121 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000122 // Named pipe constructors
Jens Geyere9651362014-03-20 22:46:17 +0200123 constructor Create( aPipe : THandle; aOwnsHandle : Boolean;
124 const aTimeOut : DWORD); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000125 constructor Create( const aPipeName : string;
126 const aShareMode: DWORD = 0;
127 const aSecurityAttributes: PSecurityAttributes = nil;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200128 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000129 end;
130
131
Jens Geyer06045cf2013-03-27 20:26:25 +0200132 TNamedPipeTransportServerEndImpl = class( TNamedPipeTransportClientEndImpl)
Roger Meier79655fb2012-10-20 20:59:41 +0000133 strict private
134 FHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000135 public
Roger Meier79655fb2012-10-20 20:59:41 +0000136 // ITransport
137 procedure Close; override;
Jens Geyere9651362014-03-20 22:46:17 +0200138 constructor Create( aPipe : THandle; aOwnsHandle : Boolean;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200139 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); reintroduce;
Roger Meier79655fb2012-10-20 20:59:41 +0000140 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000141
Roger Meier79655fb2012-10-20 20:59:41 +0000142
Jens Geyer06045cf2013-03-27 20:26:25 +0200143 TAnonymousPipeTransportImpl = class( TPipeTransportBase)
Roger Meier79655fb2012-10-20 20:59:41 +0000144 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000145 // Anonymous pipe constructor
146 constructor Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000147 end;
148
149
Roger Meier79655fb2012-10-20 20:59:41 +0000150 //--- Server Transports ---
151
152
Jens Geyer06045cf2013-03-27 20:26:25 +0200153 IAnonymousPipeServerTransport = interface( IServerTransport)
Roger Meier3bef8c22012-10-06 06:58:00 +0000154 ['{7AEE6793-47B9-4E49-981A-C39E9108E9AD}']
155 // Server side anonymous pipe ends
Roger Meier79655fb2012-10-20 20:59:41 +0000156 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000157 function WriteHandle : THandle;
158 // Client side anonymous pipe ends
159 function ClientAnonRead : THandle;
160 function ClientAnonWrite : THandle;
161 end;
162
163
Jens Geyer06045cf2013-03-27 20:26:25 +0200164 INamedPipeServerTransport = interface( IServerTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000165 ['{9DF9EE48-D065-40AF-8F67-D33037D3D960}']
166 function Handle : THandle;
167 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000168
Roger Meier79655fb2012-10-20 20:59:41 +0000169
Jens Geyer06045cf2013-03-27 20:26:25 +0200170 TPipeServerTransportBase = class( TServerTransportImpl)
Jens Geyere9651362014-03-20 22:46:17 +0200171 strict protected
172 FStopServer : TEvent;
Jens Geyer06045cf2013-03-27 20:26:25 +0200173 procedure InternalClose; virtual; abstract;
Jens Geyere9651362014-03-20 22:46:17 +0200174 function QueryStopServer : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000175 public
Jens Geyere9651362014-03-20 22:46:17 +0200176 constructor Create;
177 destructor Destroy; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000178 procedure Listen; override;
Jens Geyer06045cf2013-03-27 20:26:25 +0200179 procedure Close; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000180 end;
181
182
Jens Geyer06045cf2013-03-27 20:26:25 +0200183 TAnonymousPipeServerTransportImpl = class( TPipeServerTransportBase, IAnonymousPipeServerTransport)
Jens Geyere9651362014-03-20 22:46:17 +0200184 strict private
Roger Meier79655fb2012-10-20 20:59:41 +0000185 FBufSize : DWORD;
186
187 // Server side anonymous pipe handles
188 FReadHandle,
Roger Meier3bef8c22012-10-06 06:58:00 +0000189 FWriteHandle : THandle;
190
191 //Client side anonymous pipe handles
192 FClientAnonRead,
193 FClientAnonWrite : THandle;
194
195 protected
Jens Geyer01640402013-09-25 21:12:21 +0200196 function Accept(const fnAccepting: TProc): ITransport; override;
Roger Meier3bef8c22012-10-06 06:58:00 +0000197
Roger Meier3bef8c22012-10-06 06:58:00 +0000198 function CreateAnonPipe : Boolean;
199
Jens Geyer06045cf2013-03-27 20:26:25 +0200200 // IAnonymousPipeServerTransport
Roger Meier79655fb2012-10-20 20:59:41 +0000201 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000202 function WriteHandle : THandle;
203 function ClientAnonRead : THandle;
204 function ClientAnonWrite : THandle;
205
Jens Geyer06045cf2013-03-27 20:26:25 +0200206 procedure InternalClose; override;
207
Roger Meier3bef8c22012-10-06 06:58:00 +0000208 public
Roger Meier79655fb2012-10-20 20:59:41 +0000209 constructor Create( aBufsize : Cardinal = 4096);
Roger Meier3bef8c22012-10-06 06:58:00 +0000210 end;
211
212
Jens Geyer06045cf2013-03-27 20:26:25 +0200213 TNamedPipeServerTransportImpl = class( TPipeServerTransportBase, INamedPipeServerTransport)
Jens Geyere9651362014-03-20 22:46:17 +0200214 strict private
Roger Meier79655fb2012-10-20 20:59:41 +0000215 FPipeName : string;
216 FMaxConns : DWORD;
217 FBufSize : DWORD;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100218 FTimeout : DWORD;
Jens Geyer06045cf2013-03-27 20:26:25 +0200219 FHandle : THandle;
220 FConnected : Boolean;
Jens Geyer01640402013-09-25 21:12:21 +0200221
222
Jens Geyere9651362014-03-20 22:46:17 +0200223 strict protected
Jens Geyer01640402013-09-25 21:12:21 +0200224 function Accept(const fnAccepting: TProc): ITransport; override;
Jens Geyer06045cf2013-03-27 20:26:25 +0200225 function CreateNamedPipe : THandle;
226 function CreateTransportInstance : ITransport;
Roger Meier79655fb2012-10-20 20:59:41 +0000227
Jens Geyer06045cf2013-03-27 20:26:25 +0200228 // INamedPipeServerTransport
Roger Meier79655fb2012-10-20 20:59:41 +0000229 function Handle : THandle;
Jens Geyer06045cf2013-03-27 20:26:25 +0200230 procedure InternalClose; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000231
232 public
233 constructor Create( aPipename : string; aBufsize : Cardinal = 4096;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100234 aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES;
Jens Geyer2ad6c302015-02-26 19:38:53 +0100235 aTimeOut : Cardinal = INFINITE);
Roger Meier79655fb2012-10-20 20:59:41 +0000236 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000237
238
239implementation
240
241
Roger Meier79655fb2012-10-20 20:59:41 +0000242procedure ClosePipeHandle( var hPipe : THandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000243begin
Roger Meier79655fb2012-10-20 20:59:41 +0000244 if hPipe <> INVALID_HANDLE_VALUE
245 then try
246 CloseHandle( hPipe);
247 finally
248 hPipe := INVALID_HANDLE_VALUE;
249 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000250end;
251
252
Roger Meier79655fb2012-10-20 20:59:41 +0000253function DuplicatePipeHandle( const hSource : THandle) : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000254begin
Roger Meier79655fb2012-10-20 20:59:41 +0000255 if not DuplicateHandle( GetCurrentProcess, hSource,
256 GetCurrentProcess, @result,
257 0, FALSE, DUPLICATE_SAME_ACCESS)
258 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
259 'DuplicateHandle: '+SysErrorMessage(GetLastError));
Roger Meier3bef8c22012-10-06 06:58:00 +0000260end;
261
262
Roger Meier79655fb2012-10-20 20:59:41 +0000263
Jens Geyer06045cf2013-03-27 20:26:25 +0200264{ TPipeStreamBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000265
266
Jens Geyere9651362014-03-20 22:46:17 +0200267constructor TPipeStreamBase.Create( aEnableOverlapped : Boolean;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200268 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT);
Roger Meier79655fb2012-10-20 20:59:41 +0000269begin
270 inherited Create;
Jens Geyere9651362014-03-20 22:46:17 +0200271 ASSERT( aTimeout > 0);
272 FPipe := INVALID_HANDLE_VALUE;
273 FTimeout := aTimeOut;
274 FOverlapped := aEnableOverlapped;
Roger Meier79655fb2012-10-20 20:59:41 +0000275end;
276
277
Jens Geyer06045cf2013-03-27 20:26:25 +0200278destructor TPipeStreamBase.Destroy;
Roger Meier3bef8c22012-10-06 06:58:00 +0000279begin
280 try
281 Close;
282 finally
283 inherited Destroy;
284 end;
285end;
286
287
Jens Geyer06045cf2013-03-27 20:26:25 +0200288procedure TPipeStreamBase.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000289begin
Roger Meier79655fb2012-10-20 20:59:41 +0000290 ClosePipeHandle( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000291end;
292
293
Jens Geyer06045cf2013-03-27 20:26:25 +0200294procedure TPipeStreamBase.Flush;
Roger Meier3bef8c22012-10-06 06:58:00 +0000295begin
Jens Geyer0d227b12015-12-02 19:50:55 +0100296 FlushFileBuffers( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000297end;
298
299
Jens Geyer06045cf2013-03-27 20:26:25 +0200300function TPipeStreamBase.IsOpen: Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000301begin
302 result := (FPipe <> INVALID_HANDLE_VALUE);
303end;
304
305
Jens Geyer06045cf2013-03-27 20:26:25 +0200306procedure TPipeStreamBase.Write(const buffer: TBytes; offset, count: Integer);
Jens Geyere9651362014-03-20 22:46:17 +0200307begin
308 if FOverlapped
309 then WriteOverlapped( buffer, offset, count)
310 else WriteDirect( buffer, offset, count);
311end;
312
313
314function TPipeStreamBase.Read( var buffer: TBytes; offset, count: Integer): Integer;
315begin
316 if FOverlapped
317 then result := ReadOverlapped( buffer, offset, count)
318 else result := ReadDirect( buffer, offset, count);
319end;
320
321
322procedure TPipeStreamBase.WriteDirect(const buffer: TBytes; offset, count: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000323var cbWritten : DWORD;
324begin
325 if not IsOpen
326 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
327 'Called write on non-open pipe');
328
329 if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil)
330 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
331 'Write to pipe failed');
332end;
333
334
Jens Geyere9651362014-03-20 22:46:17 +0200335function TPipeStreamBase.ReadDirect( var buffer: TBytes; offset, count: Integer): Integer;
Roger Meier79655fb2012-10-20 20:59:41 +0000336var cbRead, dwErr : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000337 bytes, retries : LongInt;
338 bOk : Boolean;
339const INTERVAL = 10; // ms
340begin
341 if not IsOpen
342 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
343 'Called read on non-open pipe');
344
345 // MSDN: Handle can be a handle to a named pipe instance,
346 // or it can be a handle to the read end of an anonymous pipe,
347 // The handle must have GENERIC_READ access to the pipe.
348 if FTimeOut <> INFINITE then begin
349 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
350 while TRUE do begin
351 if IsOpen
352 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
353 and (bytes > 0)
354 then Break; // there are data
355
Roger Meier79655fb2012-10-20 20:59:41 +0000356 dwErr := GetLastError;
Jens Geyer06045cf2013-03-27 20:26:25 +0200357 if (dwErr = ERROR_INVALID_HANDLE)
358 or (dwErr = ERROR_BROKEN_PIPE)
Roger Meier79655fb2012-10-20 20:59:41 +0000359 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
360 then begin
361 result := 0; // other side closed the pipe
362 Exit;
363 end;
364
Roger Meier3bef8c22012-10-06 06:58:00 +0000365 Dec( retries);
366 if retries > 0
367 then Sleep( INTERVAL)
368 else raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
369 'Pipe read timed out');
370 end;
371 end;
372
373 // read the data (or block INFINITE-ly)
374 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, nil);
375 if (not bOk) and (GetLastError() <> ERROR_MORE_DATA)
376 then result := 0 // No more data, possibly because client disconnected.
377 else result := cbRead;
378end;
379
380
Jens Geyere9651362014-03-20 22:46:17 +0200381procedure TPipeStreamBase.WriteOverlapped(const buffer: TBytes; offset, count: Integer);
382var cbWritten, dwWait, dwError : DWORD;
383 overlapped : IOverlappedHelper;
384begin
385 if not IsOpen
386 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
387 'Called write on non-open pipe');
388
389 overlapped := TOverlappedHelperImpl.Create;
390
391 if not WriteFile( FPipe, buffer[offset], count, cbWritten, overlapped.OverlappedPtr)
392 then begin
393 dwError := GetLastError;
394 case dwError of
395 ERROR_IO_PENDING : begin
396 dwWait := overlapped.WaitFor(FTimeout);
397
398 if (dwWait = WAIT_TIMEOUT)
399 then raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
400 'Pipe write timed out');
401
402 if (dwWait <> WAIT_OBJECT_0)
403 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbWritten, TRUE)
404 then raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
405 'Pipe write error');
406 end;
407
408 else
409 raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
410 SysErrorMessage(dwError));
411 end;
412 end;
413
414 ASSERT( DWORD(count) = cbWritten);
415end;
416
417
418function TPipeStreamBase.ReadOverlapped( var buffer: TBytes; offset, count: Integer): Integer;
419var cbRead, dwWait, dwError : DWORD;
420 bOk : Boolean;
421 overlapped : IOverlappedHelper;
422begin
423 if not IsOpen
424 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
425 'Called read on non-open pipe');
426
427 overlapped := TOverlappedHelperImpl.Create;
428
429 // read the data
430 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, overlapped.OverlappedPtr);
431 if not bOk then begin
432 dwError := GetLastError;
433 case dwError of
434 ERROR_IO_PENDING : begin
435 dwWait := overlapped.WaitFor(FTimeout);
436
437 if (dwWait = WAIT_TIMEOUT)
438 then raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
439 'Pipe read timed out');
440
441 if (dwWait <> WAIT_OBJECT_0)
442 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbRead, TRUE)
443 then raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
444 'Pipe read error');
445 end;
446
447 else
448 raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
449 SysErrorMessage(dwError));
450 end;
451 end;
452
453 ASSERT( cbRead > 0); // see TTransportImpl.ReadAll()
454 ASSERT( cbRead = DWORD(count));
455 result := cbRead;
456end;
457
458
Jens Geyer06045cf2013-03-27 20:26:25 +0200459function TPipeStreamBase.ToArray: TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000460var bytes : LongInt;
461begin
462 SetLength( result, 0);
463 bytes := 0;
464
465 if IsOpen
466 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
467 and (bytes > 0)
468 then begin
469 SetLength( result, bytes);
470 Read( result, 0, bytes);
471 end;
472end;
473
474
Roger Meier79655fb2012-10-20 20:59:41 +0000475{ TNamedPipeStreamImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000476
477
Jens Geyere9651362014-03-20 22:46:17 +0200478constructor TNamedPipeStreamImpl.Create( const aPipeName : string;
479 const aEnableOverlapped : Boolean;
480 const aShareMode: DWORD;
Roger Meier79655fb2012-10-20 20:59:41 +0000481 const aSecurityAttributes: PSecurityAttributes;
482 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000483begin
Jens Geyere9651362014-03-20 22:46:17 +0200484 inherited Create( aEnableOverlapped, aTimeout);
Roger Meier79655fb2012-10-20 20:59:41 +0000485
486 FPipeName := aPipeName;
487 FShareMode := aShareMode;
488 FSecurityAttribs := aSecurityAttributes;
489
490 if Copy(FPipeName,1,2) <> '\\'
491 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
Roger Meier3bef8c22012-10-06 06:58:00 +0000492end;
493
494
Roger Meier79655fb2012-10-20 20:59:41 +0000495procedure TNamedPipeStreamImpl.Open;
496var hPipe : THandle;
Roger Meier79655fb2012-10-20 20:59:41 +0000497begin
498 if IsOpen then Exit;
499
500 // open that thingy
501
502 if not WaitNamedPipe( PChar(FPipeName), FTimeout)
503 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
504 'Unable to open pipe, '+SysErrorMessage(GetLastError));
505
506 hPipe := CreateFile( PChar( FPipeName),
507 GENERIC_READ or GENERIC_WRITE,
508 FShareMode, // sharing
509 FSecurityAttribs, // security attributes
510 OPEN_EXISTING, // opens existing pipe
Jens Geyere9651362014-03-20 22:46:17 +0200511 FILE_FLAG_OVERLAPPED or FILE_FLAG_WRITE_THROUGH, // async+fast, please
Roger Meier79655fb2012-10-20 20:59:41 +0000512 0); // no template file
513
514 if hPipe = INVALID_HANDLE_VALUE
515 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
516 'Unable to open pipe, '+SysErrorMessage(GetLastError));
517
Roger Meier79655fb2012-10-20 20:59:41 +0000518 // everything fine
519 FPipe := hPipe;
520end;
521
522
523{ THandlePipeStreamImpl }
524
525
Jens Geyere9651362014-03-20 22:46:17 +0200526constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle;
527 const aOwnsHandle, aEnableOverlapped : Boolean;
528 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000529begin
Jens Geyere9651362014-03-20 22:46:17 +0200530 inherited Create( aEnableOverlapped, aTimeOut);
Roger Meier79655fb2012-10-20 20:59:41 +0000531
532 if aOwnsHandle
533 then FSrcHandle := aPipeHandle
534 else FSrcHandle := DuplicatePipeHandle( aPipeHandle);
535
536 Open;
537end;
538
539
540destructor THandlePipeStreamImpl.Destroy;
541begin
542 try
543 ClosePipeHandle( FSrcHandle);
544 finally
545 inherited Destroy;
546 end;
547end;
548
549
550procedure THandlePipeStreamImpl.Open;
551begin
552 if not IsOpen
553 then FPipe := DuplicatePipeHandle( FSrcHandle);
554end;
555
556
Jens Geyer06045cf2013-03-27 20:26:25 +0200557{ TPipeTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000558
559
Jens Geyer06045cf2013-03-27 20:26:25 +0200560function TPipeTransportBase.GetIsOpen: Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000561begin
Jens Geyer0b20cc82013-03-07 20:47:01 +0100562 result := (FInputStream <> nil) and (FInputStream.IsOpen)
563 and (FOutputStream <> nil) and (FOutputStream.IsOpen);
Roger Meier79655fb2012-10-20 20:59:41 +0000564end;
565
566
Jens Geyer06045cf2013-03-27 20:26:25 +0200567procedure TPipeTransportBase.Open;
Roger Meier79655fb2012-10-20 20:59:41 +0000568begin
569 FInputStream.Open;
570 FOutputStream.Open;
571end;
572
573
Jens Geyer06045cf2013-03-27 20:26:25 +0200574procedure TPipeTransportBase.Close;
Roger Meier79655fb2012-10-20 20:59:41 +0000575begin
576 FInputStream.Close;
577 FOutputStream.Close;
578end;
579
580
Jens Geyer06045cf2013-03-27 20:26:25 +0200581{ TNamedPipeTransportClientEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000582
583
Jens Geyer06045cf2013-03-27 20:26:25 +0200584constructor TNamedPipeTransportClientEndImpl.Create( const aPipeName : string; const aShareMode: DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000585 const aSecurityAttributes: PSecurityAttributes;
586 const aTimeOut : DWORD);
587// Named pipe constructor
588begin
Roger Meier79655fb2012-10-20 20:59:41 +0000589 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200590 FInputStream := TNamedPipeStreamImpl.Create( aPipeName, TRUE, aShareMode, aSecurityAttributes, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000591 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000592end;
593
594
Jens Geyere9651362014-03-20 22:46:17 +0200595constructor TNamedPipeTransportClientEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
596 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000597// Named pipe constructor
598begin
Roger Meier79655fb2012-10-20 20:59:41 +0000599 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200600 FInputStream := THandlePipeStreamImpl.Create( aPipe, TRUE, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000601 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000602end;
603
604
Jens Geyer06045cf2013-03-27 20:26:25 +0200605{ TNamedPipeTransportServerEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000606
607
Jens Geyere9651362014-03-20 22:46:17 +0200608constructor TNamedPipeTransportServerEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
609 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000610// Named pipe constructor
Roger Meier3bef8c22012-10-06 06:58:00 +0000611begin
Roger Meier79655fb2012-10-20 20:59:41 +0000612 FHandle := DuplicatePipeHandle( aPipe);
Jens Geyere9651362014-03-20 22:46:17 +0200613 inherited Create( aPipe, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000614end;
615
616
Jens Geyer06045cf2013-03-27 20:26:25 +0200617procedure TNamedPipeTransportServerEndImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000618begin
Roger Meier79655fb2012-10-20 20:59:41 +0000619 FlushFileBuffers( FHandle);
620 DisconnectNamedPipe( FHandle); // force client off the pipe
621 ClosePipeHandle( FHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000622
Roger Meier79655fb2012-10-20 20:59:41 +0000623 inherited Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000624end;
625
626
Jens Geyer06045cf2013-03-27 20:26:25 +0200627{ TAnonymousPipeTransportImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000628
629
Jens Geyer06045cf2013-03-27 20:26:25 +0200630constructor TAnonymousPipeTransportImpl.Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean);
Roger Meier3bef8c22012-10-06 06:58:00 +0000631// Anonymous pipe constructor
632begin
Roger Meier79655fb2012-10-20 20:59:41 +0000633 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200634 // overlapped is not supported with AnonPipes, see MSDN
635 FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles, FALSE);
636 FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles, FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +0000637end;
638
639
Jens Geyer06045cf2013-03-27 20:26:25 +0200640{ TPipeServerTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000641
642
Jens Geyere9651362014-03-20 22:46:17 +0200643constructor TPipeServerTransportBase.Create;
644begin
645 inherited Create;
646 FStopServer := TEvent.Create(nil,TRUE,FALSE,''); // manual reset
647end;
648
649
650destructor TPipeServerTransportBase.Destroy;
651begin
652 try
653 FreeAndNil( FStopServer);
654 finally
655 inherited Destroy;
656 end;
657end;
658
659
660function TPipeServerTransportBase.QueryStopServer : Boolean;
661begin
662 result := (FStopServer = nil)
663 or (FStopServer.WaitFor(0) <> wrTimeout);
664end;
665
666
Jens Geyer06045cf2013-03-27 20:26:25 +0200667procedure TPipeServerTransportBase.Listen;
Roger Meier3bef8c22012-10-06 06:58:00 +0000668begin
Jens Geyere9651362014-03-20 22:46:17 +0200669 FStopServer.ResetEvent;
Roger Meier3bef8c22012-10-06 06:58:00 +0000670end;
671
672
Jens Geyer06045cf2013-03-27 20:26:25 +0200673procedure TPipeServerTransportBase.Close;
674begin
Jens Geyere9651362014-03-20 22:46:17 +0200675 FStopServer.SetEvent;
Jens Geyer06045cf2013-03-27 20:26:25 +0200676 InternalClose;
677end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000678
679
Jens Geyer06045cf2013-03-27 20:26:25 +0200680{ TAnonymousPipeServerTransportImpl }
681
682
683constructor TAnonymousPipeServerTransportImpl.Create( aBufsize : Cardinal);
Roger Meier3bef8c22012-10-06 06:58:00 +0000684// Anonymous pipe CTOR
685begin
686 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000687 FBufsize := aBufSize;
Roger Meier79655fb2012-10-20 20:59:41 +0000688 FReadHandle := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000689 FWriteHandle := INVALID_HANDLE_VALUE;
690 FClientAnonRead := INVALID_HANDLE_VALUE;
691 FClientAnonWrite := INVALID_HANDLE_VALUE;
692
693 // The anonymous pipe needs to be created first so that the server can
694 // pass the handles on to the client before the serve (acceptImpl)
695 // blocking call.
696 if not CreateAnonPipe
697 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
698 ClassName+'.Create() failed');
699end;
700
701
Jens Geyer01640402013-09-25 21:12:21 +0200702function TAnonymousPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Roger Meier3bef8c22012-10-06 06:58:00 +0000703var buf : Byte;
704 br : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000705begin
Jens Geyer01640402013-09-25 21:12:21 +0200706 if Assigned(fnAccepting)
707 then fnAccepting();
708
Roger Meier79655fb2012-10-20 20:59:41 +0000709 // This 0-byte read serves merely as a blocking call.
710 if not ReadFile( FReadHandle, buf, 0, br, nil)
711 and (GetLastError() <> ERROR_MORE_DATA)
712 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
713 'TServerPipe unable to initiate pipe communication');
Jens Geyer06045cf2013-03-27 20:26:25 +0200714
715 // create the transport impl
716 result := TAnonymousPipeTransportImpl.Create( FReadHandle, FWriteHandle, FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +0000717end;
718
719
Jens Geyer06045cf2013-03-27 20:26:25 +0200720procedure TAnonymousPipeServerTransportImpl.InternalClose;
Roger Meier3bef8c22012-10-06 06:58:00 +0000721begin
Roger Meier79655fb2012-10-20 20:59:41 +0000722 ClosePipeHandle( FReadHandle);
723 ClosePipeHandle( FWriteHandle);
724 ClosePipeHandle( FClientAnonRead);
725 ClosePipeHandle( FClientAnonWrite);
Roger Meier3bef8c22012-10-06 06:58:00 +0000726end;
727
728
Jens Geyer06045cf2013-03-27 20:26:25 +0200729function TAnonymousPipeServerTransportImpl.ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000730begin
Roger Meier79655fb2012-10-20 20:59:41 +0000731 result := FReadHandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000732end;
733
734
Jens Geyer06045cf2013-03-27 20:26:25 +0200735function TAnonymousPipeServerTransportImpl.WriteHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000736begin
737 result := FWriteHandle;
738end;
739
740
Jens Geyer06045cf2013-03-27 20:26:25 +0200741function TAnonymousPipeServerTransportImpl.ClientAnonRead : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000742begin
743 result := FClientAnonRead;
744end;
745
746
Jens Geyer06045cf2013-03-27 20:26:25 +0200747function TAnonymousPipeServerTransportImpl.ClientAnonWrite : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000748begin
749 result := FClientAnonWrite;
750end;
751
752
Jens Geyer06045cf2013-03-27 20:26:25 +0200753function TAnonymousPipeServerTransportImpl.CreateAnonPipe : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000754var sd : PSECURITY_DESCRIPTOR;
755 sa : SECURITY_ATTRIBUTES; //TSecurityAttributes;
756 hCAR, hPipeW, hCAW, hPipe : THandle;
757begin
758 result := FALSE;
759
760 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
Jens Geyerb64a7742013-01-23 20:58:47 +0100761 try
762 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
763 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
Roger Meier79655fb2012-10-20 20:59:41 +0000764
Jens Geyerb64a7742013-01-23 20:58:47 +0100765 sa.nLength := sizeof( sa);
766 sa.lpSecurityDescriptor := sd;
767 sa.bInheritHandle := TRUE; //allow passing handle to child
Roger Meier79655fb2012-10-20 20:59:41 +0000768
Jens Geyerb64a7742013-01-23 20:58:47 +0100769 if not CreatePipe( hCAR, hPipeW, @sa, FBufSize) then begin //create stdin pipe
Jens Geyer06045cf2013-03-27 20:26:25 +0200770 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
771 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100772 Exit;
773 end;
774
775 if not CreatePipe( hPipe, hCAW, @sa, FBufSize) then begin //create stdout pipe
Jens Geyerb64a7742013-01-23 20:58:47 +0100776 CloseHandle( hCAR);
777 CloseHandle( hPipeW);
Jens Geyer06045cf2013-03-27 20:26:25 +0200778 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
779 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100780 Exit;
781 end;
782
783 FClientAnonRead := hCAR;
784 FClientAnonWrite := hCAW;
785 FReadHandle := hPipe;
786 FWriteHandle := hPipeW;
787
788 result := TRUE;
Jens Geyerd5436f52014-10-03 19:50:38 +0200789
Jens Geyerb64a7742013-01-23 20:58:47 +0100790 finally
791 if sd <> nil then LocalFree( Cardinal(sd));
Roger Meier79655fb2012-10-20 20:59:41 +0000792 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000793end;
794
795
Jens Geyer06045cf2013-03-27 20:26:25 +0200796{ TNamedPipeServerTransportImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000797
798
Jens Geyer06045cf2013-03-27 20:26:25 +0200799constructor TNamedPipeServerTransportImpl.Create( aPipename : string; aBufsize, aMaxConns, aTimeOut : Cardinal);
Roger Meier79655fb2012-10-20 20:59:41 +0000800// Named Pipe CTOR
801begin
802 inherited Create;
Jens Geyere9651362014-03-20 22:46:17 +0200803 ASSERT( aTimeout > 0);
Jens Geyer06045cf2013-03-27 20:26:25 +0200804 FPipeName := aPipename;
805 FBufsize := aBufSize;
806 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
807 FHandle := INVALID_HANDLE_VALUE;
808 FTimeout := aTimeOut;
809 FConnected := FALSE;
Roger Meier79655fb2012-10-20 20:59:41 +0000810
811 if Copy(FPipeName,1,2) <> '\\'
812 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
813end;
814
815
Jens Geyer01640402013-09-25 21:12:21 +0200816function TNamedPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Jens Geyer06045cf2013-03-27 20:26:25 +0200817var dwError, dwWait, dwDummy : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +0200818 overlapped : IOverlappedHelper;
819 handles : array[0..1] of THandle;
Jens Geyer01640402013-09-25 21:12:21 +0200820begin
Jens Geyere9651362014-03-20 22:46:17 +0200821 overlapped := TOverlappedHelperImpl.Create;
Jens Geyer01640402013-09-25 21:12:21 +0200822
Jens Geyere9651362014-03-20 22:46:17 +0200823 ASSERT( not FConnected);
Jens Geyer2ad6c302015-02-26 19:38:53 +0100824 CreateNamedPipe;
Jens Geyere9651362014-03-20 22:46:17 +0200825 while not FConnected do begin
Jens Geyer2ad6c302015-02-26 19:38:53 +0100826
827 if QueryStopServer
828 then Abort;
Roger Meier79655fb2012-10-20 20:59:41 +0000829
Jens Geyere9651362014-03-20 22:46:17 +0200830 if Assigned(fnAccepting)
831 then fnAccepting();
Jens Geyer01640402013-09-25 21:12:21 +0200832
Jens Geyere9651362014-03-20 22:46:17 +0200833 // Wait for the client to connect; if it succeeds, the
834 // function returns a nonzero value. If the function returns
835 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
836 if ConnectNamedPipe( Handle, overlapped.OverlappedPtr) then begin
837 FConnected := TRUE;
838 Break;
Jens Geyer01640402013-09-25 21:12:21 +0200839 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000840
Jens Geyere9651362014-03-20 22:46:17 +0200841 // ConnectNamedPipe() returns FALSE for OverlappedIO, even if connected.
842 // We have to check GetLastError() explicitly to find out
843 dwError := GetLastError;
844 case dwError of
845 ERROR_PIPE_CONNECTED : begin
846 FConnected := not QueryStopServer; // special case: pipe immediately connected
847 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000848
Jens Geyere9651362014-03-20 22:46:17 +0200849 ERROR_IO_PENDING : begin
850 handles[0] := overlapped.WaitHandle;
851 handles[1] := FStopServer.Handle;
852 dwWait := WaitForMultipleObjects( 2, @handles, FALSE, FTimeout);
853 FConnected := (dwWait = WAIT_OBJECT_0)
854 and GetOverlappedResult( Handle, overlapped.Overlapped, dwDummy, TRUE)
855 and not QueryStopServer;
856 end;
857
858 else
859 InternalClose;
860 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
861 'Client connection failed');
862 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000863 end;
Jens Geyere9651362014-03-20 22:46:17 +0200864
865 // create the transport impl
866 result := CreateTransportInstance;
Roger Meier79655fb2012-10-20 20:59:41 +0000867end;
868
869
Jens Geyer06045cf2013-03-27 20:26:25 +0200870function TNamedPipeServerTransportImpl.CreateTransportInstance : ITransport;
871// create the transport impl
872var hPipe : THandle;
Roger Meier79655fb2012-10-20 20:59:41 +0000873begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200874 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
875 try
876 FConnected := FALSE;
Jens Geyere9651362014-03-20 22:46:17 +0200877 result := TNamedPipeTransportServerEndImpl.Create( hPipe, TRUE, FTimeout);
Jens Geyer06045cf2013-03-27 20:26:25 +0200878 except
Jens Geyer01640402013-09-25 21:12:21 +0200879 ClosePipeHandle(hPipe);
880 raise;
881 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000882end;
883
884
Jens Geyer06045cf2013-03-27 20:26:25 +0200885procedure TNamedPipeServerTransportImpl.InternalClose;
886var hPipe : THandle;
887begin
888 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
889 if hPipe = INVALID_HANDLE_VALUE then Exit;
890
891 try
892 if FConnected
893 then FlushFileBuffers( hPipe)
894 else CancelIo( hPipe);
895 DisconnectNamedPipe( hPipe);
896 finally
897 ClosePipeHandle( hPipe);
898 FConnected := FALSE;
899 end;
900end;
901
902
903function TNamedPipeServerTransportImpl.Handle : THandle;
904begin
905 {$IFDEF WIN64}
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200906 result := THandle( InterlockedExchangeAdd64( Int64(FHandle), 0));
Jens Geyer06045cf2013-03-27 20:26:25 +0200907 {$ELSE}
908 result := THandle( InterlockedExchangeAdd( Integer(FHandle), 0));
909 {$ENDIF}
910end;
911
912
913function TNamedPipeServerTransportImpl.CreateNamedPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000914var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
915 everyone_sid : PSID;
916 ea : EXPLICIT_ACCESS;
917 acl : PACL;
918 sd : PSECURITY_DESCRIPTOR;
919 sa : SECURITY_ATTRIBUTES;
Roger Meier3bef8c22012-10-06 06:58:00 +0000920const
921 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
922 SECURITY_WORLD_RID = $00000000;
923begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100924 sd := nil;
Roger Meier3bef8c22012-10-06 06:58:00 +0000925 everyone_sid := nil;
Jens Geyerb64a7742013-01-23 20:58:47 +0100926 try
Jens Geyer06045cf2013-03-27 20:26:25 +0200927 ASSERT( (FHandle = INVALID_HANDLE_VALUE) and not FConnected);
928
Jens Geyerb64a7742013-01-23 20:58:47 +0100929 // Windows - set security to allow non-elevated apps
930 // to access pipes created by elevated apps.
931 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
932 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000933
Jens Geyerb64a7742013-01-23 20:58:47 +0100934 ZeroMemory( @ea, SizeOf(ea));
935 ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
936 ea.grfAccessMode := SET_ACCESS;
937 ea.grfInheritance := NO_INHERITANCE;
938 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
939 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
940 ea.Trustee.ptstrName := PChar(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000941
Jens Geyerb64a7742013-01-23 20:58:47 +0100942 acl := nil;
943 SetEntriesInAcl( 1, @ea, nil, acl);
Roger Meier3bef8c22012-10-06 06:58:00 +0000944
Jens Geyerb64a7742013-01-23 20:58:47 +0100945 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
946 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
947 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
Roger Meier3bef8c22012-10-06 06:58:00 +0000948
Jens Geyerb64a7742013-01-23 20:58:47 +0100949 sa.nLength := SizeOf(sa);
950 sa.lpSecurityDescriptor := sd;
951 sa.bInheritHandle := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000952
Jens Geyerb64a7742013-01-23 20:58:47 +0100953 // Create an instance of the named pipe
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200954 {$IFDEF OLD_UNIT_NAMES}
955 result := Windows.CreateNamedPipe(
956 {$ELSE}
957 result := Winapi.Windows.CreateNamedPipe(
958 {$ENDIF}
959 PChar( FPipeName), // pipe name
960 PIPE_ACCESS_DUPLEX or // read/write access
961 FILE_FLAG_OVERLAPPED, // async mode
962 PIPE_TYPE_BYTE or // byte type pipe
963 PIPE_READMODE_BYTE, // byte read mode
964 FMaxConns, // max. instances
965 FBufSize, // output buffer size
966 FBufSize, // input buffer size
967 FTimeout, // time-out, see MSDN
968 @sa // default security attribute
969 );
Roger Meier3bef8c22012-10-06 06:58:00 +0000970
Jens Geyer06045cf2013-03-27 20:26:25 +0200971 if( result <> INVALID_HANDLE_VALUE)
972 then InterlockedExchangePointer( Pointer(FHandle), Pointer(result))
973 else raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
Jens Geyerb64a7742013-01-23 20:58:47 +0100974 'CreateNamedPipe() failed ' + IntToStr(GetLastError));
975
976 finally
977 if sd <> nil then LocalFree( Cardinal( sd));
978 if acl <> nil then LocalFree( Cardinal( acl));
979 if everyone_sid <> nil then FreeSid(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000980 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000981end;
982
983
Roger Meier3bef8c22012-10-06 06:58:00 +0000984
985end.
986
987
988