Skip to content

Commit 5519887

Browse files
authored
Merge | Move ReliabilitySection / BestEffortCleanup to framework specific files (#2984)
* Move reliabilitysection to netcore/netfx specific files * Move BestEffortCleanup to netfx file * Move AssignPendingDNSInfo to netfx file * Move RunReliably to netfx only * Move SNIErrorDetails to netcore file * Port relevant change from master
1 parent 882045a commit 5519887

File tree

6 files changed

+256
-246
lines changed

6 files changed

+256
-246
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@
659659
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs" />
660660
<Compile Include="Microsoft\Data\SqlClient\SqlTransaction.cs" />
661661
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
662-
<Compile Include="Microsoft\Data\SqlClient\TdsParser.NetCoreApp.cs" />
662+
<Compile Include="Microsoft\Data\SqlClient\TdsParser.netcore.cs" />
663663
<Compile Include="Microsoft\Data\SqlClient\TdsParser.RegisterEncoding.cs" />
664664
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.netcore.cs" />
665665
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectManaged.cs" />

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,10 @@
2626

2727
namespace Microsoft.Data.SqlClient
2828
{
29-
30-
internal struct SNIErrorDetails
31-
{
32-
public string errorMessage;
33-
public uint nativeError;
34-
public uint sniErrorNumber;
35-
public int provider;
36-
public uint lineNumber;
37-
public string function;
38-
public Exception exception;
39-
}
40-
4129
// The TdsParser Object controls reading/writing to the netlib, parsing the tds,
4230
// and surfacing objects to the user.
4331
internal sealed partial class TdsParser
4432
{
45-
internal struct ReliabilitySection
46-
{
47-
/// <summary>
48-
/// This is a no-op in netcore version. Only needed for merging with netfx codebase.
49-
/// </summary>
50-
[Conditional("NETFRAMEWORK")]
51-
internal static void Assert(string message)
52-
{
53-
}
54-
55-
[Conditional("NETFRAMEWORK")]
56-
internal void Start()
57-
{
58-
}
59-
60-
[Conditional("NETFRAMEWORK")]
61-
internal void Stop()
62-
{
63-
}
64-
}
65-
6633
private static int _objectTypeCount; // EventSource counter
6734
private readonly SqlClientLogger _logger = new SqlClientLogger();
6835

@@ -1959,8 +1926,6 @@ internal void PrepareResetConnection(bool preserveTransaction)
19591926
_fPreserveTransaction = preserveTransaction;
19601927
}
19611928

1962-
1963-
19641929
internal bool Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
19651930
{
19661931
bool syncOverAsync = stateObj._syncOverAsync;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.NetCoreApp.cs renamed to src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.netcore.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,38 @@ namespace Microsoft.Data.SqlClient
1010
{
1111
internal sealed partial class TdsParser
1212
{
13+
internal struct SNIErrorDetails
14+
{
15+
public string errorMessage;
16+
public uint nativeError;
17+
public uint sniErrorNumber;
18+
public int provider;
19+
public uint lineNumber;
20+
public string function;
21+
public Exception exception;
22+
}
23+
24+
internal struct ReliabilitySection
25+
{
26+
/// <summary>
27+
/// This is a no-op in netcore version. Only needed for merging with netfx codebase.
28+
/// </summary>
29+
[Conditional("NETFRAMEWORK")]
30+
internal static void Assert(string message)
31+
{
32+
}
33+
34+
[Conditional("NETFRAMEWORK")]
35+
internal void Start()
36+
{
37+
}
38+
39+
[Conditional("NETFRAMEWORK")]
40+
internal void Stop()
41+
{
42+
}
43+
}
44+
1345
internal static void FillGuidBytes(Guid guid, Span<byte> buffer) => guid.TryWriteBytes(buffer);
1446

1547
internal static void FillDoubleBytes(double value, Span<byte> buffer) => BinaryPrimitives.TryWriteInt64LittleEndian(buffer, BitConverter.DoubleToInt64Bits(value));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@
844844
<Compile Include="Microsoft\Data\SqlClient\SqlSequentialTextReaderSmi.cs" />
845845
<Compile Include="Microsoft\Data\SqlClient\SqlTransaction.cs" />
846846
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
847+
<Compile Include="Microsoft\Data\SqlClient\TdsParser.netfx.cs" />
847848
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.netfx.cs" />
848849
<Compile Include="Microsoft\Data\SqlTypes\SqlStreamChars.cs" />
849850
<Compile Include="Microsoft\Data\SqlTypes\SqlTypeWorkarounds.netfx.cs" />

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

Lines changed: 1 addition & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -67,89 +67,7 @@ internal int ObjectID
6767
/// Verify client encryption possibility.
6868
/// </summary>
6969
private bool ClientOSEncryptionSupport => SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
70-
71-
// ReliabilitySection Usage:
72-
//
73-
// #if DEBUG
74-
// TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
75-
//
76-
// RuntimeHelpers.PrepareConstrainedRegions();
77-
// try {
78-
// tdsReliabilitySection.Start();
79-
// #else
80-
// {
81-
// #endif //DEBUG
82-
//
83-
// // code that requires reliability
84-
//
85-
// }
86-
// #if DEBUG
87-
// finally {
88-
// tdsReliabilitySection.Stop();
89-
// }
90-
// #endif //DEBUG
91-
92-
internal struct ReliabilitySection
93-
{
94-
#if DEBUG
95-
// do not allocate TLS data in RETAIL bits
96-
[ThreadStatic]
97-
private static int s_reliabilityCount; // initialized to 0 by CLR
98-
99-
private bool m_started; // initialized to false (not started) by CLR
100-
#endif //DEBUG
101-
102-
[Conditional("DEBUG")]
103-
internal void Start()
104-
{
105-
#if DEBUG
106-
Debug.Assert(!m_started);
107-
108-
RuntimeHelpers.PrepareConstrainedRegions();
109-
try
110-
{
111-
}
112-
finally
113-
{
114-
++s_reliabilityCount;
115-
m_started = true;
116-
}
117-
#endif //DEBUG
118-
}
119-
120-
[Conditional("DEBUG")]
121-
internal void Stop()
122-
{
123-
#if DEBUG
124-
// cannot assert m_started - ThreadAbortException can be raised before Start is called
125-
126-
if (m_started)
127-
{
128-
Debug.Assert(s_reliabilityCount > 0);
129-
130-
RuntimeHelpers.PrepareConstrainedRegions();
131-
try
132-
{
133-
}
134-
finally
135-
{
136-
--s_reliabilityCount;
137-
m_started = false;
138-
}
139-
}
140-
#endif //DEBUG
141-
}
142-
143-
// you need to setup for a thread abort somewhere before you call this method
144-
[Conditional("DEBUG")]
145-
internal static void Assert(string message)
146-
{
147-
#if DEBUG
148-
Debug.Assert(s_reliabilityCount > 0, message);
149-
#endif //DEBUG
150-
}
151-
}
152-
70+
15371
// Default state object for parser
15472
internal TdsParserStateObject _physicalStateObj = null; // Default stateObj and connection for Dbnetlib and non-MARS SNI.
15573

@@ -806,60 +724,6 @@ internal void Connect(ServerInfo serverInfo,
806724
return;
807725
}
808726

809-
// Retrieve the IP and port number from native SNI for TCP protocol. The IP information is stored temporarily in the
810-
// pendingSQLDNSObject but not in the DNS Cache at this point. We only add items to the DNS Cache after we receive the
811-
// IsSupported flag as true in the feature ext ack from server.
812-
internal void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey)
813-
{
814-
UInt32 result;
815-
ushort portFromSNI = 0;
816-
string IPStringFromSNI = string.Empty;
817-
IPAddress IPFromSNI;
818-
isTcpProtocol = false;
819-
Provider providerNumber = Provider.INVALID_PROV;
820-
821-
if (string.IsNullOrEmpty(userProtocol))
822-
{
823-
824-
result = SNINativeMethodWrapper.SniGetProviderNumber(_physicalStateObj.Handle, ref providerNumber);
825-
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetProviderNumber");
826-
isTcpProtocol = (providerNumber == Provider.TCP_PROV);
827-
}
828-
else if (userProtocol == TdsEnums.TCP)
829-
{
830-
isTcpProtocol = true;
831-
}
832-
833-
// serverInfo.UserProtocol could be empty
834-
if (isTcpProtocol)
835-
{
836-
result = SNINativeMethodWrapper.SniGetConnectionPort(_physicalStateObj.Handle, ref portFromSNI);
837-
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionPort");
838-
839-
840-
result = SNINativeMethodWrapper.SniGetConnectionIPString(_physicalStateObj.Handle, ref IPStringFromSNI);
841-
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionIPString");
842-
843-
_connHandler.pendingSQLDNSObject = new SQLDNSInfo(DNSCacheKey, null, null, portFromSNI.ToString());
844-
845-
if (IPAddress.TryParse(IPStringFromSNI, out IPFromSNI))
846-
{
847-
if (System.Net.Sockets.AddressFamily.InterNetwork == IPFromSNI.AddressFamily)
848-
{
849-
_connHandler.pendingSQLDNSObject.AddrIPv4 = IPStringFromSNI;
850-
}
851-
else if (System.Net.Sockets.AddressFamily.InterNetworkV6 == IPFromSNI.AddressFamily)
852-
{
853-
_connHandler.pendingSQLDNSObject.AddrIPv6 = IPStringFromSNI;
854-
}
855-
}
856-
}
857-
else
858-
{
859-
_connHandler.pendingSQLDNSObject = null;
860-
}
861-
}
862-
863727
internal void RemoveEncryption()
864728
{
865729
Debug.Assert((_encryptionOption & EncryptionOptions.OPTIONS_MASK) == EncryptionOptions.LOGIN, "Invalid encryption option state");
@@ -990,41 +854,6 @@ internal void PutSession(TdsParserStateObject session)
990854
}
991855
}
992856

993-
// This is called from a ThreadAbort - ensure that it can be run from a CER Catch
994-
internal void BestEffortCleanup()
995-
{
996-
_state = TdsParserState.Broken;
997-
998-
var stateObj = _physicalStateObj;
999-
if (stateObj != null)
1000-
{
1001-
var stateObjHandle = stateObj.Handle;
1002-
if (stateObjHandle != null)
1003-
{
1004-
stateObjHandle.Dispose();
1005-
}
1006-
}
1007-
1008-
if (_fMARS)
1009-
{
1010-
var sessionPool = _sessionPool;
1011-
if (sessionPool != null)
1012-
{
1013-
sessionPool.BestEffortCleanup();
1014-
}
1015-
1016-
var marsStateObj = _pMarsPhysicalConObj;
1017-
if (marsStateObj != null)
1018-
{
1019-
var marsStateObjHandle = marsStateObj.Handle;
1020-
if (marsStateObjHandle != null)
1021-
{
1022-
marsStateObjHandle.Dispose();
1023-
}
1024-
}
1025-
}
1026-
}
1027-
1028857
private void SendPreLoginHandshake(
1029858
byte[] instanceName,
1030859
SqlConnectionEncryptOption encrypt,
@@ -2249,44 +2078,6 @@ internal void PrepareResetConnection(bool preserveTransaction)
22492078
_fPreserveTransaction = preserveTransaction;
22502079
}
22512080

2252-
internal bool RunReliably(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
2253-
{
2254-
RuntimeHelpers.PrepareConstrainedRegions();
2255-
try
2256-
{
2257-
#if DEBUG
2258-
TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
2259-
RuntimeHelpers.PrepareConstrainedRegions();
2260-
try
2261-
{
2262-
tdsReliabilitySection.Start();
2263-
#endif //DEBUG
2264-
return Run(runBehavior, cmdHandler, dataStream, bulkCopyHandler, stateObj);
2265-
#if DEBUG
2266-
}
2267-
finally
2268-
{
2269-
tdsReliabilitySection.Stop();
2270-
}
2271-
#endif //DEBUG
2272-
}
2273-
catch (OutOfMemoryException)
2274-
{
2275-
_connHandler.DoomThisConnection();
2276-
throw;
2277-
}
2278-
catch (StackOverflowException)
2279-
{
2280-
_connHandler.DoomThisConnection();
2281-
throw;
2282-
}
2283-
catch (ThreadAbortException)
2284-
{
2285-
_connHandler.DoomThisConnection();
2286-
throw;
2287-
}
2288-
}
2289-
22902081
internal bool Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
22912082
{
22922083
bool syncOverAsync = stateObj._syncOverAsync;

0 commit comments

Comments
 (0)