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