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