blob: 82ba62dca9b720f6c1592cdb074f60acf71c0815 [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
Jens Geyerdd074e72016-04-19 23:31:33 +0200146 constructor Create(const aPipeRead, aPipeWrite : THandle;
147 aOwnsHandles : Boolean;
148 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000149 end;
150
151
Roger Meier79655fb2012-10-20 20:59:41 +0000152 //--- Server Transports ---
153
154
Jens Geyer06045cf2013-03-27 20:26:25 +0200155 IAnonymousPipeServerTransport = interface( IServerTransport)
Roger Meier3bef8c22012-10-06 06:58:00 +0000156 ['{7AEE6793-47B9-4E49-981A-C39E9108E9AD}']
157 // Server side anonymous pipe ends
Roger Meier79655fb2012-10-20 20:59:41 +0000158 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000159 function WriteHandle : THandle;
160 // Client side anonymous pipe ends
161 function ClientAnonRead : THandle;
162 function ClientAnonWrite : THandle;
163 end;
164
165
Jens Geyer06045cf2013-03-27 20:26:25 +0200166 INamedPipeServerTransport = interface( IServerTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000167 ['{9DF9EE48-D065-40AF-8F67-D33037D3D960}']
168 function Handle : THandle;
169 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000170
Roger Meier79655fb2012-10-20 20:59:41 +0000171
Jens Geyer06045cf2013-03-27 20:26:25 +0200172 TPipeServerTransportBase = class( TServerTransportImpl)
Jens Geyere9651362014-03-20 22:46:17 +0200173 strict protected
174 FStopServer : TEvent;
Jens Geyer06045cf2013-03-27 20:26:25 +0200175 procedure InternalClose; virtual; abstract;
Jens Geyere9651362014-03-20 22:46:17 +0200176 function QueryStopServer : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000177 public
Jens Geyere9651362014-03-20 22:46:17 +0200178 constructor Create;
179 destructor Destroy; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000180 procedure Listen; override;
Jens Geyer06045cf2013-03-27 20:26:25 +0200181 procedure Close; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000182 end;
183
184
Jens Geyer06045cf2013-03-27 20:26:25 +0200185 TAnonymousPipeServerTransportImpl = class( TPipeServerTransportBase, IAnonymousPipeServerTransport)
Jens Geyere9651362014-03-20 22:46:17 +0200186 strict private
Roger Meier79655fb2012-10-20 20:59:41 +0000187 FBufSize : DWORD;
188
189 // Server side anonymous pipe handles
190 FReadHandle,
Roger Meier3bef8c22012-10-06 06:58:00 +0000191 FWriteHandle : THandle;
192
193 //Client side anonymous pipe handles
194 FClientAnonRead,
195 FClientAnonWrite : THandle;
196
Jens Geyerdd074e72016-04-19 23:31:33 +0200197 FTimeOut: DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000198 protected
Jens Geyer01640402013-09-25 21:12:21 +0200199 function Accept(const fnAccepting: TProc): ITransport; override;
Roger Meier3bef8c22012-10-06 06:58:00 +0000200
Roger Meier3bef8c22012-10-06 06:58:00 +0000201 function CreateAnonPipe : Boolean;
202
Jens Geyer06045cf2013-03-27 20:26:25 +0200203 // IAnonymousPipeServerTransport
Roger Meier79655fb2012-10-20 20:59:41 +0000204 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000205 function WriteHandle : THandle;
206 function ClientAnonRead : THandle;
207 function ClientAnonWrite : THandle;
208
Jens Geyer06045cf2013-03-27 20:26:25 +0200209 procedure InternalClose; override;
210
Roger Meier3bef8c22012-10-06 06:58:00 +0000211 public
Jens Geyerdd074e72016-04-19 23:31:33 +0200212 constructor Create(aBufsize : Cardinal = 4096; aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +0000213 end;
214
215
Jens Geyer06045cf2013-03-27 20:26:25 +0200216 TNamedPipeServerTransportImpl = class( TPipeServerTransportBase, INamedPipeServerTransport)
Jens Geyere9651362014-03-20 22:46:17 +0200217 strict private
Roger Meier79655fb2012-10-20 20:59:41 +0000218 FPipeName : string;
219 FMaxConns : DWORD;
220 FBufSize : DWORD;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100221 FTimeout : DWORD;
Jens Geyer06045cf2013-03-27 20:26:25 +0200222 FHandle : THandle;
223 FConnected : Boolean;
Jens Geyer01640402013-09-25 21:12:21 +0200224
225
Jens Geyere9651362014-03-20 22:46:17 +0200226 strict protected
Jens Geyer01640402013-09-25 21:12:21 +0200227 function Accept(const fnAccepting: TProc): ITransport; override;
Jens Geyer06045cf2013-03-27 20:26:25 +0200228 function CreateNamedPipe : THandle;
229 function CreateTransportInstance : ITransport;
Roger Meier79655fb2012-10-20 20:59:41 +0000230
Jens Geyer06045cf2013-03-27 20:26:25 +0200231 // INamedPipeServerTransport
Roger Meier79655fb2012-10-20 20:59:41 +0000232 function Handle : THandle;
Jens Geyer06045cf2013-03-27 20:26:25 +0200233 procedure InternalClose; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000234
235 public
236 constructor Create( aPipename : string; aBufsize : Cardinal = 4096;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100237 aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES;
Jens Geyer2ad6c302015-02-26 19:38:53 +0100238 aTimeOut : Cardinal = INFINITE);
Roger Meier79655fb2012-10-20 20:59:41 +0000239 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000240
241
242implementation
243
244
Roger Meier79655fb2012-10-20 20:59:41 +0000245procedure ClosePipeHandle( var hPipe : THandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000246begin
Roger Meier79655fb2012-10-20 20:59:41 +0000247 if hPipe <> INVALID_HANDLE_VALUE
248 then try
249 CloseHandle( hPipe);
250 finally
251 hPipe := INVALID_HANDLE_VALUE;
252 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000253end;
254
255
Roger Meier79655fb2012-10-20 20:59:41 +0000256function DuplicatePipeHandle( const hSource : THandle) : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000257begin
Roger Meier79655fb2012-10-20 20:59:41 +0000258 if not DuplicateHandle( GetCurrentProcess, hSource,
259 GetCurrentProcess, @result,
260 0, FALSE, DUPLICATE_SAME_ACCESS)
261 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
262 'DuplicateHandle: '+SysErrorMessage(GetLastError));
Roger Meier3bef8c22012-10-06 06:58:00 +0000263end;
264
265
Roger Meier79655fb2012-10-20 20:59:41 +0000266
Jens Geyer06045cf2013-03-27 20:26:25 +0200267{ TPipeStreamBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000268
269
Jens Geyere9651362014-03-20 22:46:17 +0200270constructor TPipeStreamBase.Create( aEnableOverlapped : Boolean;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200271 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT);
Roger Meier79655fb2012-10-20 20:59:41 +0000272begin
273 inherited Create;
Jens Geyere9651362014-03-20 22:46:17 +0200274 ASSERT( aTimeout > 0);
275 FPipe := INVALID_HANDLE_VALUE;
276 FTimeout := aTimeOut;
277 FOverlapped := aEnableOverlapped;
Roger Meier79655fb2012-10-20 20:59:41 +0000278end;
279
280
Jens Geyer06045cf2013-03-27 20:26:25 +0200281destructor TPipeStreamBase.Destroy;
Roger Meier3bef8c22012-10-06 06:58:00 +0000282begin
283 try
284 Close;
285 finally
286 inherited Destroy;
287 end;
288end;
289
290
Jens Geyer06045cf2013-03-27 20:26:25 +0200291procedure TPipeStreamBase.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000292begin
Roger Meier79655fb2012-10-20 20:59:41 +0000293 ClosePipeHandle( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000294end;
295
296
Jens Geyer06045cf2013-03-27 20:26:25 +0200297procedure TPipeStreamBase.Flush;
Roger Meier3bef8c22012-10-06 06:58:00 +0000298begin
Jens Geyer0d227b12015-12-02 19:50:55 +0100299 FlushFileBuffers( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000300end;
301
302
Jens Geyer06045cf2013-03-27 20:26:25 +0200303function TPipeStreamBase.IsOpen: Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000304begin
305 result := (FPipe <> INVALID_HANDLE_VALUE);
306end;
307
308
Jens Geyer06045cf2013-03-27 20:26:25 +0200309procedure TPipeStreamBase.Write(const buffer: TBytes; offset, count: Integer);
Jens Geyere9651362014-03-20 22:46:17 +0200310begin
311 if FOverlapped
312 then WriteOverlapped( buffer, offset, count)
313 else WriteDirect( buffer, offset, count);
314end;
315
316
317function TPipeStreamBase.Read( var buffer: TBytes; offset, count: Integer): Integer;
318begin
319 if FOverlapped
320 then result := ReadOverlapped( buffer, offset, count)
321 else result := ReadDirect( buffer, offset, count);
322end;
323
324
325procedure TPipeStreamBase.WriteDirect(const buffer: TBytes; offset, count: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000326var cbWritten : DWORD;
327begin
328 if not IsOpen
329 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
330 'Called write on non-open pipe');
331
332 if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil)
333 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
334 'Write to pipe failed');
335end;
336
337
Jens Geyere9651362014-03-20 22:46:17 +0200338function TPipeStreamBase.ReadDirect( var buffer: TBytes; offset, count: Integer): Integer;
Roger Meier79655fb2012-10-20 20:59:41 +0000339var cbRead, dwErr : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000340 bytes, retries : LongInt;
341 bOk : Boolean;
342const INTERVAL = 10; // ms
343begin
344 if not IsOpen
345 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
346 'Called read on non-open pipe');
347
348 // MSDN: Handle can be a handle to a named pipe instance,
349 // or it can be a handle to the read end of an anonymous pipe,
350 // The handle must have GENERIC_READ access to the pipe.
351 if FTimeOut <> INFINITE then begin
352 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
353 while TRUE do begin
Jens Geyer5988f482016-04-19 23:01:24 +0200354 if not PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) then begin
355 dwErr := GetLastError;
356 if (dwErr = ERROR_INVALID_HANDLE)
357 or (dwErr = ERROR_BROKEN_PIPE)
358 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
359 then begin
360 result := 0; // other side closed the pipe
361 Exit;
362 end;
363 end
364 else if bytes > 0 then begin
365 Break; // there are data
Roger Meier79655fb2012-10-20 20:59:41 +0000366 end;
367
Roger Meier3bef8c22012-10-06 06:58:00 +0000368 Dec( retries);
369 if retries > 0
370 then Sleep( INTERVAL)
371 else raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
372 'Pipe read timed out');
373 end;
374 end;
375
376 // read the data (or block INFINITE-ly)
377 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, nil);
378 if (not bOk) and (GetLastError() <> ERROR_MORE_DATA)
379 then result := 0 // No more data, possibly because client disconnected.
380 else result := cbRead;
381end;
382
383
Jens Geyere9651362014-03-20 22:46:17 +0200384procedure TPipeStreamBase.WriteOverlapped(const buffer: TBytes; offset, count: Integer);
385var cbWritten, dwWait, dwError : DWORD;
386 overlapped : IOverlappedHelper;
387begin
388 if not IsOpen
389 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
390 'Called write on non-open pipe');
391
392 overlapped := TOverlappedHelperImpl.Create;
393
394 if not WriteFile( FPipe, buffer[offset], count, cbWritten, overlapped.OverlappedPtr)
395 then begin
396 dwError := GetLastError;
397 case dwError of
398 ERROR_IO_PENDING : begin
399 dwWait := overlapped.WaitFor(FTimeout);
400
401 if (dwWait = WAIT_TIMEOUT)
402 then raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
403 'Pipe write timed out');
404
405 if (dwWait <> WAIT_OBJECT_0)
406 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbWritten, TRUE)
407 then raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
408 'Pipe write error');
409 end;
410
411 else
412 raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
413 SysErrorMessage(dwError));
414 end;
415 end;
416
417 ASSERT( DWORD(count) = cbWritten);
418end;
419
420
421function TPipeStreamBase.ReadOverlapped( var buffer: TBytes; offset, count: Integer): Integer;
422var cbRead, dwWait, dwError : DWORD;
423 bOk : Boolean;
424 overlapped : IOverlappedHelper;
425begin
426 if not IsOpen
427 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
428 'Called read on non-open pipe');
429
430 overlapped := TOverlappedHelperImpl.Create;
431
432 // read the data
433 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, overlapped.OverlappedPtr);
434 if not bOk then begin
435 dwError := GetLastError;
436 case dwError of
437 ERROR_IO_PENDING : begin
438 dwWait := overlapped.WaitFor(FTimeout);
439
440 if (dwWait = WAIT_TIMEOUT)
441 then raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
442 'Pipe read timed out');
443
444 if (dwWait <> WAIT_OBJECT_0)
445 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbRead, TRUE)
446 then raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
447 'Pipe read error');
448 end;
449
450 else
451 raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
452 SysErrorMessage(dwError));
453 end;
454 end;
455
456 ASSERT( cbRead > 0); // see TTransportImpl.ReadAll()
457 ASSERT( cbRead = DWORD(count));
458 result := cbRead;
459end;
460
461
Jens Geyer06045cf2013-03-27 20:26:25 +0200462function TPipeStreamBase.ToArray: TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000463var bytes : LongInt;
464begin
465 SetLength( result, 0);
466 bytes := 0;
467
468 if IsOpen
469 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
470 and (bytes > 0)
471 then begin
472 SetLength( result, bytes);
473 Read( result, 0, bytes);
474 end;
475end;
476
477
Roger Meier79655fb2012-10-20 20:59:41 +0000478{ TNamedPipeStreamImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000479
480
Jens Geyere9651362014-03-20 22:46:17 +0200481constructor TNamedPipeStreamImpl.Create( const aPipeName : string;
482 const aEnableOverlapped : Boolean;
483 const aShareMode: DWORD;
Roger Meier79655fb2012-10-20 20:59:41 +0000484 const aSecurityAttributes: PSecurityAttributes;
485 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000486begin
Jens Geyere9651362014-03-20 22:46:17 +0200487 inherited Create( aEnableOverlapped, aTimeout);
Roger Meier79655fb2012-10-20 20:59:41 +0000488
489 FPipeName := aPipeName;
490 FShareMode := aShareMode;
491 FSecurityAttribs := aSecurityAttributes;
492
493 if Copy(FPipeName,1,2) <> '\\'
494 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
Roger Meier3bef8c22012-10-06 06:58:00 +0000495end;
496
497
Roger Meier79655fb2012-10-20 20:59:41 +0000498procedure TNamedPipeStreamImpl.Open;
499var hPipe : THandle;
Jens Geyerb89b5b92016-04-19 23:09:41 +0200500 retries, timeout, dwErr : DWORD;
501const INTERVAL = 10; // ms
Roger Meier79655fb2012-10-20 20:59:41 +0000502begin
503 if IsOpen then Exit;
504
Jens Geyerb89b5b92016-04-19 23:09:41 +0200505 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
506 timeout := FTimeOut;
507
508 // if the server hasn't gotten to the point where the pipe has been created, at least wait the timeout
509 // According to MSDN, if no instances of the specified named pipe exist, the WaitNamedPipe function
510 // returns IMMEDIATELY, regardless of the time-out value.
511 while not WaitNamedPipe( PChar(FPipeName), INTERVAL) do begin
512 dwErr := GetLastError;
513 if dwErr <> ERROR_FILE_NOT_FOUND
514 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
515 'Unable to open pipe, '+SysErrorMessage(dwErr));
516
517 if timeout <> INFINITE then begin
518 if (retries > 0)
519 then Dec(retries)
520 else raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
521 'Unable to open pipe, timed out');
522 end;
523
524 Sleep(INTERVAL)
525 end;
526
Roger Meier79655fb2012-10-20 20:59:41 +0000527 // open that thingy
Roger Meier79655fb2012-10-20 20:59:41 +0000528 hPipe := CreateFile( PChar( FPipeName),
529 GENERIC_READ or GENERIC_WRITE,
530 FShareMode, // sharing
531 FSecurityAttribs, // security attributes
532 OPEN_EXISTING, // opens existing pipe
Jens Geyere9651362014-03-20 22:46:17 +0200533 FILE_FLAG_OVERLAPPED or FILE_FLAG_WRITE_THROUGH, // async+fast, please
Roger Meier79655fb2012-10-20 20:59:41 +0000534 0); // no template file
535
536 if hPipe = INVALID_HANDLE_VALUE
537 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
538 'Unable to open pipe, '+SysErrorMessage(GetLastError));
539
Roger Meier79655fb2012-10-20 20:59:41 +0000540 // everything fine
541 FPipe := hPipe;
542end;
543
544
545{ THandlePipeStreamImpl }
546
547
Jens Geyere9651362014-03-20 22:46:17 +0200548constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle;
549 const aOwnsHandle, aEnableOverlapped : Boolean;
550 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000551begin
Jens Geyere9651362014-03-20 22:46:17 +0200552 inherited Create( aEnableOverlapped, aTimeOut);
Roger Meier79655fb2012-10-20 20:59:41 +0000553
554 if aOwnsHandle
555 then FSrcHandle := aPipeHandle
556 else FSrcHandle := DuplicatePipeHandle( aPipeHandle);
557
558 Open;
559end;
560
561
562destructor THandlePipeStreamImpl.Destroy;
563begin
564 try
565 ClosePipeHandle( FSrcHandle);
566 finally
567 inherited Destroy;
568 end;
569end;
570
571
572procedure THandlePipeStreamImpl.Open;
573begin
574 if not IsOpen
575 then FPipe := DuplicatePipeHandle( FSrcHandle);
576end;
577
578
Jens Geyer06045cf2013-03-27 20:26:25 +0200579{ TPipeTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000580
581
Jens Geyer06045cf2013-03-27 20:26:25 +0200582function TPipeTransportBase.GetIsOpen: Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000583begin
Jens Geyer0b20cc82013-03-07 20:47:01 +0100584 result := (FInputStream <> nil) and (FInputStream.IsOpen)
585 and (FOutputStream <> nil) and (FOutputStream.IsOpen);
Roger Meier79655fb2012-10-20 20:59:41 +0000586end;
587
588
Jens Geyer06045cf2013-03-27 20:26:25 +0200589procedure TPipeTransportBase.Open;
Roger Meier79655fb2012-10-20 20:59:41 +0000590begin
591 FInputStream.Open;
592 FOutputStream.Open;
593end;
594
595
Jens Geyer06045cf2013-03-27 20:26:25 +0200596procedure TPipeTransportBase.Close;
Roger Meier79655fb2012-10-20 20:59:41 +0000597begin
598 FInputStream.Close;
599 FOutputStream.Close;
600end;
601
602
Jens Geyer06045cf2013-03-27 20:26:25 +0200603{ TNamedPipeTransportClientEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000604
605
Jens Geyer06045cf2013-03-27 20:26:25 +0200606constructor TNamedPipeTransportClientEndImpl.Create( const aPipeName : string; const aShareMode: DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000607 const aSecurityAttributes: PSecurityAttributes;
608 const aTimeOut : DWORD);
609// Named pipe constructor
610begin
Roger Meier79655fb2012-10-20 20:59:41 +0000611 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200612 FInputStream := TNamedPipeStreamImpl.Create( aPipeName, TRUE, aShareMode, aSecurityAttributes, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000613 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000614end;
615
616
Jens Geyere9651362014-03-20 22:46:17 +0200617constructor TNamedPipeTransportClientEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
618 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000619// Named pipe constructor
620begin
Roger Meier79655fb2012-10-20 20:59:41 +0000621 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200622 FInputStream := THandlePipeStreamImpl.Create( aPipe, TRUE, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000623 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000624end;
625
626
Jens Geyer06045cf2013-03-27 20:26:25 +0200627{ TNamedPipeTransportServerEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000628
629
Jens Geyere9651362014-03-20 22:46:17 +0200630constructor TNamedPipeTransportServerEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
631 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000632// Named pipe constructor
Roger Meier3bef8c22012-10-06 06:58:00 +0000633begin
Roger Meier79655fb2012-10-20 20:59:41 +0000634 FHandle := DuplicatePipeHandle( aPipe);
Jens Geyere9651362014-03-20 22:46:17 +0200635 inherited Create( aPipe, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000636end;
637
638
Jens Geyer06045cf2013-03-27 20:26:25 +0200639procedure TNamedPipeTransportServerEndImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000640begin
Roger Meier79655fb2012-10-20 20:59:41 +0000641 FlushFileBuffers( FHandle);
642 DisconnectNamedPipe( FHandle); // force client off the pipe
643 ClosePipeHandle( FHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000644
Roger Meier79655fb2012-10-20 20:59:41 +0000645 inherited Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000646end;
647
648
Jens Geyer06045cf2013-03-27 20:26:25 +0200649{ TAnonymousPipeTransportImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000650
651
Jens Geyerdd074e72016-04-19 23:31:33 +0200652constructor TAnonymousPipeTransportImpl.Create( const aPipeRead, aPipeWrite : THandle;
653 aOwnsHandles : Boolean;
654 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +0000655// Anonymous pipe constructor
656begin
Roger Meier79655fb2012-10-20 20:59:41 +0000657 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200658 // overlapped is not supported with AnonPipes, see MSDN
Jens Geyerdd074e72016-04-19 23:31:33 +0200659 FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles, FALSE, aTimeOut);
660 FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles, FALSE, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000661end;
662
663
Jens Geyer06045cf2013-03-27 20:26:25 +0200664{ TPipeServerTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000665
666
Jens Geyere9651362014-03-20 22:46:17 +0200667constructor TPipeServerTransportBase.Create;
668begin
669 inherited Create;
670 FStopServer := TEvent.Create(nil,TRUE,FALSE,''); // manual reset
671end;
672
673
674destructor TPipeServerTransportBase.Destroy;
675begin
676 try
677 FreeAndNil( FStopServer);
678 finally
679 inherited Destroy;
680 end;
681end;
682
683
684function TPipeServerTransportBase.QueryStopServer : Boolean;
685begin
686 result := (FStopServer = nil)
687 or (FStopServer.WaitFor(0) <> wrTimeout);
688end;
689
690
Jens Geyer06045cf2013-03-27 20:26:25 +0200691procedure TPipeServerTransportBase.Listen;
Roger Meier3bef8c22012-10-06 06:58:00 +0000692begin
Jens Geyere9651362014-03-20 22:46:17 +0200693 FStopServer.ResetEvent;
Roger Meier3bef8c22012-10-06 06:58:00 +0000694end;
695
696
Jens Geyer06045cf2013-03-27 20:26:25 +0200697procedure TPipeServerTransportBase.Close;
698begin
Jens Geyere9651362014-03-20 22:46:17 +0200699 FStopServer.SetEvent;
Jens Geyer06045cf2013-03-27 20:26:25 +0200700 InternalClose;
701end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000702
703
Jens Geyer06045cf2013-03-27 20:26:25 +0200704{ TAnonymousPipeServerTransportImpl }
705
706
Jens Geyerdd074e72016-04-19 23:31:33 +0200707constructor TAnonymousPipeServerTransportImpl.Create(aBufsize : Cardinal; aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000708// Anonymous pipe CTOR
709begin
710 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000711 FBufsize := aBufSize;
Roger Meier79655fb2012-10-20 20:59:41 +0000712 FReadHandle := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000713 FWriteHandle := INVALID_HANDLE_VALUE;
714 FClientAnonRead := INVALID_HANDLE_VALUE;
715 FClientAnonWrite := INVALID_HANDLE_VALUE;
Jens Geyerdd074e72016-04-19 23:31:33 +0200716 FTimeOut := aTimeOut;
Roger Meier3bef8c22012-10-06 06:58:00 +0000717
718 // The anonymous pipe needs to be created first so that the server can
719 // pass the handles on to the client before the serve (acceptImpl)
720 // blocking call.
721 if not CreateAnonPipe
722 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
723 ClassName+'.Create() failed');
724end;
725
726
Jens Geyer01640402013-09-25 21:12:21 +0200727function TAnonymousPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Roger Meier3bef8c22012-10-06 06:58:00 +0000728var buf : Byte;
729 br : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000730begin
Jens Geyer01640402013-09-25 21:12:21 +0200731 if Assigned(fnAccepting)
732 then fnAccepting();
733
Roger Meier79655fb2012-10-20 20:59:41 +0000734 // This 0-byte read serves merely as a blocking call.
735 if not ReadFile( FReadHandle, buf, 0, br, nil)
736 and (GetLastError() <> ERROR_MORE_DATA)
737 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
738 'TServerPipe unable to initiate pipe communication');
Jens Geyer06045cf2013-03-27 20:26:25 +0200739
740 // create the transport impl
Jens Geyerdd074e72016-04-19 23:31:33 +0200741 result := TAnonymousPipeTransportImpl.Create( FReadHandle, FWriteHandle, FALSE, FTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000742end;
743
744
Jens Geyer06045cf2013-03-27 20:26:25 +0200745procedure TAnonymousPipeServerTransportImpl.InternalClose;
Roger Meier3bef8c22012-10-06 06:58:00 +0000746begin
Roger Meier79655fb2012-10-20 20:59:41 +0000747 ClosePipeHandle( FReadHandle);
748 ClosePipeHandle( FWriteHandle);
749 ClosePipeHandle( FClientAnonRead);
750 ClosePipeHandle( FClientAnonWrite);
Roger Meier3bef8c22012-10-06 06:58:00 +0000751end;
752
753
Jens Geyer06045cf2013-03-27 20:26:25 +0200754function TAnonymousPipeServerTransportImpl.ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000755begin
Roger Meier79655fb2012-10-20 20:59:41 +0000756 result := FReadHandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000757end;
758
759
Jens Geyer06045cf2013-03-27 20:26:25 +0200760function TAnonymousPipeServerTransportImpl.WriteHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000761begin
762 result := FWriteHandle;
763end;
764
765
Jens Geyer06045cf2013-03-27 20:26:25 +0200766function TAnonymousPipeServerTransportImpl.ClientAnonRead : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000767begin
768 result := FClientAnonRead;
769end;
770
771
Jens Geyer06045cf2013-03-27 20:26:25 +0200772function TAnonymousPipeServerTransportImpl.ClientAnonWrite : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000773begin
774 result := FClientAnonWrite;
775end;
776
777
Jens Geyer06045cf2013-03-27 20:26:25 +0200778function TAnonymousPipeServerTransportImpl.CreateAnonPipe : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000779var sd : PSECURITY_DESCRIPTOR;
780 sa : SECURITY_ATTRIBUTES; //TSecurityAttributes;
781 hCAR, hPipeW, hCAW, hPipe : THandle;
782begin
783 result := FALSE;
784
785 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
Jens Geyerb64a7742013-01-23 20:58:47 +0100786 try
787 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
788 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
Roger Meier79655fb2012-10-20 20:59:41 +0000789
Jens Geyerb64a7742013-01-23 20:58:47 +0100790 sa.nLength := sizeof( sa);
791 sa.lpSecurityDescriptor := sd;
792 sa.bInheritHandle := TRUE; //allow passing handle to child
Roger Meier79655fb2012-10-20 20:59:41 +0000793
Jens Geyerb64a7742013-01-23 20:58:47 +0100794 if not CreatePipe( hCAR, hPipeW, @sa, FBufSize) then begin //create stdin pipe
Jens Geyer06045cf2013-03-27 20:26:25 +0200795 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
796 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100797 Exit;
798 end;
799
800 if not CreatePipe( hPipe, hCAW, @sa, FBufSize) then begin //create stdout pipe
Jens Geyerb64a7742013-01-23 20:58:47 +0100801 CloseHandle( hCAR);
802 CloseHandle( hPipeW);
Jens Geyer06045cf2013-03-27 20:26:25 +0200803 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
804 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100805 Exit;
806 end;
807
808 FClientAnonRead := hCAR;
809 FClientAnonWrite := hCAW;
810 FReadHandle := hPipe;
811 FWriteHandle := hPipeW;
812
813 result := TRUE;
Jens Geyerd5436f52014-10-03 19:50:38 +0200814
Jens Geyerb64a7742013-01-23 20:58:47 +0100815 finally
816 if sd <> nil then LocalFree( Cardinal(sd));
Roger Meier79655fb2012-10-20 20:59:41 +0000817 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000818end;
819
820
Jens Geyer06045cf2013-03-27 20:26:25 +0200821{ TNamedPipeServerTransportImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000822
823
Jens Geyer06045cf2013-03-27 20:26:25 +0200824constructor TNamedPipeServerTransportImpl.Create( aPipename : string; aBufsize, aMaxConns, aTimeOut : Cardinal);
Roger Meier79655fb2012-10-20 20:59:41 +0000825// Named Pipe CTOR
826begin
827 inherited Create;
Jens Geyere9651362014-03-20 22:46:17 +0200828 ASSERT( aTimeout > 0);
Jens Geyer06045cf2013-03-27 20:26:25 +0200829 FPipeName := aPipename;
830 FBufsize := aBufSize;
831 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
832 FHandle := INVALID_HANDLE_VALUE;
833 FTimeout := aTimeOut;
834 FConnected := FALSE;
Roger Meier79655fb2012-10-20 20:59:41 +0000835
836 if Copy(FPipeName,1,2) <> '\\'
837 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
838end;
839
840
Jens Geyer01640402013-09-25 21:12:21 +0200841function TNamedPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Jens Geyer06045cf2013-03-27 20:26:25 +0200842var dwError, dwWait, dwDummy : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +0200843 overlapped : IOverlappedHelper;
844 handles : array[0..1] of THandle;
Jens Geyer01640402013-09-25 21:12:21 +0200845begin
Jens Geyere9651362014-03-20 22:46:17 +0200846 overlapped := TOverlappedHelperImpl.Create;
Jens Geyer01640402013-09-25 21:12:21 +0200847
Jens Geyere9651362014-03-20 22:46:17 +0200848 ASSERT( not FConnected);
Jens Geyer2ad6c302015-02-26 19:38:53 +0100849 CreateNamedPipe;
Jens Geyere9651362014-03-20 22:46:17 +0200850 while not FConnected do begin
Jens Geyer2ad6c302015-02-26 19:38:53 +0100851
852 if QueryStopServer
853 then Abort;
Roger Meier79655fb2012-10-20 20:59:41 +0000854
Jens Geyere9651362014-03-20 22:46:17 +0200855 if Assigned(fnAccepting)
856 then fnAccepting();
Jens Geyer01640402013-09-25 21:12:21 +0200857
Jens Geyere9651362014-03-20 22:46:17 +0200858 // Wait for the client to connect; if it succeeds, the
859 // function returns a nonzero value. If the function returns
860 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
861 if ConnectNamedPipe( Handle, overlapped.OverlappedPtr) then begin
862 FConnected := TRUE;
863 Break;
Jens Geyer01640402013-09-25 21:12:21 +0200864 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000865
Jens Geyere9651362014-03-20 22:46:17 +0200866 // ConnectNamedPipe() returns FALSE for OverlappedIO, even if connected.
867 // We have to check GetLastError() explicitly to find out
868 dwError := GetLastError;
869 case dwError of
870 ERROR_PIPE_CONNECTED : begin
871 FConnected := not QueryStopServer; // special case: pipe immediately connected
872 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000873
Jens Geyere9651362014-03-20 22:46:17 +0200874 ERROR_IO_PENDING : begin
875 handles[0] := overlapped.WaitHandle;
876 handles[1] := FStopServer.Handle;
877 dwWait := WaitForMultipleObjects( 2, @handles, FALSE, FTimeout);
878 FConnected := (dwWait = WAIT_OBJECT_0)
879 and GetOverlappedResult( Handle, overlapped.Overlapped, dwDummy, TRUE)
880 and not QueryStopServer;
881 end;
882
883 else
884 InternalClose;
885 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
886 'Client connection failed');
887 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000888 end;
Jens Geyere9651362014-03-20 22:46:17 +0200889
890 // create the transport impl
891 result := CreateTransportInstance;
Roger Meier79655fb2012-10-20 20:59:41 +0000892end;
893
894
Jens Geyer06045cf2013-03-27 20:26:25 +0200895function TNamedPipeServerTransportImpl.CreateTransportInstance : ITransport;
896// create the transport impl
897var hPipe : THandle;
Roger Meier79655fb2012-10-20 20:59:41 +0000898begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200899 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
900 try
901 FConnected := FALSE;
Jens Geyere9651362014-03-20 22:46:17 +0200902 result := TNamedPipeTransportServerEndImpl.Create( hPipe, TRUE, FTimeout);
Jens Geyer06045cf2013-03-27 20:26:25 +0200903 except
Jens Geyer01640402013-09-25 21:12:21 +0200904 ClosePipeHandle(hPipe);
905 raise;
906 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000907end;
908
909
Jens Geyer06045cf2013-03-27 20:26:25 +0200910procedure TNamedPipeServerTransportImpl.InternalClose;
911var hPipe : THandle;
912begin
913 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
914 if hPipe = INVALID_HANDLE_VALUE then Exit;
915
916 try
917 if FConnected
918 then FlushFileBuffers( hPipe)
919 else CancelIo( hPipe);
920 DisconnectNamedPipe( hPipe);
921 finally
922 ClosePipeHandle( hPipe);
923 FConnected := FALSE;
924 end;
925end;
926
927
928function TNamedPipeServerTransportImpl.Handle : THandle;
929begin
930 {$IFDEF WIN64}
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200931 result := THandle( InterlockedExchangeAdd64( Int64(FHandle), 0));
Jens Geyer06045cf2013-03-27 20:26:25 +0200932 {$ELSE}
933 result := THandle( InterlockedExchangeAdd( Integer(FHandle), 0));
934 {$ENDIF}
935end;
936
937
938function TNamedPipeServerTransportImpl.CreateNamedPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000939var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
940 everyone_sid : PSID;
941 ea : EXPLICIT_ACCESS;
942 acl : PACL;
943 sd : PSECURITY_DESCRIPTOR;
944 sa : SECURITY_ATTRIBUTES;
Roger Meier3bef8c22012-10-06 06:58:00 +0000945const
946 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
947 SECURITY_WORLD_RID = $00000000;
948begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100949 sd := nil;
Roger Meier3bef8c22012-10-06 06:58:00 +0000950 everyone_sid := nil;
Jens Geyerb64a7742013-01-23 20:58:47 +0100951 try
Jens Geyer06045cf2013-03-27 20:26:25 +0200952 ASSERT( (FHandle = INVALID_HANDLE_VALUE) and not FConnected);
953
Jens Geyerb64a7742013-01-23 20:58:47 +0100954 // Windows - set security to allow non-elevated apps
955 // to access pipes created by elevated apps.
956 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
957 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000958
Jens Geyerb64a7742013-01-23 20:58:47 +0100959 ZeroMemory( @ea, SizeOf(ea));
960 ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
961 ea.grfAccessMode := SET_ACCESS;
962 ea.grfInheritance := NO_INHERITANCE;
963 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
964 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
965 ea.Trustee.ptstrName := PChar(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000966
Jens Geyerb64a7742013-01-23 20:58:47 +0100967 acl := nil;
968 SetEntriesInAcl( 1, @ea, nil, acl);
Roger Meier3bef8c22012-10-06 06:58:00 +0000969
Jens Geyerb64a7742013-01-23 20:58:47 +0100970 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
971 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
972 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
Roger Meier3bef8c22012-10-06 06:58:00 +0000973
Jens Geyerb64a7742013-01-23 20:58:47 +0100974 sa.nLength := SizeOf(sa);
975 sa.lpSecurityDescriptor := sd;
976 sa.bInheritHandle := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000977
Jens Geyerb64a7742013-01-23 20:58:47 +0100978 // Create an instance of the named pipe
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200979 {$IFDEF OLD_UNIT_NAMES}
980 result := Windows.CreateNamedPipe(
981 {$ELSE}
982 result := Winapi.Windows.CreateNamedPipe(
983 {$ENDIF}
984 PChar( FPipeName), // pipe name
985 PIPE_ACCESS_DUPLEX or // read/write access
986 FILE_FLAG_OVERLAPPED, // async mode
987 PIPE_TYPE_BYTE or // byte type pipe
988 PIPE_READMODE_BYTE, // byte read mode
989 FMaxConns, // max. instances
990 FBufSize, // output buffer size
991 FBufSize, // input buffer size
992 FTimeout, // time-out, see MSDN
993 @sa // default security attribute
994 );
Roger Meier3bef8c22012-10-06 06:58:00 +0000995
Jens Geyer06045cf2013-03-27 20:26:25 +0200996 if( result <> INVALID_HANDLE_VALUE)
997 then InterlockedExchangePointer( Pointer(FHandle), Pointer(result))
998 else raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
Jens Geyerb64a7742013-01-23 20:58:47 +0100999 'CreateNamedPipe() failed ' + IntToStr(GetLastError));
1000
1001 finally
1002 if sd <> nil then LocalFree( Cardinal( sd));
1003 if acl <> nil then LocalFree( Cardinal( acl));
1004 if everyone_sid <> nil then FreeSid(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +00001005 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001006end;
1007
1008
Roger Meier3bef8c22012-10-06 06:58:00 +00001009
1010end.
1011
1012
1013