blob: 77a343b0cac8b6499566ffb0583afe8b562e2aee [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;
Jens Geyer85827152018-01-12 21:20:59 +0100331 pData : PByte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000332begin
333 if not IsOpen
Jens Geyere0e32402016-04-20 21:50:48 +0200334 then raise TTransportExceptionNotOpen.Create('Called write on non-open pipe');
Roger Meier3bef8c22012-10-06 06:58:00 +0000335
Jens Geyerd4df9172017-10-25 22:30:23 +0200336 // if necessary, send the data in chunks
337 // there's a system limit around 0x10000 bytes that we hit otherwise
338 // MSDN: "Pipe write operations across a network are limited to 65,535 bytes per write. For more information regarding pipes, see the Remarks section."
339 nBytes := Min( 15*4096, count); // 16 would exceed the limit
Jens Geyer85827152018-01-12 21:20:59 +0100340 pData := pBuf;
341 Inc( pData, offset);
Jens Geyerd4df9172017-10-25 22:30:23 +0200342 while nBytes > 0 do begin
Jens Geyer85827152018-01-12 21:20:59 +0100343 if not WriteFile( FPipe, pData^, nBytes, cbWritten, nil)
Jens Geyerd4df9172017-10-25 22:30:23 +0200344 then raise TTransportExceptionNotOpen.Create('Write to pipe failed');
345
Jens Geyer85827152018-01-12 21:20:59 +0100346 Inc( pData, cbWritten);
Jens Geyerd4df9172017-10-25 22:30:23 +0200347 Dec( count, cbWritten);
348 nBytes := Min( nBytes, count);
349 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000350end;
351
352
Jens Geyer17c3ad92017-09-05 20:31:27 +0200353procedure TPipeStreamBase.WriteOverlapped( const pBuf : Pointer; offset: Integer; count: Integer);
Jens Geyerd4df9172017-10-25 22:30:23 +0200354var cbWritten, dwWait, dwError, nBytes : DWORD;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200355 overlapped : IOverlappedHelper;
Jens Geyer85827152018-01-12 21:20:59 +0100356 pData : PByte;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200357begin
358 if not IsOpen
359 then raise TTransportExceptionNotOpen.Create('Called write on non-open pipe');
360
Jens Geyerd4df9172017-10-25 22:30:23 +0200361 // if necessary, send the data in chunks
362 // there's a system limit around 0x10000 bytes that we hit otherwise
363 // MSDN: "Pipe write operations across a network are limited to 65,535 bytes per write. For more information regarding pipes, see the Remarks section."
364 nBytes := Min( 15*4096, count); // 16 would exceed the limit
Jens Geyer85827152018-01-12 21:20:59 +0100365 pData := pBuf;
366 Inc( pData, offset);
Jens Geyerd4df9172017-10-25 22:30:23 +0200367 while nBytes > 0 do begin
368 overlapped := TOverlappedHelperImpl.Create;
Jens Geyer85827152018-01-12 21:20:59 +0100369 if not WriteFile( FPipe, pData^, nBytes, cbWritten, overlapped.OverlappedPtr)
Jens Geyerd4df9172017-10-25 22:30:23 +0200370 then begin
371 dwError := GetLastError;
372 case dwError of
373 ERROR_IO_PENDING : begin
374 dwWait := overlapped.WaitFor(FTimeout);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200375
Jens Geyer00645162018-02-01 23:38:10 +0100376 if (dwWait = WAIT_TIMEOUT) then begin
377 CancelIo( FPipe); // prevents possible AV on invalid overlapped ptr
378 raise TTransportExceptionTimedOut.Create('Pipe write timed out');
379 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200380
Jens Geyerd4df9172017-10-25 22:30:23 +0200381 if (dwWait <> WAIT_OBJECT_0)
382 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbWritten, TRUE)
383 then raise TTransportExceptionUnknown.Create('Pipe write error');
384 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200385
Jens Geyerd4df9172017-10-25 22:30:23 +0200386 else
387 raise TTransportExceptionUnknown.Create(SysErrorMessage(dwError));
Jens Geyer17c3ad92017-09-05 20:31:27 +0200388 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200389 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200390
Jens Geyerd4df9172017-10-25 22:30:23 +0200391 ASSERT( DWORD(nBytes) = cbWritten);
392
Jens Geyer85827152018-01-12 21:20:59 +0100393 Inc( pData, cbWritten);
Jens Geyerd4df9172017-10-25 22:30:23 +0200394 Dec( count, cbWritten);
395 nBytes := Min( nBytes, count);
396 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200397end;
398
399
400function TPipeStreamBase.ReadDirect( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer;
Jens Geyerd4df9172017-10-25 22:30:23 +0200401var cbRead, dwErr, nRemaining : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000402 bytes, retries : LongInt;
403 bOk : Boolean;
Jens Geyer85827152018-01-12 21:20:59 +0100404 pData : PByte;
Roger Meier3bef8c22012-10-06 06:58:00 +0000405const INTERVAL = 10; // ms
406begin
407 if not IsOpen
Jens Geyere0e32402016-04-20 21:50:48 +0200408 then raise TTransportExceptionNotOpen.Create('Called read on non-open pipe');
Roger Meier3bef8c22012-10-06 06:58:00 +0000409
410 // MSDN: Handle can be a handle to a named pipe instance,
411 // or it can be a handle to the read end of an anonymous pipe,
412 // The handle must have GENERIC_READ access to the pipe.
413 if FTimeOut <> INFINITE then begin
414 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
415 while TRUE do begin
Jens Geyer5988f482016-04-19 23:01:24 +0200416 if not PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) then begin
417 dwErr := GetLastError;
418 if (dwErr = ERROR_INVALID_HANDLE)
419 or (dwErr = ERROR_BROKEN_PIPE)
420 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
421 then begin
422 result := 0; // other side closed the pipe
423 Exit;
424 end;
425 end
426 else if bytes > 0 then begin
427 Break; // there are data
Roger Meier79655fb2012-10-20 20:59:41 +0000428 end;
429
Roger Meier3bef8c22012-10-06 06:58:00 +0000430 Dec( retries);
431 if retries > 0
432 then Sleep( INTERVAL)
Jens Geyere0e32402016-04-20 21:50:48 +0200433 else raise TTransportExceptionTimedOut.Create('Pipe read timed out');
Roger Meier3bef8c22012-10-06 06:58:00 +0000434 end;
435 end;
436
Jens Geyerd4df9172017-10-25 22:30:23 +0200437 result := 0;
438 nRemaining := count;
Jens Geyer85827152018-01-12 21:20:59 +0100439 pData := pBuf;
440 Inc( pData, offset);
Jens Geyerd4df9172017-10-25 22:30:23 +0200441 while nRemaining > 0 do begin
442 // read the data (or block INFINITE-ly)
Jens Geyer85827152018-01-12 21:20:59 +0100443 bOk := ReadFile( FPipe, pData^, nRemaining, cbRead, nil);
Jens Geyerd4df9172017-10-25 22:30:23 +0200444 if (not bOk) and (GetLastError() <> ERROR_MORE_DATA)
445 then Break; // No more data, possibly because client disconnected.
446
447 Dec( nRemaining, cbRead);
Jens Geyer85827152018-01-12 21:20:59 +0100448 Inc( pData, cbRead);
Jens Geyerd4df9172017-10-25 22:30:23 +0200449 Inc( result, cbRead);
450 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000451end;
452
453
Jens Geyer17c3ad92017-09-05 20:31:27 +0200454function TPipeStreamBase.ReadOverlapped( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer;
Jens Geyerd4df9172017-10-25 22:30:23 +0200455var cbRead, dwWait, dwError, nRemaining : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +0200456 bOk : Boolean;
457 overlapped : IOverlappedHelper;
Jens Geyer85827152018-01-12 21:20:59 +0100458 pData : PByte;
Jens Geyere9651362014-03-20 22:46:17 +0200459begin
460 if not IsOpen
Jens Geyere0e32402016-04-20 21:50:48 +0200461 then raise TTransportExceptionNotOpen.Create('Called read on non-open pipe');
Jens Geyere9651362014-03-20 22:46:17 +0200462
Jens Geyerd4df9172017-10-25 22:30:23 +0200463 result := 0;
464 nRemaining := count;
Jens Geyer85827152018-01-12 21:20:59 +0100465 pData := pBuf;
466 Inc( pData, offset);
Jens Geyerd4df9172017-10-25 22:30:23 +0200467 while nRemaining > 0 do begin
468 overlapped := TOverlappedHelperImpl.Create;
Jens Geyere9651362014-03-20 22:46:17 +0200469
Jens Geyerd4df9172017-10-25 22:30:23 +0200470 // read the data
Jens Geyer85827152018-01-12 21:20:59 +0100471 bOk := ReadFile( FPipe, pData^, nRemaining, cbRead, overlapped.OverlappedPtr);
Jens Geyerd4df9172017-10-25 22:30:23 +0200472 if not bOk then begin
473 dwError := GetLastError;
474 case dwError of
475 ERROR_IO_PENDING : begin
476 dwWait := overlapped.WaitFor(FTimeout);
Jens Geyere9651362014-03-20 22:46:17 +0200477
Jens Geyer00645162018-02-01 23:38:10 +0100478 if (dwWait = WAIT_TIMEOUT) then begin
479 CancelIo( FPipe); // prevents possible AV on invalid overlapped ptr
480 raise TTransportExceptionTimedOut.Create('Pipe read timed out');
481 end;
Jens Geyere9651362014-03-20 22:46:17 +0200482
Jens Geyerd4df9172017-10-25 22:30:23 +0200483 if (dwWait <> WAIT_OBJECT_0)
484 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbRead, TRUE)
485 then raise TTransportExceptionUnknown.Create('Pipe read error');
486 end;
487
488 else
489 raise TTransportExceptionUnknown.Create(SysErrorMessage(dwError));
Jens Geyere9651362014-03-20 22:46:17 +0200490 end;
Jens Geyere9651362014-03-20 22:46:17 +0200491 end;
Jens Geyere9651362014-03-20 22:46:17 +0200492
Jens Geyerd4df9172017-10-25 22:30:23 +0200493 ASSERT( cbRead > 0); // see TTransportImpl.ReadAll()
494 ASSERT( cbRead <= DWORD(nRemaining));
495 Dec( nRemaining, cbRead);
Jens Geyer85827152018-01-12 21:20:59 +0100496 Inc( pData, cbRead);
Jens Geyerd4df9172017-10-25 22:30:23 +0200497 Inc( result, cbRead);
498 end;
Jens Geyere9651362014-03-20 22:46:17 +0200499end;
500
501
Jens Geyer06045cf2013-03-27 20:26:25 +0200502function TPipeStreamBase.ToArray: TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000503var bytes : LongInt;
504begin
505 SetLength( result, 0);
506 bytes := 0;
507
508 if IsOpen
509 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
510 and (bytes > 0)
511 then begin
512 SetLength( result, bytes);
513 Read( result, 0, bytes);
514 end;
515end;
516
517
Roger Meier79655fb2012-10-20 20:59:41 +0000518{ TNamedPipeStreamImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000519
520
Jens Geyere9651362014-03-20 22:46:17 +0200521constructor TNamedPipeStreamImpl.Create( const aPipeName : string;
522 const aEnableOverlapped : Boolean;
523 const aShareMode: DWORD;
Roger Meier79655fb2012-10-20 20:59:41 +0000524 const aSecurityAttributes: PSecurityAttributes;
Jens Geyer653f0de2016-04-20 12:46:57 +0200525 const aTimeOut, aOpenTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000526begin
Jens Geyer653f0de2016-04-20 12:46:57 +0200527 inherited Create( aEnableOverlapped, aTimeout, aOpenTimeOut);
Roger Meier79655fb2012-10-20 20:59:41 +0000528
529 FPipeName := aPipeName;
530 FShareMode := aShareMode;
531 FSecurityAttribs := aSecurityAttributes;
532
533 if Copy(FPipeName,1,2) <> '\\'
534 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
Roger Meier3bef8c22012-10-06 06:58:00 +0000535end;
536
537
Roger Meier79655fb2012-10-20 20:59:41 +0000538procedure TNamedPipeStreamImpl.Open;
539var hPipe : THandle;
Jens Geyerb89b5b92016-04-19 23:09:41 +0200540 retries, timeout, dwErr : DWORD;
541const INTERVAL = 10; // ms
Roger Meier79655fb2012-10-20 20:59:41 +0000542begin
543 if IsOpen then Exit;
544
Jens Geyer653f0de2016-04-20 12:46:57 +0200545 retries := Max( 1, Round( 1.0 * FOpenTimeOut / INTERVAL));
546 timeout := FOpenTimeOut;
Jens Geyerb89b5b92016-04-19 23:09:41 +0200547
548 // if the server hasn't gotten to the point where the pipe has been created, at least wait the timeout
549 // According to MSDN, if no instances of the specified named pipe exist, the WaitNamedPipe function
550 // returns IMMEDIATELY, regardless of the time-out value.
Jens Geyer653f0de2016-04-20 12:46:57 +0200551 // Always use INTERVAL, since WaitNamedPipe(0) defaults to some other value
Jens Geyerb89b5b92016-04-19 23:09:41 +0200552 while not WaitNamedPipe( PChar(FPipeName), INTERVAL) do begin
553 dwErr := GetLastError;
554 if dwErr <> ERROR_FILE_NOT_FOUND
Jens Geyere0e32402016-04-20 21:50:48 +0200555 then raise TTransportExceptionNotOpen.Create('Unable to open pipe, '+SysErrorMessage(dwErr));
Jens Geyerb89b5b92016-04-19 23:09:41 +0200556
557 if timeout <> INFINITE then begin
558 if (retries > 0)
559 then Dec(retries)
Jens Geyere0e32402016-04-20 21:50:48 +0200560 else raise TTransportExceptionNotOpen.Create('Unable to open pipe, timed out');
Jens Geyerb89b5b92016-04-19 23:09:41 +0200561 end;
562
563 Sleep(INTERVAL)
564 end;
565
Roger Meier79655fb2012-10-20 20:59:41 +0000566 // open that thingy
Roger Meier79655fb2012-10-20 20:59:41 +0000567 hPipe := CreateFile( PChar( FPipeName),
568 GENERIC_READ or GENERIC_WRITE,
569 FShareMode, // sharing
570 FSecurityAttribs, // security attributes
571 OPEN_EXISTING, // opens existing pipe
Jens Geyere9651362014-03-20 22:46:17 +0200572 FILE_FLAG_OVERLAPPED or FILE_FLAG_WRITE_THROUGH, // async+fast, please
Roger Meier79655fb2012-10-20 20:59:41 +0000573 0); // no template file
574
575 if hPipe = INVALID_HANDLE_VALUE
Jens Geyere0e32402016-04-20 21:50:48 +0200576 then raise TTransportExceptionNotOpen.Create('Unable to open pipe, '+SysErrorMessage(GetLastError));
Roger Meier79655fb2012-10-20 20:59:41 +0000577
Roger Meier79655fb2012-10-20 20:59:41 +0000578 // everything fine
579 FPipe := hPipe;
580end;
581
582
583{ THandlePipeStreamImpl }
584
585
Jens Geyere9651362014-03-20 22:46:17 +0200586constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle;
587 const aOwnsHandle, aEnableOverlapped : Boolean;
588 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000589begin
Jens Geyere9651362014-03-20 22:46:17 +0200590 inherited Create( aEnableOverlapped, aTimeOut);
Roger Meier79655fb2012-10-20 20:59:41 +0000591
592 if aOwnsHandle
593 then FSrcHandle := aPipeHandle
594 else FSrcHandle := DuplicatePipeHandle( aPipeHandle);
595
596 Open;
597end;
598
599
600destructor THandlePipeStreamImpl.Destroy;
601begin
602 try
603 ClosePipeHandle( FSrcHandle);
604 finally
605 inherited Destroy;
606 end;
607end;
608
609
610procedure THandlePipeStreamImpl.Open;
611begin
612 if not IsOpen
613 then FPipe := DuplicatePipeHandle( FSrcHandle);
614end;
615
616
Jens Geyer06045cf2013-03-27 20:26:25 +0200617{ TPipeTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000618
619
Jens Geyer06045cf2013-03-27 20:26:25 +0200620function TPipeTransportBase.GetIsOpen: Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000621begin
Jens Geyer0b20cc82013-03-07 20:47:01 +0100622 result := (FInputStream <> nil) and (FInputStream.IsOpen)
623 and (FOutputStream <> nil) and (FOutputStream.IsOpen);
Roger Meier79655fb2012-10-20 20:59:41 +0000624end;
625
626
Jens Geyer06045cf2013-03-27 20:26:25 +0200627procedure TPipeTransportBase.Open;
Roger Meier79655fb2012-10-20 20:59:41 +0000628begin
629 FInputStream.Open;
630 FOutputStream.Open;
631end;
632
633
Jens Geyer06045cf2013-03-27 20:26:25 +0200634procedure TPipeTransportBase.Close;
Roger Meier79655fb2012-10-20 20:59:41 +0000635begin
636 FInputStream.Close;
637 FOutputStream.Close;
638end;
639
640
Jens Geyer06045cf2013-03-27 20:26:25 +0200641{ TNamedPipeTransportClientEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000642
643
Jens Geyer06045cf2013-03-27 20:26:25 +0200644constructor TNamedPipeTransportClientEndImpl.Create( const aPipeName : string; const aShareMode: DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000645 const aSecurityAttributes: PSecurityAttributes;
Jens Geyer653f0de2016-04-20 12:46:57 +0200646 const aTimeOut, aOpenTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000647// Named pipe constructor
648begin
Roger Meier79655fb2012-10-20 20:59:41 +0000649 inherited Create( nil, nil);
Jens Geyer653f0de2016-04-20 12:46:57 +0200650 FInputStream := TNamedPipeStreamImpl.Create( aPipeName, TRUE, aShareMode, aSecurityAttributes, aTimeOut, aOpenTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000651 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000652end;
653
654
Jens Geyere9651362014-03-20 22:46:17 +0200655constructor TNamedPipeTransportClientEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
656 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000657// Named pipe constructor
658begin
Roger Meier79655fb2012-10-20 20:59:41 +0000659 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200660 FInputStream := THandlePipeStreamImpl.Create( aPipe, TRUE, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000661 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000662end;
663
664
Jens Geyer06045cf2013-03-27 20:26:25 +0200665{ TNamedPipeTransportServerEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000666
667
Jens Geyere9651362014-03-20 22:46:17 +0200668constructor TNamedPipeTransportServerEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
669 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000670// Named pipe constructor
Roger Meier3bef8c22012-10-06 06:58:00 +0000671begin
Roger Meier79655fb2012-10-20 20:59:41 +0000672 FHandle := DuplicatePipeHandle( aPipe);
Jens Geyere9651362014-03-20 22:46:17 +0200673 inherited Create( aPipe, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000674end;
675
676
Jens Geyer06045cf2013-03-27 20:26:25 +0200677procedure TNamedPipeTransportServerEndImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000678begin
Roger Meier79655fb2012-10-20 20:59:41 +0000679 FlushFileBuffers( FHandle);
680 DisconnectNamedPipe( FHandle); // force client off the pipe
681 ClosePipeHandle( FHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000682
Roger Meier79655fb2012-10-20 20:59:41 +0000683 inherited Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000684end;
685
686
Jens Geyer06045cf2013-03-27 20:26:25 +0200687{ TAnonymousPipeTransportImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000688
689
Jens Geyerdd074e72016-04-19 23:31:33 +0200690constructor TAnonymousPipeTransportImpl.Create( const aPipeRead, aPipeWrite : THandle;
691 aOwnsHandles : Boolean;
692 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +0000693// Anonymous pipe constructor
694begin
Roger Meier79655fb2012-10-20 20:59:41 +0000695 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200696 // overlapped is not supported with AnonPipes, see MSDN
Jens Geyerdd074e72016-04-19 23:31:33 +0200697 FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles, FALSE, aTimeOut);
698 FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles, FALSE, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000699end;
700
701
Jens Geyer06045cf2013-03-27 20:26:25 +0200702{ TPipeServerTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000703
704
Jens Geyere9651362014-03-20 22:46:17 +0200705constructor TPipeServerTransportBase.Create;
706begin
707 inherited Create;
708 FStopServer := TEvent.Create(nil,TRUE,FALSE,''); // manual reset
709end;
710
711
712destructor TPipeServerTransportBase.Destroy;
713begin
714 try
715 FreeAndNil( FStopServer);
716 finally
717 inherited Destroy;
718 end;
719end;
720
721
722function TPipeServerTransportBase.QueryStopServer : Boolean;
723begin
724 result := (FStopServer = nil)
725 or (FStopServer.WaitFor(0) <> wrTimeout);
726end;
727
728
Jens Geyer06045cf2013-03-27 20:26:25 +0200729procedure TPipeServerTransportBase.Listen;
Roger Meier3bef8c22012-10-06 06:58:00 +0000730begin
Jens Geyere9651362014-03-20 22:46:17 +0200731 FStopServer.ResetEvent;
Roger Meier3bef8c22012-10-06 06:58:00 +0000732end;
733
734
Jens Geyer06045cf2013-03-27 20:26:25 +0200735procedure TPipeServerTransportBase.Close;
736begin
Jens Geyere9651362014-03-20 22:46:17 +0200737 FStopServer.SetEvent;
Jens Geyer06045cf2013-03-27 20:26:25 +0200738 InternalClose;
739end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000740
741
Jens Geyer06045cf2013-03-27 20:26:25 +0200742{ TAnonymousPipeServerTransportImpl }
743
744
Jens Geyerdd074e72016-04-19 23:31:33 +0200745constructor TAnonymousPipeServerTransportImpl.Create(aBufsize : Cardinal; aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000746// Anonymous pipe CTOR
747begin
748 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000749 FBufsize := aBufSize;
Roger Meier79655fb2012-10-20 20:59:41 +0000750 FReadHandle := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000751 FWriteHandle := INVALID_HANDLE_VALUE;
752 FClientAnonRead := INVALID_HANDLE_VALUE;
753 FClientAnonWrite := INVALID_HANDLE_VALUE;
Jens Geyerdd074e72016-04-19 23:31:33 +0200754 FTimeOut := aTimeOut;
Roger Meier3bef8c22012-10-06 06:58:00 +0000755
756 // The anonymous pipe needs to be created first so that the server can
757 // pass the handles on to the client before the serve (acceptImpl)
758 // blocking call.
759 if not CreateAnonPipe
Jens Geyere0e32402016-04-20 21:50:48 +0200760 then raise TTransportExceptionNotOpen.Create(ClassName+'.Create() failed');
Roger Meier3bef8c22012-10-06 06:58:00 +0000761end;
762
763
Jens Geyer01640402013-09-25 21:12:21 +0200764function TAnonymousPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Roger Meier3bef8c22012-10-06 06:58:00 +0000765var buf : Byte;
766 br : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000767begin
Jens Geyer01640402013-09-25 21:12:21 +0200768 if Assigned(fnAccepting)
769 then fnAccepting();
770
Roger Meier79655fb2012-10-20 20:59:41 +0000771 // This 0-byte read serves merely as a blocking call.
772 if not ReadFile( FReadHandle, buf, 0, br, nil)
773 and (GetLastError() <> ERROR_MORE_DATA)
Jens Geyere0e32402016-04-20 21:50:48 +0200774 then raise TTransportExceptionNotOpen.Create('TServerPipe unable to initiate pipe communication');
Jens Geyer06045cf2013-03-27 20:26:25 +0200775
776 // create the transport impl
Jens Geyerdd074e72016-04-19 23:31:33 +0200777 result := TAnonymousPipeTransportImpl.Create( FReadHandle, FWriteHandle, FALSE, FTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000778end;
779
780
Jens Geyer06045cf2013-03-27 20:26:25 +0200781procedure TAnonymousPipeServerTransportImpl.InternalClose;
Roger Meier3bef8c22012-10-06 06:58:00 +0000782begin
Roger Meier79655fb2012-10-20 20:59:41 +0000783 ClosePipeHandle( FReadHandle);
784 ClosePipeHandle( FWriteHandle);
785 ClosePipeHandle( FClientAnonRead);
786 ClosePipeHandle( FClientAnonWrite);
Roger Meier3bef8c22012-10-06 06:58:00 +0000787end;
788
789
Jens Geyer06045cf2013-03-27 20:26:25 +0200790function TAnonymousPipeServerTransportImpl.ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000791begin
Roger Meier79655fb2012-10-20 20:59:41 +0000792 result := FReadHandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000793end;
794
795
Jens Geyer06045cf2013-03-27 20:26:25 +0200796function TAnonymousPipeServerTransportImpl.WriteHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000797begin
798 result := FWriteHandle;
799end;
800
801
Jens Geyer06045cf2013-03-27 20:26:25 +0200802function TAnonymousPipeServerTransportImpl.ClientAnonRead : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000803begin
804 result := FClientAnonRead;
805end;
806
807
Jens Geyer06045cf2013-03-27 20:26:25 +0200808function TAnonymousPipeServerTransportImpl.ClientAnonWrite : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000809begin
810 result := FClientAnonWrite;
811end;
812
813
Jens Geyer06045cf2013-03-27 20:26:25 +0200814function TAnonymousPipeServerTransportImpl.CreateAnonPipe : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000815var sd : PSECURITY_DESCRIPTOR;
816 sa : SECURITY_ATTRIBUTES; //TSecurityAttributes;
817 hCAR, hPipeW, hCAW, hPipe : THandle;
818begin
Roger Meier79655fb2012-10-20 20:59:41 +0000819 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
Jens Geyerb64a7742013-01-23 20:58:47 +0100820 try
821 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
822 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
Roger Meier79655fb2012-10-20 20:59:41 +0000823
Jens Geyerb64a7742013-01-23 20:58:47 +0100824 sa.nLength := sizeof( sa);
825 sa.lpSecurityDescriptor := sd;
826 sa.bInheritHandle := TRUE; //allow passing handle to child
Roger Meier79655fb2012-10-20 20:59:41 +0000827
Jens Geyer17c3ad92017-09-05 20:31:27 +0200828 Result := CreatePipe( hCAR, hPipeW, @sa, FBufSize); //create stdin pipe
829 if not Result then begin //create stdin pipe
Jens Geyere0e32402016-04-20 21:50:48 +0200830 raise TTransportExceptionNotOpen.Create('TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100831 Exit;
832 end;
833
Jens Geyer17c3ad92017-09-05 20:31:27 +0200834 Result := CreatePipe( hPipe, hCAW, @sa, FBufSize); //create stdout pipe
835 if not Result then begin //create stdout pipe
Jens Geyerb64a7742013-01-23 20:58:47 +0100836 CloseHandle( hCAR);
837 CloseHandle( hPipeW);
Jens Geyere0e32402016-04-20 21:50:48 +0200838 raise TTransportExceptionNotOpen.Create('TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100839 Exit;
840 end;
841
842 FClientAnonRead := hCAR;
843 FClientAnonWrite := hCAW;
844 FReadHandle := hPipe;
845 FWriteHandle := hPipeW;
Jens Geyerb64a7742013-01-23 20:58:47 +0100846 finally
847 if sd <> nil then LocalFree( Cardinal(sd));
Roger Meier79655fb2012-10-20 20:59:41 +0000848 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000849end;
850
851
Jens Geyer06045cf2013-03-27 20:26:25 +0200852{ TNamedPipeServerTransportImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000853
854
Jens Geyer06045cf2013-03-27 20:26:25 +0200855constructor TNamedPipeServerTransportImpl.Create( aPipename : string; aBufsize, aMaxConns, aTimeOut : Cardinal);
Roger Meier79655fb2012-10-20 20:59:41 +0000856// Named Pipe CTOR
857begin
858 inherited Create;
Jens Geyere9651362014-03-20 22:46:17 +0200859 ASSERT( aTimeout > 0);
Jens Geyer06045cf2013-03-27 20:26:25 +0200860 FPipeName := aPipename;
861 FBufsize := aBufSize;
862 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
863 FHandle := INVALID_HANDLE_VALUE;
864 FTimeout := aTimeOut;
865 FConnected := FALSE;
Roger Meier79655fb2012-10-20 20:59:41 +0000866
867 if Copy(FPipeName,1,2) <> '\\'
868 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
869end;
870
871
Jens Geyer01640402013-09-25 21:12:21 +0200872function TNamedPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Jens Geyer06045cf2013-03-27 20:26:25 +0200873var dwError, dwWait, dwDummy : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +0200874 overlapped : IOverlappedHelper;
875 handles : array[0..1] of THandle;
Jens Geyer01640402013-09-25 21:12:21 +0200876begin
Jens Geyere9651362014-03-20 22:46:17 +0200877 overlapped := TOverlappedHelperImpl.Create;
Jens Geyer01640402013-09-25 21:12:21 +0200878
Jens Geyere9651362014-03-20 22:46:17 +0200879 ASSERT( not FConnected);
Jens Geyer2ad6c302015-02-26 19:38:53 +0100880 CreateNamedPipe;
Jens Geyere9651362014-03-20 22:46:17 +0200881 while not FConnected do begin
Jens Geyer2ad6c302015-02-26 19:38:53 +0100882
Jens Geyer00645162018-02-01 23:38:10 +0100883 if QueryStopServer then begin
884 InternalClose;
885 Abort;
886 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000887
Jens Geyere9651362014-03-20 22:46:17 +0200888 if Assigned(fnAccepting)
889 then fnAccepting();
Jens Geyer01640402013-09-25 21:12:21 +0200890
Jens Geyere9651362014-03-20 22:46:17 +0200891 // Wait for the client to connect; if it succeeds, the
892 // function returns a nonzero value. If the function returns
893 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
894 if ConnectNamedPipe( Handle, overlapped.OverlappedPtr) then begin
895 FConnected := TRUE;
896 Break;
Jens Geyer01640402013-09-25 21:12:21 +0200897 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000898
Jens Geyere9651362014-03-20 22:46:17 +0200899 // ConnectNamedPipe() returns FALSE for OverlappedIO, even if connected.
900 // We have to check GetLastError() explicitly to find out
901 dwError := GetLastError;
902 case dwError of
903 ERROR_PIPE_CONNECTED : begin
904 FConnected := not QueryStopServer; // special case: pipe immediately connected
905 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000906
Jens Geyere9651362014-03-20 22:46:17 +0200907 ERROR_IO_PENDING : begin
908 handles[0] := overlapped.WaitHandle;
909 handles[1] := FStopServer.Handle;
910 dwWait := WaitForMultipleObjects( 2, @handles, FALSE, FTimeout);
911 FConnected := (dwWait = WAIT_OBJECT_0)
912 and GetOverlappedResult( Handle, overlapped.Overlapped, dwDummy, TRUE)
913 and not QueryStopServer;
914 end;
915
916 else
917 InternalClose;
Jens Geyere0e32402016-04-20 21:50:48 +0200918 raise TTransportExceptionNotOpen.Create('Client connection failed');
Jens Geyere9651362014-03-20 22:46:17 +0200919 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000920 end;
Jens Geyere9651362014-03-20 22:46:17 +0200921
922 // create the transport impl
923 result := CreateTransportInstance;
Roger Meier79655fb2012-10-20 20:59:41 +0000924end;
925
926
Jens Geyer06045cf2013-03-27 20:26:25 +0200927function TNamedPipeServerTransportImpl.CreateTransportInstance : ITransport;
928// create the transport impl
929var hPipe : THandle;
Roger Meier79655fb2012-10-20 20:59:41 +0000930begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200931 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
932 try
933 FConnected := FALSE;
Jens Geyere9651362014-03-20 22:46:17 +0200934 result := TNamedPipeTransportServerEndImpl.Create( hPipe, TRUE, FTimeout);
Jens Geyer06045cf2013-03-27 20:26:25 +0200935 except
Jens Geyer01640402013-09-25 21:12:21 +0200936 ClosePipeHandle(hPipe);
937 raise;
938 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000939end;
940
941
Jens Geyer06045cf2013-03-27 20:26:25 +0200942procedure TNamedPipeServerTransportImpl.InternalClose;
943var hPipe : THandle;
944begin
945 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
946 if hPipe = INVALID_HANDLE_VALUE then Exit;
947
948 try
949 if FConnected
950 then FlushFileBuffers( hPipe)
951 else CancelIo( hPipe);
952 DisconnectNamedPipe( hPipe);
953 finally
954 ClosePipeHandle( hPipe);
955 FConnected := FALSE;
956 end;
957end;
958
959
960function TNamedPipeServerTransportImpl.Handle : THandle;
961begin
962 {$IFDEF WIN64}
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200963 result := THandle( InterlockedExchangeAdd64( Int64(FHandle), 0));
Jens Geyer06045cf2013-03-27 20:26:25 +0200964 {$ELSE}
965 result := THandle( InterlockedExchangeAdd( Integer(FHandle), 0));
966 {$ENDIF}
967end;
968
969
970function TNamedPipeServerTransportImpl.CreateNamedPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000971var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
972 everyone_sid : PSID;
973 ea : EXPLICIT_ACCESS;
974 acl : PACL;
975 sd : PSECURITY_DESCRIPTOR;
976 sa : SECURITY_ATTRIBUTES;
Roger Meier3bef8c22012-10-06 06:58:00 +0000977const
978 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
979 SECURITY_WORLD_RID = $00000000;
980begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100981 sd := nil;
Roger Meier3bef8c22012-10-06 06:58:00 +0000982 everyone_sid := nil;
Jens Geyerb64a7742013-01-23 20:58:47 +0100983 try
Jens Geyer06045cf2013-03-27 20:26:25 +0200984 ASSERT( (FHandle = INVALID_HANDLE_VALUE) and not FConnected);
985
Jens Geyerb64a7742013-01-23 20:58:47 +0100986 // Windows - set security to allow non-elevated apps
987 // to access pipes created by elevated apps.
988 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
989 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000990
Jens Geyerb64a7742013-01-23 20:58:47 +0100991 ZeroMemory( @ea, SizeOf(ea));
992 ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
993 ea.grfAccessMode := SET_ACCESS;
994 ea.grfInheritance := NO_INHERITANCE;
995 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
996 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
997 ea.Trustee.ptstrName := PChar(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000998
Jens Geyerb64a7742013-01-23 20:58:47 +0100999 acl := nil;
1000 SetEntriesInAcl( 1, @ea, nil, acl);
Roger Meier3bef8c22012-10-06 06:58:00 +00001001
Jens Geyerb64a7742013-01-23 20:58:47 +01001002 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
1003 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
1004 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
Roger Meier3bef8c22012-10-06 06:58:00 +00001005
Jens Geyerb64a7742013-01-23 20:58:47 +01001006 sa.nLength := SizeOf(sa);
1007 sa.lpSecurityDescriptor := sd;
1008 sa.bInheritHandle := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +00001009
Jens Geyerb64a7742013-01-23 20:58:47 +01001010 // Create an instance of the named pipe
Jens Geyer9f7f11e2016-04-14 21:37:11 +02001011 {$IFDEF OLD_UNIT_NAMES}
1012 result := Windows.CreateNamedPipe(
1013 {$ELSE}
1014 result := Winapi.Windows.CreateNamedPipe(
1015 {$ENDIF}
1016 PChar( FPipeName), // pipe name
1017 PIPE_ACCESS_DUPLEX or // read/write access
1018 FILE_FLAG_OVERLAPPED, // async mode
1019 PIPE_TYPE_BYTE or // byte type pipe
1020 PIPE_READMODE_BYTE, // byte read mode
1021 FMaxConns, // max. instances
1022 FBufSize, // output buffer size
1023 FBufSize, // input buffer size
1024 FTimeout, // time-out, see MSDN
1025 @sa // default security attribute
1026 );
Roger Meier3bef8c22012-10-06 06:58:00 +00001027
Jens Geyer06045cf2013-03-27 20:26:25 +02001028 if( result <> INVALID_HANDLE_VALUE)
1029 then InterlockedExchangePointer( Pointer(FHandle), Pointer(result))
Jens Geyere0e32402016-04-20 21:50:48 +02001030 else raise TTransportExceptionNotOpen.Create('CreateNamedPipe() failed ' + IntToStr(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +01001031
1032 finally
1033 if sd <> nil then LocalFree( Cardinal( sd));
1034 if acl <> nil then LocalFree( Cardinal( acl));
1035 if everyone_sid <> nil then FreeSid(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +00001036 end;
Roger Meier3bef8c22012-10-06 06:58:00 +00001037end;
1038
1039
Roger Meier3bef8c22012-10-06 06:58:00 +00001040
1041end.
1042
1043
1044