From c79425e69f9437aae077a5f5c31385c4766bdfd1 Mon Sep 17 00:00:00 2001
From: Edward Neal <55035479+edwardneal@users.noreply.github.com>
Date: Fri, 16 May 2025 19:36:51 +0100
Subject: [PATCH 01/28] Port PacketHandle to netfx
---
.../src/Microsoft.Data.SqlClient.csproj | 4 ++--
.../netfx/src/Microsoft.Data.SqlClient.csproj | 3 +++
...ore.Windows.cs => PacketHandle.Windows.cs} | 23 +++++++++++++++----
3 files changed, 23 insertions(+), 7 deletions(-)
rename src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/{PacketHandle.netcore.Windows.cs => PacketHandle.Windows.cs} (78%)
diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
index eb556a79ec..eb0504f047 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
@@ -878,8 +878,8 @@
Microsoft\Data\SqlClient\LocalDb\LocalDbApi.Windows.cs
-
- Microsoft\Data\SqlClient\PacketHandle.netcore.Windows.cs
+
+ Microsoft\Data\SqlClient\PacketHandle.Windows.cs
Microsoft\Data\SqlClient\SessionHandle.Windows.cs
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
index e1c3277fb7..23380b07a6 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
@@ -447,6 +447,9 @@
Microsoft\Data\SqlClient\Packet.cs
+
+ Microsoft\Data\SqlClient\PacketHandle.Windows.cs
+
Microsoft\Data\SqlClient\ParameterPeekAheadValue.cs
diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/PacketHandle.netcore.Windows.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/PacketHandle.Windows.cs
similarity index 78%
rename from src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/PacketHandle.netcore.Windows.cs
rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/PacketHandle.Windows.cs
index aca8292100..8811729e94 100644
--- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/PacketHandle.netcore.Windows.cs
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/PacketHandle.Windows.cs
@@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-#if NET
-
using System;
namespace Microsoft.Data.SqlClient
@@ -22,13 +20,16 @@ internal readonly ref struct PacketHandle
{
public const int NativePointerType = 1;
public const int NativePacketType = 2;
+#if NET
public const int ManagedPacketType = 3;
public readonly SNI.SNIPacket ManagedPacket;
+#endif
public readonly SNIPacket NativePacket;
public readonly IntPtr NativePointer;
public readonly int Type;
+#if NET
private PacketHandle(IntPtr nativePointer, SNIPacket nativePacket, SNI.SNIPacket managedPacket, int type)
{
Type = type;
@@ -36,7 +37,16 @@ private PacketHandle(IntPtr nativePointer, SNIPacket nativePacket, SNI.SNIPacket
NativePointer = nativePointer;
NativePacket = nativePacket;
}
+#else
+ private PacketHandle(IntPtr nativePointer, SNIPacket nativePacket, int type)
+ {
+ Type = type;
+ NativePointer = nativePointer;
+ NativePacket = nativePacket;
+ }
+#endif
+#if NET
public static PacketHandle FromManagedPacket(SNI.SNIPacket managedPacket) =>
new PacketHandle(default, default, managedPacket, ManagedPacketType);
@@ -45,9 +55,12 @@ public static PacketHandle FromNativePointer(IntPtr nativePointer) =>
public static PacketHandle FromNativePacket(SNIPacket nativePacket) =>
new PacketHandle(default, nativePacket, default, NativePacketType);
+#else
+ public static PacketHandle FromNativePointer(IntPtr nativePointer) =>
+ new PacketHandle(nativePointer, default, NativePointerType);
-
+ public static PacketHandle FromNativePacket(SNIPacket nativePacket) =>
+ new PacketHandle(default, nativePacket, NativePacketType);
+#endif
}
}
-
-#endif
From e41cf0e710238cb381bacf143a2a38bdab57b512 Mon Sep 17 00:00:00 2001
From: Edward Neal <55035479+edwardneal@users.noreply.github.com>
Date: Fri, 16 May 2025 19:46:05 +0100
Subject: [PATCH 02/28] Remove PacketHandle alias shim
---
.../src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs | 2 --
.../Data/SqlClient/TdsParserStateObject.Multiplexer.cs | 3 ---
.../src/Microsoft/Data/SqlClient/TdsParserStateObject.cs | 1 -
3 files changed, 6 deletions(-)
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs
index 49f350f904..9de21620b9 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs
@@ -18,8 +18,6 @@
namespace Microsoft.Data.SqlClient
{
- using PacketHandle = IntPtr;
-
internal partial class TdsParserStateObject
{
protected SNIHandle _sessionHandle = null; // the SNI handle we're to work on
diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.Multiplexer.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.Multiplexer.cs
index 19d3d5add2..f3847c436e 100644
--- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.Multiplexer.cs
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.Multiplexer.cs
@@ -7,9 +7,6 @@
namespace Microsoft.Data.SqlClient
{
-#if NETFRAMEWORK
- using PacketHandle = IntPtr;
-#endif
partial class TdsParserStateObject
{
private Packet _partialPacket;
diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs
index 8cbc275043..61cc18b536 100644
--- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs
@@ -19,7 +19,6 @@
namespace Microsoft.Data.SqlClient
{
#if NETFRAMEWORK
- using PacketHandle = IntPtr;
using RuntimeHelpers = System.Runtime.CompilerServices.RuntimeHelpers;
#endif
From b1a44134c4cea97dd5cc348330f8331557c44387 Mon Sep 17 00:00:00 2001
From: Edward Neal <55035479+edwardneal@users.noreply.github.com>
Date: Fri, 16 May 2025 23:18:38 +0100
Subject: [PATCH 03/28] Align netfx use of PacketHandle
---
.../SqlClient/TdsParserStateObject.netfx.cs | 95 +++++++++++++------
.../SqlClient/TdsParserSafeHandles.Windows.cs | 8 --
.../TdsParserStateObject.Multiplexer.cs | 5 -
.../TdsParserStateObject.TestHarness.cs | 5 +-
4 files changed, 68 insertions(+), 45 deletions(-)
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs
index 9de21620b9..d061e759c9 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs
@@ -136,27 +136,35 @@ internal void CreatePhysicalSNIHandle(
ipPreference, cachedDNSInfo, hostNameInCertificate);
}
- internal bool IsPacketEmpty(PacketHandle readPacket) => readPacket == default;
+ internal bool IsPacketEmpty(PacketHandle readPacket)
+ {
+ Debug.Assert(readPacket.Type == PacketHandle.NativePointerType || readPacket.Type == 0, "unexpected packet type when requiring NativePointer");
+ return IntPtr.Zero == readPacket.NativePointer;
+ }
internal PacketHandle ReadSyncOverAsync(int timeoutRemaining, out uint error)
{
SNIHandle handle = Handle ?? throw ADP.ClosedConnectionError();
- PacketHandle readPacket = default;
- error = SniNativeWrapper.SniReadSyncOverAsync(handle, ref readPacket, timeoutRemaining);
- return readPacket;
+ IntPtr readPacketPtr = IntPtr.Zero;
+ error = SniNativeWrapper.SniReadSyncOverAsync(handle, ref readPacketPtr, timeoutRemaining);
+ return PacketHandle.FromNativePointer(readPacketPtr);
}
internal PacketHandle ReadAsync(SessionHandle handle, out uint error)
{
- PacketHandle readPacket = default;
- error = SniNativeWrapper.SniReadAsync(handle.NativeHandle, ref readPacket);
- return readPacket;
+ IntPtr readPacketPtr = IntPtr.Zero;
+ error = SniNativeWrapper.SniReadAsync(handle.NativeHandle, ref readPacketPtr);
+ return PacketHandle.FromNativePointer(readPacketPtr);
}
internal uint CheckConnection() => SniNativeWrapper.SniCheckConnection(Handle);
- internal void ReleasePacket(PacketHandle syncReadPacket) => SniNativeWrapper.SniPacketRelease(syncReadPacket);
-
+ internal void ReleasePacket(PacketHandle syncReadPacket)
+ {
+ Debug.Assert(syncReadPacket.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
+ SniNativeWrapper.SniPacketRelease(syncReadPacket.NativePointer);
+ }
+
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal int DecrementPendingCallbacks(bool release)
{
@@ -375,7 +383,13 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error)
private uint GetSniPacket(PacketHandle packet, ref uint dataSize)
{
- return SniNativeWrapper.SniPacketGetData(packet, _inBuff, ref dataSize);
+ return SniPacketGetData(packet, _inBuff, ref dataSize);
+ }
+
+ private uint SniPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize)
+ {
+ Debug.Assert(packet.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
+ return SniNativeWrapper.SniPacketGetData(packet.NativePointer, _inBuff, ref dataSize);
}
public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
@@ -409,7 +423,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
bool processFinallyBlock = true;
try
{
- Debug.Assert(IntPtr.Zero == packet || IntPtr.Zero != packet && source != null, "AsyncResult null on callback");
+ Debug.Assert(CheckPacket(packet, source), "AsyncResult null on callback");
if (_parser.MARSOn)
{
@@ -479,6 +493,13 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
}
}
+ private bool CheckPacket(PacketHandle packet, TaskCompletionSource