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