blob: 9b7f842c1243354f0da6a0aca1461f2a4b51f415 [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
Jens Geyer17c3ad92017-09-05 20:31:27 +020051 procedure Write( const pBuf : Pointer; offset, count : Integer); override;
52 function Read( const pBuf : Pointer; const buflen : Integer; 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 Geyer17c3ad92017-09-05 20:31:27 +020057 function ReadDirect( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; overload;
58 function ReadOverlapped( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; overload;
59 procedure WriteDirect( const pBuf : Pointer; offset: Integer; count: Integer); overload;
60 procedure WriteOverlapped( const pBuf : Pointer; offset: Integer; count: Integer); overload;
Jens Geyere9651362014-03-20 22:46:17 +020061
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 Geyer17c3ad92017-09-05 20:31:27 +0200313procedure TPipeStreamBase.Write( const pBuf : Pointer; offset, count : Integer);
Jens Geyere9651362014-03-20 22:46:17 +0200314begin
315 if FOverlapped
Jens Geyer17c3ad92017-09-05 20:31:27 +0200316 then WriteOverlapped( pBuf, offset, count)
317 else WriteDirect( pBuf, offset, count);
Jens Geyere9651362014-03-20 22:46:17 +0200318end;
319
320
Jens Geyer17c3ad92017-09-05 20:31:27 +0200321function TPipeStreamBase.Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer;
Jens Geyere9651362014-03-20 22:46:17 +0200322begin
323 if FOverlapped
Jens Geyer17c3ad92017-09-05 20:31:27 +0200324 then result := ReadOverlapped( pBuf, buflen, offset, count)
325 else result := ReadDirect( pBuf, buflen, offset, count);
Jens Geyere9651362014-03-20 22:46:17 +0200326end;
327
328
Jens Geyer17c3ad92017-09-05 20:31:27 +0200329procedure TPipeStreamBase.WriteDirect( const pBuf : Pointer; offset: Integer; 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
Jens Geyer17c3ad92017-09-05 20:31:27 +0200335 if not WriteFile( FPipe, PByteArray(pBuf)^[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 Geyer17c3ad92017-09-05 20:31:27 +0200340procedure TPipeStreamBase.WriteOverlapped( const pBuf : Pointer; offset: Integer; count: Integer);
341var cbWritten, dwWait, dwError : DWORD;
342 overlapped : IOverlappedHelper;
343begin
344 if not IsOpen
345 then raise TTransportExceptionNotOpen.Create('Called write on non-open pipe');
346
347 overlapped := TOverlappedHelperImpl.Create;
348
349 if not WriteFile( FPipe, PByteArray(pBuf)^[offset], count, cbWritten, overlapped.OverlappedPtr)
350 then begin
351 dwError := GetLastError;
352 case dwError of
353 ERROR_IO_PENDING : begin
354 dwWait := overlapped.WaitFor(FTimeout);
355
356 if (dwWait = WAIT_TIMEOUT)
357 then raise TTransportExceptionTimedOut.Create('Pipe write timed out');
358
359 if (dwWait <> WAIT_OBJECT_0)
360 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbWritten, TRUE)
361 then raise TTransportExceptionUnknown.Create('Pipe write error');
362 end;
363
364 else
365 raise TTransportExceptionUnknown.Create(SysErrorMessage(dwError));
366 end;
367 end;
368
369 ASSERT( DWORD(count) = cbWritten);
370end;
371
372
373function TPipeStreamBase.ReadDirect( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer;
Roger Meier79655fb2012-10-20 20:59:41 +0000374var cbRead, dwErr : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000375 bytes, retries : LongInt;
376 bOk : Boolean;
377const INTERVAL = 10; // ms
378begin
379 if not IsOpen
Jens Geyere0e32402016-04-20 21:50:48 +0200380 then raise TTransportExceptionNotOpen.Create('Called read on non-open pipe');
Roger Meier3bef8c22012-10-06 06:58:00 +0000381
382 // MSDN: Handle can be a handle to a named pipe instance,
383 // or it can be a handle to the read end of an anonymous pipe,
384 // The handle must have GENERIC_READ access to the pipe.
385 if FTimeOut <> INFINITE then begin
386 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
387 while TRUE do begin
Jens Geyer5988f482016-04-19 23:01:24 +0200388 if not PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) then begin
389 dwErr := GetLastError;
390 if (dwErr = ERROR_INVALID_HANDLE)
391 or (dwErr = ERROR_BROKEN_PIPE)
392 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
393 then begin
394 result := 0; // other side closed the pipe
395 Exit;
396 end;
397 end
398 else if bytes > 0 then begin
399 Break; // there are data
Roger Meier79655fb2012-10-20 20:59:41 +0000400 end;
401
Roger Meier3bef8c22012-10-06 06:58:00 +0000402 Dec( retries);
403 if retries > 0
404 then Sleep( INTERVAL)
Jens Geyere0e32402016-04-20 21:50:48 +0200405 else raise TTransportExceptionTimedOut.Create('Pipe read timed out');
Roger Meier3bef8c22012-10-06 06:58:00 +0000406 end;
407 end;
408
409 // read the data (or block INFINITE-ly)
Jens Geyer17c3ad92017-09-05 20:31:27 +0200410 bOk := ReadFile( FPipe, PByteArray(pBuf)^[offset], count, cbRead, nil);
Roger Meier3bef8c22012-10-06 06:58:00 +0000411 if (not bOk) and (GetLastError() <> ERROR_MORE_DATA)
412 then result := 0 // No more data, possibly because client disconnected.
413 else result := cbRead;
414end;
415
416
Jens Geyer17c3ad92017-09-05 20:31:27 +0200417function TPipeStreamBase.ReadOverlapped( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer;
Jens Geyere9651362014-03-20 22:46:17 +0200418var 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
Jens Geyer17c3ad92017-09-05 20:31:27 +0200428 bOk := ReadFile( FPipe, PByteArray(pBuf)^[offset], count, cbRead, overlapped.OverlappedPtr);
Jens Geyere9651362014-03-20 22:46:17 +0200429 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
Roger Meier79655fb2012-10-20 20:59:41 +0000771 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
Jens Geyerb64a7742013-01-23 20:58:47 +0100772 try
773 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
774 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
Roger Meier79655fb2012-10-20 20:59:41 +0000775
Jens Geyerb64a7742013-01-23 20:58:47 +0100776 sa.nLength := sizeof( sa);
777 sa.lpSecurityDescriptor := sd;
778 sa.bInheritHandle := TRUE; //allow passing handle to child
Roger Meier79655fb2012-10-20 20:59:41 +0000779
Jens Geyer17c3ad92017-09-05 20:31:27 +0200780 Result := CreatePipe( hCAR, hPipeW, @sa, FBufSize); //create stdin pipe
781 if not Result then begin //create stdin pipe
Jens Geyere0e32402016-04-20 21:50:48 +0200782 raise TTransportExceptionNotOpen.Create('TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100783 Exit;
784 end;
785
Jens Geyer17c3ad92017-09-05 20:31:27 +0200786 Result := CreatePipe( hPipe, hCAW, @sa, FBufSize); //create stdout pipe
787 if not Result 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;
Jens Geyerb64a7742013-01-23 20:58:47 +0100798 finally
799 if sd <> nil then LocalFree( Cardinal(sd));
Roger Meier79655fb2012-10-20 20:59:41 +0000800 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000801end;
802
803
Jens Geyer06045cf2013-03-27 20:26:25 +0200804{ TNamedPipeServerTransportImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000805
806
Jens Geyer06045cf2013-03-27 20:26:25 +0200807constructor TNamedPipeServerTransportImpl.Create( aPipename : string; aBufsize, aMaxConns, aTimeOut : Cardinal);
Roger Meier79655fb2012-10-20 20:59:41 +0000808// Named Pipe CTOR
809begin
810 inherited Create;
Jens Geyere9651362014-03-20 22:46:17 +0200811 ASSERT( aTimeout > 0);
Jens Geyer06045cf2013-03-27 20:26:25 +0200812 FPipeName := aPipename;
813 FBufsize := aBufSize;
814 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
815 FHandle := INVALID_HANDLE_VALUE;
816 FTimeout := aTimeOut;
817 FConnected := FALSE;
Roger Meier79655fb2012-10-20 20:59:41 +0000818
819 if Copy(FPipeName,1,2) <> '\\'
820 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
821end;
822
823
Jens Geyer01640402013-09-25 21:12:21 +0200824function TNamedPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Jens Geyer06045cf2013-03-27 20:26:25 +0200825var dwError, dwWait, dwDummy : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +0200826 overlapped : IOverlappedHelper;
827 handles : array[0..1] of THandle;
Jens Geyer01640402013-09-25 21:12:21 +0200828begin
Jens Geyere9651362014-03-20 22:46:17 +0200829 overlapped := TOverlappedHelperImpl.Create;
Jens Geyer01640402013-09-25 21:12:21 +0200830
Jens Geyere9651362014-03-20 22:46:17 +0200831 ASSERT( not FConnected);
Jens Geyer2ad6c302015-02-26 19:38:53 +0100832 CreateNamedPipe;
Jens Geyere9651362014-03-20 22:46:17 +0200833 while not FConnected do begin
Jens Geyer2ad6c302015-02-26 19:38:53 +0100834
835 if QueryStopServer
836 then Abort;
Roger Meier79655fb2012-10-20 20:59:41 +0000837
Jens Geyere9651362014-03-20 22:46:17 +0200838 if Assigned(fnAccepting)
839 then fnAccepting();
Jens Geyer01640402013-09-25 21:12:21 +0200840
Jens Geyere9651362014-03-20 22:46:17 +0200841 // Wait for the client to connect; if it succeeds, the
842 // function returns a nonzero value. If the function returns
843 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
844 if ConnectNamedPipe( Handle, overlapped.OverlappedPtr) then begin
845 FConnected := TRUE;
846 Break;
Jens Geyer01640402013-09-25 21:12:21 +0200847 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000848
Jens Geyere9651362014-03-20 22:46:17 +0200849 // ConnectNamedPipe() returns FALSE for OverlappedIO, even if connected.
850 // We have to check GetLastError() explicitly to find out
851 dwError := GetLastError;
852 case dwError of
853 ERROR_PIPE_CONNECTED : begin
854 FConnected := not QueryStopServer; // special case: pipe immediately connected
855 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000856
Jens Geyere9651362014-03-20 22:46:17 +0200857 ERROR_IO_PENDING : begin
858 handles[0] := overlapped.WaitHandle;
859 handles[1] := FStopServer.Handle;
860 dwWait := WaitForMultipleObjects( 2, @handles, FALSE, FTimeout);
861 FConnected := (dwWait = WAIT_OBJECT_0)
862 and GetOverlappedResult( Handle, overlapped.Overlapped, dwDummy, TRUE)
863 and not QueryStopServer;
864 end;
865
866 else
867 InternalClose;
Jens Geyere0e32402016-04-20 21:50:48 +0200868 raise TTransportExceptionNotOpen.Create('Client connection failed');
Jens Geyere9651362014-03-20 22:46:17 +0200869 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000870 end;
Jens Geyere9651362014-03-20 22:46:17 +0200871
872 // create the transport impl
873 result := CreateTransportInstance;
Roger Meier79655fb2012-10-20 20:59:41 +0000874end;
875
876
Jens Geyer06045cf2013-03-27 20:26:25 +0200877function TNamedPipeServerTransportImpl.CreateTransportInstance : ITransport;
878// create the transport impl
879var hPipe : THandle;
Roger Meier79655fb2012-10-20 20:59:41 +0000880begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200881 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
882 try
883 FConnected := FALSE;
Jens Geyere9651362014-03-20 22:46:17 +0200884 result := TNamedPipeTransportServerEndImpl.Create( hPipe, TRUE, FTimeout);
Jens Geyer06045cf2013-03-27 20:26:25 +0200885 except
Jens Geyer01640402013-09-25 21:12:21 +0200886 ClosePipeHandle(hPipe);
887 raise;
888 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000889end;
890
891
Jens Geyer06045cf2013-03-27 20:26:25 +0200892procedure TNamedPipeServerTransportImpl.InternalClose;
893var hPipe : THandle;
894begin
895 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
896 if hPipe = INVALID_HANDLE_VALUE then Exit;
897
898 try
899 if FConnected
900 then FlushFileBuffers( hPipe)
901 else CancelIo( hPipe);
902 DisconnectNamedPipe( hPipe);
903 finally
904 ClosePipeHandle( hPipe);
905 FConnected := FALSE;
906 end;
907end;
908
909
910function TNamedPipeServerTransportImpl.Handle : THandle;
911begin
912 {$IFDEF WIN64}
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200913 result := THandle( InterlockedExchangeAdd64( Int64(FHandle), 0));
Jens Geyer06045cf2013-03-27 20:26:25 +0200914 {$ELSE}
915 result := THandle( InterlockedExchangeAdd( Integer(FHandle), 0));
916 {$ENDIF}
917end;
918
919
920function TNamedPipeServerTransportImpl.CreateNamedPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000921var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
922 everyone_sid : PSID;
923 ea : EXPLICIT_ACCESS;
924 acl : PACL;
925 sd : PSECURITY_DESCRIPTOR;
926 sa : SECURITY_ATTRIBUTES;
Roger Meier3bef8c22012-10-06 06:58:00 +0000927const
928 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
929 SECURITY_WORLD_RID = $00000000;
930begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100931 sd := nil;
Roger Meier3bef8c22012-10-06 06:58:00 +0000932 everyone_sid := nil;
Jens Geyerb64a7742013-01-23 20:58:47 +0100933 try
Jens Geyer06045cf2013-03-27 20:26:25 +0200934 ASSERT( (FHandle = INVALID_HANDLE_VALUE) and not FConnected);
935
Jens Geyerb64a7742013-01-23 20:58:47 +0100936 // Windows - set security to allow non-elevated apps
937 // to access pipes created by elevated apps.
938 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
939 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000940
Jens Geyerb64a7742013-01-23 20:58:47 +0100941 ZeroMemory( @ea, SizeOf(ea));
942 ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
943 ea.grfAccessMode := SET_ACCESS;
944 ea.grfInheritance := NO_INHERITANCE;
945 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
946 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
947 ea.Trustee.ptstrName := PChar(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000948
Jens Geyerb64a7742013-01-23 20:58:47 +0100949 acl := nil;
950 SetEntriesInAcl( 1, @ea, nil, acl);
Roger Meier3bef8c22012-10-06 06:58:00 +0000951
Jens Geyerb64a7742013-01-23 20:58:47 +0100952 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
953 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
954 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
Roger Meier3bef8c22012-10-06 06:58:00 +0000955
Jens Geyerb64a7742013-01-23 20:58:47 +0100956 sa.nLength := SizeOf(sa);
957 sa.lpSecurityDescriptor := sd;
958 sa.bInheritHandle := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000959
Jens Geyerb64a7742013-01-23 20:58:47 +0100960 // Create an instance of the named pipe
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200961 {$IFDEF OLD_UNIT_NAMES}
962 result := Windows.CreateNamedPipe(
963 {$ELSE}
964 result := Winapi.Windows.CreateNamedPipe(
965 {$ENDIF}
966 PChar( FPipeName), // pipe name
967 PIPE_ACCESS_DUPLEX or // read/write access
968 FILE_FLAG_OVERLAPPED, // async mode
969 PIPE_TYPE_BYTE or // byte type pipe
970 PIPE_READMODE_BYTE, // byte read mode
971 FMaxConns, // max. instances
972 FBufSize, // output buffer size
973 FBufSize, // input buffer size
974 FTimeout, // time-out, see MSDN
975 @sa // default security attribute
976 );
Roger Meier3bef8c22012-10-06 06:58:00 +0000977
Jens Geyer06045cf2013-03-27 20:26:25 +0200978 if( result <> INVALID_HANDLE_VALUE)
979 then InterlockedExchangePointer( Pointer(FHandle), Pointer(result))
Jens Geyere0e32402016-04-20 21:50:48 +0200980 else raise TTransportExceptionNotOpen.Create('CreateNamedPipe() failed ' + IntToStr(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100981
982 finally
983 if sd <> nil then LocalFree( Cardinal( sd));
984 if acl <> nil then LocalFree( Cardinal( acl));
985 if everyone_sid <> nil then FreeSid(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000986 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000987end;
988
989
Roger Meier3bef8c22012-10-06 06:58:00 +0000990
991end.
992
993
994