Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge to shared - TdsParserSafeHandles #1604

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParameterSetter.cs">
<Link>Microsoft\Data\SqlClient\TdsParameterSetter.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.cs">
<Link>Microsoft\Data\SqlClient\TdsParserSafeHandles.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs">
<Link>Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs</Link>
</Compile>
Expand Down Expand Up @@ -744,7 +747,6 @@
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\SNI\LocalDB.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserSafeHandles.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Windows.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,9 @@
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParameterSetter.cs">
<Link>Microsoft\Data\SqlClient\TdsParameterSetter.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.cs">
<Link>Microsoft\Data\SqlClient\TdsParserSafeHandles.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs">
<Link>Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs</Link>
</Compile>
Expand Down Expand Up @@ -631,7 +634,6 @@
<Compile Include="Microsoft\Data\SqlClient\SqlUtil.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserHelperClasses.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserSafeHandles.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserSessionPool.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStaticMethods.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
#if NETFRAMEWORK
using System.Runtime.CompilerServices;
using Microsoft.Data.Common;
#endif

namespace Microsoft.Data.SqlClient
{
Expand All @@ -23,7 +27,7 @@ internal sealed partial class SNILoadHandle : SafeHandle

private SNILoadHandle() : base(IntPtr.Zero, true)
{
// From security review - SafeHandle guarantees this is only called once.
// SQL BU DT 346588 - from security review - SafeHandle guarantees this is only called once.
// The reason for the safehandle is guaranteed initialization and termination of SNI to
// ensure SNI terminates and cleans up properly.
try
Expand Down Expand Up @@ -81,7 +85,12 @@ override protected bool ReleaseHandle()
return true;
}

#if NETFRAMEWORK
// TODO: update references
public uint SNIStatus => _sniStatus;
#else
public uint Status => _sniStatus;
#endif

public EncryptionOptions Options => _encryptionOption;

Expand All @@ -101,7 +110,11 @@ private static void ReadDispatcher(IntPtr key, IntPtr packet, uint error)

if (null != stateObj)
{
#if NETFRAMEWORK
stateObj.ReadAsyncCallback(IntPtr.Zero, packet, error);
#else
stateObj.ReadAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error);
#endif // NETFRAMEWORK
}
}
}
Expand All @@ -122,7 +135,11 @@ private static void WriteDispatcher(IntPtr key, IntPtr packet, uint error)

if (null != stateObj)
{
#if NETFRAMEWORK
stateObj.WriteAsyncCallback(IntPtr.Zero, packet, error);
#else
stateObj.WriteAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error);
#endif // NETFRAMEWORK
}
}
}
Expand All @@ -144,10 +161,17 @@ internal SNIHandle(
bool flushCache,
bool fSync,
bool fParallel,
#if NETFRAMEWORK
TransparentNetworkResolutionState transparentNetworkResolutionState,
int totalTimeout,
#endif
SqlConnectionIPAddressPreference ipPreference,
SQLDNSInfo cachedDNSInfo)
: base(IntPtr.Zero, true)
{
#if NETFRAMEWORK
RuntimeHelpers.PrepareConstrainedRegions();
#endif
try
{ }
finally
Expand All @@ -156,11 +180,21 @@ internal SNIHandle(
instanceName = new byte[256]; // Size as specified by netlibs.
if (ignoreSniOpenTimeout)
{
// UNDONE: ITEM12001110 (DB Mirroring Reconnect) Old behavior of not truly honoring timeout presevered
// for non-failover scenarios to avoid breaking changes as part of a QFE. Consider fixing timeout
// handling in next full release and removing ignoreSniOpenTimeout parameter.
timeout = Timeout.Infinite; // -1 == native SNIOPEN_TIMEOUT_VALUE / INFINITE
}

#if NETFRAMEWORK
int transparentNetworkResolutionStateNo = (int)transparentNetworkResolutionState;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what the plans are for transparent network resolution. It may be a feature that should come to netcore eventually. It might be an idea to have a new compiler constant for it, @David-Engel is probably the person who would know.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current sentiment around TNIR is that we can bring connection string and SqlConnectionStringBuilder parity to netcore but we should not actually implement the TNIR logic. Apparently it caused more support issues than it solved for Azure. Customers that really need the driver to initiate parallel connection attempts against each IP DNS resolves to should use MSF.

_status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle,
spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, transparentNetworkResolutionStateNo, totalTimeout,
ADP.IsAzureSqlServerEndpoint(serverName), ipPreference, cachedDNSInfo);
#else
_status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle,
spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, ipPreference, cachedDNSInfo);
#endif // NETFRAMEWORK
}
}

Expand Down