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