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