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 | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 242 | TNamedPipeServerTransportImpl = class( TPipeServerTransportBase, INamedPipeServerTransport) |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 243 | strict private |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 244 | FPipeName : string; |
| 245 | FMaxConns : DWORD; |
| 246 | FBufSize : DWORD; |
Jens Geyer | 0b20cc8 | 2013-03-07 20:47:01 +0100 | [diff] [blame] | 247 | FTimeout : DWORD; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 248 | FHandle : THandle; |
| 249 | FConnected : Boolean; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 250 | |
| 251 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 252 | strict protected |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 253 | function Accept(const fnAccepting: TProc): ITransport; override; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 254 | function CreateNamedPipe : THandle; |
| 255 | function CreateTransportInstance : ITransport; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 256 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 257 | // INamedPipeServerTransport |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 258 | function Handle : THandle; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 259 | procedure InternalClose; override; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 260 | |
| 261 | public |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 262 | constructor Create( const aPipename : string; |
| 263 | const aBufsize : Cardinal = 4096; |
| 264 | const aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES; |
| 265 | const aTimeOut : Cardinal = INFINITE; |
| 266 | const aConfig : IThriftConfiguration = nil |
| 267 | ); reintroduce; overload; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 268 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 269 | |
| 270 | |
| 271 | implementation |
| 272 | |
| 273 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 274 | procedure ClosePipeHandle( var hPipe : THandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 275 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 276 | if hPipe <> INVALID_HANDLE_VALUE |
| 277 | then try |
| 278 | CloseHandle( hPipe); |
| 279 | finally |
| 280 | hPipe := INVALID_HANDLE_VALUE; |
| 281 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 282 | end; |
| 283 | |
| 284 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 285 | function DuplicatePipeHandle( const hSource : THandle) : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 286 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 287 | if not DuplicateHandle( GetCurrentProcess, hSource, |
| 288 | GetCurrentProcess, @result, |
| 289 | 0, FALSE, DUPLICATE_SAME_ACCESS) |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 290 | then raise TTransportExceptionNotOpen.Create('DuplicateHandle: '+SysErrorMessage(GetLastError)); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 291 | end; |
| 292 | |
| 293 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 294 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 295 | { TPipeStreamBase } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 296 | |
| 297 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 298 | constructor TPipeStreamBase.Create( aEnableOverlapped : Boolean; const aTimeOut, aOpenTimeOut : DWORD); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 299 | begin |
| 300 | inherited Create; |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame] | 301 | FPipe := INVALID_HANDLE_VALUE; |
| 302 | FTimeout := aTimeOut; |
| 303 | FOpenTimeOut := aOpenTimeOut; |
| 304 | FOverlapped := aEnableOverlapped; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 305 | ASSERT( FTimeout > 0); // FOpenTimeout may be 0 |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 306 | end; |
| 307 | |
| 308 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 309 | destructor TPipeStreamBase.Destroy; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 310 | begin |
| 311 | try |
| 312 | Close; |
| 313 | finally |
| 314 | inherited Destroy; |
| 315 | end; |
| 316 | end; |
| 317 | |
| 318 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 319 | procedure TPipeStreamBase.Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 320 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 321 | ClosePipeHandle( FPipe); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 322 | end; |
| 323 | |
| 324 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 325 | procedure TPipeStreamBase.Flush; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 326 | begin |
Jens Geyer | 0d227b1 | 2015-12-02 19:50:55 +0100 | [diff] [blame] | 327 | FlushFileBuffers( FPipe); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 328 | end; |
| 329 | |
| 330 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 331 | function TPipeStreamBase.IsOpen: Boolean; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 332 | begin |
| 333 | result := (FPipe <> INVALID_HANDLE_VALUE); |
| 334 | end; |
| 335 | |
| 336 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 337 | procedure TPipeStreamBase.Write( const pBuf : Pointer; offset, count : Integer); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 338 | begin |
| 339 | if FOverlapped |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 340 | then WriteOverlapped( pBuf, offset, count) |
| 341 | else WriteDirect( pBuf, offset, count); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 342 | end; |
| 343 | |
| 344 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 345 | 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] | 346 | begin |
| 347 | if FOverlapped |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 348 | then result := ReadOverlapped( pBuf, buflen, offset, count) |
| 349 | else result := ReadDirect( pBuf, buflen, offset, count); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 350 | end; |
| 351 | |
| 352 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 353 | procedure TPipeStreamBase.WriteDirect( const pBuf : Pointer; offset: Integer; count: Integer); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 354 | var cbWritten, nBytes : DWORD; |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 355 | pData : PByte; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 356 | begin |
| 357 | if not IsOpen |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 358 | then raise TTransportExceptionNotOpen.Create('Called write on non-open pipe'); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 359 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 360 | // if necessary, send the data in chunks |
| 361 | // there's a system limit around 0x10000 bytes that we hit otherwise |
| 362 | // MSDN: "Pipe write operations across a network are limited to 65,535 bytes per write. For more information regarding pipes, see the Remarks section." |
| 363 | nBytes := Min( 15*4096, count); // 16 would exceed the limit |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 364 | pData := pBuf; |
| 365 | Inc( pData, offset); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 366 | while nBytes > 0 do begin |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 367 | if not WriteFile( FPipe, pData^, nBytes, cbWritten, nil) |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 368 | then raise TTransportExceptionNotOpen.Create('Write to pipe failed'); |
| 369 | |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 370 | Inc( pData, cbWritten); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 371 | Dec( count, cbWritten); |
| 372 | nBytes := Min( nBytes, count); |
| 373 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 374 | end; |
| 375 | |
| 376 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 377 | procedure TPipeStreamBase.WriteOverlapped( const pBuf : Pointer; offset: Integer; count: Integer); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 378 | var cbWritten, dwWait, dwError, nBytes : DWORD; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 379 | overlapped : IOverlappedHelper; |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 380 | pData : PByte; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 381 | begin |
| 382 | if not IsOpen |
| 383 | then raise TTransportExceptionNotOpen.Create('Called write on non-open pipe'); |
| 384 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 385 | // if necessary, send the data in chunks |
| 386 | // there's a system limit around 0x10000 bytes that we hit otherwise |
| 387 | // MSDN: "Pipe write operations across a network are limited to 65,535 bytes per write. For more information regarding pipes, see the Remarks section." |
| 388 | nBytes := Min( 15*4096, count); // 16 would exceed the limit |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 389 | pData := pBuf; |
| 390 | Inc( pData, offset); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 391 | while nBytes > 0 do begin |
| 392 | overlapped := TOverlappedHelperImpl.Create; |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 393 | if not WriteFile( FPipe, pData^, nBytes, cbWritten, overlapped.OverlappedPtr) |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 394 | then begin |
| 395 | dwError := GetLastError; |
| 396 | case dwError of |
| 397 | ERROR_IO_PENDING : begin |
| 398 | dwWait := overlapped.WaitFor(FTimeout); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 399 | |
Jens Geyer | 0064516 | 2018-02-01 23:38:10 +0100 | [diff] [blame] | 400 | if (dwWait = WAIT_TIMEOUT) then begin |
| 401 | CancelIo( FPipe); // prevents possible AV on invalid overlapped ptr |
| 402 | raise TTransportExceptionTimedOut.Create('Pipe write timed out'); |
| 403 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 404 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 405 | if (dwWait <> WAIT_OBJECT_0) |
| 406 | or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbWritten, TRUE) |
| 407 | then raise TTransportExceptionUnknown.Create('Pipe write error'); |
| 408 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 409 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 410 | else |
| 411 | raise TTransportExceptionUnknown.Create(SysErrorMessage(dwError)); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 412 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 413 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 414 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 415 | ASSERT( DWORD(nBytes) = cbWritten); |
| 416 | |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 417 | Inc( pData, cbWritten); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 418 | Dec( count, cbWritten); |
| 419 | nBytes := Min( nBytes, count); |
| 420 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 421 | end; |
| 422 | |
| 423 | |
| 424 | 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] | 425 | var cbRead, dwErr, nRemaining : DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 426 | bytes, retries : LongInt; |
| 427 | bOk : Boolean; |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 428 | pData : PByte; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 429 | const INTERVAL = 10; // ms |
| 430 | begin |
| 431 | if not IsOpen |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 432 | then raise TTransportExceptionNotOpen.Create('Called read on non-open pipe'); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 433 | |
| 434 | // MSDN: Handle can be a handle to a named pipe instance, |
| 435 | // or it can be a handle to the read end of an anonymous pipe, |
| 436 | // The handle must have GENERIC_READ access to the pipe. |
| 437 | if FTimeOut <> INFINITE then begin |
| 438 | retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL)); |
| 439 | while TRUE do begin |
Jens Geyer | 5988f48 | 2016-04-19 23:01:24 +0200 | [diff] [blame] | 440 | if not PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) then begin |
| 441 | dwErr := GetLastError; |
| 442 | if (dwErr = ERROR_INVALID_HANDLE) |
| 443 | or (dwErr = ERROR_BROKEN_PIPE) |
| 444 | or (dwErr = ERROR_PIPE_NOT_CONNECTED) |
| 445 | then begin |
| 446 | result := 0; // other side closed the pipe |
| 447 | Exit; |
| 448 | end; |
| 449 | end |
| 450 | else if bytes > 0 then begin |
| 451 | Break; // there are data |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 452 | end; |
| 453 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 454 | Dec( retries); |
| 455 | if retries > 0 |
| 456 | then Sleep( INTERVAL) |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 457 | else raise TTransportExceptionTimedOut.Create('Pipe read timed out'); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 458 | end; |
| 459 | end; |
| 460 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 461 | result := 0; |
| 462 | nRemaining := count; |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 463 | pData := pBuf; |
| 464 | Inc( pData, offset); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 465 | while nRemaining > 0 do begin |
| 466 | // read the data (or block INFINITE-ly) |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 467 | bOk := ReadFile( FPipe, pData^, nRemaining, cbRead, nil); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 468 | if (not bOk) and (GetLastError() <> ERROR_MORE_DATA) |
| 469 | then Break; // No more data, possibly because client disconnected. |
| 470 | |
| 471 | Dec( nRemaining, cbRead); |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 472 | Inc( pData, cbRead); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 473 | Inc( result, cbRead); |
| 474 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 475 | end; |
| 476 | |
| 477 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 478 | 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] | 479 | var cbRead, dwWait, dwError, nRemaining : DWORD; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 480 | bOk : Boolean; |
| 481 | overlapped : IOverlappedHelper; |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 482 | pData : PByte; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 483 | begin |
| 484 | if not IsOpen |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 485 | then raise TTransportExceptionNotOpen.Create('Called read on non-open pipe'); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 486 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 487 | result := 0; |
| 488 | nRemaining := count; |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 489 | pData := pBuf; |
| 490 | Inc( pData, offset); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 491 | while nRemaining > 0 do begin |
| 492 | overlapped := TOverlappedHelperImpl.Create; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 493 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 494 | // read the data |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 495 | bOk := ReadFile( FPipe, pData^, nRemaining, cbRead, overlapped.OverlappedPtr); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 496 | if not bOk then begin |
| 497 | dwError := GetLastError; |
| 498 | case dwError of |
| 499 | ERROR_IO_PENDING : begin |
| 500 | dwWait := overlapped.WaitFor(FTimeout); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 501 | |
Jens Geyer | 0064516 | 2018-02-01 23:38:10 +0100 | [diff] [blame] | 502 | if (dwWait = WAIT_TIMEOUT) then begin |
| 503 | CancelIo( FPipe); // prevents possible AV on invalid overlapped ptr |
| 504 | raise TTransportExceptionTimedOut.Create('Pipe read timed out'); |
| 505 | end; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 506 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 507 | if (dwWait <> WAIT_OBJECT_0) |
| 508 | or not GetOverlappedResult( FPipe, overlapped.Overlapped, cbRead, TRUE) |
| 509 | then raise TTransportExceptionUnknown.Create('Pipe read error'); |
| 510 | end; |
| 511 | |
| 512 | else |
| 513 | raise TTransportExceptionUnknown.Create(SysErrorMessage(dwError)); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 514 | end; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 515 | end; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 516 | |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 517 | ASSERT( cbRead > 0); // see TTransportImpl.ReadAll() |
| 518 | ASSERT( cbRead <= DWORD(nRemaining)); |
| 519 | Dec( nRemaining, cbRead); |
Jens Geyer | 8582715 | 2018-01-12 21:20:59 +0100 | [diff] [blame] | 520 | Inc( pData, cbRead); |
Jens Geyer | d4df917 | 2017-10-25 22:30:23 +0200 | [diff] [blame] | 521 | Inc( result, cbRead); |
| 522 | end; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 523 | end; |
| 524 | |
| 525 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 526 | function TPipeStreamBase.ToArray: TBytes; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 527 | var bytes : LongInt; |
| 528 | begin |
| 529 | SetLength( result, 0); |
| 530 | bytes := 0; |
| 531 | |
| 532 | if IsOpen |
| 533 | and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil) |
| 534 | and (bytes > 0) |
| 535 | then begin |
| 536 | SetLength( result, bytes); |
| 537 | Read( result, 0, bytes); |
| 538 | end; |
| 539 | end; |
| 540 | |
| 541 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 542 | { TNamedPipeStreamImpl } |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 543 | |
| 544 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 545 | constructor TNamedPipeStreamImpl.Create( const aPipeName : string; |
| 546 | const aEnableOverlapped : Boolean; |
| 547 | const aShareMode: DWORD; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 548 | const aSecurityAttributes: PSecurityAttributes; |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame] | 549 | const aTimeOut, aOpenTimeOut : DWORD); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 550 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 551 | inherited Create( aEnableOverlapped, aTimeOut, aOpenTimeOut); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 552 | |
| 553 | FPipeName := aPipeName; |
| 554 | FShareMode := aShareMode; |
| 555 | FSecurityAttribs := aSecurityAttributes; |
| 556 | |
| 557 | if Copy(FPipeName,1,2) <> '\\' |
| 558 | then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 559 | end; |
| 560 | |
| 561 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 562 | procedure TNamedPipeStreamImpl.Open; |
| 563 | var hPipe : THandle; |
Jens Geyer | b89b5b9 | 2016-04-19 23:09:41 +0200 | [diff] [blame] | 564 | retries, timeout, dwErr : DWORD; |
| 565 | const INTERVAL = 10; // ms |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 566 | begin |
| 567 | if IsOpen then Exit; |
| 568 | |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame] | 569 | retries := Max( 1, Round( 1.0 * FOpenTimeOut / INTERVAL)); |
| 570 | timeout := FOpenTimeOut; |
Jens Geyer | b89b5b9 | 2016-04-19 23:09:41 +0200 | [diff] [blame] | 571 | |
| 572 | // if the server hasn't gotten to the point where the pipe has been created, at least wait the timeout |
| 573 | // According to MSDN, if no instances of the specified named pipe exist, the WaitNamedPipe function |
| 574 | // returns IMMEDIATELY, regardless of the time-out value. |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame] | 575 | // Always use INTERVAL, since WaitNamedPipe(0) defaults to some other value |
Jens Geyer | b89b5b9 | 2016-04-19 23:09:41 +0200 | [diff] [blame] | 576 | while not WaitNamedPipe( PChar(FPipeName), INTERVAL) do begin |
| 577 | dwErr := GetLastError; |
| 578 | if dwErr <> ERROR_FILE_NOT_FOUND |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 579 | then raise TTransportExceptionNotOpen.Create('Unable to open pipe, '+SysErrorMessage(dwErr)); |
Jens Geyer | b89b5b9 | 2016-04-19 23:09:41 +0200 | [diff] [blame] | 580 | |
| 581 | if timeout <> INFINITE then begin |
| 582 | if (retries > 0) |
| 583 | then Dec(retries) |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 584 | else raise TTransportExceptionNotOpen.Create('Unable to open pipe, timed out'); |
Jens Geyer | b89b5b9 | 2016-04-19 23:09:41 +0200 | [diff] [blame] | 585 | end; |
| 586 | |
| 587 | Sleep(INTERVAL) |
| 588 | end; |
| 589 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 590 | // open that thingy |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 591 | hPipe := CreateFile( PChar( FPipeName), |
| 592 | GENERIC_READ or GENERIC_WRITE, |
| 593 | FShareMode, // sharing |
| 594 | FSecurityAttribs, // security attributes |
| 595 | OPEN_EXISTING, // opens existing pipe |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 596 | FILE_FLAG_OVERLAPPED or FILE_FLAG_WRITE_THROUGH, // async+fast, please |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 597 | 0); // no template file |
| 598 | |
| 599 | if hPipe = INVALID_HANDLE_VALUE |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 600 | then raise TTransportExceptionNotOpen.Create('Unable to open pipe, '+SysErrorMessage(GetLastError)); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 601 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 602 | // everything fine |
| 603 | FPipe := hPipe; |
| 604 | end; |
| 605 | |
| 606 | |
| 607 | { THandlePipeStreamImpl } |
| 608 | |
| 609 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 610 | constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle; |
| 611 | const aOwnsHandle, aEnableOverlapped : Boolean; |
| 612 | const aTimeOut : DWORD); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 613 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 614 | inherited Create( aEnableOverlapped, aTimeout, aTimeout); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 615 | |
| 616 | if aOwnsHandle |
| 617 | then FSrcHandle := aPipeHandle |
| 618 | else FSrcHandle := DuplicatePipeHandle( aPipeHandle); |
| 619 | |
| 620 | Open; |
| 621 | end; |
| 622 | |
| 623 | |
| 624 | destructor THandlePipeStreamImpl.Destroy; |
| 625 | begin |
| 626 | try |
| 627 | ClosePipeHandle( FSrcHandle); |
| 628 | finally |
| 629 | inherited Destroy; |
| 630 | end; |
| 631 | end; |
| 632 | |
| 633 | |
| 634 | procedure THandlePipeStreamImpl.Open; |
| 635 | begin |
| 636 | if not IsOpen |
| 637 | then FPipe := DuplicatePipeHandle( FSrcHandle); |
| 638 | end; |
| 639 | |
| 640 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 641 | { TPipeTransportBase } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 642 | |
| 643 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 644 | function TPipeTransportBase.GetIsOpen: Boolean; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 645 | begin |
Jens Geyer | 0b20cc8 | 2013-03-07 20:47:01 +0100 | [diff] [blame] | 646 | result := (FInputStream <> nil) and (FInputStream.IsOpen) |
| 647 | and (FOutputStream <> nil) and (FOutputStream.IsOpen); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 648 | end; |
| 649 | |
| 650 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 651 | procedure TPipeTransportBase.Open; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 652 | begin |
| 653 | FInputStream.Open; |
| 654 | FOutputStream.Open; |
| 655 | end; |
| 656 | |
| 657 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 658 | procedure TPipeTransportBase.Close; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 659 | begin |
| 660 | FInputStream.Close; |
| 661 | FOutputStream.Close; |
| 662 | end; |
| 663 | |
| 664 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 665 | { TNamedPipeTransportClientEndImpl } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 666 | |
| 667 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 668 | constructor TNamedPipeTransportClientEndImpl.Create( const aPipeName : string; |
| 669 | const aShareMode: DWORD; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 670 | const aSecurityAttributes: PSecurityAttributes; |
| 671 | const aTimeOut, aOpenTimeOut : DWORD; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 672 | const aConfig : IThriftConfiguration); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 673 | // Named pipe constructor |
| 674 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 675 | inherited Create( nil, nil, aConfig); |
Jens Geyer | 653f0de | 2016-04-20 12:46:57 +0200 | [diff] [blame] | 676 | FInputStream := TNamedPipeStreamImpl.Create( aPipeName, TRUE, aShareMode, aSecurityAttributes, aTimeOut, aOpenTimeOut); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 677 | FOutputStream := FInputStream; // true for named pipes |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 678 | end; |
| 679 | |
| 680 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 681 | constructor TNamedPipeTransportClientEndImpl.Create( const aPipe : THandle; |
| 682 | const aOwnsHandle : Boolean; |
| 683 | const aTimeOut : DWORD; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 684 | const aConfig : IThriftConfiguration); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 685 | // Named pipe constructor |
| 686 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 687 | inherited Create( nil, nil, aConfig); |
| 688 | FInputStream := THandlePipeStreamImpl.Create( aPipe, aOwnsHandle, TRUE, aTimeOut); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 689 | FOutputStream := FInputStream; // true for named pipes |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 690 | end; |
| 691 | |
| 692 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 693 | { TNamedPipeTransportServerEndImpl } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 694 | |
| 695 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 696 | constructor TNamedPipeTransportServerEndImpl.Create( const aPipe : THandle; |
| 697 | const aOwnsHandle : Boolean; |
| 698 | const aTimeOut : DWORD; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 699 | const aConfig : IThriftConfiguration); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 700 | // Named pipe constructor |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 701 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 702 | FHandle := DuplicatePipeHandle( aPipe); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 703 | inherited Create( aPipe, aOwnsHandle, aTimeout, aConfig); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 704 | end; |
| 705 | |
| 706 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 707 | procedure TNamedPipeTransportServerEndImpl.Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 708 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 709 | FlushFileBuffers( FHandle); |
| 710 | DisconnectNamedPipe( FHandle); // force client off the pipe |
| 711 | ClosePipeHandle( FHandle); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 712 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 713 | inherited Close; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 714 | end; |
| 715 | |
| 716 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 717 | { TAnonymousPipeTransportImpl } |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 718 | |
| 719 | |
Jens Geyer | dd074e7 | 2016-04-19 23:31:33 +0200 | [diff] [blame] | 720 | constructor TAnonymousPipeTransportImpl.Create( const aPipeRead, aPipeWrite : THandle; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 721 | const aOwnsHandles : Boolean; |
| 722 | const aTimeOut : DWORD; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 723 | const aConfig : IThriftConfiguration); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 724 | // Anonymous pipe constructor |
| 725 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 726 | inherited Create( nil, nil, aConfig); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 727 | // overlapped is not supported with AnonPipes, see MSDN |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 728 | FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles, FALSE, aTimeout); |
| 729 | FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles, FALSE, aTimeout); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 730 | end; |
| 731 | |
| 732 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 733 | { TPipeServerTransportBase } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 734 | |
| 735 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 736 | constructor TPipeServerTransportBase.Create( const aConfig : IThriftConfiguration); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 737 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 738 | inherited Create( aConfig); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 739 | FStopServer := TEvent.Create(nil,TRUE,FALSE,''); // manual reset |
| 740 | end; |
| 741 | |
| 742 | |
| 743 | destructor TPipeServerTransportBase.Destroy; |
| 744 | begin |
| 745 | try |
| 746 | FreeAndNil( FStopServer); |
| 747 | finally |
| 748 | inherited Destroy; |
| 749 | end; |
| 750 | end; |
| 751 | |
| 752 | |
| 753 | function TPipeServerTransportBase.QueryStopServer : Boolean; |
| 754 | begin |
| 755 | result := (FStopServer = nil) |
| 756 | or (FStopServer.WaitFor(0) <> wrTimeout); |
| 757 | end; |
| 758 | |
| 759 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 760 | procedure TPipeServerTransportBase.Listen; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 761 | begin |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 762 | FStopServer.ResetEvent; |
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 | procedure TPipeServerTransportBase.Close; |
| 767 | begin |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 768 | FStopServer.SetEvent; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 769 | InternalClose; |
| 770 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 771 | |
| 772 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 773 | { TAnonymousPipeServerTransportImpl } |
| 774 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 775 | constructor TAnonymousPipeServerTransportImpl.Create( const aBufsize : Cardinal; |
| 776 | const aTimeOut : DWORD; |
| 777 | const aConfig : IThriftConfiguration); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 778 | // Anonymous pipe CTOR |
| 779 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 780 | inherited Create(aConfig); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 781 | FBufsize := aBufSize; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 782 | FReadHandle := INVALID_HANDLE_VALUE; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 783 | FWriteHandle := INVALID_HANDLE_VALUE; |
| 784 | FClientAnonRead := INVALID_HANDLE_VALUE; |
| 785 | FClientAnonWrite := INVALID_HANDLE_VALUE; |
Jens Geyer | dd074e7 | 2016-04-19 23:31:33 +0200 | [diff] [blame] | 786 | FTimeOut := aTimeOut; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 787 | |
| 788 | // The anonymous pipe needs to be created first so that the server can |
| 789 | // pass the handles on to the client before the serve (acceptImpl) |
| 790 | // blocking call. |
| 791 | if not CreateAnonPipe |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 792 | then raise TTransportExceptionNotOpen.Create(ClassName+'.Create() failed'); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 793 | end; |
| 794 | |
| 795 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 796 | function TAnonymousPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 797 | var buf : Byte; |
| 798 | br : DWORD; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 799 | begin |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 800 | if Assigned(fnAccepting) |
| 801 | then fnAccepting(); |
| 802 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 803 | // This 0-byte read serves merely as a blocking call. |
| 804 | if not ReadFile( FReadHandle, buf, 0, br, nil) |
| 805 | and (GetLastError() <> ERROR_MORE_DATA) |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 806 | then raise TTransportExceptionNotOpen.Create('TServerPipe unable to initiate pipe communication'); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 807 | |
| 808 | // create the transport impl |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 809 | result := TAnonymousPipeTransportImpl.Create( FReadHandle, FWriteHandle, FALSE, FTimeOut, Configuration); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 810 | end; |
| 811 | |
| 812 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 813 | procedure TAnonymousPipeServerTransportImpl.InternalClose; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 814 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 815 | ClosePipeHandle( FReadHandle); |
| 816 | ClosePipeHandle( FWriteHandle); |
| 817 | ClosePipeHandle( FClientAnonRead); |
| 818 | ClosePipeHandle( FClientAnonWrite); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 819 | end; |
| 820 | |
| 821 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 822 | function TAnonymousPipeServerTransportImpl.ReadHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 823 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 824 | result := FReadHandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 825 | end; |
| 826 | |
| 827 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 828 | function TAnonymousPipeServerTransportImpl.WriteHandle : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 829 | begin |
| 830 | result := FWriteHandle; |
| 831 | end; |
| 832 | |
| 833 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 834 | function TAnonymousPipeServerTransportImpl.ClientAnonRead : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 835 | begin |
| 836 | result := FClientAnonRead; |
| 837 | end; |
| 838 | |
| 839 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 840 | function TAnonymousPipeServerTransportImpl.ClientAnonWrite : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 841 | begin |
| 842 | result := FClientAnonWrite; |
| 843 | end; |
| 844 | |
| 845 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 846 | function TAnonymousPipeServerTransportImpl.CreateAnonPipe : Boolean; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 847 | var sd : PSECURITY_DESCRIPTOR; |
| 848 | sa : SECURITY_ATTRIBUTES; //TSecurityAttributes; |
| 849 | hCAR, hPipeW, hCAW, hPipe : THandle; |
| 850 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 851 | sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH)); |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 852 | try |
| 853 | Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION)); |
| 854 | Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE)); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 855 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 856 | sa.nLength := sizeof( sa); |
| 857 | sa.lpSecurityDescriptor := sd; |
| 858 | sa.bInheritHandle := TRUE; //allow passing handle to child |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 859 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 860 | Result := CreatePipe( hCAR, hPipeW, @sa, FBufSize); //create stdin pipe |
| 861 | if not Result then begin //create stdin pipe |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 862 | raise TTransportExceptionNotOpen.Create('TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError)); |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 863 | Exit; |
| 864 | end; |
| 865 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 866 | Result := CreatePipe( hPipe, hCAW, @sa, FBufSize); //create stdout pipe |
| 867 | if not Result then begin //create stdout pipe |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 868 | CloseHandle( hCAR); |
| 869 | CloseHandle( hPipeW); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 870 | raise TTransportExceptionNotOpen.Create('TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError)); |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 871 | Exit; |
| 872 | end; |
| 873 | |
| 874 | FClientAnonRead := hCAR; |
| 875 | FClientAnonWrite := hCAW; |
| 876 | FReadHandle := hPipe; |
| 877 | FWriteHandle := hPipeW; |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 878 | finally |
| 879 | if sd <> nil then LocalFree( Cardinal(sd)); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 880 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 881 | end; |
| 882 | |
| 883 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 884 | { TNamedPipeServerTransportImpl } |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 885 | |
| 886 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 887 | constructor TNamedPipeServerTransportImpl.Create( const aPipename : string; |
| 888 | const aBufsize, aMaxConns, aTimeOut : Cardinal; |
| 889 | const aConfig : IThriftConfiguration); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 890 | // Named Pipe CTOR |
| 891 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 892 | inherited Create( aConfig); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 893 | FPipeName := aPipename; |
| 894 | FBufsize := aBufSize; |
| 895 | FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns)); |
| 896 | FHandle := INVALID_HANDLE_VALUE; |
| 897 | FTimeout := aTimeOut; |
| 898 | FConnected := FALSE; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 899 | ASSERT( FTimeout > 0); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 900 | |
| 901 | if Copy(FPipeName,1,2) <> '\\' |
| 902 | then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost |
| 903 | end; |
| 904 | |
| 905 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 906 | function TNamedPipeServerTransportImpl.Accept(const fnAccepting: TProc): ITransport; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 907 | var dwError, dwWait, dwDummy : DWORD; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 908 | overlapped : IOverlappedHelper; |
| 909 | handles : array[0..1] of THandle; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 910 | begin |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 911 | overlapped := TOverlappedHelperImpl.Create; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 912 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 913 | ASSERT( not FConnected); |
Jens Geyer | 2ad6c30 | 2015-02-26 19:38:53 +0100 | [diff] [blame] | 914 | CreateNamedPipe; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 915 | while not FConnected do begin |
Jens Geyer | 2ad6c30 | 2015-02-26 19:38:53 +0100 | [diff] [blame] | 916 | |
Jens Geyer | 0064516 | 2018-02-01 23:38:10 +0100 | [diff] [blame] | 917 | if QueryStopServer then begin |
| 918 | InternalClose; |
| 919 | Abort; |
| 920 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 921 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 922 | if Assigned(fnAccepting) |
| 923 | then fnAccepting(); |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 924 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 925 | // Wait for the client to connect; if it succeeds, the |
| 926 | // function returns a nonzero value. If the function returns |
| 927 | // zero, GetLastError should return ERROR_PIPE_CONNECTED. |
| 928 | if ConnectNamedPipe( Handle, overlapped.OverlappedPtr) then begin |
| 929 | FConnected := TRUE; |
| 930 | Break; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 931 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 932 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 933 | // ConnectNamedPipe() returns FALSE for OverlappedIO, even if connected. |
| 934 | // We have to check GetLastError() explicitly to find out |
| 935 | dwError := GetLastError; |
| 936 | case dwError of |
| 937 | ERROR_PIPE_CONNECTED : begin |
| 938 | FConnected := not QueryStopServer; // special case: pipe immediately connected |
| 939 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 940 | |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 941 | ERROR_IO_PENDING : begin |
| 942 | handles[0] := overlapped.WaitHandle; |
| 943 | handles[1] := FStopServer.Handle; |
| 944 | dwWait := WaitForMultipleObjects( 2, @handles, FALSE, FTimeout); |
| 945 | FConnected := (dwWait = WAIT_OBJECT_0) |
| 946 | and GetOverlappedResult( Handle, overlapped.Overlapped, dwDummy, TRUE) |
| 947 | and not QueryStopServer; |
| 948 | end; |
| 949 | |
| 950 | else |
| 951 | InternalClose; |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 952 | raise TTransportExceptionNotOpen.Create('Client connection failed'); |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 953 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 954 | end; |
Jens Geyer | e965136 | 2014-03-20 22:46:17 +0200 | [diff] [blame] | 955 | |
| 956 | // create the transport impl |
| 957 | result := CreateTransportInstance; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 958 | end; |
| 959 | |
| 960 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 961 | function TNamedPipeServerTransportImpl.CreateTransportInstance : ITransport; |
| 962 | // create the transport impl |
| 963 | var hPipe : THandle; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 964 | begin |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 965 | hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE))); |
| 966 | try |
| 967 | FConnected := FALSE; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 968 | result := TNamedPipeTransportServerEndImpl.Create( hPipe, TRUE, FTimeout, Configuration); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 969 | except |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 970 | ClosePipeHandle(hPipe); |
| 971 | raise; |
| 972 | end; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 973 | end; |
| 974 | |
| 975 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 976 | procedure TNamedPipeServerTransportImpl.InternalClose; |
| 977 | var hPipe : THandle; |
| 978 | begin |
| 979 | hPipe := THandle( InterlockedExchangePointer( Pointer(FHandle), Pointer(INVALID_HANDLE_VALUE))); |
| 980 | if hPipe = INVALID_HANDLE_VALUE then Exit; |
| 981 | |
| 982 | try |
| 983 | if FConnected |
| 984 | then FlushFileBuffers( hPipe) |
| 985 | else CancelIo( hPipe); |
| 986 | DisconnectNamedPipe( hPipe); |
| 987 | finally |
| 988 | ClosePipeHandle( hPipe); |
| 989 | FConnected := FALSE; |
| 990 | end; |
| 991 | end; |
| 992 | |
| 993 | |
| 994 | function TNamedPipeServerTransportImpl.Handle : THandle; |
| 995 | begin |
| 996 | {$IFDEF WIN64} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 997 | result := THandle( InterlockedExchangeAdd64( Int64(FHandle), 0)); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 998 | {$ELSE} |
| 999 | result := THandle( InterlockedExchangeAdd( Integer(FHandle), 0)); |
| 1000 | {$ENDIF} |
| 1001 | end; |
| 1002 | |
| 1003 | |
| 1004 | function TNamedPipeServerTransportImpl.CreateNamedPipe : THandle; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1005 | var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ; |
| 1006 | everyone_sid : PSID; |
| 1007 | ea : EXPLICIT_ACCESS; |
| 1008 | acl : PACL; |
| 1009 | sd : PSECURITY_DESCRIPTOR; |
| 1010 | sa : SECURITY_ATTRIBUTES; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1011 | const |
| 1012 | SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1)); |
| 1013 | SECURITY_WORLD_RID = $00000000; |
| 1014 | begin |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 1015 | sd := nil; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1016 | everyone_sid := nil; |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 1017 | try |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 1018 | ASSERT( (FHandle = INVALID_HANDLE_VALUE) and not FConnected); |
| 1019 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 1020 | // Windows - set security to allow non-elevated apps |
| 1021 | // to access pipes created by elevated apps. |
| 1022 | SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY; |
| 1023 | 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] | 1024 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 1025 | ZeroMemory( @ea, SizeOf(ea)); |
| 1026 | ea.grfAccessPermissions := GENERIC_ALL; //SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL; |
| 1027 | ea.grfAccessMode := SET_ACCESS; |
| 1028 | ea.grfInheritance := NO_INHERITANCE; |
| 1029 | ea.Trustee.TrusteeForm := TRUSTEE_IS_SID; |
| 1030 | ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP; |
| 1031 | ea.Trustee.ptstrName := PChar(everyone_sid); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1032 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 1033 | acl := nil; |
| 1034 | SetEntriesInAcl( 1, @ea, nil, acl); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1035 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 1036 | sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH)); |
| 1037 | Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION)); |
| 1038 | Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE)); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1039 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 1040 | sa.nLength := SizeOf(sa); |
| 1041 | sa.lpSecurityDescriptor := sd; |
| 1042 | sa.bInheritHandle := FALSE; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1043 | |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 1044 | // Create an instance of the named pipe |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 1045 | {$IFDEF OLD_UNIT_NAMES} |
| 1046 | result := Windows.CreateNamedPipe( |
| 1047 | {$ELSE} |
| 1048 | result := Winapi.Windows.CreateNamedPipe( |
| 1049 | {$ENDIF} |
| 1050 | PChar( FPipeName), // pipe name |
| 1051 | PIPE_ACCESS_DUPLEX or // read/write access |
| 1052 | FILE_FLAG_OVERLAPPED, // async mode |
| 1053 | PIPE_TYPE_BYTE or // byte type pipe |
| 1054 | PIPE_READMODE_BYTE, // byte read mode |
| 1055 | FMaxConns, // max. instances |
| 1056 | FBufSize, // output buffer size |
| 1057 | FBufSize, // input buffer size |
| 1058 | FTimeout, // time-out, see MSDN |
| 1059 | @sa // default security attribute |
| 1060 | ); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1061 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 1062 | if( result <> INVALID_HANDLE_VALUE) |
| 1063 | then InterlockedExchangePointer( Pointer(FHandle), Pointer(result)) |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1064 | else raise TTransportExceptionNotOpen.Create('CreateNamedPipe() failed ' + IntToStr(GetLastError)); |
Jens Geyer | b64a774 | 2013-01-23 20:58:47 +0100 | [diff] [blame] | 1065 | |
| 1066 | finally |
| 1067 | if sd <> nil then LocalFree( Cardinal( sd)); |
| 1068 | if acl <> nil then LocalFree( Cardinal( acl)); |
| 1069 | if everyone_sid <> nil then FreeSid(everyone_sid); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1070 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1071 | end; |
| 1072 | |
| 1073 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 1074 | |
| 1075 | end. |
| 1076 | |
| 1077 | |
| 1078 | |