@@ -153,14 +153,25 @@ internal X509CertificateCollection ClientCertificates
153
153
154
154
internal void InitializeConnection ( string host , int port )
155
155
{
156
- _tcpClient . ConnectAsync ( host , port ) . GetAwaiter ( ) . GetResult ( ) ;
156
+ _tcpClient . Connect ( host , port ) ;
157
+ _networkStream = _tcpClient . GetStream ( ) ;
158
+ }
159
+
160
+ internal IAsyncResult BeginInitializeConnection ( string host , int port , AsyncCallback callback , object state )
161
+ {
162
+ return _tcpClient . BeginConnect ( host , port , callback , state ) ;
163
+ }
164
+
165
+ internal void EndInitializeConnection ( IAsyncResult result )
166
+ {
167
+ _tcpClient . EndConnect ( result ) ;
157
168
_networkStream = _tcpClient . GetStream ( ) ;
158
169
}
159
170
160
171
internal IAsyncResult BeginGetConnection ( ContextAwareResult outerResult , AsyncCallback callback , object state , string host , int port )
161
172
{
162
173
ConnectAndHandshakeAsyncResult result = new ConnectAndHandshakeAsyncResult ( this , host , port , outerResult , callback , state ) ;
163
- result . GetConnection ( false ) ;
174
+ result . GetConnection ( ) ;
164
175
return result ;
165
176
}
166
177
@@ -559,31 +570,61 @@ internal static void End(IAsyncResult result)
559
570
}
560
571
}
561
572
562
- internal void GetConnection ( bool synchronous )
573
+ internal void GetConnection ( )
563
574
{
564
575
if ( GlobalLog . IsEnabled )
565
576
{
566
- GlobalLog . Enter ( "ConnectAndHandshakeAsyncResult#" + LoggingHash . HashString ( this ) + "::Connect: sync=" + ( synchronous ? "true" : "false" ) ) ;
577
+ GlobalLog . Enter ( "ConnectAndHandshakeAsyncResult#" + LoggingHash . HashString ( this ) + "::Connect:" ) ;
567
578
}
568
579
if ( _connection . _isConnected )
569
580
{
570
581
throw new InvalidOperationException ( SR . SmtpAlreadyConnected ) ;
571
582
}
572
583
573
- _connection . InitializeConnection ( _host , _port ) ;
584
+ InitializeConnection ( ) ;
585
+ }
574
586
575
- if ( GlobalLog . IsEnabled )
587
+ private void InitializeConnection ( )
588
+ {
589
+ IAsyncResult result = _connection . BeginInitializeConnection ( _host , _port , InitializeConnectionCallback , this ) ;
590
+ if ( result . CompletedSynchronously )
576
591
{
577
- GlobalLog . Print ( "ConnectAndHandshakeAsyncResult#" + LoggingHash . HashString ( this ) + "::Connect returned" + LoggingHash . HashString ( this ) ) ;
578
- }
592
+ _connection . EndInitializeConnection ( result ) ;
593
+ if ( GlobalLog . IsEnabled )
594
+ {
595
+ GlobalLog . Print ( "ConnectAndHandshakeAsyncResult#" + LoggingHash . HashString ( this ) + "::Connect returned" + LoggingHash . HashString ( this ) ) ;
596
+ }
579
597
580
- try
581
- {
582
- Handshake ( ) ;
598
+ try
599
+ {
600
+ Handshake ( ) ;
601
+ }
602
+ catch ( Exception e )
603
+ {
604
+ InvokeCallback ( e ) ;
605
+ }
583
606
}
584
- catch ( Exception e )
607
+ }
608
+
609
+ private static void InitializeConnectionCallback ( IAsyncResult result )
610
+ {
611
+ if ( ! result . CompletedSynchronously )
585
612
{
586
- InvokeCallback ( e ) ;
613
+ ConnectAndHandshakeAsyncResult thisPtr = ( ConnectAndHandshakeAsyncResult ) result . AsyncState ;
614
+ thisPtr . _connection . EndInitializeConnection ( result ) ;
615
+ if ( GlobalLog . IsEnabled )
616
+ {
617
+ GlobalLog . Print ( "ConnectAndHandshakeAsyncResult#" + LoggingHash . HashString ( thisPtr ) + "::Connect returned" + LoggingHash . HashString ( thisPtr ) ) ;
618
+ }
619
+
620
+ try
621
+ {
622
+ thisPtr . Handshake ( ) ;
623
+ }
624
+ catch ( Exception e )
625
+ {
626
+ thisPtr . InvokeCallback ( e ) ;
627
+ }
587
628
}
588
629
}
589
630
0 commit comments