15
15
using Microsoft . Build . Shared ;
16
16
#if FEATURE_SECURITY_PERMISSIONS || FEATURE_PIPE_SECURITY
17
17
using System . Security . AccessControl ;
18
-
19
18
#endif
20
19
#if FEATURE_PIPE_SECURITY && FEATURE_NAMED_PIPE_SECURITY_CONSTRUCTOR
21
20
using System . Security . Principal ;
22
21
#endif
23
- #if ! FEATURE_APM
22
+ #if NET451_OR_GREATER || NETCOREAPP
24
23
using System . Threading . Tasks ;
25
24
#endif
26
25
@@ -511,7 +510,7 @@ private void PacketPumpProc()
511
510
}
512
511
}
513
512
514
- private void RunReadLoop ( Stream localReadPipe , Stream localWritePipe ,
513
+ private void RunReadLoop ( BufferedReadStream localReadPipe , NamedPipeServerStream localWritePipe ,
515
514
ConcurrentQueue < INodePacket > localPacketQueue , AutoResetEvent localPacketAvailable , AutoResetEvent localTerminatePacketPump )
516
515
{
517
516
// Ordering of the wait handles is important. The first signalled wait handle in the array
@@ -520,25 +519,30 @@ private void RunReadLoop(Stream localReadPipe, Stream localWritePipe,
520
519
// spammed to the endpoint and it never gets an opportunity to shutdown.
521
520
CommunicationsUtilities . Trace ( "Entering read loop." ) ;
522
521
byte [ ] headerByte = new byte [ 5 ] ;
523
- #if FEATURE_APM
524
- IAsyncResult result = localReadPipe . BeginRead ( headerByte , 0 , headerByte . Length , null , null ) ;
525
- #else
522
+ #if NET451_OR_GREATER
523
+ Task < int > readTask = localReadPipe . ReadAsync ( headerByte , 0 , headerByte . Length , CancellationToken . None ) ;
524
+ #elif NETCOREAPP
526
525
Task < int > readTask = CommunicationsUtilities . ReadAsync ( localReadPipe , headerByte , headerByte . Length ) ;
526
+ #else
527
+ IAsyncResult result = localReadPipe . BeginRead ( headerByte , 0 , headerByte . Length , null , null ) ;
527
528
#endif
528
529
529
- bool exitLoop = false ;
530
- do
530
+ // Ordering is important. We want packetAvailable to supercede terminate otherwise we will not properly wait for all
531
+ // packets to be sent by other threads which are shutting down, such as the logging thread.
532
+ WaitHandle [ ] handles = new WaitHandle [ ]
531
533
{
532
- // Ordering is important. We want packetAvailable to supercede terminate otherwise we will not properly wait for all
533
- // packets to be sent by other threads which are shutting down, such as the logging thread.
534
- WaitHandle [ ] handles = new WaitHandle [ ] {
535
- #if FEATURE_APM
536
- result . AsyncWaitHandle ,
534
+ #if NET451_OR_GREATER || NETCOREAPP
535
+ ( ( IAsyncResult ) readTask ) . AsyncWaitHandle ,
537
536
#else
538
- ( ( IAsyncResult ) readTask ) . AsyncWaitHandle ,
537
+ result . AsyncWaitHandle ,
539
538
#endif
540
- localPacketAvailable, localTerminatePacketPump } ;
539
+ localPacketAvailable,
540
+ localTerminatePacketPump ,
541
+ } ;
541
542
543
+ bool exitLoop = false ;
544
+ do
545
+ {
542
546
int waitId = WaitHandle . WaitAny ( handles ) ;
543
547
switch ( waitId )
544
548
{
@@ -547,10 +551,10 @@ private void RunReadLoop(Stream localReadPipe, Stream localWritePipe,
547
551
int bytesRead = 0 ;
548
552
try
549
553
{
550
- #if FEATURE_APM
551
- bytesRead = localReadPipe . EndRead ( result ) ;
552
- #else
554
+ #if NET451_OR_GREATER || NETCOREAPP
553
555
bytesRead = readTask . Result ;
556
+ #else
557
+ bytesRead = localReadPipe . EndRead ( result ) ;
554
558
#endif
555
559
}
556
560
catch ( Exception e )
@@ -591,7 +595,7 @@ private void RunReadLoop(Stream localReadPipe, Stream localWritePipe,
591
595
break ;
592
596
}
593
597
594
- NodePacketType packetType = ( NodePacketType ) Enum . ToObject ( typeof ( NodePacketType ) , headerByte [ 0 ] ) ;
598
+ NodePacketType packetType = ( NodePacketType ) headerByte [ 0 ] ;
595
599
596
600
try
597
601
{
@@ -607,10 +611,18 @@ private void RunReadLoop(Stream localReadPipe, Stream localWritePipe,
607
611
break ;
608
612
}
609
613
610
- #if FEATURE_APM
614
+ #if NET451_OR_GREATER
615
+ readTask = localReadPipe . ReadAsync ( headerByte , 0 , headerByte . Length , CancellationToken . None ) ;
616
+ #elif NETCOREAPP
617
+ readTask = CommunicationsUtilities . ReadAsync ( localReadPipe , headerByte , headerByte . Length ) ;
618
+ #else
611
619
result = localReadPipe . BeginRead ( headerByte , 0 , headerByte . Length , null , null ) ;
620
+ #endif
621
+
622
+ #if NET451_OR_GREATER || NETCOREAPP
623
+ handles [ 0 ] = ( ( IAsyncResult ) readTask ) . AsyncWaitHandle ;
612
624
#else
613
- readTask = CommunicationsUtilities . ReadAsync ( localReadPipe , headerByte , headerByte . Length ) ;
625
+ handles [ 0 ] = result . AsyncWaitHandle ;
614
626
#endif
615
627
}
616
628
@@ -673,8 +685,8 @@ private void RunReadLoop(Stream localReadPipe, Stream localWritePipe,
673
685
while ( ! exitLoop ) ;
674
686
}
675
687
676
- #endregion
688
+ #endregion
677
689
678
- #endregion
690
+ #endregion
679
691
}
680
692
}
0 commit comments