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} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 22 | {$I Thrift.Defines.inc} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 23 | |
| 24 | interface |
| 25 | |
| 26 | uses |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 27 | {$IFDEF OLD_UNIT_NAMES} |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 28 | Windows, SysUtils, Math, AccCtrl, AclAPI, SyncObjs, |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 29 | {$ELSE} |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 30 | Winapi.Windows, System.SysUtils, System.Math, Winapi.AccCtrl, Winapi.AclAPI, System.SyncObjs, |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 31 | {$ENDIF} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 32 | Thrift.Transport, |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 33 | Thrift.Utils, |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 34 | Thrift.Stream; |
| 35 | |
| 36 | const |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 37 | DEFAULT_THRIFT_PIPE_OPEN_TIMEOUT = 10; // default: fail fast on open |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 38 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 39 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 40 | type |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 41 | //--- Pipe Streams --- |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 42 | |
| 43 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 44 | TPipeStreamBase = class( TThriftStreamImpl) |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 45 | strict protected |
| 46 | FPipe : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 47 | FTimeout : DWORD; |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 48 | FOpenTimeOut : DWORD; // separate value to allow for fail-fast-on-open scenarios |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 49 | FOverlapped : Boolean; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 50 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 51 | procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override; |
| 52 | function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 53 | //procedure Open; override; - see derived classes |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 54 | procedure Close; override; |
| 55 | procedure Flush; override; |
| 56 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 57 | function ReadDirect( var buffer: TBytes; offset: Integer; count: Integer): Integer; |
| 58 | function ReadOverlapped( var buffer: TBytes; offset: Integer; count: Integer): Integer; |
| 59 | procedure WriteDirect( const buffer: TBytes; offset: Integer; count: Integer); |
| 60 | procedure WriteOverlapped( const buffer: TBytes; offset: Integer; count: Integer); |
| 61 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 62 | function IsOpen: Boolean; override; |
| 63 | function ToArray: TBytes; override; |
| 64 | public |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 65 | constructor Create( aEnableOverlapped : Boolean; |
| 66 | const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT; |
| 67 | const aOpenTimeOut : DWORD = DEFAULT_THRIFT_PIPE_OPEN_TIMEOUT); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 68 | destructor Destroy; override; |
| 69 | end; |
| 70 | |
| 71 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 72 | TNamedPipeStreamImpl = class sealed( TPipeStreamBase) |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 73 | strict private |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 74 | FPipeName : string; |
| 75 | FShareMode : DWORD; |
| 76 | FSecurityAttribs : PSecurityAttributes; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 77 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 78 | strict protected |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 79 | procedure Open; override; |
| 80 | |
| 81 | public |
| 82 | constructor Create( const aPipeName : string; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 83 | const aEnableOverlapped : Boolean; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 84 | const aShareMode: DWORD = 0; |
| 85 | const aSecurityAttributes: PSecurityAttributes = nil; |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 86 | const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT; |
| 87 | const aOpenTimeOut : DWORD = DEFAULT_THRIFT_PIPE_OPEN_TIMEOUT); overload; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 88 | end; |
| 89 | |
| 90 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 91 | THandlePipeStreamImpl = class sealed( TPipeStreamBase) |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 92 | strict private |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 93 | FSrcHandle : THandle; |
| 94 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 95 | strict protected |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 96 | procedure Open; override; |
| 97 | |
| 98 | public |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 99 | constructor Create( const aPipeHandle : THandle; |
| 100 | const aOwnsHandle, aEnableOverlapped : Boolean; |
Jens Geyer | 3e8d927 | 2014-09-14 20:10:40 +0200 | [diff] [blame] | 101 | const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 102 | destructor Destroy; override; |
| 103 | end; |
| 104 | |
| 105 | |
| 106 | //--- Pipe Transports --- |
| 107 | |
| 108 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 109 | IPipeTransport = interface( IStreamTransport) |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 110 | ['{5E05CC85-434F-428F-BFB2-856A168B5558}'] |
| 111 | end; |
| 112 | |
| 113 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 114 | TPipeTransportBase = class( TStreamTransportImpl, IPipeTransport) |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 115 | public |
| 116 | // ITransport |
| 117 | function GetIsOpen: Boolean; override; |
| 118 | procedure Open; override; |
| 119 | procedure Close; override; |
| 120 | end; |
| 121 | |
| 122 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 123 | TNamedPipeTransportClientEndImpl = class( TPipeTransportBase) |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 124 | public |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 125 | // Named pipe constructors |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 126 | constructor Create( aPipe : THandle; aOwnsHandle : Boolean; |
| 127 | const aTimeOut : DWORD); overload; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 128 | constructor Create( const aPipeName : string; |
| 129 | const aShareMode: DWORD = 0; |
| 130 | const aSecurityAttributes: PSecurityAttributes = nil; |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 131 | const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT; |
| 132 | const aOpenTimeOut : DWORD = DEFAULT_THRIFT_PIPE_OPEN_TIMEOUT); overload; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 133 | end; |
| 134 | |
| 135 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 136 | TNamedPipeTransportServerEndImpl = class( TNamedPipeTransportClientEndImpl) |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 137 | strict private |
| 138 | FHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 139 | public |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 140 | // ITransport |
| 141 | procedure Close; override; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 142 | constructor Create( aPipe : THandle; aOwnsHandle : Boolean; |
Jens Geyer | 3e8d927 | 2014-09-14 20:10:40 +0200 | [diff] [blame] | 143 | const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); reintroduce; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 144 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 145 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 146 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 147 | TAnonymousPipeTransportImpl = class( TPipeTransportBase) |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 148 | public |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 149 | // Anonymous pipe constructor |
Jens Geyer | dd074e7 | 2016-04-19 23:31:33 +0200 | [diff] [blame] | 150 | constructor Create(const aPipeRead, aPipeWrite : THandle; |
| 151 | aOwnsHandles : Boolean; |
| 152 | const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); overload; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 153 | end; |
| 154 | |
| 155 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 156 | //--- Server Transports --- |
| 157 | |
| 158 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 159 | IAnonymousPipeServerTransport = interface( IServerTransport) |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 160 | ['{7AEE6793-47B9-4E49-981A-C39E9108E9AD}'] |
| 161 | // Server side anonymous pipe ends |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 162 | function ReadHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 163 | function WriteHandle : THandle; |
| 164 | // Client side anonymous pipe ends |
| 165 | function ClientAnonRead : THandle; |
| 166 | function ClientAnonWrite : THandle; |
| 167 | end; |
| 168 | |
| 169 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 170 | INamedPipeServerTransport = interface( IServerTransport) |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 171 | ['{9DF9EE48-D065-40AF-8F67-D33037D3D960}'] |
| 172 | function Handle : THandle; |
| 173 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 174 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 175 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 176 | TPipeServerTransportBase = class( TServerTransportImpl) |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 177 | strict protected |
| 178 | FStopServer : TEvent; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 179 | procedure InternalClose; virtual; abstract; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 180 | function QueryStopServer : Boolean; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 181 | public |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 182 | constructor Create; |
| 183 | destructor Destroy; override; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 184 | procedure Listen; override; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 185 | procedure Close; override; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 186 | end; |
| 187 | |
| 188 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 189 | TAnonymousPipeServerTransportImpl = class( TPipeServerTransportBase, IAnonymousPipeServerTransport) |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 190 | strict private |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 191 | FBufSize : DWORD; |
| 192 | |
| 193 | // Server side anonymous pipe handles |
| 194 | FReadHandle, |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 195 | FWriteHandle : THandle; |
| 196 | |
| 197 | //Client side anonymous pipe handles |
| 198 | FClientAnonRead, |
| 199 | FClientAnonWrite : THandle; |
| 200 | |
Jens Geyer | dd074e7 | 2016-04-19 23:31:33 +0200 | [diff] [blame] | 201 | FTimeOut: DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 202 | protected |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 203 | function Accept(const fnAccepting: TProc): ITransport; override; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 204 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 205 | function CreateAnonPipe : Boolean; |
| 206 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 207 | // IAnonymousPipeServerTransport |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 208 | function ReadHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 209 | function WriteHandle : THandle; |
| 210 | function ClientAnonRead : THandle; |
| 211 | function ClientAnonWrite : THandle; |
| 212 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 213 | procedure InternalClose; override; |
| 214 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 215 | public |
Jens Geyer | dd074e7 | 2016-04-19 23:31:33 +0200 | [diff] [blame] | 216 | constructor Create(aBufsize : Cardinal = 4096; aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 217 | end; |
| 218 | |
| 219 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 220 | TNamedPipeServerTransportImpl = class( TPipeServerTransportBase, INamedPipeServerTransport) |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 221 | strict private |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 222 | FPipeName : string; |
| 223 | FMaxConns : DWORD; |
| 224 | FBufSize : DWORD; |
Jens Geyer | 0b20cc8 | 2013-03-07 20:47:01 +0100 | [diff] [blame] | 225 | FTimeout : DWORD; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 226 | FHandle : THandle; |
| 227 | FConnected : Boolean; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 228 | |
| 229 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 230 | strict protected |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 231 | function Accept(const fnAccepting: TProc): ITransport; override; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 232 | function CreateNamedPipe : THandle; |
| 233 | function CreateTransportInstance : ITransport; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 234 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 235 | // INamedPipeServerTransport |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 236 | function Handle : THandle; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 237 | procedure InternalClose; override; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 238 | |
| 239 | public |
| 240 | constructor Create( aPipename : string; aBufsize : Cardinal = 4096; |
Jens Geyer | 0b20cc8 | 2013-03-07 20:47:01 +0100 | [diff] [blame] | 241 | aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES; |
Jens Geyer | 2ad6c30 | 2015-02-26 19:38:53 +0100 | [diff] [blame] | 242 | aTimeOut : Cardinal = INFINITE); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 243 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 244 | |
| 245 | |
| 246 | implementation |
| 247 | |
| 248 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 249 | procedure ClosePipeHandle( var hPipe : THandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 250 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 251 | if hPipe <> INVALID_HANDLE_VALUE |
| 252 | then try |
| 253 | CloseHandle( hPipe); |
| 254 | finally |
| 255 | hPipe := INVALID_HANDLE_VALUE; |
| 256 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 257 | end; |
| 258 | |
| 259 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 260 | function DuplicatePipeHandle( const hSource : THandle) : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 261 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 262 | if not DuplicateHandle( GetCurrentProcess, hSource, |
| 263 | GetCurrentProcess, @result, |
| 264 | 0, FALSE, DUPLICATE_SAME_ACCESS) |
| 265 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 266 | 'DuplicateHandle: '+SysErrorMessage(GetLastError)); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 267 | end; |
| 268 | |
| 269 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 270 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 271 | { TPipeStreamBase } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 272 | |
| 273 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 274 | constructor TPipeStreamBase.Create( aEnableOverlapped : Boolean; |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 275 | const aTimeOut, aOpenTimeOut : DWORD); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 276 | begin |
| 277 | inherited Create; |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 278 | ASSERT( aTimeout > 0); // aOpenTimeout may be 0 |
| 279 | FPipe := INVALID_HANDLE_VALUE; |
| 280 | FTimeout := aTimeOut; |
| 281 | FOpenTimeOut := aOpenTimeOut; |
| 282 | FOverlapped := aEnableOverlapped; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 283 | end; |
| 284 | |
| 285 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 286 | destructor TPipeStreamBase.Destroy; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 287 | begin |
| 288 | try |
| 289 | Close; |
| 290 | finally |
| 291 | inherited Destroy; |
| 292 | end; |
| 293 | end; |
| 294 | |
| 295 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 296 | procedure TPipeStreamBase.Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 297 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 298 | ClosePipeHandle( FPipe); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 299 | end; |
| 300 | |
| 301 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 302 | procedure TPipeStreamBase.Flush; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 303 | begin |
Jens Geyer | 0d227b1 | 2015-12-02 19:50:55 +0100 | [diff] [blame] | 304 | FlushFileBuffers( FPipe); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 305 | end; |
| 306 | |
| 307 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 308 | function TPipeStreamBase.IsOpen: Boolean; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 309 | begin |
| 310 | result := (FPipe <> INVALID_HANDLE_VALUE); |
| 311 | end; |
| 312 | |
| 313 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 314 | procedure TPipeStreamBase.Write(const buffer: TBytes; offset, count: Integer); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 315 | begin |
| 316 | if FOverlapped |
| 317 | then WriteOverlapped( buffer, offset, count) |
| 318 | else WriteDirect( buffer, offset, count); |
| 319 | end; |
| 320 | |
| 321 | |
| 322 | function TPipeStreamBase.Read( var buffer: TBytes; offset, count: Integer): Integer; |
| 323 | begin |
| 324 | if FOverlapped |
| 325 | then result := ReadOverlapped( buffer, offset, count) |
| 326 | else result := ReadDirect( buffer, offset, count); |
| 327 | end; |
| 328 | |
| 329 | |
| 330 | procedure TPipeStreamBase.WriteDirect(const buffer: TBytes; offset, count: Integer); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 331 | var cbWritten : DWORD; |
| 332 | begin |
| 333 | if not IsOpen |
| 334 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 335 | 'Called write on non-open pipe'); |
| 336 | |
| 337 | if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil) |
| 338 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 339 | 'Write to pipe failed'); |
| 340 | end; |
| 341 | |
| 342 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 343 | function TPipeStreamBase.ReadDirect( var buffer: TBytes; offset, count: Integer): Integer; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 344 | var cbRead, dwErr : DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 345 | bytes, retries : LongInt; |
| 346 | bOk : Boolean; |
| 347 | const INTERVAL = 10; // ms |
| 348 | begin |
| 349 | if not IsOpen |
| 350 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 351 | 'Called read on non-open pipe'); |
| 352 | |
| 353 | // MSDN: Handle can be a handle to a named pipe instance, |
| 354 | // or it can be a handle to the read end of an anonymous pipe, |
| 355 | // The handle must have GENERIC_READ access to the pipe. |
| 356 | if FTimeOut <> INFINITE then begin |
| 357 | retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL)); |
| 358 | while TRUE do begin |
Jens Geyer | 5988f48 | 2016-04-19 23:01:24 +0200 | [diff] [blame] | 359 | if not PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) then begin |
| 360 | dwErr := GetLastError; |
| 361 | if (dwErr = ERROR_INVALID_HANDLE) |
| 362 | or (dwErr = ERROR_BROKEN_PIPE) |
| 363 | or (dwErr = ERROR_PIPE_NOT_CONNECTED) |
| 364 | then begin |
| 365 | result := 0; // other side closed the pipe |
| 366 | Exit; |
| 367 | end; |
| 368 | end |
| 369 | else if bytes > 0 then begin |
| 370 | Break; // there are data |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 371 | end; |
| 372 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 373 | Dec( retries); |
| 374 | if retries > 0 |
| 375 | then Sleep( INTERVAL) |
| 376 | else raise TTransportException.Create( TTransportException.TExceptionType.TimedOut, |
| 377 | 'Pipe read timed out'); |
| 378 | end; |
| 379 | end; |
| 380 | |
| 381 | // read the data (or block INFINITE-ly) |
| 382 | bOk := ReadFile( FPipe, buffer[offset], count, cbRead, nil); |
| 383 | if (not bOk) and (GetLastError() <> ERROR_MORE_DATA) |
| 384 | then result := 0 // No more data, possibly because client disconnected. |
| 385 | else result := cbRead; |
| 386 | end; |
| 387 | |
| 388 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 389 | procedure TPipeStreamBase.WriteOverlapped(const buffer: TBytes; offset, count: Integer); |
| 390 | var cbWritten, dwWait, dwError : DWORD; |
| 391 | overlapped : IOverlappedHelper; |
| 392 | begin |
| 393 | if not IsOpen |
| 394 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 395 | 'Called write on non-open pipe'); |
| 396 | |
| 397 | overlapped := TOverlappedHelperImpl.Create; |
| 398 | |
| 399 | if not WriteFile( FPipe, buffer[offset], count, cbWritten, overlapped.OverlappedPtr) |
| 400 | then begin |
| 401 | dwError := GetLastError; |
| 402 | case dwError of |
| 403 | ERROR_IO_PENDING : begin |
| 404 | dwWait := overlapped.WaitFor(FTimeout); |
| 405 | |
| 406 | if (dwWait = WAIT_TIMEOUT) |
| 407 | then raise TTransportException.Create( TTransportException.TExceptionType.TimedOut, |
| 408 | 'Pipe write timed out'); |
| 409 | |
| 410 | if (dwWait <> WAIT_OBJECT_0) |
| 411 | or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbWritten, TRUE) |
| 412 | then raise TTransportException.Create( TTransportException.TExceptionType.Unknown, |
| 413 | 'Pipe write error'); |
| 414 | end; |
| 415 | |
| 416 | else |
| 417 | raise TTransportException.Create( TTransportException.TExceptionType.Unknown, |
| 418 | SysErrorMessage(dwError)); |
| 419 | end; |
| 420 | end; |
| 421 | |
| 422 | ASSERT( DWORD(count) = cbWritten); |
| 423 | end; |
| 424 | |
| 425 | |
| 426 | function TPipeStreamBase.ReadOverlapped( var buffer: TBytes; offset, count: Integer): Integer; |
| 427 | var cbRead, dwWait, dwError : DWORD; |
| 428 | bOk : Boolean; |
| 429 | overlapped : IOverlappedHelper; |
| 430 | begin |
| 431 | if not IsOpen |
| 432 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 433 | 'Called read on non-open pipe'); |
| 434 | |
| 435 | overlapped := TOverlappedHelperImpl.Create; |
| 436 | |
| 437 | // read the data |
| 438 | bOk := ReadFile( FPipe, buffer[offset], count, cbRead, overlapped.OverlappedPtr); |
| 439 | if not bOk then begin |
| 440 | dwError := GetLastError; |
| 441 | case dwError of |
| 442 | ERROR_IO_PENDING : begin |
| 443 | dwWait := overlapped.WaitFor(FTimeout); |
| 444 | |
| 445 | if (dwWait = WAIT_TIMEOUT) |
| 446 | then raise TTransportException.Create( TTransportException.TExceptionType.TimedOut, |
| 447 | 'Pipe read timed out'); |
| 448 | |
| 449 | if (dwWait <> WAIT_OBJECT_0) |
| 450 | or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbRead, TRUE) |
| 451 | then raise TTransportException.Create( TTransportException.TExceptionType.Unknown, |
| 452 | 'Pipe read error'); |
| 453 | end; |
| 454 | |
| 455 | else |
| 456 | raise TTransportException.Create( TTransportException.TExceptionType.Unknown, |
| 457 | SysErrorMessage(dwError)); |
| 458 | end; |
| 459 | end; |
| 460 | |
| 461 | ASSERT( cbRead > 0); // see TTransportImpl.ReadAll() |
| 462 | ASSERT( cbRead = DWORD(count)); |
| 463 | result := cbRead; |
| 464 | end; |
| 465 | |
| 466 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 467 | function TPipeStreamBase.ToArray: TBytes; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 468 | var bytes : LongInt; |
| 469 | begin |
| 470 | SetLength( result, 0); |
| 471 | bytes := 0; |
| 472 | |
| 473 | if IsOpen |
| 474 | and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) |
| 475 | and (bytes > 0) |
| 476 | then begin |
| 477 | SetLength( result, bytes); |
| 478 | Read( result, 0, bytes); |
| 479 | end; |
| 480 | end; |
| 481 | |
| 482 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 483 | { TNamedPipeStreamImpl } |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 484 | |
| 485 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 486 | constructor TNamedPipeStreamImpl.Create( const aPipeName : string; |
| 487 | const aEnableOverlapped : Boolean; |
| 488 | const aShareMode: DWORD; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 489 | const aSecurityAttributes: PSecurityAttributes; |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 490 | const aTimeOut, aOpenTimeOut : DWORD); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 491 | begin |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 492 | inherited Create( aEnableOverlapped, aTimeout, aOpenTimeOut); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 493 | |
| 494 | FPipeName := aPipeName; |
| 495 | FShareMode := aShareMode; |
| 496 | FSecurityAttribs := aSecurityAttributes; |
| 497 | |
| 498 | if Copy(FPipeName,1,2) <> '\\' |
| 499 | then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 500 | end; |
| 501 | |
| 502 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 503 | procedure TNamedPipeStreamImpl.Open; |
| 504 | var hPipe : THandle; |
Jens Geyer | b89b5b9 | 2016-04-19 23:09:41 +0200 | [diff] [blame] | 505 | retries, timeout, dwErr : DWORD; |
| 506 | const INTERVAL = 10; // ms |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 507 | begin |
| 508 | if IsOpen then Exit; |
| 509 | |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 510 | retries := Max( 1, Round( 1.0 * FOpenTimeOut / INTERVAL)); |
| 511 | timeout := FOpenTimeOut; |
Jens Geyer | b89b5b9 | 2016-04-19 23:09:41 +0200 | [diff] [blame] | 512 | |
| 513 | // if the server hasn't gotten to the point where the pipe has been created, at least wait the timeout |
| 514 | // According to MSDN, if no instances of the specified named pipe exist, the WaitNamedPipe function |
| 515 | // returns IMMEDIATELY, regardless of the time-out value. |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 516 | // Always use INTERVAL, since WaitNamedPipe(0) defaults to some other value |
Jens Geyer | b89b5b9 | 2016-04-19 23:09:41 +0200 | [diff] [blame] | 517 | while not WaitNamedPipe( PChar(FPipeName), INTERVAL) do begin |
| 518 | dwErr := GetLastError; |
| 519 | if dwErr <> ERROR_FILE_NOT_FOUND |
| 520 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 521 | 'Unable to open pipe, '+SysErrorMessage(dwErr)); |
| 522 | |
| 523 | if timeout <> INFINITE then begin |
| 524 | if (retries > 0) |
| 525 | then Dec(retries) |
| 526 | else raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 527 | 'Unable to open pipe, timed out'); |
| 528 | end; |
| 529 | |
| 530 | Sleep(INTERVAL) |
| 531 | end; |
| 532 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 533 | // open that thingy |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 534 | hPipe := CreateFile( PChar( FPipeName), |
| 535 | GENERIC_READ or GENERIC_WRITE, |
| 536 | FShareMode, // sharing |
| 537 | FSecurityAttribs, // security attributes |
| 538 | OPEN_EXISTING, // opens existing pipe |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 539 | FILE_FLAG_OVERLAPPED or FILE_FLAG_WRITE_THROUGH, // async+fast, please |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 540 | 0); // no template file |
| 541 | |
| 542 | if hPipe = INVALID_HANDLE_VALUE |
| 543 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 544 | 'Unable to open pipe, '+SysErrorMessage(GetLastError)); |
| 545 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 546 | // everything fine |
| 547 | FPipe := hPipe; |
| 548 | end; |
| 549 | |
| 550 | |
| 551 | { THandlePipeStreamImpl } |
| 552 | |
| 553 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 554 | constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle; |
| 555 | const aOwnsHandle, aEnableOverlapped : Boolean; |
| 556 | const aTimeOut : DWORD); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 557 | begin |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 558 | inherited Create( aEnableOverlapped, aTimeOut); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 559 | |
| 560 | if aOwnsHandle |
| 561 | then FSrcHandle := aPipeHandle |
| 562 | else FSrcHandle := DuplicatePipeHandle( aPipeHandle); |
| 563 | |
| 564 | Open; |
| 565 | end; |
| 566 | |
| 567 | |
| 568 | destructor THandlePipeStreamImpl.Destroy; |
| 569 | begin |
| 570 | try |
| 571 | ClosePipeHandle( FSrcHandle); |
| 572 | finally |
| 573 | inherited Destroy; |
| 574 | end; |
| 575 | end; |
| 576 | |
| 577 | |
| 578 | procedure THandlePipeStreamImpl.Open; |
| 579 | begin |
| 580 | if not IsOpen |
| 581 | then FPipe := DuplicatePipeHandle( FSrcHandle); |
| 582 | end; |
| 583 | |
| 584 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 585 | { TPipeTransportBase } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 586 | |
| 587 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 588 | function TPipeTransportBase.GetIsOpen: Boolean; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 589 | begin |
Jens Geyer | 0b20cc8 | 2013-03-07 20:47:01 +0100 | [diff] [blame] | 590 | result := (FInputStream <> nil) and (FInputStream.IsOpen) |
| 591 | and (FOutputStream <> nil) and (FOutputStream.IsOpen); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 592 | end; |
| 593 | |
| 594 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 595 | procedure TPipeTransportBase.Open; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 596 | begin |
| 597 | FInputStream.Open; |
| 598 | FOutputStream.Open; |
| 599 | end; |
| 600 | |
| 601 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 602 | procedure TPipeTransportBase.Close; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 603 | begin |
| 604 | FInputStream.Close; |
| 605 | FOutputStream.Close; |
| 606 | end; |
| 607 | |
| 608 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 609 | { TNamedPipeTransportClientEndImpl } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 610 | |
| 611 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 612 | constructor TNamedPipeTransportClientEndImpl.Create( const aPipeName : string; const aShareMode: DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 613 | const aSecurityAttributes: PSecurityAttributes; |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 614 | const aTimeOut, aOpenTimeOut : DWORD); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 615 | // Named pipe constructor |
| 616 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 617 | inherited Create( nil, nil); |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame^] | 618 | FInputStream := TNamedPipeStreamImpl.Create( aPipeName, TRUE, aShareMode, aSecurityAttributes, aTimeOut, aOpenTimeOut); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 619 | FOutputStream := FInputStream; // true for named pipes |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 620 | end; |
| 621 | |
| 622 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 623 | constructor TNamedPipeTransportClientEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean; |
| 624 | const aTimeOut : DWORD); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 625 | // Named pipe constructor |
| 626 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 627 | inherited Create( nil, nil); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 628 | FInputStream := THandlePipeStreamImpl.Create( aPipe, TRUE, aOwnsHandle, aTimeOut); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 629 | FOutputStream := FInputStream; // true for named pipes |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 630 | end; |
| 631 | |
| 632 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 633 | { TNamedPipeTransportServerEndImpl } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 634 | |
| 635 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 636 | constructor TNamedPipeTransportServerEndImpl.Create( aPipe : THandle; aOwnsHandle : Boolean; |
| 637 | const aTimeOut : DWORD); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 638 | // Named pipe constructor |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 639 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 640 | FHandle := DuplicatePipeHandle( aPipe); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 641 | inherited Create( aPipe, aOwnsHandle, aTimeOut); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 642 | end; |
| 643 | |
| 644 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 645 | procedure TNamedPipeTransportServerEndImpl.Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 646 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 647 | FlushFileBuffers( FHandle); |
| 648 | DisconnectNamedPipe( FHandle); // force client off the pipe |
| 649 | ClosePipeHandle( FHandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 650 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 651 | inherited Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 652 | end; |
| 653 | |
| 654 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 655 | { TAnonymousPipeTransportImpl } |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 656 | |
| 657 | |
Jens Geyer | dd074e7 | 2016-04-19 23:31:33 +0200 | [diff] [blame] | 658 | constructor TAnonymousPipeTransportImpl.Create( const aPipeRead, aPipeWrite : THandle; |
| 659 | aOwnsHandles : Boolean; |
| 660 | const aTimeOut : DWORD = DEFAULT_THRIFT_TIMEOUT); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 661 | // Anonymous pipe constructor |
| 662 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 663 | inherited Create( nil, nil); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 664 | // overlapped is not supported with AnonPipes, see MSDN |
Jens Geyer | dd074e7 | 2016-04-19 23:31:33 +0200 | [diff] [blame] | 665 | FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles, FALSE, aTimeOut); |
| 666 | FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles, FALSE, aTimeOut); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 667 | end; |
| 668 | |
| 669 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 670 | { TPipeServerTransportBase } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 671 | |
| 672 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 673 | constructor TPipeServerTransportBase.Create; |
| 674 | begin |
| 675 | inherited Create; |
| 676 | FStopServer := TEvent.Create(nil,TRUE,FALSE,''); // manual reset |
| 677 | end; |
| 678 | |
| 679 | |
| 680 | destructor TPipeServerTransportBase.Destroy; |
| 681 | begin |
| 682 | try |
| 683 | FreeAndNil( FStopServer); |
| 684 | finally |
| 685 | inherited Destroy; |
| 686 | end; |
| 687 | end; |
| 688 | |
| 689 | |
| 690 | function TPipeServerTransportBase.QueryStopServer : Boolean; |
| 691 | begin |
| 692 | result := (FStopServer = nil) |
| 693 | or (FStopServer.WaitFor(0) <> wrTimeout); |
| 694 | end; |
| 695 | |
| 696 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 697 | procedure TPipeServerTransportBase.Listen; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 698 | begin |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 699 | FStopServer.ResetEvent; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 700 | end; |
| 701 | |
| 702 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 703 | procedure TPipeServerTransportBase.Close; |
| 704 | begin |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 705 | FStopServer.SetEvent; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 706 | InternalClose; |
| 707 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 708 | |
| 709 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 710 | { TAnonymousPipeServerTransportImpl } |
| 711 | |
| 712 | |
Jens Geyer | dd074e7 | 2016-04-19 23:31:33 +0200 | [diff] [blame] | 713 | constructor TAnonymousPipeServerTransportImpl.Create(aBufsize : Cardinal; aTimeOut : DWORD); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 714 | // Anonymous pipe CTOR |
| 715 | begin |
| 716 | inherited Create; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 717 | FBufsize := aBufSize; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 718 | FReadHandle := INVALID_HANDLE_VALUE; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 719 | FWriteHandle := INVALID_HANDLE_VALUE; |
| 720 | FClientAnonRead := INVALID_HANDLE_VALUE; |
| 721 | FClientAnonWrite := INVALID_HANDLE_VALUE; |
Jens Geyer | dd074e7 | 2016-04-19 23:31:33 +0200 | [diff] [blame] | 722 | FTimeOut := aTimeOut; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 723 | |
| 724 | // The anonymous pipe needs to be created first so that the server can |
| 725 | // pass the handles on to the client before the serve (acceptImpl) |
| 726 | // blocking call. |
| 727 | if not CreateAnonPipe |
| 728 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 729 | ClassName+'.Create() failed'); |
| 730 | end; |
| 731 | |
| 732 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 733 | function TAnonymousPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 734 | var buf : Byte; |
| 735 | br : DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 736 | begin |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 737 | if Assigned(fnAccepting) |
| 738 | then fnAccepting(); |
| 739 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 740 | // This 0-byte read serves merely as a blocking call. |
| 741 | if not ReadFile( FReadHandle, buf, 0, br, nil) |
| 742 | and (GetLastError() <> ERROR_MORE_DATA) |
| 743 | then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 744 | 'TServerPipe unable to initiate pipe communication'); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 745 | |
| 746 | // create the transport impl |
Jens Geyer | dd074e7 | 2016-04-19 23:31:33 +0200 | [diff] [blame] | 747 | result := TAnonymousPipeTransportImpl.Create( FReadHandle, FWriteHandle, FALSE, FTimeOut); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 748 | end; |
| 749 | |
| 750 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 751 | procedure TAnonymousPipeServerTransportImpl.InternalClose; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 752 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 753 | ClosePipeHandle( FReadHandle); |
| 754 | ClosePipeHandle( FWriteHandle); |
| 755 | ClosePipeHandle( FClientAnonRead); |
| 756 | ClosePipeHandle( FClientAnonWrite); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 757 | end; |
| 758 | |
| 759 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 760 | function TAnonymousPipeServerTransportImpl.ReadHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 761 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 762 | result := FReadHandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 763 | end; |
| 764 | |
| 765 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 766 | function TAnonymousPipeServerTransportImpl.WriteHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 767 | begin |
| 768 | result := FWriteHandle; |
| 769 | end; |
| 770 | |
| 771 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 772 | function TAnonymousPipeServerTransportImpl.ClientAnonRead : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 773 | begin |
| 774 | result := FClientAnonRead; |
| 775 | end; |
| 776 | |
| 777 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 778 | function TAnonymousPipeServerTransportImpl.ClientAnonWrite : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 779 | begin |
| 780 | result := FClientAnonWrite; |
| 781 | end; |
| 782 | |
| 783 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 784 | function TAnonymousPipeServerTransportImpl.CreateAnonPipe : Boolean; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 785 | var sd : PSECURITY_DESCRIPTOR; |
| 786 | sa : SECURITY_ATTRIBUTES; //TSecurityAttributes; |
| 787 | hCAR, hPipeW, hCAW, hPipe : THandle; |
| 788 | begin |
| 789 | result := FALSE; |
| 790 | |
| 791 | sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH)); |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 792 | try |
| 793 | Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION)); |
| 794 | Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE)); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 795 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 796 | sa.nLength := sizeof( sa); |
| 797 | sa.lpSecurityDescriptor := sd; |
| 798 | sa.bInheritHandle := TRUE; //allow passing handle to child |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 799 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 800 | if not CreatePipe( hCAR, hPipeW, @sa, FBufSize) then begin //create stdin pipe |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 801 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 802 | 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError)); |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 803 | Exit; |
| 804 | end; |
| 805 | |
| 806 | if not CreatePipe( hPipe, hCAW, @sa, FBufSize) then begin //create stdout pipe |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 807 | CloseHandle( hCAR); |
| 808 | CloseHandle( hPipeW); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 809 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 810 | 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError)); |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 811 | Exit; |
| 812 | end; |
| 813 | |
| 814 | FClientAnonRead := hCAR; |
| 815 | FClientAnonWrite := hCAW; |
| 816 | FReadHandle := hPipe; |
| 817 | FWriteHandle := hPipeW; |
| 818 | |
| 819 | result := TRUE; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 820 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 821 | finally |
| 822 | if sd <> nil then LocalFree( Cardinal(sd)); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 823 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 824 | end; |
| 825 | |
| 826 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 827 | { TNamedPipeServerTransportImpl } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 828 | |
| 829 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 830 | constructor TNamedPipeServerTransportImpl.Create( aPipename : string; aBufsize, aMaxConns, aTimeOut : Cardinal); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 831 | // Named Pipe CTOR |
| 832 | begin |
| 833 | inherited Create; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 834 | ASSERT( aTimeout > 0); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 835 | FPipeName := aPipename; |
| 836 | FBufsize := aBufSize; |
| 837 | FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns)); |
| 838 | FHandle := INVALID_HANDLE_VALUE; |
| 839 | FTimeout := aTimeOut; |
| 840 | FConnected := FALSE; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 841 | |
| 842 | if Copy(FPipeName,1,2) <> '\\' |
| 843 | then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost |
| 844 | end; |
| 845 | |
| 846 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 847 | function TNamedPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 848 | var dwError, dwWait, dwDummy : DWORD; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 849 | overlapped : IOverlappedHelper; |
| 850 | handles : array[0..1] of THandle; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 851 | begin |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 852 | overlapped := TOverlappedHelperImpl.Create; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 853 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 854 | ASSERT( not FConnected); |
Jens Geyer | 2ad6c30 | 2015-02-26 19:38:53 +0100 | [diff] [blame] | 855 | CreateNamedPipe; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 856 | while not FConnected do begin |
Jens Geyer | 2ad6c30 | 2015-02-26 19:38:53 +0100 | [diff] [blame] | 857 | |
| 858 | if QueryStopServer |
| 859 | then Abort; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 860 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 861 | if Assigned(fnAccepting) |
| 862 | then fnAccepting(); |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 863 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 864 | // Wait for the client to connect; if it succeeds, the |
| 865 | // function returns a nonzero value. If the function returns |
| 866 | // zero, GetLastError should return ERROR_PIPE_CONNECTED. |
| 867 | if ConnectNamedPipe( Handle, overlapped.OverlappedPtr) then begin |
| 868 | FConnected := TRUE; |
| 869 | Break; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 870 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 871 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 872 | // ConnectNamedPipe() returns FALSE for OverlappedIO, even if connected. |
| 873 | // We have to check GetLastError() explicitly to find out |
| 874 | dwError := GetLastError; |
| 875 | case dwError of |
| 876 | ERROR_PIPE_CONNECTED : begin |
| 877 | FConnected := not QueryStopServer; // special case: pipe immediately connected |
| 878 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 879 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 880 | ERROR_IO_PENDING : begin |
| 881 | handles[0] := overlapped.WaitHandle; |
| 882 | handles[1] := FStopServer.Handle; |
| 883 | dwWait := WaitForMultipleObjects( 2, @handles, FALSE, FTimeout); |
| 884 | FConnected := (dwWait = WAIT_OBJECT_0) |
| 885 | and GetOverlappedResult( Handle, overlapped.Overlapped, dwDummy, TRUE) |
| 886 | and not QueryStopServer; |
| 887 | end; |
| 888 | |
| 889 | else |
| 890 | InternalClose; |
| 891 | raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
| 892 | 'Client connection failed'); |
| 893 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 894 | end; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 895 | |
| 896 | // create the transport impl |
| 897 | result := CreateTransportInstance; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 898 | end; |
| 899 | |
| 900 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 901 | function TNamedPipeServerTransportImpl.CreateTransportInstance : ITransport; |
| 902 | // create the transport impl |
| 903 | var hPipe : THandle; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 904 | begin |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 905 | hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE))); |
| 906 | try |
| 907 | FConnected := FALSE; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 908 | result := TNamedPipeTransportServerEndImpl.Create( hPipe, TRUE, FTimeout); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 909 | except |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 910 | ClosePipeHandle(hPipe); |
| 911 | raise; |
| 912 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 913 | end; |
| 914 | |
| 915 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 916 | procedure TNamedPipeServerTransportImpl.InternalClose; |
| 917 | var hPipe : THandle; |
| 918 | begin |
| 919 | hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE))); |
| 920 | if hPipe = INVALID_HANDLE_VALUE then Exit; |
| 921 | |
| 922 | try |
| 923 | if FConnected |
| 924 | then FlushFileBuffers( hPipe) |
| 925 | else CancelIo( hPipe); |
| 926 | DisconnectNamedPipe( hPipe); |
| 927 | finally |
| 928 | ClosePipeHandle( hPipe); |
| 929 | FConnected := FALSE; |
| 930 | end; |
| 931 | end; |
| 932 | |
| 933 | |
| 934 | function TNamedPipeServerTransportImpl.Handle : THandle; |
| 935 | begin |
| 936 | {$IFDEF WIN64} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 937 | result := THandle( InterlockedExchangeAdd64( Int64(FHandle), 0)); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 938 | {$ELSE} |
| 939 | result := THandle( InterlockedExchangeAdd( Integer(FHandle), 0)); |
| 940 | {$ENDIF} |
| 941 | end; |
| 942 | |
| 943 | |
| 944 | function TNamedPipeServerTransportImpl.CreateNamedPipe : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 945 | var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ; |
| 946 | everyone_sid : PSID; |
| 947 | ea : EXPLICIT_ACCESS; |
| 948 | acl : PACL; |
| 949 | sd : PSECURITY_DESCRIPTOR; |
| 950 | sa : SECURITY_ATTRIBUTES; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 951 | const |
| 952 | SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1)); |
| 953 | SECURITY_WORLD_RID = $00000000; |
| 954 | begin |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 955 | sd := nil; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 956 | everyone_sid := nil; |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 957 | try |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 958 | ASSERT( (FHandle = INVALID_HANDLE_VALUE) and not FConnected); |
| 959 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 960 | // Windows - set security to allow non-elevated apps |
| 961 | // to access pipes created by elevated apps. |
| 962 | SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY; |
| 963 | 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] | 964 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 965 | ZeroMemory( @ea, SizeOf(ea)); |
| 966 | ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL; |
| 967 | ea.grfAccessMode := SET_ACCESS; |
| 968 | ea.grfInheritance := NO_INHERITANCE; |
| 969 | ea.Trustee.TrusteeForm := TRUSTEE_IS_SID; |
| 970 | ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP; |
| 971 | ea.Trustee.ptstrName := PChar(everyone_sid); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 972 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 973 | acl := nil; |
| 974 | SetEntriesInAcl( 1, @ea, nil, acl); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 975 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 976 | sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH)); |
| 977 | Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION)); |
| 978 | Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE)); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 979 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 980 | sa.nLength := SizeOf(sa); |
| 981 | sa.lpSecurityDescriptor := sd; |
| 982 | sa.bInheritHandle := FALSE; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 983 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 984 | // Create an instance of the named pipe |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 985 | {$IFDEF OLD_UNIT_NAMES} |
| 986 | result := Windows.CreateNamedPipe( |
| 987 | {$ELSE} |
| 988 | result := Winapi.Windows.CreateNamedPipe( |
| 989 | {$ENDIF} |
| 990 | PChar( FPipeName), // pipe name |
| 991 | PIPE_ACCESS_DUPLEX or // read/write access |
| 992 | FILE_FLAG_OVERLAPPED, // async mode |
| 993 | PIPE_TYPE_BYTE or // byte type pipe |
| 994 | PIPE_READMODE_BYTE, // byte read mode |
| 995 | FMaxConns, // max. instances |
| 996 | FBufSize, // output buffer size |
| 997 | FBufSize, // input buffer size |
| 998 | FTimeout, // time-out, see MSDN |
| 999 | @sa // default security attribute |
| 1000 | ); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1001 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 1002 | if( result <> INVALID_HANDLE_VALUE) |
| 1003 | then InterlockedExchangePointer( Pointer(FHandle), Pointer(result)) |
| 1004 | else raise TTransportException.Create( TTransportException.TExceptionType.NotOpen, |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 1005 | 'CreateNamedPipe() failed ' + IntToStr(GetLastError)); |
| 1006 | |
| 1007 | finally |
| 1008 | if sd <> nil then LocalFree( Cardinal( sd)); |
| 1009 | if acl <> nil then LocalFree( Cardinal( acl)); |
| 1010 | if everyone_sid <> nil then FreeSid(everyone_sid); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1011 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1012 | end; |
| 1013 | |
| 1014 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1015 | |
| 1016 | end. |
| 1017 | |
| 1018 | |
| 1019 | |