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; |
| 199 | |
| 200 | function CreateNamedPipe : Boolean; |
| 201 | |
| 202 | // INamedServerPipe |
| 203 | function Handle : THandle; |
| 204 | |
| 205 | public |
| 206 | constructor Create( aPipename : string; aBufsize : Cardinal = 4096; |
| 207 | aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES); |
| 208 | |
| 209 | procedure Close; override; |
| 210 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | implementation |
| 214 | |
| 215 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 216 | procedure ClosePipeHandle( var hPipe : THandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 217 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 218 | if hPipe <> INVALID_HANDLE_VALUE |
| 219 | then try |
| 220 | CloseHandle( hPipe); |
| 221 | finally |
| 222 | hPipe := INVALID_HANDLE_VALUE; |
| 223 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 224 | end; |
| 225 | |
| 226 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 227 | function DuplicatePipeHandle( const hSource : THandle) : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 228 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 229 | if not DuplicateHandle( GetCurrentProcess, hSource, |
| 230 | GetCurrentProcess, @result, |
| 231 | 0, FALSE, DUPLICATE_SAME_ACCESS) |
| 232 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 233 | 'DuplicateHandle: '+SysErrorMessage(GetLastError)); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 234 | end; |
| 235 | |
| 236 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 237 | |
| 238 | { TPipeStreamBaseImpl } |
| 239 | |
| 240 | |
| 241 | constructor TPipeStreamBaseImpl.Create( const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); |
| 242 | begin |
| 243 | inherited Create; |
| 244 | FPipe := INVALID_HANDLE_VALUE; |
| 245 | FTimeout := aTimeOut; |
| 246 | end; |
| 247 | |
| 248 | |
| 249 | destructor TPipeStreamBaseImpl.Destroy; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 250 | begin |
| 251 | try |
| 252 | Close; |
| 253 | finally |
| 254 | inherited Destroy; |
| 255 | end; |
| 256 | end; |
| 257 | |
| 258 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 259 | procedure TPipeStreamBaseImpl.Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 260 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 261 | ClosePipeHandle( FPipe); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 262 | end; |
| 263 | |
| 264 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 265 | procedure TPipeStreamBaseImpl.Flush; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 266 | begin |
| 267 | // nothing to do |
| 268 | end; |
| 269 | |
| 270 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 271 | function TPipeStreamBaseImpl.IsOpen: Boolean; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 272 | begin |
| 273 | result := (FPipe <> INVALID_HANDLE_VALUE); |
| 274 | end; |
| 275 | |
| 276 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 277 | procedure TPipeStreamBaseImpl.Write(const buffer: TBytes; offset, count: Integer); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 278 | var cbWritten : DWORD; |
| 279 | begin |
| 280 | if not IsOpen |
| 281 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 282 | 'Called write on non-open pipe'); |
| 283 | |
| 284 | if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil) |
| 285 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 286 | 'Write to pipe failed'); |
| 287 | end; |
| 288 | |
| 289 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 290 | function TPipeStreamBaseImpl.Read( var buffer: TBytes; offset, count: Integer): Integer; |
| 291 | var cbRead, dwErr : DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 292 | bytes, retries : LongInt; |
| 293 | bOk : Boolean; |
| 294 | const INTERVAL = 10; // ms |
| 295 | begin |
| 296 | if not IsOpen |
| 297 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 298 | 'Called read on non-open pipe'); |
| 299 | |
| 300 | // MSDN: Handle can be a handle to a named pipe instance, |
| 301 | // or it can be a handle to the read end of an anonymous pipe, |
| 302 | // The handle must have GENERIC_READ access to the pipe. |
| 303 | if FTimeOut <> INFINITE then begin |
| 304 | retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL)); |
| 305 | while TRUE do begin |
| 306 | if IsOpen |
| 307 | and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) |
| 308 | and (bytes > 0) |
| 309 | then Break; // there are data |
| 310 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 311 | dwErr := GetLastError; |
| 312 | if (dwErr = ERROR_BROKEN_PIPE) |
| 313 | or (dwErr = ERROR_PIPE_NOT_CONNECTED) |
| 314 | then begin |
| 315 | result := 0; // other side closed the pipe |
| 316 | Exit; |
| 317 | end; |
| 318 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 319 | Dec( retries); |
| 320 | if retries > 0 |
| 321 | then Sleep( INTERVAL) |
| 322 | else raise TTransportException.Create( TTransportException.TExceptionType.TimedOut, |
| 323 | 'Pipe read timed out'); |
| 324 | end; |
| 325 | end; |
| 326 | |
| 327 | // read the data (or block INFINITE-ly) |
| 328 | bOk := ReadFile( FPipe, buffer[offset], count, cbRead, nil); |
| 329 | if (not bOk) and (GetLastError() <> ERROR_MORE_DATA) |
| 330 | then result := 0 // No more data, possibly because client disconnected. |
| 331 | else result := cbRead; |
| 332 | end; |
| 333 | |
| 334 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 335 | function TPipeStreamBaseImpl.ToArray: TBytes; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 336 | var bytes : LongInt; |
| 337 | begin |
| 338 | SetLength( result, 0); |
| 339 | bytes := 0; |
| 340 | |
| 341 | if IsOpen |
| 342 | and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) |
| 343 | and (bytes > 0) |
| 344 | then begin |
| 345 | SetLength( result, bytes); |
| 346 | Read( result, 0, bytes); |
| 347 | end; |
| 348 | end; |
| 349 | |
| 350 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 351 | { TNamedPipeStreamImpl } |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 352 | |
| 353 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 354 | constructor TNamedPipeStreamImpl.Create( const aPipeName : string; const aShareMode: DWORD; |
| 355 | const aSecurityAttributes: PSecurityAttributes; |
| 356 | const aTimeOut : DWORD); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 357 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 358 | inherited Create( aTimeout); |
| 359 | |
| 360 | FPipeName := aPipeName; |
| 361 | FShareMode := aShareMode; |
| 362 | FSecurityAttribs := aSecurityAttributes; |
| 363 | |
| 364 | if Copy(FPipeName,1,2) <> '\\' |
| 365 | then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 366 | end; |
| 367 | |
| 368 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 369 | procedure TNamedPipeStreamImpl.Open; |
| 370 | var hPipe : THandle; |
| 371 | dwMode : DWORD; |
| 372 | begin |
| 373 | if IsOpen then Exit; |
| 374 | |
| 375 | // open that thingy |
| 376 | |
| 377 | if not WaitNamedPipe( PChar(FPipeName), FTimeout) |
| 378 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 379 | 'Unable to open pipe, '+SysErrorMessage(GetLastError)); |
| 380 | |
| 381 | hPipe := CreateFile( PChar( FPipeName), |
| 382 | GENERIC_READ or GENERIC_WRITE, |
| 383 | FShareMode, // sharing |
| 384 | FSecurityAttribs, // security attributes |
| 385 | OPEN_EXISTING, // opens existing pipe |
| 386 | 0, // default attributes |
| 387 | 0); // no template file |
| 388 | |
| 389 | if hPipe = INVALID_HANDLE_VALUE |
| 390 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 391 | 'Unable to open pipe, '+SysErrorMessage(GetLastError)); |
| 392 | |
| 393 | // pipe connected; change to message-read mode. |
| 394 | dwMode := PIPE_READMODE_MESSAGE; |
| 395 | if not SetNamedPipeHandleState( hPipe, dwMode, nil, nil) then begin |
| 396 | Close; |
| 397 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 398 | 'SetNamedPipeHandleState failed'); |
| 399 | end; |
| 400 | |
| 401 | // everything fine |
| 402 | FPipe := hPipe; |
| 403 | end; |
| 404 | |
| 405 | |
| 406 | { THandlePipeStreamImpl } |
| 407 | |
| 408 | |
| 409 | constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle; aOwnsHandle : Boolean); |
| 410 | begin |
| 411 | inherited Create( DEFAULT_THRIFT_PIPE_TIMEOUT); |
| 412 | |
| 413 | if aOwnsHandle |
| 414 | then FSrcHandle := aPipeHandle |
| 415 | else FSrcHandle := DuplicatePipeHandle( aPipeHandle); |
| 416 | |
| 417 | Open; |
| 418 | end; |
| 419 | |
| 420 | |
| 421 | destructor THandlePipeStreamImpl.Destroy; |
| 422 | begin |
| 423 | try |
| 424 | ClosePipeHandle( FSrcHandle); |
| 425 | finally |
| 426 | inherited Destroy; |
| 427 | end; |
| 428 | end; |
| 429 | |
| 430 | |
| 431 | procedure THandlePipeStreamImpl.Open; |
| 432 | begin |
| 433 | if not IsOpen |
| 434 | then FPipe := DuplicatePipeHandle( FSrcHandle); |
| 435 | end; |
| 436 | |
| 437 | |
| 438 | { TPipeTransportBaseImpl } |
| 439 | |
| 440 | |
| 441 | function TPipeTransportBaseImpl.GetIsOpen: Boolean; |
| 442 | begin |
| 443 | result := (FInputStream <> nil); |
| 444 | end; |
| 445 | |
| 446 | |
| 447 | procedure TPipeTransportBaseImpl.Open; |
| 448 | begin |
| 449 | FInputStream.Open; |
| 450 | FOutputStream.Open; |
| 451 | end; |
| 452 | |
| 453 | |
| 454 | procedure TPipeTransportBaseImpl.Close; |
| 455 | begin |
| 456 | FInputStream.Close; |
| 457 | FOutputStream.Close; |
| 458 | end; |
| 459 | |
| 460 | |
| 461 | { TNamedPipeImpl } |
| 462 | |
| 463 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 464 | constructor TNamedPipeImpl.Create( const aPipeName : string; const aShareMode: DWORD; |
| 465 | const aSecurityAttributes: PSecurityAttributes; |
| 466 | const aTimeOut : DWORD); |
| 467 | // Named pipe constructor |
| 468 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 469 | inherited Create( nil, nil); |
| 470 | FInputStream := TNamedPipeStreamImpl.Create( aPipeName, aShareMode, aSecurityAttributes, aTimeOut); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 471 | FOutputStream := FInputStream; // true for named pipes |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 472 | end; |
| 473 | |
| 474 | |
| 475 | constructor TNamedPipeImpl.Create( aPipe : THandle; aOwnsHandle : Boolean); |
| 476 | // Named pipe constructor |
| 477 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 478 | inherited Create( nil, nil); |
| 479 | FInputStream := THandlePipeStreamImpl.Create( aPipe, aOwnsHandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 480 | FOutputStream := FInputStream; // true for named pipes |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 481 | end; |
| 482 | |
| 483 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 484 | { TNamedPipeServerImpl } |
| 485 | |
| 486 | |
| 487 | constructor TNamedPipeServerImpl.Create( aPipe : THandle; aOwnsHandle : Boolean); |
| 488 | // Named pipe constructor |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 489 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 490 | FHandle := DuplicatePipeHandle( aPipe); |
| 491 | inherited Create( aPipe, aOwnsHandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 492 | end; |
| 493 | |
| 494 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 495 | procedure TNamedPipeServerImpl.Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 496 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 497 | FlushFileBuffers( FHandle); |
| 498 | DisconnectNamedPipe( FHandle); // force client off the pipe |
| 499 | ClosePipeHandle( FHandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 500 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 501 | inherited Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 502 | end; |
| 503 | |
| 504 | |
| 505 | { TAnonymousPipeImpl } |
| 506 | |
| 507 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 508 | constructor TAnonymousPipeImpl.Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean); |
| 509 | // Anonymous pipe constructor |
| 510 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 511 | inherited Create( nil, nil); |
| 512 | FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles); |
| 513 | FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 514 | end; |
| 515 | |
| 516 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 517 | { TServerPipeBaseImpl } |
| 518 | |
| 519 | |
| 520 | procedure TServerPipeBaseImpl.Listen; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 521 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 522 | // not much to do here |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 523 | end; |
| 524 | |
| 525 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 526 | { TAnonymousServerPipeImpl } |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 527 | |
| 528 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 529 | constructor TAnonymousServerPipeImpl.Create( aBufsize : Cardinal); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 530 | // Anonymous pipe CTOR |
| 531 | begin |
| 532 | inherited Create; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 533 | FBufsize := aBufSize; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 534 | FReadHandle := INVALID_HANDLE_VALUE; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 535 | FWriteHandle := INVALID_HANDLE_VALUE; |
| 536 | FClientAnonRead := INVALID_HANDLE_VALUE; |
| 537 | FClientAnonWrite := INVALID_HANDLE_VALUE; |
| 538 | |
| 539 | // The anonymous pipe needs to be created first so that the server can |
| 540 | // pass the handles on to the client before the serve (acceptImpl) |
| 541 | // blocking call. |
| 542 | if not CreateAnonPipe |
| 543 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 544 | ClassName+'.Create() failed'); |
| 545 | end; |
| 546 | |
| 547 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 548 | function TAnonymousServerPipeImpl.AcceptImpl: ITransport; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 549 | var buf : Byte; |
| 550 | br : DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 551 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 552 | // This 0-byte read serves merely as a blocking call. |
| 553 | if not ReadFile( FReadHandle, buf, 0, br, nil) |
| 554 | and (GetLastError() <> ERROR_MORE_DATA) |
| 555 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 556 | 'TServerPipe unable to initiate pipe communication'); |
| 557 | result := TAnonymousPipeImpl.Create( FReadHandle, FWriteHandle, FALSE); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 558 | end; |
| 559 | |
| 560 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 561 | procedure TAnonymousServerPipeImpl.Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 562 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 563 | ClosePipeHandle( FReadHandle); |
| 564 | ClosePipeHandle( FWriteHandle); |
| 565 | ClosePipeHandle( FClientAnonRead); |
| 566 | ClosePipeHandle( FClientAnonWrite); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 567 | end; |
| 568 | |
| 569 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 570 | function TAnonymousServerPipeImpl.ReadHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 571 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 572 | result := FReadHandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 573 | end; |
| 574 | |
| 575 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 576 | function TAnonymousServerPipeImpl.WriteHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 577 | begin |
| 578 | result := FWriteHandle; |
| 579 | end; |
| 580 | |
| 581 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 582 | function TAnonymousServerPipeImpl.ClientAnonRead : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 583 | begin |
| 584 | result := FClientAnonRead; |
| 585 | end; |
| 586 | |
| 587 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 588 | function TAnonymousServerPipeImpl.ClientAnonWrite : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 589 | begin |
| 590 | result := FClientAnonWrite; |
| 591 | end; |
| 592 | |
| 593 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame^] | 594 | function TAnonymousServerPipeImpl.CreateAnonPipe : Boolean; |
| 595 | var sd : PSECURITY_DESCRIPTOR; |
| 596 | sa : SECURITY_ATTRIBUTES; //TSecurityAttributes; |
| 597 | hCAR, hPipeW, hCAW, hPipe : THandle; |
| 598 | begin |
| 599 | result := FALSE; |
| 600 | |
| 601 | sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH)); |
| 602 | Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION)); |
| 603 | Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE)); |
| 604 | |
| 605 | sa.nLength := sizeof( sa); |
| 606 | sa.lpSecurityDescriptor := sd; |
| 607 | sa.bInheritHandle := TRUE; //allow passing handle to child |
| 608 | |
| 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 | end; |
| 628 | |
| 629 | |
| 630 | { TNamedServerPipeImpl } |
| 631 | |
| 632 | |
| 633 | constructor TNamedServerPipeImpl.Create( aPipename : string; aBufsize, aMaxConns : Cardinal); |
| 634 | // Named Pipe CTOR |
| 635 | begin |
| 636 | inherited Create; |
| 637 | FPipeName := aPipename; |
| 638 | FBufsize := aBufSize; |
| 639 | FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns)); |
| 640 | FHandle := INVALID_HANDLE_VALUE; |
| 641 | |
| 642 | if Copy(FPipeName,1,2) <> '\\' |
| 643 | then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost |
| 644 | end; |
| 645 | |
| 646 | |
| 647 | function TNamedServerPipeImpl.AcceptImpl: ITransport; |
| 648 | var connectRet : Boolean; |
| 649 | begin |
| 650 | if not CreateNamedPipe() |
| 651 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 652 | 'TServerPipe CreateNamedPipe failed'); |
| 653 | |
| 654 | // Wait for the client to connect; if it succeeds, the |
| 655 | // function returns a nonzero value. If the function returns |
| 656 | // zero, GetLastError should return ERROR_PIPE_CONNECTED. |
| 657 | if ConnectNamedPipe( FHandle,nil) |
| 658 | then connectRet := TRUE |
| 659 | else connectRet := (GetLastError() = ERROR_PIPE_CONNECTED); |
| 660 | |
| 661 | if not connectRet then begin |
| 662 | Close; |
| 663 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 664 | 'TServerPipe: client connection failed'); |
| 665 | end; |
| 666 | |
| 667 | result := TNamedPipeServerImpl.Create( FHandle, TRUE); |
| 668 | end; |
| 669 | |
| 670 | |
| 671 | procedure TNamedServerPipeImpl.Close; |
| 672 | begin |
| 673 | if FHandle <> INVALID_HANDLE_VALUE |
| 674 | then try |
| 675 | FlushFileBuffers( FHandle); |
| 676 | DisconnectNamedPipe( FHandle); |
| 677 | finally |
| 678 | ClosePipeHandle( FHandle); |
| 679 | end; |
| 680 | end; |
| 681 | |
| 682 | |
| 683 | function TNamedServerPipeImpl.Handle : THandle; |
| 684 | begin |
| 685 | result := FHandle; |
| 686 | end; |
| 687 | |
| 688 | |
| 689 | function TNamedServerPipeImpl.CreateNamedPipe : Boolean; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 690 | var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ; |
| 691 | everyone_sid : PSID; |
| 692 | ea : EXPLICIT_ACCESS; |
| 693 | acl : PACL; |
| 694 | sd : PSECURITY_DESCRIPTOR; |
| 695 | sa : SECURITY_ATTRIBUTES; |
| 696 | hPipe : THandle; |
| 697 | const |
| 698 | SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1)); |
| 699 | SECURITY_WORLD_RID = $00000000; |
| 700 | begin |
| 701 | // Windows - set security to allow non-elevated apps |
| 702 | // to access pipes created by elevated apps. |
| 703 | SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY; |
| 704 | everyone_sid := nil; |
| 705 | AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid); |
| 706 | |
| 707 | ZeroMemory( @ea, SizeOf(ea)); |
| 708 | ea.grfAccessPermissions := SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL; |
| 709 | ea.grfAccessMode := SET_ACCESS; |
| 710 | ea.grfInheritance := NO_INHERITANCE; |
| 711 | ea.Trustee.TrusteeForm := TRUSTEE_IS_SID; |
| 712 | ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP; |
| 713 | ea.Trustee.ptstrName := PChar(everyone_sid); |
| 714 | |
| 715 | acl := nil; |
| 716 | SetEntriesInAcl( 1, @ea, nil, acl); |
| 717 | |
| 718 | sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH)); |
| 719 | Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION)); |
| 720 | Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE)); |
| 721 | |
| 722 | sa.nLength := SizeOf(sa); |
| 723 | sa.lpSecurityDescriptor := sd; |
| 724 | sa.bInheritHandle := FALSE; |
| 725 | |
| 726 | // Create an instance of the named pipe |
| 727 | hPipe := Windows.CreateNamedPipe( PChar( FPipeName), // pipe name |
| 728 | PIPE_ACCESS_DUPLEX, // read/write access |
| 729 | PIPE_TYPE_MESSAGE or // message type pipe |
| 730 | PIPE_READMODE_MESSAGE, // message-read mode |
| 731 | FMaxConns, // max. instances |
| 732 | FBufSize, // output buffer size |
| 733 | FBufSize, // input buffer size |
| 734 | 0, // client time-out |
| 735 | @sa); // default security attribute |
| 736 | |
| 737 | if( hPipe = INVALID_HANDLE_VALUE) then begin |
| 738 | FHandle := INVALID_HANDLE_VALUE; |
| 739 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 740 | 'CreateNamedPipe() failed ' + IntToStr(GetLastError)); |
| 741 | end; |
| 742 | |
| 743 | FHandle := hPipe; |
| 744 | result := TRUE; |
| 745 | end; |
| 746 | |
| 747 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 748 | |
| 749 | end. |
| 750 | |
| 751 | |
| 752 | |