@@ -143,7 +143,7 @@ public SNITCPHandle(string serverName, int port, long timerExpire, object callba
143
143
bool reportError = true ;
144
144
145
145
// We will always first try to connect with serverName as before and let the DNS server to resolve the serverName.
146
- // If the DSN resolution fails, we will try with IPs in the DNS cache if existed. We try with IPv4 first and followed by IPv6 if
146
+ // If the DSN resolution fails, we will try with IPs in the DNS cache if existed. We try with IPv4 first and followed by IPv6 if
147
147
// IPv4 fails. The exceptions will be throw to upper level and be handled as before.
148
148
try
149
149
{
@@ -160,14 +160,14 @@ public SNITCPHandle(string serverName, int port, long timerExpire, object callba
160
160
{
161
161
// Retry with cached IP address
162
162
if ( ex is SocketException || ex is ArgumentException || ex is AggregateException )
163
- {
163
+ {
164
164
if ( hasCachedDNSInfo == false )
165
165
{
166
166
throw ;
167
167
}
168
168
else
169
169
{
170
- int portRetry = String . IsNullOrEmpty ( cachedDNSInfo . Port ) ? port : Int32 . Parse ( cachedDNSInfo . Port ) ;
170
+ int portRetry = String . IsNullOrEmpty ( cachedDNSInfo . Port ) ? port : Int32 . Parse ( cachedDNSInfo . Port ) ;
171
171
172
172
try
173
173
{
@@ -180,9 +180,9 @@ public SNITCPHandle(string serverName, int port, long timerExpire, object callba
180
180
_socket = Connect ( cachedDNSInfo . AddrIPv4 , portRetry , ts , isInfiniteTimeOut , cachedFQDN , ref pendingDNSInfo ) ;
181
181
}
182
182
}
183
- catch ( Exception exRetry )
183
+ catch ( Exception exRetry )
184
184
{
185
- if ( exRetry is SocketException || exRetry is ArgumentNullException
185
+ if ( exRetry is SocketException || exRetry is ArgumentNullException
186
186
|| exRetry is ArgumentException || exRetry is ArgumentOutOfRangeException || exRetry is AggregateException )
187
187
{
188
188
if ( parallel )
@@ -199,7 +199,7 @@ public SNITCPHandle(string serverName, int port, long timerExpire, object callba
199
199
throw ;
200
200
}
201
201
}
202
- }
202
+ }
203
203
}
204
204
else
205
205
{
@@ -223,10 +223,10 @@ public SNITCPHandle(string serverName, int port, long timerExpire, object callba
223
223
}
224
224
225
225
_socket . NoDelay = true ;
226
- _tcpStream = new NetworkStream ( _socket , true ) ;
226
+ _tcpStream = new SNINetworkStream ( _socket , true ) ;
227
227
228
228
_sslOverTdsStream = new SslOverTdsStream ( _tcpStream ) ;
229
- _sslStream = new SslStream ( _sslOverTdsStream , true , new RemoteCertificateValidationCallback ( ValidateServerCertificate ) , null ) ;
229
+ _sslStream = new SNISslStream ( _sslOverTdsStream , true , new RemoteCertificateValidationCallback ( ValidateServerCertificate ) ) ;
230
230
}
231
231
catch ( SocketException se )
232
232
{
@@ -331,7 +331,7 @@ private static Socket Connect(string serverName, int port, TimeSpan timeout, boo
331
331
}
332
332
333
333
CancellationTokenSource cts = null ;
334
-
334
+
335
335
void Cancel ( )
336
336
{
337
337
for ( int i = 0 ; i < sockets . Length ; ++ i )
@@ -355,7 +355,7 @@ void Cancel()
355
355
}
356
356
357
357
Socket availableSocket = null ;
358
- try
358
+ try
359
359
{
360
360
for ( int i = 0 ; i < sockets . Length ; ++ i )
361
361
{
@@ -566,45 +566,45 @@ public override uint Send(SNIPacket packet)
566
566
{
567
567
bool releaseLock = false ;
568
568
try
569
- {
570
- // is the packet is marked out out-of-band (attention packets only) it must be
571
- // sent immediately even if a send of recieve operation is already in progress
572
- // because out of band packets are used to cancel ongoing operations
573
- // so try to take the lock if possible but continue even if it can't be taken
574
- if ( packet . IsOutOfBand )
575
- {
576
- Monitor . TryEnter ( this , ref releaseLock ) ;
577
- }
578
- else
579
- {
580
- Monitor . Enter ( this ) ;
581
- releaseLock = true ;
582
- }
583
-
584
- // this lock ensures that two packets are not being written to the transport at the same time
585
- // so that sending a standard and an out-of-band packet are both written atomically no data is
586
- // interleaved
587
- lock ( _sendSync )
588
569
{
589
- try
590
- {
591
- packet . WriteToStream ( _stream ) ;
592
- return TdsEnums . SNI_SUCCESS ;
593
- }
594
- catch ( ObjectDisposedException ode )
570
+ // is the packet is marked out out-of-band (attention packets only) it must be
571
+ // sent immediately even if a send of recieve operation is already in progress
572
+ // because out of band packets are used to cancel ongoing operations
573
+ // so try to take the lock if possible but continue even if it can't be taken
574
+ if ( packet . IsOutOfBand )
595
575
{
596
- return ReportTcpSNIError ( ode ) ;
576
+ Monitor . TryEnter ( this , ref releaseLock ) ;
597
577
}
598
- catch ( SocketException se )
578
+ else
599
579
{
600
- return ReportTcpSNIError ( se ) ;
580
+ Monitor . Enter ( this ) ;
581
+ releaseLock = true ;
601
582
}
602
- catch ( IOException ioe )
583
+
584
+ // this lock ensures that two packets are not being written to the transport at the same time
585
+ // so that sending a standard and an out-of-band packet are both written atomically no data is
586
+ // interleaved
587
+ lock ( _sendSync )
603
588
{
604
- return ReportTcpSNIError ( ioe ) ;
589
+ try
590
+ {
591
+ packet . WriteToStream ( _stream ) ;
592
+ return TdsEnums . SNI_SUCCESS ;
593
+ }
594
+ catch ( ObjectDisposedException ode )
595
+ {
596
+ return ReportTcpSNIError ( ode ) ;
597
+ }
598
+ catch ( SocketException se )
599
+ {
600
+ return ReportTcpSNIError ( se ) ;
601
+ }
602
+ catch ( IOException ioe )
603
+ {
604
+ return ReportTcpSNIError ( ioe ) ;
605
+ }
605
606
}
606
607
}
607
- }
608
608
finally
609
609
{
610
610
if ( releaseLock )
@@ -633,7 +633,8 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
633
633
_socket . ReceiveTimeout = timeoutInMilliseconds ;
634
634
}
635
635
else if ( timeoutInMilliseconds == - 1 )
636
- { // SqlCient internally represents infinite timeout by -1, and for TcpClient this is translated to a timeout of 0
636
+ {
637
+ // SqlClient internally represents infinite timeout by -1, and for TcpClient this is translated to a timeout of 0
637
638
_socket . ReceiveTimeout = 0 ;
638
639
}
639
640
else
@@ -706,12 +707,17 @@ public override void SetAsyncCallbacks(SNIAsyncCallback receiveCallback, SNIAsyn
706
707
/// <returns>SNI error code</returns>
707
708
public override uint SendAsync ( SNIPacket packet , SNIAsyncCallback callback = null )
708
709
{
709
- SNIAsyncCallback cb = callback ?? _sendCallback ;
710
- lock ( this )
710
+ long scopeID = SqlClientEventSource . Log . TrySNIScopeEnterEvent ( "<sc.SNI.SNIMarsHandle.SendAsync |SNI|INFO|SCOPE>" ) ;
711
+ try
711
712
{
713
+ SNIAsyncCallback cb = callback ?? _sendCallback ;
712
714
packet . WriteToStreamAsync ( _stream , cb , SNIProviders . TCP_PROV ) ;
715
+ return TdsEnums . SNI_SUCCESS_IO_PENDING ;
716
+ }
717
+ finally
718
+ {
719
+ SqlClientEventSource . Log . TrySNIScopeLeaveEvent ( scopeID ) ;
713
720
}
714
- return TdsEnums . SNI_SUCCESS_IO_PENDING ;
715
721
}
716
722
717
723
/// <summary>
@@ -745,15 +751,15 @@ public override uint CheckConnection()
745
751
{
746
752
try
747
753
{
748
- // _socket.Poll method with argument SelectMode.SelectRead returns
754
+ // _socket.Poll method with argument SelectMode.SelectRead returns
749
755
// True : if Listen has been called and a connection is pending, or
750
756
// True : if data is available for reading, or
751
757
// True : if the connection has been closed, reset, or terminated, i.e no active connection.
752
758
// False : otherwise.
753
759
// _socket.Available property returns the number of bytes of data available to read.
754
760
//
755
- // Since _socket.Connected alone doesn't guarantee if the connection is still active, we use it in
756
- // combination with _socket.Poll method and _socket.Available == 0 check. When both of them
761
+ // Since _socket.Connected alone doesn't guarantee if the connection is still active, we use it in
762
+ // combination with _socket.Poll method and _socket.Available == 0 check. When both of them
757
763
// return true we can safely determine that the connection is no longer active.
758
764
if ( ! _socket . Connected || ( _socket . Poll ( 100 , SelectMode . SelectRead ) && _socket . Available == 0 ) )
759
765
{
0 commit comments