Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1 | (* |
| 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 | *) |
| 19 | unit Thrift.Transport.Pipes; |
| 20 | |
| 21 | {$WARN SYMBOL_PLATFORM OFF} |
| 22 | |
| 23 | interface |
| 24 | |
| 25 | uses |
| 26 | Windows, SysUtils, Math, AccCtrl, AclAPI, |
| 27 | Thrift.Transport, |
| 28 | Thrift.Console, |
| 29 | Thrift.Stream; |
| 30 | |
| 31 | const |
| 32 | DEFAULT_THRIFT_PIPE_TIMEOUT = 5 * 1000; // ms |
| 33 | |
| 34 | |
| 35 | type |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 36 | //--- Pipe Streams --- |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 37 | |
| 38 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 39 | TPipeStreamBaseImpl = class( TThriftStreamImpl) |
| 40 | strict protected |
| 41 | FPipe : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 42 | FTimeout : DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 43 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 44 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override; |
| 45 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 46 | //procedure Open; override; - see derived classes |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 47 | procedure Close; override; |
| 48 | procedure Flush; override; |
| 49 | |
| 50 | function IsOpen: Boolean; override; |
| 51 | function ToArray: TBytes; override; |
| 52 | public |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 53 | constructor Create( const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 54 | destructor Destroy; override; |
| 55 | end; |
| 56 | |
| 57 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 58 | TNamedPipeStreamImpl = class sealed( TPipeStreamBaseImpl) |
| 59 | private |
| 60 | FPipeName : string; |
| 61 | FShareMode : DWORD; |
| 62 | FSecurityAttribs : PSecurityAttributes; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 63 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 64 | protected |
| 65 | procedure Open; override; |
| 66 | |
| 67 | public |
| 68 | constructor Create( const aPipeName : string; |
| 69 | const aShareMode: DWORD = 0; |
| 70 | const aSecurityAttributes: PSecurityAttributes = nil; |
| 71 | const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); overload; |
| 72 | end; |
| 73 | |
| 74 | |
| 75 | THandlePipeStreamImpl = class sealed( TPipeStreamBaseImpl) |
| 76 | private |
| 77 | FSrcHandle : THandle; |
| 78 | |
| 79 | protected |
| 80 | procedure Open; override; |
| 81 | |
| 82 | public |
| 83 | constructor Create( const aPipeHandle : THandle; aOwnsHandle : Boolean); overload; |
| 84 | destructor Destroy; override; |
| 85 | end; |
| 86 | |
| 87 | |
| 88 | //--- Pipe Transports --- |
| 89 | |
| 90 | |
| 91 | IPipe = interface( IStreamTransport) |
| 92 | ['{5E05CC85-434F-428F-BFB2-856A168B5558}'] |
| 93 | end; |
| 94 | |
| 95 | |
| 96 | TPipeTransportBaseImpl = class( TStreamTransportImpl, IPipe) |
| 97 | public |
| 98 | // ITransport |
| 99 | function GetIsOpen: Boolean; override; |
| 100 | procedure Open; override; |
| 101 | procedure Close; override; |
| 102 | end; |
| 103 | |
| 104 | |
| 105 | TNamedPipeImpl = class( TPipeTransportBaseImpl) |
| 106 | public |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 107 | // Named pipe constructors |
| 108 | constructor Create( aPipe : THandle; aOwnsHandle : Boolean); overload; |
| 109 | constructor Create( const aPipeName : string; |
| 110 | const aShareMode: DWORD = 0; |
| 111 | const aSecurityAttributes: PSecurityAttributes = nil; |
| 112 | const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); overload; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 113 | end; |
| 114 | |
| 115 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 116 | TNamedPipeServerImpl = class( TNamedPipeImpl) |
| 117 | strict private |
| 118 | FHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 119 | public |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 120 | // ITransport |
| 121 | procedure Close; override; |
| 122 | constructor Create( aPipe : THandle; aOwnsHandle : Boolean); reintroduce; |
| 123 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 124 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 125 | |
| 126 | TAnonymousPipeImpl = class( TPipeTransportBaseImpl) |
| 127 | public |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 128 | // Anonymous pipe constructor |
| 129 | constructor Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean); overload; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 130 | end; |
| 131 | |
| 132 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 133 | //--- Server Transports --- |
| 134 | |
| 135 | |
| 136 | IAnonymousServerPipe = interface( IServerTransport) |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 137 | ['{7AEE6793-47B9-4E49-981A-C39E9108E9AD}'] |
| 138 | // Server side anonymous pipe ends |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 139 | function ReadHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 140 | function WriteHandle : THandle; |
| 141 | // Client side anonymous pipe ends |
| 142 | function ClientAnonRead : THandle; |
| 143 | function ClientAnonWrite : THandle; |
| 144 | end; |
| 145 | |
| 146 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 147 | INamedServerPipe = interface( IServerTransport) |
| 148 | ['{9DF9EE48-D065-40AF-8F67-D33037D3D960}'] |
| 149 | function Handle : THandle; |
| 150 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 151 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 152 | |
| 153 | TServerPipeBaseImpl = class( TServerTransportImpl) |
| 154 | public |
| 155 | procedure Listen; override; |
| 156 | end; |
| 157 | |
| 158 | |
| 159 | TAnonymousServerPipeImpl = class( TServerPipeBaseImpl, IAnonymousServerPipe) |
| 160 | private |
| 161 | FBufSize : DWORD; |
| 162 | |
| 163 | // Server side anonymous pipe handles |
| 164 | FReadHandle, |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 165 | FWriteHandle : THandle; |
| 166 | |
| 167 | //Client side anonymous pipe handles |
| 168 | FClientAnonRead, |
| 169 | FClientAnonWrite : THandle; |
| 170 | |
| 171 | protected |
| 172 | function AcceptImpl: ITransport; override; |
| 173 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 174 | function CreateAnonPipe : Boolean; |
| 175 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 176 | // IAnonymousServerPipe |
| 177 | function ReadHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 178 | function WriteHandle : THandle; |
| 179 | function ClientAnonRead : THandle; |
| 180 | function ClientAnonWrite : THandle; |
| 181 | |
| 182 | public |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 183 | constructor Create( aBufsize : Cardinal = 4096); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 184 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 185 | procedure Close; override; |
| 186 | end; |
| 187 | |
| 188 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 189 | TNamedServerPipeImpl = class( TServerPipeBaseImpl, INamedServerPipe) |
| 190 | private |
| 191 | FPipeName : string; |
| 192 | FMaxConns : DWORD; |
| 193 | FBufSize : DWORD; |
| 194 | |
| 195 | FHandle : THandle; |
| 196 | |
| 197 | protected |
| 198 | function AcceptImpl: ITransport; override; |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 199 | procedure CreateNamedPipe; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 200 | |
| 201 | // INamedServerPipe |
| 202 | function Handle : THandle; |
| 203 | |
| 204 | public |
| 205 | constructor Create( aPipename : string; aBufsize : Cardinal = 4096; |
| 206 | aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES); |
| 207 | |
| 208 | procedure Close; override; |
| 209 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 210 | |
| 211 | |
| 212 | implementation |
| 213 | |
| 214 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 215 | procedure ClosePipeHandle( var hPipe : THandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 216 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 217 | if hPipe <> INVALID_HANDLE_VALUE |
| 218 | then try |
| 219 | CloseHandle( hPipe); |
| 220 | finally |
| 221 | hPipe := INVALID_HANDLE_VALUE; |
| 222 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 223 | end; |
| 224 | |
| 225 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 226 | function DuplicatePipeHandle( const hSource : THandle) : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 227 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 228 | if not DuplicateHandle( GetCurrentProcess, hSource, |
| 229 | GetCurrentProcess, @result, |
| 230 | 0, FALSE, DUPLICATE_SAME_ACCESS) |
| 231 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 232 | 'DuplicateHandle: '+SysErrorMessage(GetLastError)); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 233 | end; |
| 234 | |
| 235 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 236 | |
| 237 | { TPipeStreamBaseImpl } |
| 238 | |
| 239 | |
| 240 | constructor TPipeStreamBaseImpl.Create( const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); |
| 241 | begin |
| 242 | inherited Create; |
| 243 | FPipe := INVALID_HANDLE_VALUE; |
| 244 | FTimeout := aTimeOut; |
| 245 | end; |
| 246 | |
| 247 | |
| 248 | destructor TPipeStreamBaseImpl.Destroy; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 249 | begin |
| 250 | try |
| 251 | Close; |
| 252 | finally |
| 253 | inherited Destroy; |
| 254 | end; |
| 255 | end; |
| 256 | |
| 257 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 258 | procedure TPipeStreamBaseImpl.Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 259 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 260 | ClosePipeHandle( FPipe); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 261 | end; |
| 262 | |
| 263 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 264 | procedure TPipeStreamBaseImpl.Flush; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 265 | begin |
| 266 | // nothing to do |
| 267 | end; |
| 268 | |
| 269 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 270 | function TPipeStreamBaseImpl.IsOpen: Boolean; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 271 | begin |
| 272 | result := (FPipe <> INVALID_HANDLE_VALUE); |
| 273 | end; |
| 274 | |
| 275 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 276 | procedure TPipeStreamBaseImpl.Write(const buffer: TBytes; offset, count: Integer); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 277 | var cbWritten : DWORD; |
| 278 | begin |
| 279 | if not IsOpen |
| 280 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 281 | 'Called write on non-open pipe'); |
| 282 | |
| 283 | if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil) |
| 284 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 285 | 'Write to pipe failed'); |
| 286 | end; |
| 287 | |
| 288 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 289 | function TPipeStreamBaseImpl.Read( var buffer: TBytes; offset, count: Integer): Integer; |
| 290 | var cbRead, dwErr : DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 291 | bytes, retries : LongInt; |
| 292 | bOk : Boolean; |
| 293 | const INTERVAL = 10; // ms |
| 294 | begin |
| 295 | if not IsOpen |
| 296 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 297 | 'Called read on non-open pipe'); |
| 298 | |
| 299 | // MSDN: Handle can be a handle to a named pipe instance, |
| 300 | // or it can be a handle to the read end of an anonymous pipe, |
| 301 | // The handle must have GENERIC_READ access to the pipe. |
| 302 | if FTimeOut <> INFINITE then begin |
| 303 | retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL)); |
| 304 | while TRUE do begin |
| 305 | if IsOpen |
| 306 | and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) |
| 307 | and (bytes > 0) |
| 308 | then Break; // there are data |
| 309 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 310 | dwErr := GetLastError; |
| 311 | if (dwErr = ERROR_BROKEN_PIPE) |
| 312 | or (dwErr = ERROR_PIPE_NOT_CONNECTED) |
| 313 | then begin |
| 314 | result := 0; // other side closed the pipe |
| 315 | Exit; |
| 316 | end; |
| 317 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 318 | Dec( retries); |
| 319 | if retries > 0 |
| 320 | then Sleep( INTERVAL) |
| 321 | else raise TTransportException.Create( TTransportException.TExceptionType.TimedOut, |
| 322 | 'Pipe read timed out'); |
| 323 | end; |
| 324 | end; |
| 325 | |
| 326 | // read the data (or block INFINITE-ly) |
| 327 | bOk := ReadFile( FPipe, buffer[offset], count, cbRead, nil); |
| 328 | if (not bOk) and (GetLastError() <> ERROR_MORE_DATA) |
| 329 | then result := 0 // No more data, possibly because client disconnected. |
| 330 | else result := cbRead; |
| 331 | end; |
| 332 | |
| 333 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 334 | function TPipeStreamBaseImpl.ToArray: TBytes; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 335 | var bytes : LongInt; |
| 336 | begin |
| 337 | SetLength( result, 0); |
| 338 | bytes := 0; |
| 339 | |
| 340 | if IsOpen |
| 341 | and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) |
| 342 | and (bytes > 0) |
| 343 | then begin |
| 344 | SetLength( result, bytes); |
| 345 | Read( result, 0, bytes); |
| 346 | end; |
| 347 | end; |
| 348 | |
| 349 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 350 | { TNamedPipeStreamImpl } |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 351 | |
| 352 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 353 | constructor TNamedPipeStreamImpl.Create( const aPipeName : string; const aShareMode: DWORD; |
| 354 | const aSecurityAttributes: PSecurityAttributes; |
| 355 | const aTimeOut : DWORD); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 356 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 357 | inherited Create( aTimeout); |
| 358 | |
| 359 | FPipeName := aPipeName; |
| 360 | FShareMode := aShareMode; |
| 361 | FSecurityAttribs := aSecurityAttributes; |
| 362 | |
| 363 | if Copy(FPipeName,1,2) <> '\\' |
| 364 | then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 365 | end; |
| 366 | |
| 367 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 368 | procedure TNamedPipeStreamImpl.Open; |
| 369 | var hPipe : THandle; |
| 370 | dwMode : DWORD; |
| 371 | begin |
| 372 | if IsOpen then Exit; |
| 373 | |
| 374 | // open that thingy |
| 375 | |
| 376 | if not WaitNamedPipe( PChar(FPipeName), FTimeout) |
| 377 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 378 | 'Unable to open pipe, '+SysErrorMessage(GetLastError)); |
| 379 | |
| 380 | hPipe := CreateFile( PChar( FPipeName), |
| 381 | GENERIC_READ or GENERIC_WRITE, |
| 382 | FShareMode, // sharing |
| 383 | FSecurityAttribs, // security attributes |
| 384 | OPEN_EXISTING, // opens existing pipe |
| 385 | 0, // default attributes |
| 386 | 0); // no template file |
| 387 | |
| 388 | if hPipe = INVALID_HANDLE_VALUE |
| 389 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 390 | 'Unable to open pipe, '+SysErrorMessage(GetLastError)); |
| 391 | |
| 392 | // pipe connected; change to message-read mode. |
| 393 | dwMode := PIPE_READMODE_MESSAGE; |
| 394 | if not SetNamedPipeHandleState( hPipe, dwMode, nil, nil) then begin |
| 395 | Close; |
| 396 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 397 | 'SetNamedPipeHandleState failed'); |
| 398 | end; |
| 399 | |
| 400 | // everything fine |
| 401 | FPipe := hPipe; |
| 402 | end; |
| 403 | |
| 404 | |
| 405 | { THandlePipeStreamImpl } |
| 406 | |
| 407 | |
| 408 | constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle; aOwnsHandle : Boolean); |
| 409 | begin |
| 410 | inherited Create( DEFAULT_THRIFT_PIPE_TIMEOUT); |
| 411 | |
| 412 | if aOwnsHandle |
| 413 | then FSrcHandle := aPipeHandle |
| 414 | else FSrcHandle := DuplicatePipeHandle( aPipeHandle); |
| 415 | |
| 416 | Open; |
| 417 | end; |
| 418 | |
| 419 | |
| 420 | destructor THandlePipeStreamImpl.Destroy; |
| 421 | begin |
| 422 | try |
| 423 | ClosePipeHandle( FSrcHandle); |
| 424 | finally |
| 425 | inherited Destroy; |
| 426 | end; |
| 427 | end; |
| 428 | |
| 429 | |
| 430 | procedure THandlePipeStreamImpl.Open; |
| 431 | begin |
| 432 | if not IsOpen |
| 433 | then FPipe := DuplicatePipeHandle( FSrcHandle); |
| 434 | end; |
| 435 | |
| 436 | |
| 437 | { TPipeTransportBaseImpl } |
| 438 | |
| 439 | |
| 440 | function TPipeTransportBaseImpl.GetIsOpen: Boolean; |
| 441 | begin |
| 442 | result := (FInputStream <> nil); |
| 443 | end; |
| 444 | |
| 445 | |
| 446 | procedure TPipeTransportBaseImpl.Open; |
| 447 | begin |
| 448 | FInputStream.Open; |
| 449 | FOutputStream.Open; |
| 450 | end; |
| 451 | |
| 452 | |
| 453 | procedure TPipeTransportBaseImpl.Close; |
| 454 | begin |
| 455 | FInputStream.Close; |
| 456 | FOutputStream.Close; |
| 457 | end; |
| 458 | |
| 459 | |
| 460 | { TNamedPipeImpl } |
| 461 | |
| 462 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 463 | constructor TNamedPipeImpl.Create( const aPipeName : string; const aShareMode: DWORD; |
| 464 | const aSecurityAttributes: PSecurityAttributes; |
| 465 | const aTimeOut : DWORD); |
| 466 | // Named pipe constructor |
| 467 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 468 | inherited Create( nil, nil); |
| 469 | FInputStream := TNamedPipeStreamImpl.Create( aPipeName, aShareMode, aSecurityAttributes, aTimeOut); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 470 | FOutputStream := FInputStream; // true for named pipes |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 471 | end; |
| 472 | |
| 473 | |
| 474 | constructor TNamedPipeImpl.Create( aPipe : THandle; aOwnsHandle : Boolean); |
| 475 | // Named pipe constructor |
| 476 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 477 | inherited Create( nil, nil); |
| 478 | FInputStream := THandlePipeStreamImpl.Create( aPipe, aOwnsHandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 479 | FOutputStream := FInputStream; // true for named pipes |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 480 | end; |
| 481 | |
| 482 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 483 | { TNamedPipeServerImpl } |
| 484 | |
| 485 | |
| 486 | constructor TNamedPipeServerImpl.Create( aPipe : THandle; aOwnsHandle : Boolean); |
| 487 | // Named pipe constructor |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 488 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 489 | FHandle := DuplicatePipeHandle( aPipe); |
| 490 | inherited Create( aPipe, aOwnsHandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 491 | end; |
| 492 | |
| 493 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 494 | procedure TNamedPipeServerImpl.Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 495 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 496 | FlushFileBuffers( FHandle); |
| 497 | DisconnectNamedPipe( FHandle); // force client off the pipe |
| 498 | ClosePipeHandle( FHandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 499 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 500 | inherited Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 501 | end; |
| 502 | |
| 503 | |
| 504 | { TAnonymousPipeImpl } |
| 505 | |
| 506 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 507 | constructor TAnonymousPipeImpl.Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean); |
| 508 | // Anonymous pipe constructor |
| 509 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 510 | inherited Create( nil, nil); |
| 511 | FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles); |
| 512 | FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 513 | end; |
| 514 | |
| 515 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 516 | { TServerPipeBaseImpl } |
| 517 | |
| 518 | |
| 519 | procedure TServerPipeBaseImpl.Listen; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 520 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 521 | // not much to do here |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 522 | end; |
| 523 | |
| 524 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 525 | { TAnonymousServerPipeImpl } |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 526 | |
| 527 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 528 | constructor TAnonymousServerPipeImpl.Create( aBufsize : Cardinal); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 529 | // Anonymous pipe CTOR |
| 530 | begin |
| 531 | inherited Create; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 532 | FBufsize := aBufSize; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 533 | FReadHandle := INVALID_HANDLE_VALUE; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 534 | FWriteHandle := INVALID_HANDLE_VALUE; |
| 535 | FClientAnonRead := INVALID_HANDLE_VALUE; |
| 536 | FClientAnonWrite := INVALID_HANDLE_VALUE; |
| 537 | |
| 538 | // The anonymous pipe needs to be created first so that the server can |
| 539 | // pass the handles on to the client before the serve (acceptImpl) |
| 540 | // blocking call. |
| 541 | if not CreateAnonPipe |
| 542 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 543 | ClassName+'.Create() failed'); |
| 544 | end; |
| 545 | |
| 546 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 547 | function TAnonymousServerPipeImpl.AcceptImpl: ITransport; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 548 | var buf : Byte; |
| 549 | br : DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 550 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 551 | // This 0-byte read serves merely as a blocking call. |
| 552 | if not ReadFile( FReadHandle, buf, 0, br, nil) |
| 553 | and (GetLastError() <> ERROR_MORE_DATA) |
| 554 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 555 | 'TServerPipe unable to initiate pipe communication'); |
| 556 | result := TAnonymousPipeImpl.Create( FReadHandle, FWriteHandle, FALSE); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 557 | end; |
| 558 | |
| 559 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 560 | procedure TAnonymousServerPipeImpl.Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 561 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 562 | ClosePipeHandle( FReadHandle); |
| 563 | ClosePipeHandle( FWriteHandle); |
| 564 | ClosePipeHandle( FClientAnonRead); |
| 565 | ClosePipeHandle( FClientAnonWrite); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 566 | end; |
| 567 | |
| 568 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 569 | function TAnonymousServerPipeImpl.ReadHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 570 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 571 | result := FReadHandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 572 | end; |
| 573 | |
| 574 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 575 | function TAnonymousServerPipeImpl.WriteHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 576 | begin |
| 577 | result := FWriteHandle; |
| 578 | end; |
| 579 | |
| 580 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 581 | function TAnonymousServerPipeImpl.ClientAnonRead : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 582 | begin |
| 583 | result := FClientAnonRead; |
| 584 | end; |
| 585 | |
| 586 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 587 | function TAnonymousServerPipeImpl.ClientAnonWrite : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 588 | begin |
| 589 | result := FClientAnonWrite; |
| 590 | end; |
| 591 | |
| 592 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 593 | function TAnonymousServerPipeImpl.CreateAnonPipe : Boolean; |
| 594 | var sd : PSECURITY_DESCRIPTOR; |
| 595 | sa : SECURITY_ATTRIBUTES; //TSecurityAttributes; |
| 596 | hCAR, hPipeW, hCAW, hPipe : THandle; |
| 597 | begin |
| 598 | result := FALSE; |
| 599 | |
| 600 | sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH)); |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 601 | try |
| 602 | Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION)); |
| 603 | Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE)); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 604 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 605 | sa.nLength := sizeof( sa); |
| 606 | sa.lpSecurityDescriptor := sd; |
| 607 | sa.bInheritHandle := TRUE; //allow passing handle to child |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 608 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 609 | if not CreatePipe( hCAR, hPipeW, @sa, FBufSize) then begin //create stdin pipe |
| 610 | Console.WriteLine( 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError)); |
| 611 | Exit; |
| 612 | end; |
| 613 | |
| 614 | if not CreatePipe( hPipe, hCAW, @sa, FBufSize) then begin //create stdout pipe |
| 615 | Console.WriteLine( 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError)); |
| 616 | CloseHandle( hCAR); |
| 617 | CloseHandle( hPipeW); |
| 618 | Exit; |
| 619 | end; |
| 620 | |
| 621 | FClientAnonRead := hCAR; |
| 622 | FClientAnonWrite := hCAW; |
| 623 | FReadHandle := hPipe; |
| 624 | FWriteHandle := hPipeW; |
| 625 | |
| 626 | result := TRUE; |
| 627 | |
| 628 | finally |
| 629 | if sd <> nil then LocalFree( Cardinal(sd)); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 630 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 631 | end; |
| 632 | |
| 633 | |
| 634 | { TNamedServerPipeImpl } |
| 635 | |
| 636 | |
| 637 | constructor TNamedServerPipeImpl.Create( aPipename : string; aBufsize, aMaxConns : Cardinal); |
| 638 | // Named Pipe CTOR |
| 639 | begin |
| 640 | inherited Create; |
| 641 | FPipeName := aPipename; |
| 642 | FBufsize := aBufSize; |
| 643 | FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns)); |
| 644 | FHandle := INVALID_HANDLE_VALUE; |
| 645 | |
| 646 | if Copy(FPipeName,1,2) <> '\\' |
| 647 | then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost |
| 648 | end; |
| 649 | |
| 650 | |
| 651 | function TNamedServerPipeImpl.AcceptImpl: ITransport; |
| 652 | var connectRet : Boolean; |
| 653 | begin |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 654 | CreateNamedPipe; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 655 | |
| 656 | // Wait for the client to connect; if it succeeds, the |
| 657 | // function returns a nonzero value. If the function returns |
| 658 | // zero, GetLastError should return ERROR_PIPE_CONNECTED. |
| 659 | if ConnectNamedPipe( FHandle,nil) |
| 660 | then connectRet := TRUE |
| 661 | else connectRet := (GetLastError() = ERROR_PIPE_CONNECTED); |
| 662 | |
| 663 | if not connectRet then begin |
| 664 | Close; |
| 665 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 666 | 'TServerPipe: client connection failed'); |
| 667 | end; |
| 668 | |
| 669 | result := TNamedPipeServerImpl.Create( FHandle, TRUE); |
| 670 | end; |
| 671 | |
| 672 | |
| 673 | procedure TNamedServerPipeImpl.Close; |
| 674 | begin |
| 675 | if FHandle <> INVALID_HANDLE_VALUE |
| 676 | then try |
| 677 | FlushFileBuffers( FHandle); |
| 678 | DisconnectNamedPipe( FHandle); |
| 679 | finally |
| 680 | ClosePipeHandle( FHandle); |
| 681 | end; |
| 682 | end; |
| 683 | |
| 684 | |
| 685 | function TNamedServerPipeImpl.Handle : THandle; |
| 686 | begin |
| 687 | result := FHandle; |
| 688 | end; |
| 689 | |
| 690 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 691 | procedure TNamedServerPipeImpl.CreateNamedPipe; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 692 | var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ; |
| 693 | everyone_sid : PSID; |
| 694 | ea : EXPLICIT_ACCESS; |
| 695 | acl : PACL; |
| 696 | sd : PSECURITY_DESCRIPTOR; |
| 697 | sa : SECURITY_ATTRIBUTES; |
| 698 | hPipe : THandle; |
| 699 | const |
| 700 | SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1)); |
| 701 | SECURITY_WORLD_RID = $00000000; |
| 702 | begin |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 703 | sd := nil; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 704 | everyone_sid := nil; |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 705 | try |
| 706 | // Windows - set security to allow non-elevated apps |
| 707 | // to access pipes created by elevated apps. |
| 708 | SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY; |
| 709 | AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 710 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 711 | ZeroMemory( @ea, SizeOf(ea)); |
| 712 | ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL; |
| 713 | ea.grfAccessMode := SET_ACCESS; |
| 714 | ea.grfInheritance := NO_INHERITANCE; |
| 715 | ea.Trustee.TrusteeForm := TRUSTEE_IS_SID; |
| 716 | ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP; |
| 717 | ea.Trustee.ptstrName := PChar(everyone_sid); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 718 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 719 | acl := nil; |
| 720 | SetEntriesInAcl( 1, @ea, nil, acl); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 721 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 722 | sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH)); |
| 723 | Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION)); |
| 724 | Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE)); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 725 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 726 | sa.nLength := SizeOf(sa); |
| 727 | sa.lpSecurityDescriptor := sd; |
| 728 | sa.bInheritHandle := FALSE; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 729 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 730 | // Create an instance of the named pipe |
| 731 | hPipe := Windows.CreateNamedPipe( PChar( FPipeName), // pipe name |
| 732 | PIPE_ACCESS_DUPLEX, // read/write access |
| 733 | PIPE_TYPE_MESSAGE or // message type pipe |
| 734 | PIPE_READMODE_MESSAGE, // message-read mode |
| 735 | FMaxConns, // max. instances |
| 736 | FBufSize, // output buffer size |
| 737 | FBufSize, // input buffer size |
| 738 | 0, // client time-out |
| 739 | @sa); // default security attribute |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 740 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame^] | 741 | FHandle := hPipe; |
| 742 | if( FHandle = INVALID_HANDLE_VALUE) |
| 743 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 744 | 'CreateNamedPipe() failed ' + IntToStr(GetLastError)); |
| 745 | |
| 746 | finally |
| 747 | if sd <> nil then LocalFree( Cardinal( sd)); |
| 748 | if acl <> nil then LocalFree( Cardinal( acl)); |
| 749 | if everyone_sid <> nil then FreeSid(everyone_sid); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 750 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 751 | end; |
| 752 | |
| 753 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 754 | |
| 755 | end. |
| 756 | |
| 757 | |
| 758 | |