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