blob: 9e6234154323839632d118dfa2bb22785eb66aba [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}
Nick4f5229e2016-04-14 16:43:22 +030022{$IF CompilerVersion >= 23.0}
23 {$LEGACYIFEND ON}
24{$IFEND}
Roger Meier3bef8c22012-10-06 06:58:00 +000025
26interface
27
28uses
Nick4f5229e2016-04-14 16:43:22 +030029{$IF CompilerVersion < 23.0}
Jens Geyer06045cf2013-03-27 20:26:25 +020030 Windows, SysUtils, Math, AccCtrl, AclAPI, SyncObjs,
Nick4f5229e2016-04-14 16:43:22 +030031{$ELSE}
32 Winapi.Windows, System.SysUtils, System.Math, Winapi.AccCtrl, Winapi.AclAPI, System.SyncObjs,
33{$IFEND}
Roger Meier3bef8c22012-10-06 06:58:00 +000034 Thrift.Transport,
Jens Geyere9651362014-03-20 22:46:17 +020035 Thrift.Utils,
Roger Meier3bef8c22012-10-06 06:58:00 +000036 Thrift.Stream;
37
38const
Jens Geyer3e8d9272014-09-14 20:10:40 +020039 DEFAULT_THRIFT_PIPE_TIMEOUT = DEFAULT_THRIFT_TIMEOUT deprecated 'use DEFAULT_THRIFT_TIMEOUT';
Roger Meier3bef8c22012-10-06 06:58:00 +000040
41
Jens Geyere9651362014-03-20 22:46:17 +020042
Roger Meier3bef8c22012-10-06 06:58:00 +000043type
Roger Meier79655fb2012-10-20 20:59:41 +000044 //--- Pipe Streams ---
Roger Meier3bef8c22012-10-06 06:58:00 +000045
46
Jens Geyer06045cf2013-03-27 20:26:25 +020047 TPipeStreamBase = class( TThriftStreamImpl)
Roger Meier79655fb2012-10-20 20:59:41 +000048 strict protected
49 FPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +000050 FTimeout : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +020051 FOverlapped : Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +000052
Roger Meier3bef8c22012-10-06 06:58:00 +000053 procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override;
54 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override;
Roger Meier79655fb2012-10-20 20:59:41 +000055 //procedure Open; override; - see derived classes
Roger Meier3bef8c22012-10-06 06:58:00 +000056 procedure Close; override;
57 procedure Flush; override;
58
Jens Geyere9651362014-03-20 22:46:17 +020059 function ReadDirect( var buffer: TBytes; offset: Integer; count: Integer): Integer;
60 function ReadOverlapped( var buffer: TBytes; offset: Integer; count: Integer): Integer;
61 procedure WriteDirect( const buffer: TBytes; offset: Integer; count: Integer);
62 procedure WriteOverlapped( const buffer: TBytes; offset: Integer; count: Integer);
63
Roger Meier3bef8c22012-10-06 06:58:00 +000064 function IsOpen: Boolean; override;
65 function ToArray: TBytes; override;
66 public
Jens Geyer3e8d9272014-09-14 20:10:40 +020067 constructor Create( aEnableOverlapped : Boolean; const aTimeOut : DWORD = DEFAULT_THRIFT_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 Geyer3e8d9272014-09-14 20:10:40 +020086 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload;
Roger Meier79655fb2012-10-20 20:59:41 +000087 end;
88
89
Jens Geyer06045cf2013-03-27 20:26:25 +020090 THandlePipeStreamImpl = class sealed( TPipeStreamBase)
Jens Geyere9651362014-03-20 22:46:17 +020091 strict private
Roger Meier79655fb2012-10-20 20:59:41 +000092 FSrcHandle : THandle;
93
Jens Geyere9651362014-03-20 22:46:17 +020094 strict protected
Roger Meier79655fb2012-10-20 20:59:41 +000095 procedure Open; override;
96
97 public
Jens Geyere9651362014-03-20 22:46:17 +020098 constructor Create( const aPipeHandle : THandle;
99 const aOwnsHandle, aEnableOverlapped : Boolean;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200100 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload;
Roger Meier79655fb2012-10-20 20:59:41 +0000101 destructor Destroy; override;
102 end;
103
104
105 //--- Pipe Transports ---
106
107
Jens Geyer06045cf2013-03-27 20:26:25 +0200108 IPipeTransport = interface( IStreamTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000109 ['{5E05CC85-434F-428F-BFB2-856A168B5558}']
110 end;
111
112
Jens Geyer06045cf2013-03-27 20:26:25 +0200113 TPipeTransportBase = class( TStreamTransportImpl, IPipeTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000114 public
115 // ITransport
116 function GetIsOpen: Boolean; override;
117 procedure Open; override;
118 procedure Close; override;
119 end;
120
121
Jens Geyer06045cf2013-03-27 20:26:25 +0200122 TNamedPipeTransportClientEndImpl = class( TPipeTransportBase)
Roger Meier79655fb2012-10-20 20:59:41 +0000123 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000124 // Named pipe constructors
Jens Geyere9651362014-03-20 22:46:17 +0200125 constructor Create( aPipe : THandle; aOwnsHandle : Boolean;
126 const aTimeOut : DWORD); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000127 constructor Create( const aPipeName : string;
128 const aShareMode: DWORD = 0;
129 const aSecurityAttributes: PSecurityAttributes = nil;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200130 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000131 end;
132
133
Jens Geyer06045cf2013-03-27 20:26:25 +0200134 TNamedPipeTransportServerEndImpl = class( TNamedPipeTransportClientEndImpl)
Roger Meier79655fb2012-10-20 20:59:41 +0000135 strict private
136 FHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000137 public
Roger Meier79655fb2012-10-20 20:59:41 +0000138 // ITransport
139 procedure Close; override;
Jens Geyere9651362014-03-20 22:46:17 +0200140 constructor Create( aPipe : THandle; aOwnsHandle : Boolean;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200141 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); reintroduce;
Roger Meier79655fb2012-10-20 20:59:41 +0000142 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000143
Roger Meier79655fb2012-10-20 20:59:41 +0000144
Jens Geyer06045cf2013-03-27 20:26:25 +0200145 TAnonymousPipeTransportImpl = class( TPipeTransportBase)
Roger Meier79655fb2012-10-20 20:59:41 +0000146 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000147 // Anonymous pipe constructor
148 constructor Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000149 end;
150
151
Roger Meier79655fb2012-10-20 20:59:41 +0000152 //--- Server Transports ---
153
154
Jens Geyer06045cf2013-03-27 20:26:25 +0200155 IAnonymousPipeServerTransport = interface( IServerTransport)
Roger Meier3bef8c22012-10-06 06:58:00 +0000156 ['{7AEE6793-47B9-4E49-981A-C39E9108E9AD}']
157 // Server side anonymous pipe ends
Roger Meier79655fb2012-10-20 20:59:41 +0000158 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000159 function WriteHandle : THandle;
160 // Client side anonymous pipe ends
161 function ClientAnonRead : THandle;
162 function ClientAnonWrite : THandle;
163 end;
164
165
Jens Geyer06045cf2013-03-27 20:26:25 +0200166 INamedPipeServerTransport = interface( IServerTransport)
Roger Meier79655fb2012-10-20 20:59:41 +0000167 ['{9DF9EE48-D065-40AF-8F67-D33037D3D960}']
168 function Handle : THandle;
169 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000170
Roger Meier79655fb2012-10-20 20:59:41 +0000171
Jens Geyer06045cf2013-03-27 20:26:25 +0200172 TPipeServerTransportBase = class( TServerTransportImpl)
Jens Geyere9651362014-03-20 22:46:17 +0200173 strict protected
174 FStopServer : TEvent;
Jens Geyer06045cf2013-03-27 20:26:25 +0200175 procedure InternalClose; virtual; abstract;
Jens Geyere9651362014-03-20 22:46:17 +0200176 function QueryStopServer : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000177 public
Jens Geyere9651362014-03-20 22:46:17 +0200178 constructor Create;
179 destructor Destroy; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000180 procedure Listen; override;
Jens Geyer06045cf2013-03-27 20:26:25 +0200181 procedure Close; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000182 end;
183
184
Jens Geyer06045cf2013-03-27 20:26:25 +0200185 TAnonymousPipeServerTransportImpl = class( TPipeServerTransportBase, IAnonymousPipeServerTransport)
Jens Geyere9651362014-03-20 22:46:17 +0200186 strict private
Roger Meier79655fb2012-10-20 20:59:41 +0000187 FBufSize : DWORD;
188
189 // Server side anonymous pipe handles
190 FReadHandle,
Roger Meier3bef8c22012-10-06 06:58:00 +0000191 FWriteHandle : THandle;
192
193 //Client side anonymous pipe handles
194 FClientAnonRead,
195 FClientAnonWrite : THandle;
196
197 protected
Jens Geyer01640402013-09-25 21:12:21 +0200198 function Accept(const fnAccepting: TProc): ITransport; override;
Roger Meier3bef8c22012-10-06 06:58:00 +0000199
Roger Meier3bef8c22012-10-06 06:58:00 +0000200 function CreateAnonPipe : Boolean;
201
Jens Geyer06045cf2013-03-27 20:26:25 +0200202 // IAnonymousPipeServerTransport
Roger Meier79655fb2012-10-20 20:59:41 +0000203 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000204 function WriteHandle : THandle;
205 function ClientAnonRead : THandle;
206 function ClientAnonWrite : THandle;
207
Jens Geyer06045cf2013-03-27 20:26:25 +0200208 procedure InternalClose; override;
209
Roger Meier3bef8c22012-10-06 06:58:00 +0000210 public
Roger Meier79655fb2012-10-20 20:59:41 +0000211 constructor Create( aBufsize : Cardinal = 4096);
Roger Meier3bef8c22012-10-06 06:58:00 +0000212 end;
213
214
Jens Geyer06045cf2013-03-27 20:26:25 +0200215 TNamedPipeServerTransportImpl = class( TPipeServerTransportBase, INamedPipeServerTransport)
Jens Geyere9651362014-03-20 22:46:17 +0200216 strict private
Roger Meier79655fb2012-10-20 20:59:41 +0000217 FPipeName : string;
218 FMaxConns : DWORD;
219 FBufSize : DWORD;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100220 FTimeout : DWORD;
Jens Geyer06045cf2013-03-27 20:26:25 +0200221 FHandle : THandle;
222 FConnected : Boolean;
Jens Geyer01640402013-09-25 21:12:21 +0200223
224
Jens Geyere9651362014-03-20 22:46:17 +0200225 strict protected
Jens Geyer01640402013-09-25 21:12:21 +0200226 function Accept(const fnAccepting: TProc): ITransport; override;
Jens Geyer06045cf2013-03-27 20:26:25 +0200227 function CreateNamedPipe : THandle;
228 function CreateTransportInstance : ITransport;
Roger Meier79655fb2012-10-20 20:59:41 +0000229
Jens Geyer06045cf2013-03-27 20:26:25 +0200230 // INamedPipeServerTransport
Roger Meier79655fb2012-10-20 20:59:41 +0000231 function Handle : THandle;
Jens Geyer06045cf2013-03-27 20:26:25 +0200232 procedure InternalClose; override;
Roger Meier79655fb2012-10-20 20:59:41 +0000233
234 public
235 constructor Create( aPipename : string; aBufsize : Cardinal = 4096;
Jens Geyer0b20cc82013-03-07 20:47:01 +0100236 aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES;
Jens Geyer2ad6c302015-02-26 19:38:53 +0100237 aTimeOut : Cardinal = INFINITE);
Roger Meier79655fb2012-10-20 20:59:41 +0000238 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000239
240
241implementation
242
243
Roger Meier79655fb2012-10-20 20:59:41 +0000244procedure ClosePipeHandle( var hPipe : THandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000245begin
Roger Meier79655fb2012-10-20 20:59:41 +0000246 if hPipe <> INVALID_HANDLE_VALUE
247 then try
248 CloseHandle( hPipe);
249 finally
250 hPipe := INVALID_HANDLE_VALUE;
251 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000252end;
253
254
Roger Meier79655fb2012-10-20 20:59:41 +0000255function DuplicatePipeHandle( const hSource : THandle) : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000256begin
Roger Meier79655fb2012-10-20 20:59:41 +0000257 if not DuplicateHandle( GetCurrentProcess, hSource,
258 GetCurrentProcess, @result,
259 0, FALSE, DUPLICATE_SAME_ACCESS)
260 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
261 'DuplicateHandle: '+SysErrorMessage(GetLastError));
Roger Meier3bef8c22012-10-06 06:58:00 +0000262end;
263
264
Roger Meier79655fb2012-10-20 20:59:41 +0000265
Jens Geyer06045cf2013-03-27 20:26:25 +0200266{ TPipeStreamBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000267
268
Jens Geyere9651362014-03-20 22:46:17 +0200269constructor TPipeStreamBase.Create( aEnableOverlapped : Boolean;
Jens Geyer3e8d9272014-09-14 20:10:40 +0200270 const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT);
Roger Meier79655fb2012-10-20 20:59:41 +0000271begin
272 inherited Create;
Jens Geyere9651362014-03-20 22:46:17 +0200273 ASSERT( aTimeout > 0);
274 FPipe := INVALID_HANDLE_VALUE;
275 FTimeout := aTimeOut;
276 FOverlapped := aEnableOverlapped;
Roger Meier79655fb2012-10-20 20:59:41 +0000277end;
278
279
Jens Geyer06045cf2013-03-27 20:26:25 +0200280destructor TPipeStreamBase.Destroy;
Roger Meier3bef8c22012-10-06 06:58:00 +0000281begin
282 try
283 Close;
284 finally
285 inherited Destroy;
286 end;
287end;
288
289
Jens Geyer06045cf2013-03-27 20:26:25 +0200290procedure TPipeStreamBase.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000291begin
Roger Meier79655fb2012-10-20 20:59:41 +0000292 ClosePipeHandle( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000293end;
294
295
Jens Geyer06045cf2013-03-27 20:26:25 +0200296procedure TPipeStreamBase.Flush;
Roger Meier3bef8c22012-10-06 06:58:00 +0000297begin
Jens Geyer0d227b12015-12-02 19:50:55 +0100298 FlushFileBuffers( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000299end;
300
301
Jens Geyer06045cf2013-03-27 20:26:25 +0200302function TPipeStreamBase.IsOpen: Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000303begin
304 result := (FPipe <> INVALID_HANDLE_VALUE);
305end;
306
307
Jens Geyer06045cf2013-03-27 20:26:25 +0200308procedure TPipeStreamBase.Write(const buffer: TBytes; offset, count: Integer);
Jens Geyere9651362014-03-20 22:46:17 +0200309begin
310 if FOverlapped
311 then WriteOverlapped( buffer, offset, count)
312 else WriteDirect( buffer, offset, count);
313end;
314
315
316function TPipeStreamBase.Read( var buffer: TBytes; offset, count: Integer): Integer;
317begin
318 if FOverlapped
319 then result := ReadOverlapped( buffer, offset, count)
320 else result := ReadDirect( buffer, offset, count);
321end;
322
323
324procedure TPipeStreamBase.WriteDirect(const buffer: TBytes; offset, count: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000325var cbWritten : DWORD;
326begin
327 if not IsOpen
328 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
329 'Called write on non-open pipe');
330
331 if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil)
332 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
333 'Write to pipe failed');
334end;
335
336
Jens Geyere9651362014-03-20 22:46:17 +0200337function TPipeStreamBase.ReadDirect( var buffer: TBytes; offset, count: Integer): Integer;
Roger Meier79655fb2012-10-20 20:59:41 +0000338var cbRead, dwErr : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000339 bytes, retries : LongInt;
340 bOk : Boolean;
341const INTERVAL = 10; // ms
342begin
343 if not IsOpen
344 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
345 'Called read on non-open pipe');
346
347 // MSDN: Handle can be a handle to a named pipe instance,
348 // or it can be a handle to the read end of an anonymous pipe,
349 // The handle must have GENERIC_READ access to the pipe.
350 if FTimeOut <> INFINITE then begin
351 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
352 while TRUE do begin
353 if IsOpen
354 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
355 and (bytes > 0)
356 then Break; // there are data
357
Roger Meier79655fb2012-10-20 20:59:41 +0000358 dwErr := GetLastError;
Jens Geyer06045cf2013-03-27 20:26:25 +0200359 if (dwErr = ERROR_INVALID_HANDLE)
360 or (dwErr = ERROR_BROKEN_PIPE)
Roger Meier79655fb2012-10-20 20:59:41 +0000361 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
362 then begin
363 result := 0; // other side closed the pipe
364 Exit;
365 end;
366
Roger Meier3bef8c22012-10-06 06:58:00 +0000367 Dec( retries);
368 if retries > 0
369 then Sleep( INTERVAL)
370 else raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
371 'Pipe read timed out');
372 end;
373 end;
374
375 // read the data (or block INFINITE-ly)
376 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, nil);
377 if (not bOk) and (GetLastError() <> ERROR_MORE_DATA)
378 then result := 0 // No more data, possibly because client disconnected.
379 else result := cbRead;
380end;
381
382
Jens Geyere9651362014-03-20 22:46:17 +0200383procedure TPipeStreamBase.WriteOverlapped(const buffer: TBytes; offset, count: Integer);
384var cbWritten, dwWait, dwError : DWORD;
385 overlapped : IOverlappedHelper;
386begin
387 if not IsOpen
388 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
389 'Called write on non-open pipe');
390
391 overlapped := TOverlappedHelperImpl.Create;
392
393 if not WriteFile( FPipe, buffer[offset], count, cbWritten, overlapped.OverlappedPtr)
394 then begin
395 dwError := GetLastError;
396 case dwError of
397 ERROR_IO_PENDING : begin
398 dwWait := overlapped.WaitFor(FTimeout);
399
400 if (dwWait = WAIT_TIMEOUT)
401 then raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
402 'Pipe write timed out');
403
404 if (dwWait <> WAIT_OBJECT_0)
405 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbWritten, TRUE)
406 then raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
407 'Pipe write error');
408 end;
409
410 else
411 raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
412 SysErrorMessage(dwError));
413 end;
414 end;
415
416 ASSERT( DWORD(count) = cbWritten);
417end;
418
419
420function TPipeStreamBase.ReadOverlapped( var buffer: TBytes; offset, count: Integer): Integer;
421var cbRead, dwWait, dwError : DWORD;
422 bOk : Boolean;
423 overlapped : IOverlappedHelper;
424begin
425 if not IsOpen
426 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
427 'Called read on non-open pipe');
428
429 overlapped := TOverlappedHelperImpl.Create;
430
431 // read the data
432 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, overlapped.OverlappedPtr);
433 if not bOk then begin
434 dwError := GetLastError;
435 case dwError of
436 ERROR_IO_PENDING : begin
437 dwWait := overlapped.WaitFor(FTimeout);
438
439 if (dwWait = WAIT_TIMEOUT)
440 then raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
441 'Pipe read timed out');
442
443 if (dwWait <> WAIT_OBJECT_0)
444 or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbRead, TRUE)
445 then raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
446 'Pipe read error');
447 end;
448
449 else
450 raise TTransportException.Create( TTransportException.TExceptionType.Unknown,
451 SysErrorMessage(dwError));
452 end;
453 end;
454
455 ASSERT( cbRead > 0); // see TTransportImpl.ReadAll()
456 ASSERT( cbRead = DWORD(count));
457 result := cbRead;
458end;
459
460
Jens Geyer06045cf2013-03-27 20:26:25 +0200461function TPipeStreamBase.ToArray: TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000462var bytes : LongInt;
463begin
464 SetLength( result, 0);
465 bytes := 0;
466
467 if IsOpen
468 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
469 and (bytes > 0)
470 then begin
471 SetLength( result, bytes);
472 Read( result, 0, bytes);
473 end;
474end;
475
476
Roger Meier79655fb2012-10-20 20:59:41 +0000477{ TNamedPipeStreamImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000478
479
Jens Geyere9651362014-03-20 22:46:17 +0200480constructor TNamedPipeStreamImpl.Create( const aPipeName : string;
481 const aEnableOverlapped : Boolean;
482 const aShareMode: DWORD;
Roger Meier79655fb2012-10-20 20:59:41 +0000483 const aSecurityAttributes: PSecurityAttributes;
484 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000485begin
Jens Geyere9651362014-03-20 22:46:17 +0200486 inherited Create( aEnableOverlapped, aTimeout);
Roger Meier79655fb2012-10-20 20:59:41 +0000487
488 FPipeName := aPipeName;
489 FShareMode := aShareMode;
490 FSecurityAttribs := aSecurityAttributes;
491
492 if Copy(FPipeName,1,2) <> '\\'
493 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
Roger Meier3bef8c22012-10-06 06:58:00 +0000494end;
495
496
Roger Meier79655fb2012-10-20 20:59:41 +0000497procedure TNamedPipeStreamImpl.Open;
498var hPipe : THandle;
Roger Meier79655fb2012-10-20 20:59:41 +0000499begin
500 if IsOpen then Exit;
501
502 // open that thingy
503
504 if not WaitNamedPipe( PChar(FPipeName), FTimeout)
505 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
506 'Unable to open pipe, '+SysErrorMessage(GetLastError));
507
508 hPipe := CreateFile( PChar( FPipeName),
509 GENERIC_READ or GENERIC_WRITE,
510 FShareMode, // sharing
511 FSecurityAttribs, // security attributes
512 OPEN_EXISTING, // opens existing pipe
Jens Geyere9651362014-03-20 22:46:17 +0200513 FILE_FLAG_OVERLAPPED or FILE_FLAG_WRITE_THROUGH, // async+fast, please
Roger Meier79655fb2012-10-20 20:59:41 +0000514 0); // no template file
515
516 if hPipe = INVALID_HANDLE_VALUE
517 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
518 'Unable to open pipe, '+SysErrorMessage(GetLastError));
519
Roger Meier79655fb2012-10-20 20:59:41 +0000520 // everything fine
521 FPipe := hPipe;
522end;
523
524
525{ THandlePipeStreamImpl }
526
527
Jens Geyere9651362014-03-20 22:46:17 +0200528constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle;
529 const aOwnsHandle, aEnableOverlapped : Boolean;
530 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000531begin
Jens Geyere9651362014-03-20 22:46:17 +0200532 inherited Create( aEnableOverlapped, aTimeOut);
Roger Meier79655fb2012-10-20 20:59:41 +0000533
534 if aOwnsHandle
535 then FSrcHandle := aPipeHandle
536 else FSrcHandle := DuplicatePipeHandle( aPipeHandle);
537
538 Open;
539end;
540
541
542destructor THandlePipeStreamImpl.Destroy;
543begin
544 try
545 ClosePipeHandle( FSrcHandle);
546 finally
547 inherited Destroy;
548 end;
549end;
550
551
552procedure THandlePipeStreamImpl.Open;
553begin
554 if not IsOpen
555 then FPipe := DuplicatePipeHandle( FSrcHandle);
556end;
557
558
Jens Geyer06045cf2013-03-27 20:26:25 +0200559{ TPipeTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000560
561
Jens Geyer06045cf2013-03-27 20:26:25 +0200562function TPipeTransportBase.GetIsOpen: Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000563begin
Jens Geyer0b20cc82013-03-07 20:47:01 +0100564 result := (FInputStream <> nil) and (FInputStream.IsOpen)
565 and (FOutputStream <> nil) and (FOutputStream.IsOpen);
Roger Meier79655fb2012-10-20 20:59:41 +0000566end;
567
568
Jens Geyer06045cf2013-03-27 20:26:25 +0200569procedure TPipeTransportBase.Open;
Roger Meier79655fb2012-10-20 20:59:41 +0000570begin
571 FInputStream.Open;
572 FOutputStream.Open;
573end;
574
575
Jens Geyer06045cf2013-03-27 20:26:25 +0200576procedure TPipeTransportBase.Close;
Roger Meier79655fb2012-10-20 20:59:41 +0000577begin
578 FInputStream.Close;
579 FOutputStream.Close;
580end;
581
582
Jens Geyer06045cf2013-03-27 20:26:25 +0200583{ TNamedPipeTransportClientEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000584
585
Jens Geyer06045cf2013-03-27 20:26:25 +0200586constructor TNamedPipeTransportClientEndImpl.Create( const aPipeName : string; const aShareMode: DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000587 const aSecurityAttributes: PSecurityAttributes;
588 const aTimeOut : DWORD);
589// Named pipe constructor
590begin
Roger Meier79655fb2012-10-20 20:59:41 +0000591 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200592 FInputStream := TNamedPipeStreamImpl.Create( aPipeName, TRUE, aShareMode, aSecurityAttributes, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000593 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000594end;
595
596
Jens Geyere9651362014-03-20 22:46:17 +0200597constructor TNamedPipeTransportClientEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
598 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000599// Named pipe constructor
600begin
Roger Meier79655fb2012-10-20 20:59:41 +0000601 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200602 FInputStream := THandlePipeStreamImpl.Create( aPipe, TRUE, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000603 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000604end;
605
606
Jens Geyer06045cf2013-03-27 20:26:25 +0200607{ TNamedPipeTransportServerEndImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000608
609
Jens Geyere9651362014-03-20 22:46:17 +0200610constructor TNamedPipeTransportServerEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean;
611 const aTimeOut : DWORD);
Roger Meier79655fb2012-10-20 20:59:41 +0000612// Named pipe constructor
Roger Meier3bef8c22012-10-06 06:58:00 +0000613begin
Roger Meier79655fb2012-10-20 20:59:41 +0000614 FHandle := DuplicatePipeHandle( aPipe);
Jens Geyere9651362014-03-20 22:46:17 +0200615 inherited Create( aPipe, aOwnsHandle, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000616end;
617
618
Jens Geyer06045cf2013-03-27 20:26:25 +0200619procedure TNamedPipeTransportServerEndImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000620begin
Roger Meier79655fb2012-10-20 20:59:41 +0000621 FlushFileBuffers( FHandle);
622 DisconnectNamedPipe( FHandle); // force client off the pipe
623 ClosePipeHandle( FHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000624
Roger Meier79655fb2012-10-20 20:59:41 +0000625 inherited Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000626end;
627
628
Jens Geyer06045cf2013-03-27 20:26:25 +0200629{ TAnonymousPipeTransportImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000630
631
Jens Geyer06045cf2013-03-27 20:26:25 +0200632constructor TAnonymousPipeTransportImpl.Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean);
Roger Meier3bef8c22012-10-06 06:58:00 +0000633// Anonymous pipe constructor
634begin
Roger Meier79655fb2012-10-20 20:59:41 +0000635 inherited Create( nil, nil);
Jens Geyere9651362014-03-20 22:46:17 +0200636 // overlapped is not supported with AnonPipes, see MSDN
637 FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles, FALSE);
638 FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles, FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +0000639end;
640
641
Jens Geyer06045cf2013-03-27 20:26:25 +0200642{ TPipeServerTransportBase }
Roger Meier79655fb2012-10-20 20:59:41 +0000643
644
Jens Geyere9651362014-03-20 22:46:17 +0200645constructor TPipeServerTransportBase.Create;
646begin
647 inherited Create;
648 FStopServer := TEvent.Create(nil,TRUE,FALSE,''); // manual reset
649end;
650
651
652destructor TPipeServerTransportBase.Destroy;
653begin
654 try
655 FreeAndNil( FStopServer);
656 finally
657 inherited Destroy;
658 end;
659end;
660
661
662function TPipeServerTransportBase.QueryStopServer : Boolean;
663begin
664 result := (FStopServer = nil)
665 or (FStopServer.WaitFor(0) <> wrTimeout);
666end;
667
668
Jens Geyer06045cf2013-03-27 20:26:25 +0200669procedure TPipeServerTransportBase.Listen;
Roger Meier3bef8c22012-10-06 06:58:00 +0000670begin
Jens Geyere9651362014-03-20 22:46:17 +0200671 FStopServer.ResetEvent;
Roger Meier3bef8c22012-10-06 06:58:00 +0000672end;
673
674
Jens Geyer06045cf2013-03-27 20:26:25 +0200675procedure TPipeServerTransportBase.Close;
676begin
Jens Geyere9651362014-03-20 22:46:17 +0200677 FStopServer.SetEvent;
Jens Geyer06045cf2013-03-27 20:26:25 +0200678 InternalClose;
679end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000680
681
Jens Geyer06045cf2013-03-27 20:26:25 +0200682{ TAnonymousPipeServerTransportImpl }
683
684
685constructor TAnonymousPipeServerTransportImpl.Create( aBufsize : Cardinal);
Roger Meier3bef8c22012-10-06 06:58:00 +0000686// Anonymous pipe CTOR
687begin
688 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000689 FBufsize := aBufSize;
Roger Meier79655fb2012-10-20 20:59:41 +0000690 FReadHandle := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000691 FWriteHandle := INVALID_HANDLE_VALUE;
692 FClientAnonRead := INVALID_HANDLE_VALUE;
693 FClientAnonWrite := INVALID_HANDLE_VALUE;
694
695 // The anonymous pipe needs to be created first so that the server can
696 // pass the handles on to the client before the serve (acceptImpl)
697 // blocking call.
698 if not CreateAnonPipe
699 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
700 ClassName+'.Create() failed');
701end;
702
703
Jens Geyer01640402013-09-25 21:12:21 +0200704function TAnonymousPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Roger Meier3bef8c22012-10-06 06:58:00 +0000705var buf : Byte;
706 br : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000707begin
Jens Geyer01640402013-09-25 21:12:21 +0200708 if Assigned(fnAccepting)
709 then fnAccepting();
710
Roger Meier79655fb2012-10-20 20:59:41 +0000711 // This 0-byte read serves merely as a blocking call.
712 if not ReadFile( FReadHandle, buf, 0, br, nil)
713 and (GetLastError() <> ERROR_MORE_DATA)
714 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
715 'TServerPipe unable to initiate pipe communication');
Jens Geyer06045cf2013-03-27 20:26:25 +0200716
717 // create the transport impl
718 result := TAnonymousPipeTransportImpl.Create( FReadHandle, FWriteHandle, FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +0000719end;
720
721
Jens Geyer06045cf2013-03-27 20:26:25 +0200722procedure TAnonymousPipeServerTransportImpl.InternalClose;
Roger Meier3bef8c22012-10-06 06:58:00 +0000723begin
Roger Meier79655fb2012-10-20 20:59:41 +0000724 ClosePipeHandle( FReadHandle);
725 ClosePipeHandle( FWriteHandle);
726 ClosePipeHandle( FClientAnonRead);
727 ClosePipeHandle( FClientAnonWrite);
Roger Meier3bef8c22012-10-06 06:58:00 +0000728end;
729
730
Jens Geyer06045cf2013-03-27 20:26:25 +0200731function TAnonymousPipeServerTransportImpl.ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000732begin
Roger Meier79655fb2012-10-20 20:59:41 +0000733 result := FReadHandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000734end;
735
736
Jens Geyer06045cf2013-03-27 20:26:25 +0200737function TAnonymousPipeServerTransportImpl.WriteHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000738begin
739 result := FWriteHandle;
740end;
741
742
Jens Geyer06045cf2013-03-27 20:26:25 +0200743function TAnonymousPipeServerTransportImpl.ClientAnonRead : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000744begin
745 result := FClientAnonRead;
746end;
747
748
Jens Geyer06045cf2013-03-27 20:26:25 +0200749function TAnonymousPipeServerTransportImpl.ClientAnonWrite : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000750begin
751 result := FClientAnonWrite;
752end;
753
754
Jens Geyer06045cf2013-03-27 20:26:25 +0200755function TAnonymousPipeServerTransportImpl.CreateAnonPipe : Boolean;
Roger Meier79655fb2012-10-20 20:59:41 +0000756var sd : PSECURITY_DESCRIPTOR;
757 sa : SECURITY_ATTRIBUTES; //TSecurityAttributes;
758 hCAR, hPipeW, hCAW, hPipe : THandle;
759begin
760 result := FALSE;
761
762 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
Jens Geyerb64a7742013-01-23 20:58:47 +0100763 try
764 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
765 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
Roger Meier79655fb2012-10-20 20:59:41 +0000766
Jens Geyerb64a7742013-01-23 20:58:47 +0100767 sa.nLength := sizeof( sa);
768 sa.lpSecurityDescriptor := sd;
769 sa.bInheritHandle := TRUE; //allow passing handle to child
Roger Meier79655fb2012-10-20 20:59:41 +0000770
Jens Geyerb64a7742013-01-23 20:58:47 +0100771 if not CreatePipe( hCAR, hPipeW, @sa, FBufSize) then begin //create stdin pipe
Jens Geyer06045cf2013-03-27 20:26:25 +0200772 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
773 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100774 Exit;
775 end;
776
777 if not CreatePipe( hPipe, hCAW, @sa, FBufSize) then begin //create stdout pipe
Jens Geyerb64a7742013-01-23 20:58:47 +0100778 CloseHandle( hCAR);
779 CloseHandle( hPipeW);
Jens Geyer06045cf2013-03-27 20:26:25 +0200780 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
781 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
Jens Geyerb64a7742013-01-23 20:58:47 +0100782 Exit;
783 end;
784
785 FClientAnonRead := hCAR;
786 FClientAnonWrite := hCAW;
787 FReadHandle := hPipe;
788 FWriteHandle := hPipeW;
789
790 result := TRUE;
Jens Geyerd5436f52014-10-03 19:50:38 +0200791
Jens Geyerb64a7742013-01-23 20:58:47 +0100792 finally
793 if sd <> nil then LocalFree( Cardinal(sd));
Roger Meier79655fb2012-10-20 20:59:41 +0000794 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000795end;
796
797
Jens Geyer06045cf2013-03-27 20:26:25 +0200798{ TNamedPipeServerTransportImpl }
Roger Meier79655fb2012-10-20 20:59:41 +0000799
800
Jens Geyer06045cf2013-03-27 20:26:25 +0200801constructor TNamedPipeServerTransportImpl.Create( aPipename : string; aBufsize, aMaxConns, aTimeOut : Cardinal);
Roger Meier79655fb2012-10-20 20:59:41 +0000802// Named Pipe CTOR
803begin
804 inherited Create;
Jens Geyere9651362014-03-20 22:46:17 +0200805 ASSERT( aTimeout > 0);
Jens Geyer06045cf2013-03-27 20:26:25 +0200806 FPipeName := aPipename;
807 FBufsize := aBufSize;
808 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
809 FHandle := INVALID_HANDLE_VALUE;
810 FTimeout := aTimeOut;
811 FConnected := FALSE;
Roger Meier79655fb2012-10-20 20:59:41 +0000812
813 if Copy(FPipeName,1,2) <> '\\'
814 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
815end;
816
817
Jens Geyer01640402013-09-25 21:12:21 +0200818function TNamedPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport;
Jens Geyer06045cf2013-03-27 20:26:25 +0200819var dwError, dwWait, dwDummy : DWORD;
Jens Geyere9651362014-03-20 22:46:17 +0200820 overlapped : IOverlappedHelper;
821 handles : array[0..1] of THandle;
Jens Geyer01640402013-09-25 21:12:21 +0200822begin
Jens Geyere9651362014-03-20 22:46:17 +0200823 overlapped := TOverlappedHelperImpl.Create;
Jens Geyer01640402013-09-25 21:12:21 +0200824
Jens Geyere9651362014-03-20 22:46:17 +0200825 ASSERT( not FConnected);
Jens Geyer2ad6c302015-02-26 19:38:53 +0100826 CreateNamedPipe;
Jens Geyere9651362014-03-20 22:46:17 +0200827 while not FConnected do begin
Jens Geyer2ad6c302015-02-26 19:38:53 +0100828
829 if QueryStopServer
830 then Abort;
Roger Meier79655fb2012-10-20 20:59:41 +0000831
Jens Geyere9651362014-03-20 22:46:17 +0200832 if Assigned(fnAccepting)
833 then fnAccepting();
Jens Geyer01640402013-09-25 21:12:21 +0200834
Jens Geyere9651362014-03-20 22:46:17 +0200835 // Wait for the client to connect; if it succeeds, the
836 // function returns a nonzero value. If the function returns
837 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
838 if ConnectNamedPipe( Handle, overlapped.OverlappedPtr) then begin
839 FConnected := TRUE;
840 Break;
Jens Geyer01640402013-09-25 21:12:21 +0200841 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000842
Jens Geyere9651362014-03-20 22:46:17 +0200843 // ConnectNamedPipe() returns FALSE for OverlappedIO, even if connected.
844 // We have to check GetLastError() explicitly to find out
845 dwError := GetLastError;
846 case dwError of
847 ERROR_PIPE_CONNECTED : begin
848 FConnected := not QueryStopServer; // special case: pipe immediately connected
849 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000850
Jens Geyere9651362014-03-20 22:46:17 +0200851 ERROR_IO_PENDING : begin
852 handles[0] := overlapped.WaitHandle;
853 handles[1] := FStopServer.Handle;
854 dwWait := WaitForMultipleObjects( 2, @handles, FALSE, FTimeout);
855 FConnected := (dwWait = WAIT_OBJECT_0)
856 and GetOverlappedResult( Handle, overlapped.Overlapped, dwDummy, TRUE)
857 and not QueryStopServer;
858 end;
859
860 else
861 InternalClose;
862 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
863 'Client connection failed');
864 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000865 end;
Jens Geyere9651362014-03-20 22:46:17 +0200866
867 // create the transport impl
868 result := CreateTransportInstance;
Roger Meier79655fb2012-10-20 20:59:41 +0000869end;
870
871
Jens Geyer06045cf2013-03-27 20:26:25 +0200872function TNamedPipeServerTransportImpl.CreateTransportInstance : ITransport;
873// create the transport impl
874var hPipe : THandle;
Roger Meier79655fb2012-10-20 20:59:41 +0000875begin
Jens Geyer06045cf2013-03-27 20:26:25 +0200876 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
877 try
878 FConnected := FALSE;
Jens Geyere9651362014-03-20 22:46:17 +0200879 result := TNamedPipeTransportServerEndImpl.Create( hPipe, TRUE, FTimeout);
Jens Geyer06045cf2013-03-27 20:26:25 +0200880 except
Jens Geyer01640402013-09-25 21:12:21 +0200881 ClosePipeHandle(hPipe);
882 raise;
883 end;
Roger Meier79655fb2012-10-20 20:59:41 +0000884end;
885
886
Jens Geyer06045cf2013-03-27 20:26:25 +0200887procedure TNamedPipeServerTransportImpl.InternalClose;
888var hPipe : THandle;
889begin
890 hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE)));
891 if hPipe = INVALID_HANDLE_VALUE then Exit;
892
893 try
894 if FConnected
895 then FlushFileBuffers( hPipe)
896 else CancelIo( hPipe);
897 DisconnectNamedPipe( hPipe);
898 finally
899 ClosePipeHandle( hPipe);
900 FConnected := FALSE;
901 end;
902end;
903
904
905function TNamedPipeServerTransportImpl.Handle : THandle;
Nick4f5229e2016-04-14 16:43:22 +0300906{$IFDEF WIN64}
907var
908 Hndl: Integer;
909{$ENDIF}
Jens Geyer06045cf2013-03-27 20:26:25 +0200910begin
911 {$IFDEF WIN64}
Nick4f5229e2016-04-14 16:43:22 +0300912 Hndl := Integer(FHandle);
913 result := THandle( InterlockedExchangeAdd( Hndl, 0));
Jens Geyer06045cf2013-03-27 20:26:25 +0200914 {$ELSE}
915 result := THandle( InterlockedExchangeAdd( Integer(FHandle), 0));
916 {$ENDIF}
917end;
918
919
920function TNamedPipeServerTransportImpl.CreateNamedPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000921var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
922 everyone_sid : PSID;
923 ea : EXPLICIT_ACCESS;
924 acl : PACL;
925 sd : PSECURITY_DESCRIPTOR;
926 sa : SECURITY_ATTRIBUTES;
Roger Meier3bef8c22012-10-06 06:58:00 +0000927const
928 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
929 SECURITY_WORLD_RID = $00000000;
930begin
Jens Geyerb64a7742013-01-23 20:58:47 +0100931 sd := nil;
Roger Meier3bef8c22012-10-06 06:58:00 +0000932 everyone_sid := nil;
Jens Geyerb64a7742013-01-23 20:58:47 +0100933 try
Jens Geyer06045cf2013-03-27 20:26:25 +0200934 ASSERT( (FHandle = INVALID_HANDLE_VALUE) and not FConnected);
935
Jens Geyerb64a7742013-01-23 20:58:47 +0100936 // Windows - set security to allow non-elevated apps
937 // to access pipes created by elevated apps.
938 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
939 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000940
Jens Geyerb64a7742013-01-23 20:58:47 +0100941 ZeroMemory( @ea, SizeOf(ea));
942 ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
943 ea.grfAccessMode := SET_ACCESS;
944 ea.grfInheritance := NO_INHERITANCE;
945 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
946 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
947 ea.Trustee.ptstrName := PChar(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000948
Jens Geyerb64a7742013-01-23 20:58:47 +0100949 acl := nil;
950 SetEntriesInAcl( 1, @ea, nil, acl);
Roger Meier3bef8c22012-10-06 06:58:00 +0000951
Jens Geyerb64a7742013-01-23 20:58:47 +0100952 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
953 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
954 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
Roger Meier3bef8c22012-10-06 06:58:00 +0000955
Jens Geyerb64a7742013-01-23 20:58:47 +0100956 sa.nLength := SizeOf(sa);
957 sa.lpSecurityDescriptor := sd;
958 sa.bInheritHandle := FALSE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000959
Jens Geyerb64a7742013-01-23 20:58:47 +0100960 // Create an instance of the named pipe
Nick4f5229e2016-04-14 16:43:22 +0300961{$IF CompilerVersion < 23.0}
Jens Geyer06045cf2013-03-27 20:26:25 +0200962 result := Windows.CreateNamedPipe( PChar( FPipeName), // pipe name
963 PIPE_ACCESS_DUPLEX or // read/write access
964 FILE_FLAG_OVERLAPPED, // async mode
Jens Geyere9651362014-03-20 22:46:17 +0200965 PIPE_TYPE_BYTE or // byte type pipe
966 PIPE_READMODE_BYTE, // byte read mode
Jens Geyer06045cf2013-03-27 20:26:25 +0200967 FMaxConns, // max. instances
968 FBufSize, // output buffer size
969 FBufSize, // input buffer size
970 FTimeout, // time-out, see MSDN
971 @sa); // default security attribute
Nick4f5229e2016-04-14 16:43:22 +0300972{$ELSE}
973 result := Winapi.Windows.CreateNamedPipe( PChar( FPipeName), // pipe name
974 PIPE_ACCESS_DUPLEX or // read/write access
975 FILE_FLAG_OVERLAPPED, // async mode
976 PIPE_TYPE_BYTE or // byte type pipe
977 PIPE_READMODE_BYTE, // byte read mode
978 FMaxConns, // max. instances
979 FBufSize, // output buffer size
980 FBufSize, // input buffer size
981 FTimeout, // time-out, see MSDN
982 @sa); // default security attribute
983{$IFEND}
Roger Meier3bef8c22012-10-06 06:58:00 +0000984
Jens Geyer06045cf2013-03-27 20:26:25 +0200985 if( result <> INVALID_HANDLE_VALUE)
986 then InterlockedExchangePointer( Pointer(FHandle), Pointer(result))
987 else raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
Jens Geyerb64a7742013-01-23 20:58:47 +0100988 'CreateNamedPipe() failed ' + IntToStr(GetLastError));
989
990 finally
991 if sd <> nil then LocalFree( Cardinal( sd));
992 if acl <> nil then LocalFree( Cardinal( acl));
993 if everyone_sid <> nil then FreeSid(everyone_sid);
Roger Meier3bef8c22012-10-06 06:58:00 +0000994 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000995end;
996
997
Roger Meier3bef8c22012-10-06 06:58:00 +0000998
999end.
1000
1001
1002