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