blob: aace4bb21785ae4ad952bb1097f16f9dcee78911 [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);
Jens Geyerd4df9172017-10-25 22:30:23 +0200330var cbWritten, nBytes : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000331begin
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 Geyerd4df9172017-10-25 22:30:23 +0200335 // if necessary, send the data in chunks
336 // there's a system limit around 0x10000 bytes that we hit otherwise
337 // MSDN: "Pipe write operations across a network are limited to 65,535 bytes per write. For more information regarding pipes, see the Remarks section."
338 nBytes := Min( 15*4096, count); // 16 would exceed the limit
339 while nBytes > 0 do begin
340 if not WriteFile( FPipe, PByteArray(pBuf)^[offset], nBytes, cbWritten, nil)
341 then raise TTransportExceptionNotOpen.Create('Write to pipe failed');
342
343 Inc( offset, cbWritten);
344 Dec( count, cbWritten);
345 nBytes := Min( nBytes, count);
346 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000347end;
348
349
Jens Geyer17c3ad92017-09-05 20:31:27 +0200350procedure TPipeStreamBase.WriteOverlapped( const pBuf : Pointer; offset: Integer; count: Integer);
Jens Geyerd4df9172017-10-25 22:30:23 +0200351var cbWritten, dwWait, dwError, nBytes : DWORD;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200352 overlapped : IOverlappedHelper;
353begin
354 if not IsOpen
355 then raise TTransportExceptionNotOpen.Create('Called write on non-open pipe');
356
Jens Geyerd4df9172017-10-25 22:30:23 +0200357 // if necessary, send the data in chunks
358 // there's a system limit around 0x10000 bytes that we hit otherwise
359 // MSDN: "Pipe write operations across a network are limited to 65,535 bytes per write. For more information regarding pipes, see the Remarks section."
360 nBytes := Min( 15*4096, count); // 16 would exceed the limit
361 while nBytes > 0 do begin
362 overlapped := TOverlappedHelperImpl.Create;
363 if not WriteFile( FPipe, PByteArray(pBuf)^[offset], nBytes, cbWritten, overlapped.OverlappedPtr)
364 then begin
365 dwError := GetLastError;
366 case dwError of
367 ERROR_IO_PENDING : begin
368 dwWait := overlapped.WaitFor(FTimeout);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200369
Jens Geyerd4df9172017-10-25 22:30:23 +0200370 if (dwWait = WAIT_TIMEOUT)
371 then raise TTransportExceptionTimedOut.Create('Pipe write timed out');
Jens Geyer17c3ad92017-09-05 20:31:27 +0200372
Jens Geyerd4df9172017-10-25 22:30:23 +0200373 if (dwWait <> WAIT_OBJECT_0)
374 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbWritten, TRUE)
375 then raise TTransportExceptionUnknown.Create('Pipe write error');
376 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200377
Jens Geyerd4df9172017-10-25 22:30:23 +0200378 else
379 raise TTransportExceptionUnknown.Create(SysErrorMessage(dwError));
Jens Geyer17c3ad92017-09-05 20:31:27 +0200380 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200381 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200382
Jens Geyerd4df9172017-10-25 22:30:23 +0200383 ASSERT( DWORD(nBytes) = cbWritten);
384
385 Inc( offset, cbWritten);
386 Dec( count, cbWritten);
387 nBytes := Min( nBytes, count);
388 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200389end;
390
391
392function TPipeStreamBase.ReadDirect( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer;
Jens Geyerd4df9172017-10-25 22:30:23 +0200393var cbRead, dwErr, nRemaining : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000394 bytes, retries : LongInt;
395 bOk : Boolean;
396const INTERVAL = 10; // ms
397begin
398 if not IsOpen
Jens Geyere0e32402016-04-20 21:50:48 +0200399 then raise TTransportExceptionNotOpen.Create('Called read on non-open pipe');
Roger Meier3bef8c22012-10-06 06:58:00 +0000400
401 // MSDN: Handle can be a handle to a named pipe instance,
402 // or it can be a handle to the read end of an anonymous pipe,
403 // The handle must have GENERIC_READ access to the pipe.
404 if FTimeOut <> INFINITE then begin
405 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
406 while TRUE do begin
Jens Geyer5988f482016-04-19 23:01:24 +0200407 if not PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) then begin
408 dwErr := GetLastError;
409 if (dwErr = ERROR_INVALID_HANDLE)
410 or (dwErr = ERROR_BROKEN_PIPE)
411 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
412 then begin
413 result := 0; // other side closed the pipe
414 Exit;
415 end;
416 end
417 else if bytes > 0 then begin
418 Break; // there are data
Roger Meier79655fb2012-10-20 20:59:41 +0000419 end;
420
Roger Meier3bef8c22012-10-06 06:58:00 +0000421 Dec( retries);
422 if retries > 0
423 then Sleep( INTERVAL)
Jens Geyere0e32402016-04-20 21:50:48 +0200424 else raise TTransportExceptionTimedOut.Create('Pipe read timed out');
Roger Meier3bef8c22012-10-06 06:58:00 +0000425 end;
426 end;
427
Jens Geyerd4df9172017-10-25 22:30:23 +0200428 result := 0;
429 nRemaining := count;
430 while nRemaining > 0 do begin
431 // read the data (or block INFINITE-ly)
432 bOk := ReadFile( FPipe, PByteArray(pBuf)^[offset], nRemaining, cbRead, nil);
433 if (not bOk) and (GetLastError() <> ERROR_MORE_DATA)
434 then Break; // No more data, possibly because client disconnected.
435
436 Dec( nRemaining, cbRead);
437 Inc( offset, cbRead);
438 Inc( result, cbRead);
439 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000440end;
441
442
Jens Geyer17c3ad92017-09-05 20:31:27 +0200443function TPipeStreamBase.ReadOverlapped( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer;
Jens Geyerd4df9172017-10-25 22:30:23 +0200444var cbRead, dwWait, dwError, nRemaining : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +0200445 bOk : Boolean;
446 overlapped : IOverlappedHelper;
447begin
448 if not IsOpen
Jens Geyere0e32402016-04-20 21:50:48 +0200449 then raise TTransportExceptionNotOpen.Create('Called read on non-open pipe');
Jens Geyere9651362014-03-20 22:46:17 +0200450
Jens Geyerd4df9172017-10-25 22:30:23 +0200451 result := 0;
452 nRemaining := count;
453 while nRemaining > 0 do begin
454 overlapped := TOverlappedHelperImpl.Create;
Jens Geyere9651362014-03-20 22:46:17 +0200455
Jens Geyerd4df9172017-10-25 22:30:23 +0200456 // read the data
457 bOk := ReadFile( FPipe, PByteArray(pBuf)^[offset], nRemaining, cbRead, overlapped.OverlappedPtr);
458 if not bOk then begin
459 dwError := GetLastError;
460 case dwError of
461 ERROR_IO_PENDING : begin
462 dwWait := overlapped.WaitFor(FTimeout);
Jens Geyere9651362014-03-20 22:46:17 +0200463
Jens Geyerd4df9172017-10-25 22:30:23 +0200464 if (dwWait = WAIT_TIMEOUT)
465 then raise TTransportExceptionTimedOut.Create('Pipe read timed out');
Jens Geyere9651362014-03-20 22:46:17 +0200466
Jens Geyerd4df9172017-10-25 22:30:23 +0200467 if (dwWait <> WAIT_OBJECT_0)
468 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbRead, TRUE)
469 then raise TTransportExceptionUnknown.Create('Pipe read error');
470 end;
471
472 else
473 raise TTransportExceptionUnknown.Create(SysErrorMessage(dwError));
Jens Geyere9651362014-03-20 22:46:17 +0200474 end;
Jens Geyere9651362014-03-20 22:46:17 +0200475 end;
Jens Geyere9651362014-03-20 22:46:17 +0200476
Jens Geyerd4df9172017-10-25 22:30:23 +0200477 ASSERT( cbRead > 0); // see TTransportImpl.ReadAll()
478 ASSERT( cbRead <= DWORD(nRemaining));
479 Dec( nRemaining, cbRead);
480 Inc( offset, cbRead);
481 Inc( result, cbRead);
482 end;
Jens Geyere9651362014-03-20 22:46:17 +0200483end;
484
485
Jens Geyer06045cf2013-03-27 20:26:25 +0200486function TPipeStreamBase.ToArray: TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000487var bytes : LongInt;
488begin
489 SetLength( result, 0);
490 bytes := 0;
491
492 if IsOpen
493 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
494 and (bytes > 0)
495 then begin
496 SetLength( result, bytes);
497 Read( result, 0, bytes);
498 end;
499end;
500
501
Roger Meier79655fb2012-10-20 20:59:41 +0000502{ TNamedPipeStreamImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000503
504
Jens Geyere9651362014-03-20 22:46:17 +0200505constructor TNamedPipeStreamImpl.Create( const aPipeName : string;
506 const aEnableOverlapped : Boolean;
507 const aShareMode: DWORD;
Roger Meier79655fb2012-10-20 20:59:41 +0000508 const aSecurityAttributes: PSecurityAttributes;
Jens Geyer653f0de2016-04-20 12:46:57 +0200509 const aTimeOut, aOpenTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000510begin
Jens Geyer653f0de2016-04-20 12:46:57 +0200511 inherited Create( aEnableOverlapped, aTimeout, aOpenTimeOut);
Roger Meier79655fb2012-10-20 20:59:41 +0000512
513 FPipeName := aPipeName;
514 FShareMode := aShareMode;
515 FSecurityAttribs := aSecurityAttributes;
516
517 if Copy(FPipeName,1,2) <> '\\'
518 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
Roger Meier3bef8c22012-10-06 06:58:00 +0000519end;
520
521
Roger Meier79655fb2012-10-20 20:59:41 +0000522procedure TNamedPipeStreamImpl.Open;
523var hPipe : THandle;
Jens Geyerb89b5b92016-04-19 23:09:41 +0200524 retries, timeout, dwErr : DWORD;
525const INTERVAL = 10; // ms
Roger Meier79655fb2012-10-20 20:59:41 +0000526begin
527 if IsOpen then Exit;
528
Jens Geyer653f0de2016-04-20 12:46:57 +0200529 retries := Max( 1, Round( 1.0 * FOpenTimeOut / INTERVAL));
530 timeout := FOpenTimeOut;
Jens Geyerb89b5b92016-04-19 23:09:41 +0200531
532 // if the server hasn't gotten to the point where the pipe has been created, at least wait the timeout
533 // According to MSDN, if no instances of the specified named pipe exist, the WaitNamedPipe function
534 // returns IMMEDIATELY, regardless of the time-out value.
Jens Geyer653f0de2016-04-20 12:46:57 +0200535 // Always use INTERVAL, since WaitNamedPipe(0) defaults to some other value
Jens Geyerb89b5b92016-04-19 23:09:41 +0200536 while not WaitNamedPipe( PChar(FPipeName), INTERVAL) do begin
537 dwErr := GetLastError;
538 if dwErr <> ERROR_FILE_NOT_FOUND
Jens Geyere0e32402016-04-20 21:50:48 +0200539 then raise TTransportExceptionNotOpen.Create('Unable to open pipe, '+SysErrorMessage(dwErr));
Jens Geyerb89b5b92016-04-19 23:09:41 +0200540
541 if timeout <> INFINITE then begin
542 if (retries > 0)
543 then Dec(retries)
Jens Geyere0e32402016-04-20 21:50:48 +0200544 else raise TTransportExceptionNotOpen.Create('Unable to open pipe, timed out');
Jens Geyerb89b5b92016-04-19 23:09:41 +0200545 end;
546
547 Sleep(INTERVAL)
548 end;
549
Roger Meier79655fb2012-10-20 20:59:41 +0000550 // open that thingy
Roger Meier79655fb2012-10-20 20:59:41 +0000551 hPipe := CreateFile( PChar( FPipeName),
552 GENERIC_READ or GENERIC_WRITE,
553 FShareMode, // sharing
554 FSecurityAttribs, // security attributes
555 OPEN_EXISTING, // opens existing pipe
Jens Geyere9651362014-03-20 22:46:17 +0200556 FILE_FLAG_OVERLAPPED or FILE_FLAG_WRITE_THROUGH, // async+fast, please
Roger Meier79655fb2012-10-20 20:59:41 +0000557 0); // no template file
558
559 if hPipe = INVALID_HANDLE_VALUE
Jens Geyere0e32402016-04-20 21:50:48 +0200560 then raise TTransportExceptionNotOpen.Create('Unable to open pipe, '+SysErrorMessage(GetLastError));
Roger Meier79655fb2012-10-20 20:59:41 +0000561
Roger Meier79655fb2012-10-20 20:59:41 +0000562 // everything fine
563 FPipe := hPipe;
564end;
565
566
567{ THandlePipeStreamImpl }
568
569
Jens Geyere9651362014-03-20 22:46:17 +0200570constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle;
571 const aOwnsHandle, aEnableOverlapped : Boolean;
572 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000573begin
Jens Geyere9651362014-03-20 22:46:17 +0200574 inherited Create( aEnableOverlapped, aTimeOut);
Roger Meier79655fb2012-10-20 20:59:41 +0000575
576 if aOwnsHandle
577 then FSrcHandle := aPipeHandle
578 else FSrcHandle := DuplicatePipeHandle( aPipeHandle);
579
580 Open;
581end;
582
583
584destructor THandlePipeStreamImpl.Destroy;
585begin
586 try
587 ClosePipeHandle( FSrcHandle);
588 finally
589 inherited Destroy;
590 end;
591end;
592
593
594procedure THandlePipeStreamImpl.Open;
595begin
596 if not IsOpen
597 then FPipe := DuplicatePipeHandle( FSrcHandle);
598end;
599
600
Jens Geyer06045cf2013-03-27 20:26:25 +0200601{ TPipeTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000602
603
Jens Geyer06045cf2013-03-27 20:26:25 +0200604function TPipeTransportBase.GetIsOpen: Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000605begin
Jens Geyer0b20cc82013-03-07 20:47:01 +0100606 result := (FInputStream <> nil) and (FInputStream.IsOpen)
607 and (FOutputStream <> nil) and (FOutputStream.IsOpen);
Roger Meier79655fb2012-10-20 20:59:41 +0000608end;
609
610
Jens Geyer06045cf2013-03-27 20:26:25 +0200611procedure TPipeTransportBase.Open;
Roger Meier79655fb2012-10-20 20:59:41 +0000612begin
613 FInputStream.Open;
614 FOutputStream.Open;
615end;
616
617
Jens Geyer06045cf2013-03-27 20:26:25 +0200618procedure TPipeTransportBase.Close;
Roger Meier79655fb2012-10-20 20:59:41 +0000619begin
620 FInputStream.Close;
621 FOutputStream.Close;
622end;
623
624
Jens Geyer06045cf2013-03-27 20:26:25 +0200625{ TNamedPipeTransportClientEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000626
627
Jens Geyer06045cf2013-03-27 20:26:25 +0200628constructor TNamedPipeTransportClientEndImpl.Create( const aPipeName : string; const aShareMode: DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000629 const aSecurityAttributes: PSecurityAttributes;
Jens Geyer653f0de2016-04-20 12:46:57 +0200630 const aTimeOut, aOpenTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000631// Named pipe constructor
632begin
Roger Meier79655fb2012-10-20 20:59:41 +0000633 inherited Create( nil, nil);
Jens Geyer653f0de2016-04-20 12:46:57 +0200634 FInputStream := TNamedPipeStreamImpl.Create( aPipeName, TRUE, aShareMode, aSecurityAttributes, aTimeOut, aOpenTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000635 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000636end;
637
638
Jens Geyere9651362014-03-20 22:46:17 +0200639constructor TNamedPipeTransportClientEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
640 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000641// Named pipe constructor
642begin
Roger Meier79655fb2012-10-20 20:59:41 +0000643 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200644 FInputStream := THandlePipeStreamImpl.Create( aPipe, TRUE, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000645 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000646end;
647
648
Jens Geyer06045cf2013-03-27 20:26:25 +0200649{ TNamedPipeTransportServerEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000650
651
Jens Geyere9651362014-03-20 22:46:17 +0200652constructor TNamedPipeTransportServerEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
653 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000654// Named pipe constructor
Roger Meier3bef8c22012-10-06 06:58:00 +0000655begin
Roger Meier79655fb2012-10-20 20:59:41 +0000656 FHandle := DuplicatePipeHandle( aPipe);
Jens Geyere9651362014-03-20 22:46:17 +0200657 inherited Create( aPipe, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000658end;
659
660
Jens Geyer06045cf2013-03-27 20:26:25 +0200661procedure TNamedPipeTransportServerEndImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000662begin
Roger Meier79655fb2012-10-20 20:59:41 +0000663 FlushFileBuffers( FHandle);
664 DisconnectNamedPipe( FHandle); // force client off the pipe
665 ClosePipeHandle( FHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000666
Roger Meier79655fb2012-10-20 20:59:41 +0000667 inherited Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000668end;
669
670
Jens Geyer06045cf2013-03-27 20:26:25 +0200671{ TAnonymousPipeTransportImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000672
673
Jens Geyerdd074e72016-04-19 23:31:33 +0200674constructor TAnonymousPipeTransportImpl.Create( const aPipeRead, aPipeWrite : THandle;
675 aOwnsHandles : Boolean;
676 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +0000677// Anonymous pipe constructor
678begin
Roger Meier79655fb2012-10-20 20:59:41 +0000679 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200680 // overlapped is not supported with AnonPipes, see MSDN
Jens Geyerdd074e72016-04-19 23:31:33 +0200681 FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles, FALSE, aTimeOut);
682 FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles, FALSE, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000683end;
684
685
Jens Geyer06045cf2013-03-27 20:26:25 +0200686{ TPipeServerTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000687
688
Jens Geyere9651362014-03-20 22:46:17 +0200689constructor TPipeServerTransportBase.Create;
690begin
691 inherited Create;
692 FStopServer := TEvent.Create(nil,TRUE,FALSE,''); // manual reset
693end;
694
695
696destructor TPipeServerTransportBase.Destroy;
697begin
698 try
699 FreeAndNil( FStopServer);
700 finally
701 inherited Destroy;
702 end;
703end;
704
705
706function TPipeServerTransportBase.QueryStopServer : Boolean;
707begin
708 result := (FStopServer = nil)
709 or (FStopServer.WaitFor(0) <> wrTimeout);
710end;
711
712
Jens Geyer06045cf2013-03-27 20:26:25 +0200713procedure TPipeServerTransportBase.Listen;
Roger Meier3bef8c22012-10-06 06:58:00 +0000714begin
Jens Geyere9651362014-03-20 22:46:17 +0200715 FStopServer.ResetEvent;
Roger Meier3bef8c22012-10-06 06:58:00 +0000716end;
717
718
Jens Geyer06045cf2013-03-27 20:26:25 +0200719procedure TPipeServerTransportBase.Close;
720begin
Jens Geyere9651362014-03-20 22:46:17 +0200721 FStopServer.SetEvent;
Jens Geyer06045cf2013-03-27 20:26:25 +0200722 InternalClose;
723end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000724
725
Jens Geyer06045cf2013-03-27 20:26:25 +0200726{ TAnonymousPipeServerTransportImpl }
727
728
Jens Geyerdd074e72016-04-19 23:31:33 +0200729constructor TAnonymousPipeServerTransportImpl.Create(aBufsize : Cardinal; aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000730// Anonymous pipe CTOR
731begin
732 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000733 FBufsize := aBufSize;
Roger Meier79655fb2012-10-20 20:59:41 +0000734 FReadHandle := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000735 FWriteHandle := INVALID_HANDLE_VALUE;
736 FClientAnonRead := INVALID_HANDLE_VALUE;
737 FClientAnonWrite := INVALID_HANDLE_VALUE;
Jens Geyerdd074e72016-04-19 23:31:33 +0200738 FTimeOut := aTimeOut;
Roger Meier3bef8c22012-10-06 06:58:00 +0000739
740 // The anonymous pipe needs to be created first so that the server can
741 // pass the handles on to the client before the serve (acceptImpl)
742 // blocking call.
743 if not CreateAnonPipe
Jens Geyere0e32402016-04-20 21:50:48 +0200744 then raise TTransportExceptionNotOpen.Create(ClassName+'.Create() failed');
Roger Meier3bef8c22012-10-06 06:58:00 +0000745end;
746
747
Jens Geyer01640402013-09-25 21:12:21 +0200748function TAnonymousPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Roger Meier3bef8c22012-10-06 06:58:00 +0000749var buf : Byte;
750 br : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000751begin
Jens Geyer01640402013-09-25 21:12:21 +0200752 if Assigned(fnAccepting)
753 then fnAccepting();
754
Roger Meier79655fb2012-10-20 20:59:41 +0000755 // This 0-byte read serves merely as a blocking call.
756 if not ReadFile( FReadHandle, buf, 0, br, nil)
757 and (GetLastError() <> ERROR_MORE_DATA)
Jens Geyere0e32402016-04-20 21:50:48 +0200758 then raise TTransportExceptionNotOpen.Create('TServerPipe unable to initiate pipe communication');
Jens Geyer06045cf2013-03-27 20:26:25 +0200759
760 // create the transport impl
Jens Geyerdd074e72016-04-19 23:31:33 +0200761 result := TAnonymousPipeTransportImpl.Create( FReadHandle, FWriteHandle, FALSE, FTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000762end;
763
764
Jens Geyer06045cf2013-03-27 20:26:25 +0200765procedure TAnonymousPipeServerTransportImpl.InternalClose;
Roger Meier3bef8c22012-10-06 06:58:00 +0000766begin
Roger Meier79655fb2012-10-20 20:59:41 +0000767 ClosePipeHandle( FReadHandle);
768 ClosePipeHandle( FWriteHandle);
769 ClosePipeHandle( FClientAnonRead);
770 ClosePipeHandle( FClientAnonWrite);
Roger Meier3bef8c22012-10-06 06:58:00 +0000771end;
772
773
Jens Geyer06045cf2013-03-27 20:26:25 +0200774function TAnonymousPipeServerTransportImpl.ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000775begin
Roger Meier79655fb2012-10-20 20:59:41 +0000776 result := FReadHandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000777end;
778
779
Jens Geyer06045cf2013-03-27 20:26:25 +0200780function TAnonymousPipeServerTransportImpl.WriteHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000781begin
782 result := FWriteHandle;
783end;
784
785
Jens Geyer06045cf2013-03-27 20:26:25 +0200786function TAnonymousPipeServerTransportImpl.ClientAnonRead : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000787begin
788 result := FClientAnonRead;
789end;
790
791
Jens Geyer06045cf2013-03-27 20:26:25 +0200792function TAnonymousPipeServerTransportImpl.ClientAnonWrite : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000793begin
794 result := FClientAnonWrite;
795end;
796
797
Jens Geyer06045cf2013-03-27 20:26:25 +0200798function TAnonymousPipeServerTransportImpl.CreateAnonPipe : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000799var sd : PSECURITY_DESCRIPTOR;
800 sa : SECURITY_ATTRIBUTES; //TSecurityAttributes;
801 hCAR, hPipeW, hCAW, hPipe : THandle;
802begin
Roger Meier79655fb2012-10-20 20:59:41 +0000803 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
Jens Geyerb64a7742013-01-23 20:58:47 +0100804 try
805 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
806 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
Roger Meier79655fb2012-10-20 20:59:41 +0000807
Jens Geyerb64a7742013-01-23 20:58:47 +0100808 sa.nLength := sizeof( sa);
809 sa.lpSecurityDescriptor := sd;
810 sa.bInheritHandle := TRUE; //allow passing handle to child
Roger Meier79655fb2012-10-20 20:59:41 +0000811
Jens Geyer17c3ad92017-09-05 20:31:27 +0200812 Result := CreatePipe( hCAR, hPipeW, @sa, FBufSize); //create stdin pipe
813 if not Result then begin //create stdin pipe
Jens Geyere0e32402016-04-20 21:50:48 +0200814 raise TTransportExceptionNotOpen.Create('TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100815 Exit;
816 end;
817
Jens Geyer17c3ad92017-09-05 20:31:27 +0200818 Result := CreatePipe( hPipe, hCAW, @sa, FBufSize); //create stdout pipe
819 if not Result then begin //create stdout pipe
Jens Geyerb64a7742013-01-23 20:58:47 +0100820 CloseHandle( hCAR);
821 CloseHandle( hPipeW);
Jens Geyere0e32402016-04-20 21:50:48 +0200822 raise TTransportExceptionNotOpen.Create('TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100823 Exit;
824 end;
825
826 FClientAnonRead := hCAR;
827 FClientAnonWrite := hCAW;
828 FReadHandle := hPipe;
829 FWriteHandle := hPipeW;
Jens Geyerb64a7742013-01-23 20:58:47 +0100830 finally
831 if sd <> nil then LocalFree( Cardinal(sd));
Roger Meier79655fb2012-10-20 20:59:41 +0000832 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000833end;
834
835
Jens Geyer06045cf2013-03-27 20:26:25 +0200836{ TNamedPipeServerTransportImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000837
838
Jens Geyer06045cf2013-03-27 20:26:25 +0200839constructor TNamedPipeServerTransportImpl.Create( aPipename : string; aBufsize, aMaxConns, aTimeOut : Cardinal);
Roger Meier79655fb2012-10-20 20:59:41 +0000840// Named Pipe CTOR
841begin
842 inherited Create;
Jens Geyere9651362014-03-20 22:46:17 +0200843 ASSERT( aTimeout > 0);
Jens Geyer06045cf2013-03-27 20:26:25 +0200844 FPipeName := aPipename;
845 FBufsize := aBufSize;
846 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
847 FHandle := INVALID_HANDLE_VALUE;
848 FTimeout := aTimeOut;
849 FConnected := FALSE;
Roger Meier79655fb2012-10-20 20:59:41 +0000850
851 if Copy(FPipeName,1,2) <> '\\'
852 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
853end;
854
855
Jens Geyer01640402013-09-25 21:12:21 +0200856function TNamedPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Jens Geyer06045cf2013-03-27 20:26:25 +0200857var dwError, dwWait, dwDummy : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +0200858 overlapped : IOverlappedHelper;
859 handles : array[0..1] of THandle;
Jens Geyer01640402013-09-25 21:12:21 +0200860begin
Jens Geyere9651362014-03-20 22:46:17 +0200861 overlapped := TOverlappedHelperImpl.Create;
Jens Geyer01640402013-09-25 21:12:21 +0200862
Jens Geyere9651362014-03-20 22:46:17 +0200863 ASSERT( not FConnected);
Jens Geyer2ad6c302015-02-26 19:38:53 +0100864 CreateNamedPipe;
Jens Geyere9651362014-03-20 22:46:17 +0200865 while not FConnected do begin
Jens Geyer2ad6c302015-02-26 19:38:53 +0100866
867 if QueryStopServer
868 then Abort;
Roger Meier79655fb2012-10-20 20:59:41 +0000869
Jens Geyere9651362014-03-20 22:46:17 +0200870 if Assigned(fnAccepting)
871 then fnAccepting();
Jens Geyer01640402013-09-25 21:12:21 +0200872
Jens Geyere9651362014-03-20 22:46:17 +0200873 // Wait for the client to connect; if it succeeds, the
874 // function returns a nonzero value. If the function returns
875 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
876 if ConnectNamedPipe( Handle, overlapped.OverlappedPtr) then begin
877 FConnected := TRUE;
878 Break;
Jens Geyer01640402013-09-25 21:12:21 +0200879 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000880
Jens Geyere9651362014-03-20 22:46:17 +0200881 // ConnectNamedPipe() returns FALSE for OverlappedIO, even if connected.
882 // We have to check GetLastError() explicitly to find out
883 dwError := GetLastError;
884 case dwError of
885 ERROR_PIPE_CONNECTED : begin
886 FConnected := not QueryStopServer; // special case: pipe immediately connected
887 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000888
Jens Geyere9651362014-03-20 22:46:17 +0200889 ERROR_IO_PENDING : begin
890 handles[0] := overlapped.WaitHandle;
891 handles[1] := FStopServer.Handle;
892 dwWait := WaitForMultipleObjects( 2, @handles, FALSE, FTimeout);
893 FConnected := (dwWait = WAIT_OBJECT_0)
894 and GetOverlappedResult( Handle, overlapped.Overlapped, dwDummy, TRUE)
895 and not QueryStopServer;
896 end;
897
898 else
899 InternalClose;
Jens Geyere0e32402016-04-20 21:50:48 +0200900 raise TTransportExceptionNotOpen.Create('Client connection failed');
Jens Geyere9651362014-03-20 22:46:17 +0200901 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000902 end;
Jens Geyere9651362014-03-20 22:46:17 +0200903
904 // create the transport impl
905 result := CreateTransportInstance;
Roger Meier79655fb2012-10-20 20:59:41 +0000906end;
907
908
Jens Geyer06045cf2013-03-27 20:26:25 +0200909function TNamedPipeServerTransportImpl.CreateTransportInstance : ITransport;
910// create the transport impl
911var hPipe : THandle;
Roger Meier79655fb2012-10-20 20:59:41 +0000912begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200913 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
914 try
915 FConnected := FALSE;
Jens Geyere9651362014-03-20 22:46:17 +0200916 result := TNamedPipeTransportServerEndImpl.Create( hPipe, TRUE, FTimeout);
Jens Geyer06045cf2013-03-27 20:26:25 +0200917 except
Jens Geyer01640402013-09-25 21:12:21 +0200918 ClosePipeHandle(hPipe);
919 raise;
920 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000921end;
922
923
Jens Geyer06045cf2013-03-27 20:26:25 +0200924procedure TNamedPipeServerTransportImpl.InternalClose;
925var hPipe : THandle;
926begin
927 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
928 if hPipe = INVALID_HANDLE_VALUE then Exit;
929
930 try
931 if FConnected
932 then FlushFileBuffers( hPipe)
933 else CancelIo( hPipe);
934 DisconnectNamedPipe( hPipe);
935 finally
936 ClosePipeHandle( hPipe);
937 FConnected := FALSE;
938 end;
939end;
940
941
942function TNamedPipeServerTransportImpl.Handle : THandle;
943begin
944 {$IFDEF WIN64}
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200945 result := THandle( InterlockedExchangeAdd64( Int64(FHandle), 0));
Jens Geyer06045cf2013-03-27 20:26:25 +0200946 {$ELSE}
947 result := THandle( InterlockedExchangeAdd( Integer(FHandle), 0));
948 {$ENDIF}
949end;
950
951
952function TNamedPipeServerTransportImpl.CreateNamedPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000953var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
954 everyone_sid : PSID;
955 ea : EXPLICIT_ACCESS;
956 acl : PACL;
957 sd : PSECURITY_DESCRIPTOR;
958 sa : SECURITY_ATTRIBUTES;
Roger Meier3bef8c22012-10-06 06:58:00 +0000959const
960 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
961 SECURITY_WORLD_RID = $00000000;
962begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100963 sd := nil;
Roger Meier3bef8c22012-10-06 06:58:00 +0000964 everyone_sid := nil;
Jens Geyerb64a7742013-01-23 20:58:47 +0100965 try
Jens Geyer06045cf2013-03-27 20:26:25 +0200966 ASSERT( (FHandle = INVALID_HANDLE_VALUE) and not FConnected);
967
Jens Geyerb64a7742013-01-23 20:58:47 +0100968 // Windows - set security to allow non-elevated apps
969 // to access pipes created by elevated apps.
970 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
971 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000972
Jens Geyerb64a7742013-01-23 20:58:47 +0100973 ZeroMemory( @ea, SizeOf(ea));
974 ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
975 ea.grfAccessMode := SET_ACCESS;
976 ea.grfInheritance := NO_INHERITANCE;
977 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
978 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
979 ea.Trustee.ptstrName := PChar(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000980
Jens Geyerb64a7742013-01-23 20:58:47 +0100981 acl := nil;
982 SetEntriesInAcl( 1, @ea, nil, acl);
Roger Meier3bef8c22012-10-06 06:58:00 +0000983
Jens Geyerb64a7742013-01-23 20:58:47 +0100984 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
985 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
986 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
Roger Meier3bef8c22012-10-06 06:58:00 +0000987
Jens Geyerb64a7742013-01-23 20:58:47 +0100988 sa.nLength := SizeOf(sa);
989 sa.lpSecurityDescriptor := sd;
990 sa.bInheritHandle := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000991
Jens Geyerb64a7742013-01-23 20:58:47 +0100992 // Create an instance of the named pipe
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200993 {$IFDEF OLD_UNIT_NAMES}
994 result := Windows.CreateNamedPipe(
995 {$ELSE}
996 result := Winapi.Windows.CreateNamedPipe(
997 {$ENDIF}
998 PChar( FPipeName), // pipe name
999 PIPE_ACCESS_DUPLEX or // read/write access
1000 FILE_FLAG_OVERLAPPED, // async mode
1001 PIPE_TYPE_BYTE or // byte type pipe
1002 PIPE_READMODE_BYTE, // byte read mode
1003 FMaxConns, // max. instances
1004 FBufSize, // output buffer size
1005 FBufSize, // input buffer size
1006 FTimeout, // time-out, see MSDN
1007 @sa // default security attribute
1008 );
Roger Meier3bef8c22012-10-06 06:58:00 +00001009
Jens Geyer06045cf2013-03-27 20:26:25 +02001010 if( result <> INVALID_HANDLE_VALUE)
1011 then InterlockedExchangePointer( Pointer(FHandle), Pointer(result))
Jens Geyere0e32402016-04-20 21:50:48 +02001012 else raise TTransportExceptionNotOpen.Create('CreateNamedPipe() failed ' + IntToStr(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +01001013
1014 finally
1015 if sd <> nil then LocalFree( Cardinal( sd));
1016 if acl <> nil then LocalFree( Cardinal( acl));
1017 if everyone_sid <> nil then FreeSid(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +00001018 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001019end;
1020
1021
Roger Meier3bef8c22012-10-06 06:58:00 +00001022
1023end.
1024
1025
1026