blob: d4f99aba476eb87186caa93116367798d239ec89 [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 Geyer653f0de2016-04-20 12:46:57 +020037 DEFAULT_THRIFT_PIPE_OPEN_TIMEOUT = 10; // default: fail fast on open
Roger Meier3bef8c22012-10-06 06:58:00 +000038
Jens Geyere9651362014-03-20 22:46:17 +020039
Roger Meier3bef8c22012-10-06 06:58:00 +000040type
Roger Meier79655fb2012-10-20 20:59:41 +000041 //--- Pipe Streams ---
Roger Meier3bef8c22012-10-06 06:58:00 +000042
43
Jens Geyer06045cf2013-03-27 20:26:25 +020044 TPipeStreamBase = class( TThriftStreamImpl)
Roger Meier79655fb2012-10-20 20:59:41 +000045 strict protected
46 FPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +000047 FTimeout : DWORD;
Jens Geyer653f0de2016-04-20 12:46:57 +020048 FOpenTimeOut : DWORD; // separate value to allow for fail-fast-on-open scenarios
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 Geyer653f0de2016-04-20 12:46:57 +020065 constructor Create( aEnableOverlapped : Boolean;
66 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT;
67 const aOpenTimeOut : DWORD = DEFAULT_THRIFT_PIPE_OPEN_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +000068 destructor Destroy; override;
69 end;
70
71
Jens Geyer06045cf2013-03-27 20:26:25 +020072 TNamedPipeStreamImpl = class sealed( TPipeStreamBase)
Jens Geyere9651362014-03-20 22:46:17 +020073 strict private
Roger Meier79655fb2012-10-20 20:59:41 +000074 FPipeName : string;
75 FShareMode : DWORD;
76 FSecurityAttribs : PSecurityAttributes;
Roger Meier3bef8c22012-10-06 06:58:00 +000077
Jens Geyere9651362014-03-20 22:46:17 +020078 strict protected
Roger Meier79655fb2012-10-20 20:59:41 +000079 procedure Open; override;
80
81 public
82 constructor Create( const aPipeName : string;
Jens Geyere9651362014-03-20 22:46:17 +020083 const aEnableOverlapped : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +000084 const aShareMode: DWORD = 0;
85 const aSecurityAttributes: PSecurityAttributes = nil;
Jens Geyer653f0de2016-04-20 12:46:57 +020086 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT;
87 const aOpenTimeOut : DWORD = DEFAULT_THRIFT_PIPE_OPEN_TIMEOUT); overload;
Roger Meier79655fb2012-10-20 20:59:41 +000088 end;
89
90
Jens Geyer06045cf2013-03-27 20:26:25 +020091 THandlePipeStreamImpl = class sealed( TPipeStreamBase)
Jens Geyere9651362014-03-20 22:46:17 +020092 strict private
Roger Meier79655fb2012-10-20 20:59:41 +000093 FSrcHandle : THandle;
94
Jens Geyere9651362014-03-20 22:46:17 +020095 strict protected
Roger Meier79655fb2012-10-20 20:59:41 +000096 procedure Open; override;
97
98 public
Jens Geyere9651362014-03-20 22:46:17 +020099 constructor Create( const aPipeHandle : THandle;
100 const aOwnsHandle, aEnableOverlapped : Boolean;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200101 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload;
Roger Meier79655fb2012-10-20 20:59:41 +0000102 destructor Destroy; override;
103 end;
104
105
106 //--- Pipe Transports ---
107
108
Jens Geyer06045cf2013-03-27 20:26:25 +0200109 IPipeTransport = interface( IStreamTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000110 ['{5E05CC85-434F-428F-BFB2-856A168B5558}']
111 end;
112
113
Jens Geyer06045cf2013-03-27 20:26:25 +0200114 TPipeTransportBase = class( TStreamTransportImpl, IPipeTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000115 public
116 // ITransport
117 function GetIsOpen: Boolean; override;
118 procedure Open; override;
119 procedure Close; override;
120 end;
121
122
Jens Geyer06045cf2013-03-27 20:26:25 +0200123 TNamedPipeTransportClientEndImpl = class( TPipeTransportBase)
Roger Meier79655fb2012-10-20 20:59:41 +0000124 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000125 // Named pipe constructors
Jens Geyere9651362014-03-20 22:46:17 +0200126 constructor Create( aPipe : THandle; aOwnsHandle : Boolean;
127 const aTimeOut : DWORD); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000128 constructor Create( const aPipeName : string;
129 const aShareMode: DWORD = 0;
130 const aSecurityAttributes: PSecurityAttributes = nil;
Jens Geyer653f0de2016-04-20 12:46:57 +0200131 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT;
132 const aOpenTimeOut : DWORD = DEFAULT_THRIFT_PIPE_OPEN_TIMEOUT); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000133 end;
134
135
Jens Geyer06045cf2013-03-27 20:26:25 +0200136 TNamedPipeTransportServerEndImpl = class( TNamedPipeTransportClientEndImpl)
Roger Meier79655fb2012-10-20 20:59:41 +0000137 strict private
138 FHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000139 public
Roger Meier79655fb2012-10-20 20:59:41 +0000140 // ITransport
141 procedure Close; override;
Jens Geyere9651362014-03-20 22:46:17 +0200142 constructor Create( aPipe : THandle; aOwnsHandle : Boolean;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200143 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); reintroduce;
Roger Meier79655fb2012-10-20 20:59:41 +0000144 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000145
Roger Meier79655fb2012-10-20 20:59:41 +0000146
Jens Geyer06045cf2013-03-27 20:26:25 +0200147 TAnonymousPipeTransportImpl = class( TPipeTransportBase)
Roger Meier79655fb2012-10-20 20:59:41 +0000148 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000149 // Anonymous pipe constructor
Jens Geyerdd074e72016-04-19 23:31:33 +0200150 constructor Create(const aPipeRead, aPipeWrite : THandle;
151 aOwnsHandles : Boolean;
152 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000153 end;
154
155
Roger Meier79655fb2012-10-20 20:59:41 +0000156 //--- Server Transports ---
157
158
Jens Geyer06045cf2013-03-27 20:26:25 +0200159 IAnonymousPipeServerTransport = interface( IServerTransport)
Roger Meier3bef8c22012-10-06 06:58:00 +0000160 ['{7AEE6793-47B9-4E49-981A-C39E9108E9AD}']
161 // Server side anonymous pipe ends
Roger Meier79655fb2012-10-20 20:59:41 +0000162 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000163 function WriteHandle : THandle;
164 // Client side anonymous pipe ends
165 function ClientAnonRead : THandle;
166 function ClientAnonWrite : THandle;
167 end;
168
169
Jens Geyer06045cf2013-03-27 20:26:25 +0200170 INamedPipeServerTransport = interface( IServerTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000171 ['{9DF9EE48-D065-40AF-8F67-D33037D3D960}']
172 function Handle : THandle;
173 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000174
Roger Meier79655fb2012-10-20 20:59:41 +0000175
Jens Geyer06045cf2013-03-27 20:26:25 +0200176 TPipeServerTransportBase = class( TServerTransportImpl)
Jens Geyere9651362014-03-20 22:46:17 +0200177 strict protected
178 FStopServer : TEvent;
Jens Geyer06045cf2013-03-27 20:26:25 +0200179 procedure InternalClose; virtual; abstract;
Jens Geyere9651362014-03-20 22:46:17 +0200180 function QueryStopServer : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000181 public
Jens Geyere9651362014-03-20 22:46:17 +0200182 constructor Create;
183 destructor Destroy; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000184 procedure Listen; override;
Jens Geyer06045cf2013-03-27 20:26:25 +0200185 procedure Close; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000186 end;
187
188
Jens Geyer06045cf2013-03-27 20:26:25 +0200189 TAnonymousPipeServerTransportImpl = class( TPipeServerTransportBase, IAnonymousPipeServerTransport)
Jens Geyere9651362014-03-20 22:46:17 +0200190 strict private
Roger Meier79655fb2012-10-20 20:59:41 +0000191 FBufSize : DWORD;
192
193 // Server side anonymous pipe handles
194 FReadHandle,
Roger Meier3bef8c22012-10-06 06:58:00 +0000195 FWriteHandle : THandle;
196
197 //Client side anonymous pipe handles
198 FClientAnonRead,
199 FClientAnonWrite : THandle;
200
Jens Geyerdd074e72016-04-19 23:31:33 +0200201 FTimeOut: DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000202 protected
Jens Geyer01640402013-09-25 21:12:21 +0200203 function Accept(const fnAccepting: TProc): ITransport; override;
Roger Meier3bef8c22012-10-06 06:58:00 +0000204
Roger Meier3bef8c22012-10-06 06:58:00 +0000205 function CreateAnonPipe : Boolean;
206
Jens Geyer06045cf2013-03-27 20:26:25 +0200207 // IAnonymousPipeServerTransport
Roger Meier79655fb2012-10-20 20:59:41 +0000208 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000209 function WriteHandle : THandle;
210 function ClientAnonRead : THandle;
211 function ClientAnonWrite : THandle;
212
Jens Geyer06045cf2013-03-27 20:26:25 +0200213 procedure InternalClose; override;
214
Roger Meier3bef8c22012-10-06 06:58:00 +0000215 public
Jens Geyerdd074e72016-04-19 23:31:33 +0200216 constructor Create(aBufsize : Cardinal = 4096; aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +0000217 end;
218
219
Jens Geyer06045cf2013-03-27 20:26:25 +0200220 TNamedPipeServerTransportImpl = class( TPipeServerTransportBase, INamedPipeServerTransport)
Jens Geyere9651362014-03-20 22:46:17 +0200221 strict private
Roger Meier79655fb2012-10-20 20:59:41 +0000222 FPipeName : string;
223 FMaxConns : DWORD;
224 FBufSize : DWORD;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100225 FTimeout : DWORD;
Jens Geyer06045cf2013-03-27 20:26:25 +0200226 FHandle : THandle;
227 FConnected : Boolean;
Jens Geyer01640402013-09-25 21:12:21 +0200228
229
Jens Geyere9651362014-03-20 22:46:17 +0200230 strict protected
Jens Geyer01640402013-09-25 21:12:21 +0200231 function Accept(const fnAccepting: TProc): ITransport; override;
Jens Geyer06045cf2013-03-27 20:26:25 +0200232 function CreateNamedPipe : THandle;
233 function CreateTransportInstance : ITransport;
Roger Meier79655fb2012-10-20 20:59:41 +0000234
Jens Geyer06045cf2013-03-27 20:26:25 +0200235 // INamedPipeServerTransport
Roger Meier79655fb2012-10-20 20:59:41 +0000236 function Handle : THandle;
Jens Geyer06045cf2013-03-27 20:26:25 +0200237 procedure InternalClose; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000238
239 public
240 constructor Create( aPipename : string; aBufsize : Cardinal = 4096;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100241 aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES;
Jens Geyer2ad6c302015-02-26 19:38:53 +0100242 aTimeOut : Cardinal = INFINITE);
Roger Meier79655fb2012-10-20 20:59:41 +0000243 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000244
245
246implementation
247
248
Roger Meier79655fb2012-10-20 20:59:41 +0000249procedure ClosePipeHandle( var hPipe : THandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000250begin
Roger Meier79655fb2012-10-20 20:59:41 +0000251 if hPipe <> INVALID_HANDLE_VALUE
252 then try
253 CloseHandle( hPipe);
254 finally
255 hPipe := INVALID_HANDLE_VALUE;
256 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000257end;
258
259
Roger Meier79655fb2012-10-20 20:59:41 +0000260function DuplicatePipeHandle( const hSource : THandle) : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000261begin
Roger Meier79655fb2012-10-20 20:59:41 +0000262 if not DuplicateHandle( GetCurrentProcess, hSource,
263 GetCurrentProcess, @result,
264 0, FALSE, DUPLICATE_SAME_ACCESS)
Jens Geyere0e32402016-04-20 21:50:48 +0200265 then raise TTransportExceptionNotOpen.Create('DuplicateHandle: '+SysErrorMessage(GetLastError));
Roger Meier3bef8c22012-10-06 06:58:00 +0000266end;
267
268
Roger Meier79655fb2012-10-20 20:59:41 +0000269
Jens Geyer06045cf2013-03-27 20:26:25 +0200270{ TPipeStreamBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000271
272
Jens Geyere9651362014-03-20 22:46:17 +0200273constructor TPipeStreamBase.Create( aEnableOverlapped : Boolean;
Jens Geyer653f0de2016-04-20 12:46:57 +0200274 const aTimeOut, aOpenTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000275begin
276 inherited Create;
Jens Geyer653f0de2016-04-20 12:46:57 +0200277 ASSERT( aTimeout > 0); // aOpenTimeout may be 0
278 FPipe := INVALID_HANDLE_VALUE;
279 FTimeout := aTimeOut;
280 FOpenTimeOut := aOpenTimeOut;
281 FOverlapped := aEnableOverlapped;
Roger Meier79655fb2012-10-20 20:59:41 +0000282end;
283
284
Jens Geyer06045cf2013-03-27 20:26:25 +0200285destructor TPipeStreamBase.Destroy;
Roger Meier3bef8c22012-10-06 06:58:00 +0000286begin
287 try
288 Close;
289 finally
290 inherited Destroy;
291 end;
292end;
293
294
Jens Geyer06045cf2013-03-27 20:26:25 +0200295procedure TPipeStreamBase.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000296begin
Roger Meier79655fb2012-10-20 20:59:41 +0000297 ClosePipeHandle( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000298end;
299
300
Jens Geyer06045cf2013-03-27 20:26:25 +0200301procedure TPipeStreamBase.Flush;
Roger Meier3bef8c22012-10-06 06:58:00 +0000302begin
Jens Geyer0d227b12015-12-02 19:50:55 +0100303 FlushFileBuffers( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000304end;
305
306
Jens Geyer06045cf2013-03-27 20:26:25 +0200307function TPipeStreamBase.IsOpen: Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000308begin
309 result := (FPipe <> INVALID_HANDLE_VALUE);
310end;
311
312
Jens Geyer06045cf2013-03-27 20:26:25 +0200313procedure TPipeStreamBase.Write(const buffer: TBytes; offset, count: Integer);
Jens Geyere9651362014-03-20 22:46:17 +0200314begin
315 if FOverlapped
316 then WriteOverlapped( buffer, offset, count)
317 else WriteDirect( buffer, offset, count);
318end;
319
320
321function TPipeStreamBase.Read( var buffer: TBytes; offset, count: Integer): Integer;
322begin
323 if FOverlapped
324 then result := ReadOverlapped( buffer, offset, count)
325 else result := ReadDirect( buffer, offset, count);
326end;
327
328
329procedure TPipeStreamBase.WriteDirect(const buffer: TBytes; offset, count: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000330var cbWritten : DWORD;
331begin
332 if not IsOpen
Jens Geyere0e32402016-04-20 21:50:48 +0200333 then raise TTransportExceptionNotOpen.Create('Called write on non-open pipe');
Roger Meier3bef8c22012-10-06 06:58:00 +0000334
335 if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil)
Jens Geyere0e32402016-04-20 21:50:48 +0200336 then raise TTransportExceptionNotOpen.Create('Write to pipe failed');
Roger Meier3bef8c22012-10-06 06:58:00 +0000337end;
338
339
Jens Geyere9651362014-03-20 22:46:17 +0200340function TPipeStreamBase.ReadDirect( var buffer: TBytes; offset, count: Integer): Integer;
Roger Meier79655fb2012-10-20 20:59:41 +0000341var cbRead, dwErr : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000342 bytes, retries : LongInt;
343 bOk : Boolean;
344const INTERVAL = 10; // ms
345begin
346 if not IsOpen
Jens Geyere0e32402016-04-20 21:50:48 +0200347 then raise TTransportExceptionNotOpen.Create('Called read on non-open pipe');
Roger Meier3bef8c22012-10-06 06:58:00 +0000348
349 // MSDN: Handle can be a handle to a named pipe instance,
350 // or it can be a handle to the read end of an anonymous pipe,
351 // The handle must have GENERIC_READ access to the pipe.
352 if FTimeOut <> INFINITE then begin
353 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
354 while TRUE do begin
Jens Geyer5988f482016-04-19 23:01:24 +0200355 if not PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) then begin
356 dwErr := GetLastError;
357 if (dwErr = ERROR_INVALID_HANDLE)
358 or (dwErr = ERROR_BROKEN_PIPE)
359 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
360 then begin
361 result := 0; // other side closed the pipe
362 Exit;
363 end;
364 end
365 else if bytes > 0 then begin
366 Break; // there are data
Roger Meier79655fb2012-10-20 20:59:41 +0000367 end;
368
Roger Meier3bef8c22012-10-06 06:58:00 +0000369 Dec( retries);
370 if retries > 0
371 then Sleep( INTERVAL)
Jens Geyere0e32402016-04-20 21:50:48 +0200372 else raise TTransportExceptionTimedOut.Create('Pipe read timed out');
Roger Meier3bef8c22012-10-06 06:58:00 +0000373 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
Jens Geyere0e32402016-04-20 21:50:48 +0200389 then raise TTransportExceptionNotOpen.Create('Called write on non-open pipe');
Jens Geyere9651362014-03-20 22:46:17 +0200390
391 overlapped := TOverlappedHelperImpl.Create;
392
393 if not WriteFile( FPipe, buffer[offset], count, cbWritten, overlapped.OverlappedPtr)
394 then begin
395 dwError := GetLastError;
396 case dwError of
397 ERROR_IO_PENDING : begin
398 dwWait := overlapped.WaitFor(FTimeout);
399
400 if (dwWait = WAIT_TIMEOUT)
Jens Geyere0e32402016-04-20 21:50:48 +0200401 then raise TTransportExceptionTimedOut.Create('Pipe write timed out');
Jens Geyere9651362014-03-20 22:46:17 +0200402
403 if (dwWait <> WAIT_OBJECT_0)
404 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbWritten, TRUE)
Jens Geyere0e32402016-04-20 21:50:48 +0200405 then raise TTransportExceptionUnknown.Create('Pipe write error');
Jens Geyere9651362014-03-20 22:46:17 +0200406 end;
407
408 else
Jens Geyere0e32402016-04-20 21:50:48 +0200409 raise TTransportExceptionUnknown.Create(SysErrorMessage(dwError));
Jens Geyere9651362014-03-20 22:46:17 +0200410 end;
411 end;
412
413 ASSERT( DWORD(count) = cbWritten);
414end;
415
416
417function TPipeStreamBase.ReadOverlapped( var buffer: TBytes; offset, count: Integer): Integer;
418var cbRead, dwWait, dwError : DWORD;
419 bOk : Boolean;
420 overlapped : IOverlappedHelper;
421begin
422 if not IsOpen
Jens Geyere0e32402016-04-20 21:50:48 +0200423 then raise TTransportExceptionNotOpen.Create('Called read on non-open pipe');
Jens Geyere9651362014-03-20 22:46:17 +0200424
425 overlapped := TOverlappedHelperImpl.Create;
426
427 // read the data
428 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, overlapped.OverlappedPtr);
429 if not bOk then begin
430 dwError := GetLastError;
431 case dwError of
432 ERROR_IO_PENDING : begin
433 dwWait := overlapped.WaitFor(FTimeout);
434
435 if (dwWait = WAIT_TIMEOUT)
Jens Geyere0e32402016-04-20 21:50:48 +0200436 then raise TTransportExceptionTimedOut.Create('Pipe read timed out');
Jens Geyere9651362014-03-20 22:46:17 +0200437
438 if (dwWait <> WAIT_OBJECT_0)
439 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbRead, TRUE)
Jens Geyere0e32402016-04-20 21:50:48 +0200440 then raise TTransportExceptionUnknown.Create('Pipe read error');
Jens Geyere9651362014-03-20 22:46:17 +0200441 end;
442
443 else
Jens Geyere0e32402016-04-20 21:50:48 +0200444 raise TTransportExceptionUnknown.Create(SysErrorMessage(dwError));
Jens Geyere9651362014-03-20 22:46:17 +0200445 end;
446 end;
447
448 ASSERT( cbRead > 0); // see TTransportImpl.ReadAll()
449 ASSERT( cbRead = DWORD(count));
450 result := cbRead;
451end;
452
453
Jens Geyer06045cf2013-03-27 20:26:25 +0200454function TPipeStreamBase.ToArray: TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000455var bytes : LongInt;
456begin
457 SetLength( result, 0);
458 bytes := 0;
459
460 if IsOpen
461 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
462 and (bytes > 0)
463 then begin
464 SetLength( result, bytes);
465 Read( result, 0, bytes);
466 end;
467end;
468
469
Roger Meier79655fb2012-10-20 20:59:41 +0000470{ TNamedPipeStreamImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000471
472
Jens Geyere9651362014-03-20 22:46:17 +0200473constructor TNamedPipeStreamImpl.Create( const aPipeName : string;
474 const aEnableOverlapped : Boolean;
475 const aShareMode: DWORD;
Roger Meier79655fb2012-10-20 20:59:41 +0000476 const aSecurityAttributes: PSecurityAttributes;
Jens Geyer653f0de2016-04-20 12:46:57 +0200477 const aTimeOut, aOpenTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000478begin
Jens Geyer653f0de2016-04-20 12:46:57 +0200479 inherited Create( aEnableOverlapped, aTimeout, aOpenTimeOut);
Roger Meier79655fb2012-10-20 20:59:41 +0000480
481 FPipeName := aPipeName;
482 FShareMode := aShareMode;
483 FSecurityAttribs := aSecurityAttributes;
484
485 if Copy(FPipeName,1,2) <> '\\'
486 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
Roger Meier3bef8c22012-10-06 06:58:00 +0000487end;
488
489
Roger Meier79655fb2012-10-20 20:59:41 +0000490procedure TNamedPipeStreamImpl.Open;
491var hPipe : THandle;
Jens Geyerb89b5b92016-04-19 23:09:41 +0200492 retries, timeout, dwErr : DWORD;
493const INTERVAL = 10; // ms
Roger Meier79655fb2012-10-20 20:59:41 +0000494begin
495 if IsOpen then Exit;
496
Jens Geyer653f0de2016-04-20 12:46:57 +0200497 retries := Max( 1, Round( 1.0 * FOpenTimeOut / INTERVAL));
498 timeout := FOpenTimeOut;
Jens Geyerb89b5b92016-04-19 23:09:41 +0200499
500 // if the server hasn't gotten to the point where the pipe has been created, at least wait the timeout
501 // According to MSDN, if no instances of the specified named pipe exist, the WaitNamedPipe function
502 // returns IMMEDIATELY, regardless of the time-out value.
Jens Geyer653f0de2016-04-20 12:46:57 +0200503 // Always use INTERVAL, since WaitNamedPipe(0) defaults to some other value
Jens Geyerb89b5b92016-04-19 23:09:41 +0200504 while not WaitNamedPipe( PChar(FPipeName), INTERVAL) do begin
505 dwErr := GetLastError;
506 if dwErr <> ERROR_FILE_NOT_FOUND
Jens Geyere0e32402016-04-20 21:50:48 +0200507 then raise TTransportExceptionNotOpen.Create('Unable to open pipe, '+SysErrorMessage(dwErr));
Jens Geyerb89b5b92016-04-19 23:09:41 +0200508
509 if timeout <> INFINITE then begin
510 if (retries > 0)
511 then Dec(retries)
Jens Geyere0e32402016-04-20 21:50:48 +0200512 else raise TTransportExceptionNotOpen.Create('Unable to open pipe, timed out');
Jens Geyerb89b5b92016-04-19 23:09:41 +0200513 end;
514
515 Sleep(INTERVAL)
516 end;
517
Roger Meier79655fb2012-10-20 20:59:41 +0000518 // open that thingy
Roger Meier79655fb2012-10-20 20:59:41 +0000519 hPipe := CreateFile( PChar( FPipeName),
520 GENERIC_READ or GENERIC_WRITE,
521 FShareMode, // sharing
522 FSecurityAttribs, // security attributes
523 OPEN_EXISTING, // opens existing pipe
Jens Geyere9651362014-03-20 22:46:17 +0200524 FILE_FLAG_OVERLAPPED or FILE_FLAG_WRITE_THROUGH, // async+fast, please
Roger Meier79655fb2012-10-20 20:59:41 +0000525 0); // no template file
526
527 if hPipe = INVALID_HANDLE_VALUE
Jens Geyere0e32402016-04-20 21:50:48 +0200528 then raise TTransportExceptionNotOpen.Create('Unable to open pipe, '+SysErrorMessage(GetLastError));
Roger Meier79655fb2012-10-20 20:59:41 +0000529
Roger Meier79655fb2012-10-20 20:59:41 +0000530 // everything fine
531 FPipe := hPipe;
532end;
533
534
535{ THandlePipeStreamImpl }
536
537
Jens Geyere9651362014-03-20 22:46:17 +0200538constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle;
539 const aOwnsHandle, aEnableOverlapped : Boolean;
540 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000541begin
Jens Geyere9651362014-03-20 22:46:17 +0200542 inherited Create( aEnableOverlapped, aTimeOut);
Roger Meier79655fb2012-10-20 20:59:41 +0000543
544 if aOwnsHandle
545 then FSrcHandle := aPipeHandle
546 else FSrcHandle := DuplicatePipeHandle( aPipeHandle);
547
548 Open;
549end;
550
551
552destructor THandlePipeStreamImpl.Destroy;
553begin
554 try
555 ClosePipeHandle( FSrcHandle);
556 finally
557 inherited Destroy;
558 end;
559end;
560
561
562procedure THandlePipeStreamImpl.Open;
563begin
564 if not IsOpen
565 then FPipe := DuplicatePipeHandle( FSrcHandle);
566end;
567
568
Jens Geyer06045cf2013-03-27 20:26:25 +0200569{ TPipeTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000570
571
Jens Geyer06045cf2013-03-27 20:26:25 +0200572function TPipeTransportBase.GetIsOpen: Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000573begin
Jens Geyer0b20cc82013-03-07 20:47:01 +0100574 result := (FInputStream <> nil) and (FInputStream.IsOpen)
575 and (FOutputStream <> nil) and (FOutputStream.IsOpen);
Roger Meier79655fb2012-10-20 20:59:41 +0000576end;
577
578
Jens Geyer06045cf2013-03-27 20:26:25 +0200579procedure TPipeTransportBase.Open;
Roger Meier79655fb2012-10-20 20:59:41 +0000580begin
581 FInputStream.Open;
582 FOutputStream.Open;
583end;
584
585
Jens Geyer06045cf2013-03-27 20:26:25 +0200586procedure TPipeTransportBase.Close;
Roger Meier79655fb2012-10-20 20:59:41 +0000587begin
588 FInputStream.Close;
589 FOutputStream.Close;
590end;
591
592
Jens Geyer06045cf2013-03-27 20:26:25 +0200593{ TNamedPipeTransportClientEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000594
595
Jens Geyer06045cf2013-03-27 20:26:25 +0200596constructor TNamedPipeTransportClientEndImpl.Create( const aPipeName : string; const aShareMode: DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000597 const aSecurityAttributes: PSecurityAttributes;
Jens Geyer653f0de2016-04-20 12:46:57 +0200598 const aTimeOut, aOpenTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000599// Named pipe constructor
600begin
Roger Meier79655fb2012-10-20 20:59:41 +0000601 inherited Create( nil, nil);
Jens Geyer653f0de2016-04-20 12:46:57 +0200602 FInputStream := TNamedPipeStreamImpl.Create( aPipeName, TRUE, aShareMode, aSecurityAttributes, aTimeOut, aOpenTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000603 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000604end;
605
606
Jens Geyere9651362014-03-20 22:46:17 +0200607constructor TNamedPipeTransportClientEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
608 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000609// 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 := THandlePipeStreamImpl.Create( aPipe, TRUE, aOwnsHandle, 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 Geyer06045cf2013-03-27 20:26:25 +0200617{ TNamedPipeTransportServerEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000618
619
Jens Geyere9651362014-03-20 22:46:17 +0200620constructor TNamedPipeTransportServerEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
621 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000622// Named pipe constructor
Roger Meier3bef8c22012-10-06 06:58:00 +0000623begin
Roger Meier79655fb2012-10-20 20:59:41 +0000624 FHandle := DuplicatePipeHandle( aPipe);
Jens Geyere9651362014-03-20 22:46:17 +0200625 inherited Create( aPipe, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000626end;
627
628
Jens Geyer06045cf2013-03-27 20:26:25 +0200629procedure TNamedPipeTransportServerEndImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000630begin
Roger Meier79655fb2012-10-20 20:59:41 +0000631 FlushFileBuffers( FHandle);
632 DisconnectNamedPipe( FHandle); // force client off the pipe
633 ClosePipeHandle( FHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000634
Roger Meier79655fb2012-10-20 20:59:41 +0000635 inherited Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000636end;
637
638
Jens Geyer06045cf2013-03-27 20:26:25 +0200639{ TAnonymousPipeTransportImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000640
641
Jens Geyerdd074e72016-04-19 23:31:33 +0200642constructor TAnonymousPipeTransportImpl.Create( const aPipeRead, aPipeWrite : THandle;
643 aOwnsHandles : Boolean;
644 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +0000645// Anonymous pipe constructor
646begin
Roger Meier79655fb2012-10-20 20:59:41 +0000647 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200648 // overlapped is not supported with AnonPipes, see MSDN
Jens Geyerdd074e72016-04-19 23:31:33 +0200649 FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles, FALSE, aTimeOut);
650 FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles, FALSE, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000651end;
652
653
Jens Geyer06045cf2013-03-27 20:26:25 +0200654{ TPipeServerTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000655
656
Jens Geyere9651362014-03-20 22:46:17 +0200657constructor TPipeServerTransportBase.Create;
658begin
659 inherited Create;
660 FStopServer := TEvent.Create(nil,TRUE,FALSE,''); // manual reset
661end;
662
663
664destructor TPipeServerTransportBase.Destroy;
665begin
666 try
667 FreeAndNil( FStopServer);
668 finally
669 inherited Destroy;
670 end;
671end;
672
673
674function TPipeServerTransportBase.QueryStopServer : Boolean;
675begin
676 result := (FStopServer = nil)
677 or (FStopServer.WaitFor(0) <> wrTimeout);
678end;
679
680
Jens Geyer06045cf2013-03-27 20:26:25 +0200681procedure TPipeServerTransportBase.Listen;
Roger Meier3bef8c22012-10-06 06:58:00 +0000682begin
Jens Geyere9651362014-03-20 22:46:17 +0200683 FStopServer.ResetEvent;
Roger Meier3bef8c22012-10-06 06:58:00 +0000684end;
685
686
Jens Geyer06045cf2013-03-27 20:26:25 +0200687procedure TPipeServerTransportBase.Close;
688begin
Jens Geyere9651362014-03-20 22:46:17 +0200689 FStopServer.SetEvent;
Jens Geyer06045cf2013-03-27 20:26:25 +0200690 InternalClose;
691end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000692
693
Jens Geyer06045cf2013-03-27 20:26:25 +0200694{ TAnonymousPipeServerTransportImpl }
695
696
Jens Geyerdd074e72016-04-19 23:31:33 +0200697constructor TAnonymousPipeServerTransportImpl.Create(aBufsize : Cardinal; aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000698// Anonymous pipe CTOR
699begin
700 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000701 FBufsize := aBufSize;
Roger Meier79655fb2012-10-20 20:59:41 +0000702 FReadHandle := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000703 FWriteHandle := INVALID_HANDLE_VALUE;
704 FClientAnonRead := INVALID_HANDLE_VALUE;
705 FClientAnonWrite := INVALID_HANDLE_VALUE;
Jens Geyerdd074e72016-04-19 23:31:33 +0200706 FTimeOut := aTimeOut;
Roger Meier3bef8c22012-10-06 06:58:00 +0000707
708 // The anonymous pipe needs to be created first so that the server can
709 // pass the handles on to the client before the serve (acceptImpl)
710 // blocking call.
711 if not CreateAnonPipe
Jens Geyere0e32402016-04-20 21:50:48 +0200712 then raise TTransportExceptionNotOpen.Create(ClassName+'.Create() failed');
Roger Meier3bef8c22012-10-06 06:58:00 +0000713end;
714
715
Jens Geyer01640402013-09-25 21:12:21 +0200716function TAnonymousPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Roger Meier3bef8c22012-10-06 06:58:00 +0000717var buf : Byte;
718 br : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000719begin
Jens Geyer01640402013-09-25 21:12:21 +0200720 if Assigned(fnAccepting)
721 then fnAccepting();
722
Roger Meier79655fb2012-10-20 20:59:41 +0000723 // This 0-byte read serves merely as a blocking call.
724 if not ReadFile( FReadHandle, buf, 0, br, nil)
725 and (GetLastError() <> ERROR_MORE_DATA)
Jens Geyere0e32402016-04-20 21:50:48 +0200726 then raise TTransportExceptionNotOpen.Create('TServerPipe unable to initiate pipe communication');
Jens Geyer06045cf2013-03-27 20:26:25 +0200727
728 // create the transport impl
Jens Geyerdd074e72016-04-19 23:31:33 +0200729 result := TAnonymousPipeTransportImpl.Create( FReadHandle, FWriteHandle, FALSE, FTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000730end;
731
732
Jens Geyer06045cf2013-03-27 20:26:25 +0200733procedure TAnonymousPipeServerTransportImpl.InternalClose;
Roger Meier3bef8c22012-10-06 06:58:00 +0000734begin
Roger Meier79655fb2012-10-20 20:59:41 +0000735 ClosePipeHandle( FReadHandle);
736 ClosePipeHandle( FWriteHandle);
737 ClosePipeHandle( FClientAnonRead);
738 ClosePipeHandle( FClientAnonWrite);
Roger Meier3bef8c22012-10-06 06:58:00 +0000739end;
740
741
Jens Geyer06045cf2013-03-27 20:26:25 +0200742function TAnonymousPipeServerTransportImpl.ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000743begin
Roger Meier79655fb2012-10-20 20:59:41 +0000744 result := FReadHandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000745end;
746
747
Jens Geyer06045cf2013-03-27 20:26:25 +0200748function TAnonymousPipeServerTransportImpl.WriteHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000749begin
750 result := FWriteHandle;
751end;
752
753
Jens Geyer06045cf2013-03-27 20:26:25 +0200754function TAnonymousPipeServerTransportImpl.ClientAnonRead : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000755begin
756 result := FClientAnonRead;
757end;
758
759
Jens Geyer06045cf2013-03-27 20:26:25 +0200760function TAnonymousPipeServerTransportImpl.ClientAnonWrite : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000761begin
762 result := FClientAnonWrite;
763end;
764
765
Jens Geyer06045cf2013-03-27 20:26:25 +0200766function TAnonymousPipeServerTransportImpl.CreateAnonPipe : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000767var sd : PSECURITY_DESCRIPTOR;
768 sa : SECURITY_ATTRIBUTES; //TSecurityAttributes;
769 hCAR, hPipeW, hCAW, hPipe : THandle;
770begin
771 result := FALSE;
772
773 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
Jens Geyerb64a7742013-01-23 20:58:47 +0100774 try
775 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
776 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
Roger Meier79655fb2012-10-20 20:59:41 +0000777
Jens Geyerb64a7742013-01-23 20:58:47 +0100778 sa.nLength := sizeof( sa);
779 sa.lpSecurityDescriptor := sd;
780 sa.bInheritHandle := TRUE; //allow passing handle to child
Roger Meier79655fb2012-10-20 20:59:41 +0000781
Jens Geyerb64a7742013-01-23 20:58:47 +0100782 if not CreatePipe( hCAR, hPipeW, @sa, FBufSize) then begin //create stdin pipe
Jens Geyere0e32402016-04-20 21:50:48 +0200783 raise TTransportExceptionNotOpen.Create('TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100784 Exit;
785 end;
786
787 if not CreatePipe( hPipe, hCAW, @sa, FBufSize) then begin //create stdout pipe
Jens Geyerb64a7742013-01-23 20:58:47 +0100788 CloseHandle( hCAR);
789 CloseHandle( hPipeW);
Jens Geyere0e32402016-04-20 21:50:48 +0200790 raise TTransportExceptionNotOpen.Create('TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100791 Exit;
792 end;
793
794 FClientAnonRead := hCAR;
795 FClientAnonWrite := hCAW;
796 FReadHandle := hPipe;
797 FWriteHandle := hPipeW;
798
799 result := TRUE;
Jens Geyerd5436f52014-10-03 19:50:38 +0200800
Jens Geyerb64a7742013-01-23 20:58:47 +0100801 finally
802 if sd <> nil then LocalFree( Cardinal(sd));
Roger Meier79655fb2012-10-20 20:59:41 +0000803 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000804end;
805
806
Jens Geyer06045cf2013-03-27 20:26:25 +0200807{ TNamedPipeServerTransportImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000808
809
Jens Geyer06045cf2013-03-27 20:26:25 +0200810constructor TNamedPipeServerTransportImpl.Create( aPipename : string; aBufsize, aMaxConns, aTimeOut : Cardinal);
Roger Meier79655fb2012-10-20 20:59:41 +0000811// Named Pipe CTOR
812begin
813 inherited Create;
Jens Geyere9651362014-03-20 22:46:17 +0200814 ASSERT( aTimeout > 0);
Jens Geyer06045cf2013-03-27 20:26:25 +0200815 FPipeName := aPipename;
816 FBufsize := aBufSize;
817 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
818 FHandle := INVALID_HANDLE_VALUE;
819 FTimeout := aTimeOut;
820 FConnected := FALSE;
Roger Meier79655fb2012-10-20 20:59:41 +0000821
822 if Copy(FPipeName,1,2) <> '\\'
823 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
824end;
825
826
Jens Geyer01640402013-09-25 21:12:21 +0200827function TNamedPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Jens Geyer06045cf2013-03-27 20:26:25 +0200828var dwError, dwWait, dwDummy : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +0200829 overlapped : IOverlappedHelper;
830 handles : array[0..1] of THandle;
Jens Geyer01640402013-09-25 21:12:21 +0200831begin
Jens Geyere9651362014-03-20 22:46:17 +0200832 overlapped := TOverlappedHelperImpl.Create;
Jens Geyer01640402013-09-25 21:12:21 +0200833
Jens Geyere9651362014-03-20 22:46:17 +0200834 ASSERT( not FConnected);
Jens Geyer2ad6c302015-02-26 19:38:53 +0100835 CreateNamedPipe;
Jens Geyere9651362014-03-20 22:46:17 +0200836 while not FConnected do begin
Jens Geyer2ad6c302015-02-26 19:38:53 +0100837
838 if QueryStopServer
839 then Abort;
Roger Meier79655fb2012-10-20 20:59:41 +0000840
Jens Geyere9651362014-03-20 22:46:17 +0200841 if Assigned(fnAccepting)
842 then fnAccepting();
Jens Geyer01640402013-09-25 21:12:21 +0200843
Jens Geyere9651362014-03-20 22:46:17 +0200844 // Wait for the client to connect; if it succeeds, the
845 // function returns a nonzero value. If the function returns
846 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
847 if ConnectNamedPipe( Handle, overlapped.OverlappedPtr) then begin
848 FConnected := TRUE;
849 Break;
Jens Geyer01640402013-09-25 21:12:21 +0200850 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000851
Jens Geyere9651362014-03-20 22:46:17 +0200852 // ConnectNamedPipe() returns FALSE for OverlappedIO, even if connected.
853 // We have to check GetLastError() explicitly to find out
854 dwError := GetLastError;
855 case dwError of
856 ERROR_PIPE_CONNECTED : begin
857 FConnected := not QueryStopServer; // special case: pipe immediately connected
858 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000859
Jens Geyere9651362014-03-20 22:46:17 +0200860 ERROR_IO_PENDING : begin
861 handles[0] := overlapped.WaitHandle;
862 handles[1] := FStopServer.Handle;
863 dwWait := WaitForMultipleObjects( 2, @handles, FALSE, FTimeout);
864 FConnected := (dwWait = WAIT_OBJECT_0)
865 and GetOverlappedResult( Handle, overlapped.Overlapped, dwDummy, TRUE)
866 and not QueryStopServer;
867 end;
868
869 else
870 InternalClose;
Jens Geyere0e32402016-04-20 21:50:48 +0200871 raise TTransportExceptionNotOpen.Create('Client connection failed');
Jens Geyere9651362014-03-20 22:46:17 +0200872 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000873 end;
Jens Geyere9651362014-03-20 22:46:17 +0200874
875 // create the transport impl
876 result := CreateTransportInstance;
Roger Meier79655fb2012-10-20 20:59:41 +0000877end;
878
879
Jens Geyer06045cf2013-03-27 20:26:25 +0200880function TNamedPipeServerTransportImpl.CreateTransportInstance : ITransport;
881// create the transport impl
882var hPipe : THandle;
Roger Meier79655fb2012-10-20 20:59:41 +0000883begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200884 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
885 try
886 FConnected := FALSE;
Jens Geyere9651362014-03-20 22:46:17 +0200887 result := TNamedPipeTransportServerEndImpl.Create( hPipe, TRUE, FTimeout);
Jens Geyer06045cf2013-03-27 20:26:25 +0200888 except
Jens Geyer01640402013-09-25 21:12:21 +0200889 ClosePipeHandle(hPipe);
890 raise;
891 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000892end;
893
894
Jens Geyer06045cf2013-03-27 20:26:25 +0200895procedure TNamedPipeServerTransportImpl.InternalClose;
896var hPipe : THandle;
897begin
898 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
899 if hPipe = INVALID_HANDLE_VALUE then Exit;
900
901 try
902 if FConnected
903 then FlushFileBuffers( hPipe)
904 else CancelIo( hPipe);
905 DisconnectNamedPipe( hPipe);
906 finally
907 ClosePipeHandle( hPipe);
908 FConnected := FALSE;
909 end;
910end;
911
912
913function TNamedPipeServerTransportImpl.Handle : THandle;
914begin
915 {$IFDEF WIN64}
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200916 result := THandle( InterlockedExchangeAdd64( Int64(FHandle), 0));
Jens Geyer06045cf2013-03-27 20:26:25 +0200917 {$ELSE}
918 result := THandle( InterlockedExchangeAdd( Integer(FHandle), 0));
919 {$ENDIF}
920end;
921
922
923function TNamedPipeServerTransportImpl.CreateNamedPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000924var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
925 everyone_sid : PSID;
926 ea : EXPLICIT_ACCESS;
927 acl : PACL;
928 sd : PSECURITY_DESCRIPTOR;
929 sa : SECURITY_ATTRIBUTES;
Roger Meier3bef8c22012-10-06 06:58:00 +0000930const
931 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
932 SECURITY_WORLD_RID = $00000000;
933begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100934 sd := nil;
Roger Meier3bef8c22012-10-06 06:58:00 +0000935 everyone_sid := nil;
Jens Geyerb64a7742013-01-23 20:58:47 +0100936 try
Jens Geyer06045cf2013-03-27 20:26:25 +0200937 ASSERT( (FHandle = INVALID_HANDLE_VALUE) and not FConnected);
938
Jens Geyerb64a7742013-01-23 20:58:47 +0100939 // Windows - set security to allow non-elevated apps
940 // to access pipes created by elevated apps.
941 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
942 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000943
Jens Geyerb64a7742013-01-23 20:58:47 +0100944 ZeroMemory( @ea, SizeOf(ea));
945 ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
946 ea.grfAccessMode := SET_ACCESS;
947 ea.grfInheritance := NO_INHERITANCE;
948 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
949 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
950 ea.Trustee.ptstrName := PChar(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000951
Jens Geyerb64a7742013-01-23 20:58:47 +0100952 acl := nil;
953 SetEntriesInAcl( 1, @ea, nil, acl);
Roger Meier3bef8c22012-10-06 06:58:00 +0000954
Jens Geyerb64a7742013-01-23 20:58:47 +0100955 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
956 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
957 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
Roger Meier3bef8c22012-10-06 06:58:00 +0000958
Jens Geyerb64a7742013-01-23 20:58:47 +0100959 sa.nLength := SizeOf(sa);
960 sa.lpSecurityDescriptor := sd;
961 sa.bInheritHandle := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000962
Jens Geyerb64a7742013-01-23 20:58:47 +0100963 // Create an instance of the named pipe
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200964 {$IFDEF OLD_UNIT_NAMES}
965 result := Windows.CreateNamedPipe(
966 {$ELSE}
967 result := Winapi.Windows.CreateNamedPipe(
968 {$ENDIF}
969 PChar( FPipeName), // pipe name
970 PIPE_ACCESS_DUPLEX or // read/write access
971 FILE_FLAG_OVERLAPPED, // async mode
972 PIPE_TYPE_BYTE or // byte type pipe
973 PIPE_READMODE_BYTE, // byte read mode
974 FMaxConns, // max. instances
975 FBufSize, // output buffer size
976 FBufSize, // input buffer size
977 FTimeout, // time-out, see MSDN
978 @sa // default security attribute
979 );
Roger Meier3bef8c22012-10-06 06:58:00 +0000980
Jens Geyer06045cf2013-03-27 20:26:25 +0200981 if( result <> INVALID_HANDLE_VALUE)
982 then InterlockedExchangePointer( Pointer(FHandle), Pointer(result))
Jens Geyere0e32402016-04-20 21:50:48 +0200983 else raise TTransportExceptionNotOpen.Create('CreateNamedPipe() failed ' + IntToStr(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100984
985 finally
986 if sd <> nil then LocalFree( Cardinal( sd));
987 if acl <> nil then LocalFree( Cardinal( acl));
988 if everyone_sid <> nil then FreeSid(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000989 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000990end;
991
992
Roger Meier3bef8c22012-10-06 06:58:00 +0000993
994end.
995
996
997