@@ -159,10 +159,6 @@ public String GetImpersonationUserName()
159
159
// ---- PAL layer ends here ----
160
160
// -----------------------------
161
161
162
- [ SecurityCritical ]
163
- private unsafe static readonly IOCompletionCallback s_WaitForConnectionCallback =
164
- new IOCompletionCallback ( NamedPipeServerStream . AsyncWaitForConnectionCallback ) ;
165
-
166
162
#if RunAs
167
163
// This method calls a delegate while impersonating the client. Note that we will not have
168
164
// access to the client's security token until it has written at least once to the pipe
@@ -263,7 +259,7 @@ private unsafe IAsyncResult BeginWaitForConnection(AsyncCallback callback, Objec
263
259
// Create and store async stream class library specific data in the
264
260
// async result
265
261
PipeAsyncResult asyncResult = new PipeAsyncResult ( ) ;
266
- asyncResult . _handle = InternalHandle ;
262
+ asyncResult . _threadPoolBinding = _threadPoolBinding ;
267
263
asyncResult . _userCallback = callback ;
268
264
asyncResult . _userStateObject = state ;
269
265
@@ -273,16 +269,17 @@ private unsafe IAsyncResult BeginWaitForConnection(AsyncCallback callback, Objec
273
269
ManualResetEvent waitHandle = new ManualResetEvent ( false ) ;
274
270
asyncResult . _waitHandle = waitHandle ;
275
271
276
- // Create a managed overlapped class
277
- // We will set the file offsets later
278
- Overlapped overlapped = new Overlapped ( ) ;
279
- overlapped . OffsetLow = 0 ;
280
- overlapped . OffsetHigh = 0 ;
281
- overlapped . AsyncResult = asyncResult ;
282
-
283
- // Pack the Overlapped class, and store it in the async result
284
- NativeOverlapped * intOverlapped = overlapped . Pack ( s_WaitForConnectionCallback , null ) ;
272
+ NativeOverlapped * intOverlapped = _threadPoolBinding . AllocateNativeOverlapped ( ( errorCode , numBytes , pOverlapped ) =>
273
+ {
274
+ // Unpack overlapped, free the pinned overlapped, and complete the operation
275
+ PipeAsyncResult ar = ( PipeAsyncResult ) ThreadPoolBoundHandle . GetNativeOverlappedState ( pOverlapped ) ;
276
+ Debug . Assert ( ar . _overlapped == pOverlapped ) ;
277
+ ar . _threadPoolBinding . FreeNativeOverlapped ( pOverlapped ) ;
278
+ ar . _overlapped = null ;
279
+ AsyncWaitForConnectionCallback ( errorCode , numBytes , ar ) ;
280
+ } , asyncResult , null ) ;
285
281
asyncResult . _overlapped = intOverlapped ;
282
+
286
283
if ( ! Interop . mincore . ConnectNamedPipe ( InternalHandle , intOverlapped ) )
287
284
{
288
285
int errorCode = Marshal . GetLastWin32Error ( ) ;
@@ -298,7 +295,7 @@ private unsafe IAsyncResult BeginWaitForConnection(AsyncCallback callback, Objec
298
295
299
296
// WaitForConnectionCallback will not be called because we completed synchronously.
300
297
// Either the pipe is already connected, or there was an error. Unpin and free the overlapped again.
301
- Overlapped . Free ( intOverlapped ) ;
298
+ _threadPoolBinding . FreeNativeOverlapped ( intOverlapped ) ;
302
299
asyncResult . _overlapped = null ;
303
300
304
301
// Did the client already connect to us?
@@ -398,19 +395,8 @@ private unsafe void EndWaitForConnection(IAsyncResult asyncResult)
398
395
399
396
// Callback to be called by the OS when completing the async WaitForConnection operation.
400
397
[ SecurityCritical ]
401
- unsafe private static void AsyncWaitForConnectionCallback ( uint errorCode , uint numBytes , NativeOverlapped * pOverlapped )
398
+ unsafe private static void AsyncWaitForConnectionCallback ( uint errorCode , uint numBytes , PipeAsyncResult asyncResult )
402
399
{
403
- // Unpack overlapped
404
- Overlapped overlapped = Overlapped . Unpack ( pOverlapped ) ;
405
-
406
- // Extract async result from overlapped
407
- PipeAsyncResult asyncResult = ( PipeAsyncResult ) overlapped . AsyncResult ;
408
-
409
- // Free the pinned overlapped:
410
- Debug . Assert ( asyncResult . _overlapped == pOverlapped ) ;
411
- Overlapped . Free ( pOverlapped ) ;
412
- asyncResult . _overlapped = null ;
413
-
414
400
// Special case for when the client has already connected to us.
415
401
if ( errorCode == Interop . mincore . Errors . ERROR_PIPE_CONNECTED )
416
402
{
0 commit comments