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