Skip to content

Commit e796d85

Browse files
edwardnealbenrr101
andauthored
Merge | TdsParserStateObjectFactory, TdsParserStateObject (#3291)
* Merge TdsParserStateObjectFactory * Port one-line methods to netfx TdsParserStateObjectNative --------- Co-authored-by: Ben Russell <russellben@microsoft.com>
1 parent 43128c0 commit e796d85

File tree

10 files changed

+103
-45
lines changed

10 files changed

+103
-45
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,14 +898,16 @@
898898
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs">
899899
<Link>Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs</Link>
900900
</Compile>
901+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs">
902+
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs</Link>
903+
</Compile>
901904
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SqlFileStream.Windows.cs">
902905
<Link>Microsoft\Data\SqlTypes\SqlFileStream.Windows.cs</Link>
903906
</Compile>
904907

905908
<Compile Include="Microsoft\Data\SqlClient\SNI\LocalDB.Windows.cs" />
906909
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Windows.cs" />
907910
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
908-
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs" />
909911
</ItemGroup>
910912

911913
<!-- Unix only -->
@@ -934,14 +936,16 @@
934936
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlColumnEncryptionCspProvider.netcore.Unix.cs">
935937
<Link>Microsoft\Data\SqlClinet\SqlColumnEncryptionCspProvider.netcore.Unix.cs</Link>
936938
</Compile>
939+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Unix.cs">
940+
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Unix.cs</Link>
941+
</Compile>
937942
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SqlFileStream.netcore.Unix.cs">
938943
<Link>Microsoft\Data\SqlTypes\SqlFileStream.netcore.Unix.cs</Link>
939944
</Compile>
940945

941946
<Compile Include="Microsoft\Data\Sql\SqlDataSourceEnumerator.Unix.cs" />
942947
<Compile Include="Microsoft\Data\SqlClient\SNI\LocalDB.Unix.cs" />
943948
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Unix.cs" />
944-
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Managed.cs" />
945949
</ItemGroup>
946950

947951
<!-- Resources -->

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,6 +2781,7 @@ private TdsOperationStatus TryProcessEnvChange(int tokenLength, TdsParserStateOb
27812781
// Update SNI ConsumerInfo value to be resulting packet size
27822782
uint unsignedPacketSize = (uint)packetSize;
27832783
uint bufferSizeResult = _physicalStateObj.SetConnectionBufferSize(ref unsignedPacketSize);
2784+
27842785
Debug.Assert(bufferSizeResult == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SNISetInfo");
27852786
}
27862787

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ internal TdsParserStateObject(TdsParser parser, TdsParserStateObject physicalCon
7373
////////////////
7474
// Properties //
7575
////////////////
76-
internal abstract uint DisableSsl();
77-
78-
internal abstract SSPIContextProvider CreateSSPIContextProvider();
79-
80-
internal abstract uint EnableMars(ref uint info);
8176

8277
internal abstract uint Status
8378
{
@@ -201,8 +196,6 @@ internal abstract void CreatePhysicalSNIHandle(
201196

202197
internal abstract void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey, ref SQLDNSInfo pendingDNSInfo);
203198

204-
internal abstract uint SniGetConnectionId(ref Guid clientConnectionId);
205-
206199
internal abstract bool IsFailedHandle();
207200

208201
protected abstract void CreateSessionHandle(TdsParserStateObject physicalConnection, bool async);
@@ -225,8 +218,6 @@ internal abstract void CreatePhysicalSNIHandle(
225218

226219
internal abstract uint CheckConnection();
227220

228-
internal abstract uint SetConnectionBufferSize(ref uint unsignedPacketSize);
229-
230221
internal abstract void ReleasePacket(PacketHandle syncReadPacket);
231222

232223
protected abstract uint SniPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize);

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,9 @@
840840
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObject.Multiplexer.cs">
841841
<Link>Microsoft\Data\SqlClient\TdsParserStateObject.Multiplexer.cs</Link>
842842
</Compile>
843+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs">
844+
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs</Link>
845+
</Compile>
843846
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStaticMethods.cs">
844847
<Link>Microsoft\Data\SqlClient\TdsParserStaticMethods.cs</Link>
845848
</Compile>
@@ -924,6 +927,7 @@
924927
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
925928
<Compile Include="Microsoft\Data\SqlClient\TdsParser.netfx.cs" />
926929
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.netfx.cs" />
930+
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
927931
<Compile Include="Microsoft\Data\SqlTypes\SqlStreamChars.cs" />
928932
<Compile Include="Microsoft\Data\SqlTypes\SqlTypeWorkarounds.netfx.cs" />
929933
</ItemGroup>

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal sealed partial class TdsParser
5252
/// <summary>
5353
/// Verify client encryption possibility.
5454
/// </summary>
55-
private bool ClientOSEncryptionSupport => SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
55+
private bool ClientOSEncryptionSupport => TdsParserStateObjectFactory.Singleton.ClientOSEncryptionSupport;
5656

5757
// Default state object for parser
5858
internal TdsParserStateObject _physicalStateObj = null; // Default stateObj and connection for Dbnetlib and non-MARS SNI.
@@ -87,7 +87,7 @@ internal sealed partial class TdsParser
8787

8888
internal Encoding _defaultEncoding = null; // for sql character data
8989

90-
private static EncryptionOptions s_sniSupportedEncryptionOption = SNILoadHandle.SingletonInstance.Options;
90+
private static EncryptionOptions s_sniSupportedEncryptionOption = TdsParserStateObjectFactory.Singleton.EncryptionOptions;
9191

9292
private EncryptionOptions _encryptionOption = s_sniSupportedEncryptionOption;
9393

@@ -195,7 +195,7 @@ internal TdsParser(bool MARS, bool fAsynchronous)
195195
{
196196
_fMARS = MARS; // may change during Connect to pre 2005 servers
197197

198-
_physicalStateObj = new TdsParserStateObject(this);
198+
_physicalStateObj = TdsParserStateObjectFactory.Singleton.CreateTdsParserStateObject(this);
199199
DataClassificationVersion = TdsEnums.DATA_CLASSIFICATION_NOT_ENABLED;
200200
}
201201

@@ -386,7 +386,7 @@ internal void Connect(ServerInfo serverInfo,
386386
// Clean up IsSQLDNSCachingSupported flag from previous status
387387
_connHandler.IsSQLDNSCachingSupported = false;
388388

389-
uint sniStatus = SNILoadHandle.SingletonInstance.Status;
389+
uint sniStatus = TdsParserStateObjectFactory.Singleton.SNIStatus;
390390

391391
if (sniStatus != TdsEnums.SNI_SUCCESS)
392392
{
@@ -555,7 +555,7 @@ internal void Connect(ServerInfo serverInfo,
555555
_connHandler.TimeoutErrorInternal.EndPhase(SqlConnectionTimeoutErrorPhase.InitializeConnection);
556556
_connHandler.TimeoutErrorInternal.SetAndBeginPhase(SqlConnectionTimeoutErrorPhase.SendPreLoginHandshake);
557557

558-
uint result = SniNativeWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId);
558+
uint result = _physicalStateObj.SniGetConnectionId(ref _connHandler._clientConnectionId);
559559
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");
560560

561561
// for DNS Caching phase 1
@@ -621,7 +621,7 @@ internal void Connect(ServerInfo serverInfo,
621621
ThrowExceptionAndWarning(_physicalStateObj);
622622
}
623623

624-
uint retCode = SniNativeWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId);
624+
uint retCode = _physicalStateObj.SniGetConnectionId(ref _connHandler._clientConnectionId);
625625
Debug.Assert(retCode == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");
626626
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Sending prelogin handshake");
627627

@@ -664,10 +664,8 @@ internal void RemoveEncryption()
664664
{
665665
Debug.Assert((_encryptionOption & EncryptionOptions.OPTIONS_MASK) == EncryptionOptions.LOGIN, "Invalid encryption option state");
666666

667-
uint error = 0;
667+
uint error = _physicalStateObj.DisableSsl();
668668

669-
// Remove SSL (Encryption) SNI provider since we only wanted to encrypt login.
670-
error = SniNativeWrapper.SniRemoveProvider(_physicalStateObj.Handle, Provider.SSL_PROV);
671669
if (error != TdsEnums.SNI_SUCCESS)
672670
{
673671
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
@@ -690,11 +688,8 @@ internal void EnableMars()
690688
// Cache physical stateObj and connection.
691689
_pMarsPhysicalConObj = _physicalStateObj;
692690

693-
uint error = 0;
694691
uint info = 0;
695-
696-
// Add SMUX (MARS) SNI provider.
697-
error = SniNativeWrapper.SniAddProvider(_pMarsPhysicalConObj.Handle, Provider.SMUX_PROV, ref info);
692+
uint error = _pMarsPhysicalConObj.EnableMars(ref info);
698693

699694
if (error != TdsEnums.SNI_SUCCESS)
700695
{
@@ -737,7 +732,7 @@ internal void EnableMars()
737732

738733
internal TdsParserStateObject CreateSession()
739734
{
740-
TdsParserStateObject session = new TdsParserStateObject(this, (SNIHandle)_pMarsPhysicalConObj.Handle, true);
735+
TdsParserStateObject session = TdsParserStateObjectFactory.Singleton.CreateSessionObject(this, _pMarsPhysicalConObj, true);
741736
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.TdsParser.CreateSession|ADV> {0} created session {1}", ObjectID, session.ObjectID);
742737
return session;
743738
}
@@ -2836,7 +2831,7 @@ private TdsOperationStatus TryProcessEnvChange(int tokenLength, TdsParserStateOb
28362831

28372832
// Update SNI ConsumerInfo value to be resulting packet size
28382833
uint unsignedPacketSize = (uint)packetSize;
2839-
uint bufferSizeResult = SniNativeWrapper.SniSetInfo(_physicalStateObj.Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
2834+
uint bufferSizeResult = _physicalStateObj.SetConnectionBufferSize(ref unsignedPacketSize);
28402835

28412836
Debug.Assert(bufferSizeResult == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SNISetInfo");
28422837
}

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ internal readonly ref struct SessionHandle
3030

3131
internal partial class TdsParserStateObject
3232
{
33-
private static class TdsParserStateObjectFactory
34-
{
35-
/// <summary>
36-
/// Always false in case of netfx. Only needed for merging with netcore codebase.
37-
/// </summary>
38-
internal const bool UseManagedSNI = false;
39-
}
40-
4133
private SNIHandle _sessionHandle = null; // the SNI handle we're to work on
4234

4335
// SNI variables // multiple resultsets in one batch.
@@ -62,7 +54,7 @@ private static class TdsParserStateObjectFactory
6254
// Constructors //
6355
//////////////////
6456

65-
internal TdsParserStateObject(TdsParser parser, SNIHandle physicalConnection, bool async)
57+
protected TdsParserStateObject(TdsParser parser, SNIHandle physicalConnection, bool async)
6658
{
6759
// Construct a MARS session
6860
Debug.Assert(parser != null, "no parser?");
@@ -98,8 +90,6 @@ internal TdsParserStateObject(TdsParser parser, SNIHandle physicalConnection, bo
9890
_lastSuccessfulIOTimer = parser._physicalStateObj._lastSuccessfulIOTimer;
9991
}
10092

101-
internal SSPIContextProvider CreateSSPIContextProvider() => new NativeSSPIContextProvider();
102-
10393
////////////////
10494
// Properties //
10595
////////////////
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Interop.Windows.Sni;
7+
8+
namespace Microsoft.Data.SqlClient
9+
{
10+
internal class TdsParserStateObjectNative : TdsParserStateObject
11+
{
12+
internal TdsParserStateObjectNative(TdsParser parser, TdsParserStateObject physicalConnection, bool async)
13+
: base(parser, physicalConnection.Handle, async)
14+
{
15+
}
16+
17+
internal TdsParserStateObjectNative(TdsParser parser)
18+
: base(parser)
19+
{
20+
}
21+
22+
internal override uint SniGetConnectionId(ref Guid clientConnectionId)
23+
=> SniNativeWrapper.SniGetConnectionId(Handle, ref clientConnectionId);
24+
25+
internal override uint DisableSsl()
26+
=> SniNativeWrapper.SniRemoveProvider(Handle, Provider.SSL_PROV);
27+
28+
internal override uint EnableMars(ref uint info)
29+
=> SniNativeWrapper.SniAddProvider(Handle, Provider.SMUX_PROV, ref info);
30+
31+
internal override uint SetConnectionBufferSize(ref uint unsignedPacketSize)
32+
=> SniNativeWrapper.SniSetInfo(Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
33+
34+
internal override SSPIContextProvider CreateSSPIContextProvider() => new NativeSSPIContextProvider();
35+
}
36+
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal enum TdsOperationStatus : int
3232
InvalidData = 2
3333
}
3434

35-
partial class TdsParserStateObject
35+
internal abstract partial class TdsParserStateObject
3636
{
3737
private static int s_objectTypeCount; // EventSource counter
3838
internal readonly int _objectID = Interlocked.Increment(ref s_objectTypeCount);
@@ -304,7 +304,7 @@ private enum SnapshotStatus
304304
// Constructors //
305305
//////////////////
306306

307-
internal TdsParserStateObject(TdsParser parser)
307+
protected TdsParserStateObject(TdsParser parser)
308308
{
309309
// Construct a physical connection
310310
Debug.Assert(parser != null, "no parser?");
@@ -466,6 +466,16 @@ internal long TimeoutTime
466466
}
467467
}
468468

469+
internal abstract uint SniGetConnectionId(ref Guid clientConnectionId);
470+
471+
internal abstract uint DisableSsl();
472+
473+
internal abstract SSPIContextProvider CreateSSPIContextProvider();
474+
475+
internal abstract uint EnableMars(ref uint info);
476+
477+
internal abstract uint SetConnectionBufferSize(ref uint unsignedPacketSize);
478+
469479
internal int GetTimeoutRemaining()
470480
{
471481
int remaining;
Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
#if NET
67
using Microsoft.Data.SqlClient.SNI;
8+
#endif
79

810
namespace Microsoft.Data.SqlClient
911
{
@@ -13,23 +15,43 @@ internal sealed class TdsParserStateObjectFactory
1315

1416
private const string UseManagedNetworkingOnWindows = "Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows";
1517

16-
private static bool shouldUseManagedSNI;
18+
#if NET
19+
private static bool s_shouldUseManagedSNI;
1720

1821
// If the appcontext switch is set then Use Managed SNI based on the value. Otherwise Native SNI.dll will be used by default.
19-
public static bool UseManagedSNI =>
20-
AppContext.TryGetSwitch(UseManagedNetworkingOnWindows, out shouldUseManagedSNI) ? shouldUseManagedSNI : false;
22+
public static bool UseManagedSNI =>
23+
AppContext.TryGetSwitch(UseManagedNetworkingOnWindows, out s_shouldUseManagedSNI) ? s_shouldUseManagedSNI : false;
24+
#else
25+
public const bool UseManagedSNI = false;
26+
#endif
2127

22-
public EncryptionOptions EncryptionOptions => UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.Options : SNILoadHandle.SingletonInstance.Options;
28+
public EncryptionOptions EncryptionOptions =>
29+
#if NET
30+
UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.Options : SNILoadHandle.SingletonInstance.Options;
31+
#else
32+
SNILoadHandle.SingletonInstance.Options;
33+
#endif
2334

24-
public uint SNIStatus => UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.Status : SNILoadHandle.SingletonInstance.Status;
35+
public uint SNIStatus =>
36+
#if NET
37+
UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.Status : SNILoadHandle.SingletonInstance.Status;
38+
#else
39+
SNILoadHandle.SingletonInstance.Status;
40+
#endif
2541

2642
/// <summary>
2743
/// Verify client encryption possibility.
2844
/// </summary>
29-
public bool ClientOSEncryptionSupport => UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport : SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
45+
public bool ClientOSEncryptionSupport =>
46+
#if NET
47+
UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport : SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
48+
#else
49+
SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
50+
#endif
3051

3152
public TdsParserStateObject CreateTdsParserStateObject(TdsParser parser)
3253
{
54+
#if NET
3355
if (UseManagedSNI)
3456
{
3557
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectFactory.CreateTdsParserStateObject | Info | Found AppContext switch '{0}' enabled, managed networking implementation will be used."
@@ -42,15 +64,20 @@ public TdsParserStateObject CreateTdsParserStateObject(TdsParser parser)
4264
, UseManagedNetworkingOnWindows);
4365
return new TdsParserStateObjectNative(parser);
4466
}
67+
#else
68+
return new TdsParserStateObjectNative(parser);
69+
#endif
4570
}
4671

4772
internal TdsParserStateObject CreateSessionObject(TdsParser tdsParser, TdsParserStateObject _pMarsPhysicalConObj, bool v)
4873
{
74+
#if NET
4975
if (TdsParserStateObjectFactory.UseManagedSNI)
5076
{
5177
return new TdsParserStateObjectManaged(tdsParser, _pMarsPhysicalConObj, true);
5278
}
5379
else
80+
#endif
5481
{
5582
return new TdsParserStateObjectNative(tdsParser, _pMarsPhysicalConObj, true);
5683
}

0 commit comments

Comments
 (0)