Skip to content

Commit b1f7eaf

Browse files
authored
Merge | SNI Wrapper Class Extraction (#3002)
* Extract ConsumerInfo and SqlAsyncCallbackDelegate and migrate usages * Extracting AuthProviderInfo, migrating usages * Extracting ConsumerNumber, migrating usages * Extracting IoType, Prefix, Provider, and QueryType, migrating usages * Extract secur32 interop for netfx usage only * Extracting the rest of the enums/structs, migrating usages * Extract SniErrors (and merge it at the same time), delete netcore's SNINativeMethodWrapper.Common.cs * Extract SqlDependencyProcessDispatcherStorage
1 parent 5857829 commit b1f7eaf

35 files changed

+754
-663
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Common.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs

Lines changed: 28 additions & 214 deletions
Large diffs are not rendered by default.

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
</ItemGroup>
3939
<!-- Contains common items shared between Unix and Windows -->
4040
<ItemGroup Condition="'$(OSGroup)' != 'AnyOS'">
41+
<Compile Include="$(CommonSourceRoot)\Interop\Common\Sni\SniErrors.cs">
42+
<Link>Interop\Common\Sni\SniErrors.cs</Link>
43+
</Compile>
4144
<Compile Include="$(CommonSourceRoot)Microsoft\Data\Common\ActivityCorrelator.cs">
4245
<Link>Microsoft\Data\Common\ActivityCorrelator.cs</Link>
4346
</Compile>
@@ -620,7 +623,6 @@
620623

621624
<Compile Include="Common\System\NotImplemented.cs" />
622625
<Compile Include="Common\System\Threading\Tasks\TaskToApm.cs" />
623-
<Compile Include="Interop\SNINativeMethodWrapper.Common.cs" />
624626
<Compile Include="Microsoft\Data\Common\DbConnectionOptions.cs" />
625627
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.cs" />
626628
<Compile Include="Microsoft\Data\SqlClient\Reliability\SqlConfigurableRetryLogicManager.NetCoreApp.cs" />
@@ -714,6 +716,45 @@
714716
<Compile Include="$(CommonSourceRoot)\Interop\Windows\NtDll\SecurityQualityOfService.cs">
715717
<Link>Interop\Windows\NtDll\Interop\Windows\NtDll\SecurityQualityOfService.cs</Link>
716718
</Compile>
719+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\AuthProviderInfo.cs">
720+
<Link>Interop\Windows\Sni\AuthProviderInfo.cs</Link>
721+
</Compile>
722+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\ConsumerInfo.cs">
723+
<Link>Interop\Windows\Sni\ConsumerInfo.cs</Link>
724+
</Compile>
725+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\ConsumerNumber.cs">
726+
<Link>Interop\Windows\Sni\ConsumerNumber.cs</Link>
727+
</Compile>
728+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\Delegates.cs">
729+
<Link>Interop\Windows\Sni\Delegates.cs</Link>
730+
</Compile>
731+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\IoType.cs">
732+
<Link>Interop\Windows\Sni\IoType.cs</Link>
733+
</Compile>
734+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\Prefix.cs">
735+
<Link>Interop\Windows\Sni\Prefix.cs</Link>
736+
</Compile>
737+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\Provider.cs">
738+
<Link>Interop\Windows\Sni\Provider.cs</Link>
739+
</Compile>
740+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\QueryType.cs">
741+
<Link>Interop\Windows\Sni\QueryType.cs</Link>
742+
</Compile>
743+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniClientConsumerInfo.cs">
744+
<Link>Interop\Windows\Sni\SniClientConsumerInfo.cs</Link>
745+
</Compile>
746+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniConsumerInfo.cs">
747+
<Link>Interop\Windows\Sni\SniConsumerInfo.cs</Link>
748+
</Compile>
749+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniDnsCacheInfo.cs">
750+
<Link>Interop\Windows\Sni\SniDnsCacheInfo.cs</Link>
751+
</Compile>
752+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniError.cs">
753+
<Link>Interop\Windows\Sni\SniError.cs</Link>
754+
</Compile>
755+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\TransparentNetworkResolutionMode.cs">
756+
<Link>Interop\Windows\Sni\TransparentNetworkResolutionMode.cs</Link>
757+
</Compile>
717758
<Compile Include="$(CommonSourceRoot)\Interop\Windows\UnicodeString.cs">
718759
<Link>Interop\Windows\UnicodeString.cs</Link>
719760
</Compile>

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using Microsoft.Data.SqlClient;
7+
using Interop.Windows.Sni;
78

89
namespace Microsoft.Data
910
{
@@ -21,14 +22,14 @@ private static IntPtr UserInstanceDLLHandle
2122
{
2223
if (s_userInstanceDLLHandle == IntPtr.Zero)
2324
{
24-
SNINativeMethodWrapper.SNIQueryInfo(SNINativeMethodWrapper.QTypes.SNI_QUERY_LOCALDB_HMODULE, ref s_userInstanceDLLHandle);
25+
SNINativeMethodWrapper.SNIQueryInfo(QueryType.SNI_QUERY_LOCALDB_HMODULE, ref s_userInstanceDLLHandle);
2526
if (s_userInstanceDLLHandle != IntPtr.Zero)
2627
{
2728
SqlClientEventSource.Log.TryTraceEvent("LocalDBAPI.UserInstanceDLLHandle | LocalDB - handle obtained");
2829
}
2930
else
3031
{
31-
SNINativeMethodWrapper.SNIGetLastError(out SNINativeMethodWrapper.SNI_Error sniError);
32+
SNINativeMethodWrapper.SNIGetLastError(out SniError sniError);
3233
throw CreateLocalDBException(errorMessage: StringsHelper.GetString("LocalDB_FailedGetDLLHandle"), sniError: (int)sniError.sniError);
3334
}
3435
}

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.Windows.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Diagnostics;
7+
using Interop.Windows.Sni;
78
using Microsoft.Data.SqlClient.SNI;
89

910
namespace Microsoft.Data.SqlClient
@@ -74,7 +75,7 @@ private SNIErrorDetails GetSniErrorDetails()
7475
}
7576
else
7677
{
77-
SNINativeMethodWrapper.SNIGetLastError(out SNINativeMethodWrapper.SNI_Error sniError);
78+
SNINativeMethodWrapper.SNIGetLastError(out SniError sniError);
7879
details.sniErrorNumber = sniError.sniError;
7980
details.errorMessage = sniError.errorMessage;
8081
details.nativeError = sniError.nativeError;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Threading;
1717
using System.Threading.Tasks;
1818
using System.Xml;
19+
using Interop.Common.Sni;
1920
using Microsoft.Data.Common;
2021
using Microsoft.Data.ProviderBase;
2122
using Microsoft.Data.Sql;
@@ -1490,17 +1491,17 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj)
14901491
// handle special SNI error codes that are converted into exception which is not a SqlException.
14911492
switch (details.sniErrorNumber)
14921493
{
1493-
case (int)SNINativeMethodWrapper.SniSpecialErrors.MultiSubnetFailoverWithMoreThan64IPs:
1494+
case SniErrors.MultiSubnetFailoverWithMoreThan64IPs:
14941495
// Connecting with the MultiSubnetFailover connection option to a SQL Server instance configured with more than 64 IP addresses is not supported.
14951496
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.TdsParser.ProcessSNIError|ERR|ADV> Connecting with the MultiSubnetFailover connection option to a SQL Server instance configured with more than 64 IP addresses is not supported.");
14961497
throw SQL.MultiSubnetFailoverWithMoreThan64IPs();
14971498

1498-
case (int)SNINativeMethodWrapper.SniSpecialErrors.MultiSubnetFailoverWithInstanceSpecified:
1499+
case SniErrors.MultiSubnetFailoverWithInstanceSpecified:
14991500
// Connecting to a named SQL Server instance using the MultiSubnetFailover connection option is not supported.
15001501
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.TdsParser.ProcessSNIError|ERR|ADV> Connecting to a named SQL Server instance using the MultiSubnetFailover connection option is not supported.");
15011502
throw SQL.MultiSubnetFailoverWithInstanceSpecified();
15021503

1503-
case (int)SNINativeMethodWrapper.SniSpecialErrors.MultiSubnetFailoverWithNonTcpProtocol:
1504+
case SniErrors.MultiSubnetFailoverWithNonTcpProtocol:
15041505
// Connecting to a SQL Server instance using the MultiSubnetFailover connection option is only supported when using the TCP protocol.
15051506
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.TdsParser.ProcessSNIError|ERR|ADV> Connecting to a SQL Server instance using the MultiSubnetFailover connection option is only supported when using the TCP protocol.");
15061507
throw SQL.MultiSubnetFailoverWithNonTcpProtocol();
@@ -1587,7 +1588,7 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj)
15871588
errorMessage = SQL.GetSNIErrorMessage((int)details.sniErrorNumber);
15881589

15891590
// If its a LocalDB error, then nativeError actually contains a LocalDB-specific error code, not a win32 error code
1590-
if (details.sniErrorNumber == (int)SNINativeMethodWrapper.SniSpecialErrors.LocalDBErrorCode)
1591+
if (details.sniErrorNumber == SniErrors.LocalDBErrorCode)
15911592
{
15921593
errorMessage += LocalDBAPI.GetLocalDBMessage((int)details.nativeError);
15931594
win32ErrorCode = 0;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.Net;
89
using System.Runtime.InteropServices;
910
using System.Security.Authentication;
11+
using System.Text;
1012
using System.Threading.Tasks;
13+
using Interop.Windows.Sni;
1114
using Microsoft.Data.Common;
12-
using System.Net;
13-
using System.Text;
1415
using Microsoft.Data.ProviderBase;
1516

1617
namespace Microsoft.Data.SqlClient
@@ -63,7 +64,7 @@ protected override void CreateSessionHandle(TdsParserStateObject physicalConnect
6364
{
6465
Debug.Assert(physicalConnection is TdsParserStateObjectNative, "Expected a stateObject of type " + this.GetType());
6566
TdsParserStateObjectNative nativeSNIObject = physicalConnection as TdsParserStateObjectNative;
66-
SNINativeMethodWrapper.ConsumerInfo myInfo = CreateConsumerInfo(async);
67+
ConsumerInfo myInfo = CreateConsumerInfo(async);
6768

6869
SQLDNSInfo cachedDNSInfo;
6970
bool ret = SQLFallbackDNSCache.Instance.GetDNSInfo(_parser.FQDNforDNSCache, out cachedDNSInfo);
@@ -78,14 +79,14 @@ internal override void AssignPendingDNSInfo(string userProtocol, string DNSCache
7879
string IPStringFromSNI = string.Empty;
7980
IPAddress IPFromSNI;
8081
_parser.isTcpProtocol = false;
81-
SNINativeMethodWrapper.ProviderEnum providerNumber = SNINativeMethodWrapper.ProviderEnum.INVALID_PROV;
82+
Provider providerNumber = Provider.INVALID_PROV;
8283

8384
if (string.IsNullOrEmpty(userProtocol))
8485
{
8586

8687
result = SNINativeMethodWrapper.SniGetProviderNumber(Handle, ref providerNumber);
8788
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetProviderNumber");
88-
_parser.isTcpProtocol = (providerNumber == SNINativeMethodWrapper.ProviderEnum.TCP_PROV);
89+
_parser.isTcpProtocol = (providerNumber == Provider.TCP_PROV);
8990
}
9091
else if (userProtocol == TdsEnums.TCP)
9192
{
@@ -121,9 +122,9 @@ internal override void AssignPendingDNSInfo(string userProtocol, string DNSCache
121122
}
122123
}
123124

124-
private SNINativeMethodWrapper.ConsumerInfo CreateConsumerInfo(bool async)
125+
private ConsumerInfo CreateConsumerInfo(bool async)
125126
{
126-
SNINativeMethodWrapper.ConsumerInfo myInfo = new SNINativeMethodWrapper.ConsumerInfo();
127+
ConsumerInfo myInfo = new ConsumerInfo();
127128

128129
Debug.Assert(_outBuff.Length == _inBuff.Length, "Unexpected unequal buffers.");
129130

@@ -175,7 +176,7 @@ internal override void CreatePhysicalSNIHandle(
175176
}
176177
}
177178

178-
SNINativeMethodWrapper.ConsumerInfo myInfo = CreateConsumerInfo(async);
179+
ConsumerInfo myInfo = CreateConsumerInfo(async);
179180
SQLDNSInfo cachedDNSInfo;
180181
bool ret = SQLFallbackDNSCache.Instance.GetDNSInfo(cachedFQDN, out cachedDNSInfo);
181182

@@ -345,7 +346,7 @@ internal override PacketHandle GetResetWritePacket(int dataSize)
345346
{
346347
if (_sniPacket != null)
347348
{
348-
SNINativeMethodWrapper.SNIPacketReset(Handle, SNINativeMethodWrapper.IOType.WRITE, _sniPacket, SNINativeMethodWrapper.ConsumerNumber.SNI_Consumer_SNI);
349+
SNINativeMethodWrapper.SNIPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI);
349350
}
350351
else
351352
{
@@ -381,24 +382,24 @@ internal override uint SniGetConnectionId(ref Guid clientConnectionId)
381382
=> SNINativeMethodWrapper.SniGetConnectionId(Handle, ref clientConnectionId);
382383

383384
internal override uint DisableSsl()
384-
=> SNINativeMethodWrapper.SNIRemoveProvider(Handle, SNINativeMethodWrapper.ProviderEnum.SSL_PROV);
385+
=> SNINativeMethodWrapper.SNIRemoveProvider(Handle, Provider.SSL_PROV);
385386

386387
internal override uint EnableMars(ref uint info)
387-
=> SNINativeMethodWrapper.SNIAddProvider(Handle, SNINativeMethodWrapper.ProviderEnum.SMUX_PROV, ref info);
388+
=> SNINativeMethodWrapper.SNIAddProvider(Handle, Provider.SMUX_PROV, ref info);
388389

389390
internal override uint EnableSsl(ref uint info, bool tlsFirst, string serverCertificateFilename)
390391
{
391-
SNINativeMethodWrapper.AuthProviderInfo authInfo = new SNINativeMethodWrapper.AuthProviderInfo();
392+
AuthProviderInfo authInfo = new AuthProviderInfo();
392393
authInfo.flags = info;
393394
authInfo.tlsFirst = tlsFirst;
394395
authInfo.serverCertFileName = serverCertificateFilename;
395396

396397
// Add SSL (Encryption) SNI provider.
397-
return SNINativeMethodWrapper.SNIAddProvider(Handle, SNINativeMethodWrapper.ProviderEnum.SSL_PROV, ref authInfo);
398+
return SNINativeMethodWrapper.SNIAddProvider(Handle, Provider.SSL_PROV, ref authInfo);
398399
}
399400

400401
internal override uint SetConnectionBufferSize(ref uint unsignedPacketSize)
401-
=> SNINativeMethodWrapper.SNISetInfo(Handle, SNINativeMethodWrapper.QTypes.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
402+
=> SNINativeMethodWrapper.SNISetInfo(Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
402403

403404
internal override uint WaitForSSLHandShakeToComplete(out int protocolVersion)
404405
{
@@ -471,7 +472,7 @@ public SNIPacket Take(SNIHandle sniHandle)
471472
{
472473
// Success - reset the packet
473474
packet = _packets.Pop();
474-
SNINativeMethodWrapper.SNIPacketReset(sniHandle, SNINativeMethodWrapper.IOType.WRITE, packet, SNINativeMethodWrapper.ConsumerNumber.SNI_Consumer_SNI);
475+
SNINativeMethodWrapper.SNIPacketReset(sniHandle, IoType.WRITE, packet, ConsumerNumber.SNI_Consumer_SNI);
475476
}
476477
else
477478
{

0 commit comments

Comments
 (0)