Skip to content

Commit b1a4413

Browse files
committed
Align netfx use of PacketHandle
1 parent e41cf0e commit b1a4413

File tree

4 files changed

+68
-45
lines changed

4 files changed

+68
-45
lines changed

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

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,35 @@ internal void CreatePhysicalSNIHandle(
136136
ipPreference, cachedDNSInfo, hostNameInCertificate);
137137
}
138138

139-
internal bool IsPacketEmpty(PacketHandle readPacket) => readPacket == default;
139+
internal bool IsPacketEmpty(PacketHandle readPacket)
140+
{
141+
Debug.Assert(readPacket.Type == PacketHandle.NativePointerType || readPacket.Type == 0, "unexpected packet type when requiring NativePointer");
142+
return IntPtr.Zero == readPacket.NativePointer;
143+
}
140144

141145
internal PacketHandle ReadSyncOverAsync(int timeoutRemaining, out uint error)
142146
{
143147
SNIHandle handle = Handle ?? throw ADP.ClosedConnectionError();
144-
PacketHandle readPacket = default;
145-
error = SniNativeWrapper.SniReadSyncOverAsync(handle, ref readPacket, timeoutRemaining);
146-
return readPacket;
148+
IntPtr readPacketPtr = IntPtr.Zero;
149+
error = SniNativeWrapper.SniReadSyncOverAsync(handle, ref readPacketPtr, timeoutRemaining);
150+
return PacketHandle.FromNativePointer(readPacketPtr);
147151
}
148152

149153
internal PacketHandle ReadAsync(SessionHandle handle, out uint error)
150154
{
151-
PacketHandle readPacket = default;
152-
error = SniNativeWrapper.SniReadAsync(handle.NativeHandle, ref readPacket);
153-
return readPacket;
155+
IntPtr readPacketPtr = IntPtr.Zero;
156+
error = SniNativeWrapper.SniReadAsync(handle.NativeHandle, ref readPacketPtr);
157+
return PacketHandle.FromNativePointer(readPacketPtr);
154158
}
155159

156160
internal uint CheckConnection() => SniNativeWrapper.SniCheckConnection(Handle);
157161

158-
internal void ReleasePacket(PacketHandle syncReadPacket) => SniNativeWrapper.SniPacketRelease(syncReadPacket);
159-
162+
internal void ReleasePacket(PacketHandle syncReadPacket)
163+
{
164+
Debug.Assert(syncReadPacket.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
165+
SniNativeWrapper.SniPacketRelease(syncReadPacket.NativePointer);
166+
}
167+
160168
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
161169
internal int DecrementPendingCallbacks(bool release)
162170
{
@@ -375,7 +383,13 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error)
375383

376384
private uint GetSniPacket(PacketHandle packet, ref uint dataSize)
377385
{
378-
return SniNativeWrapper.SniPacketGetData(packet, _inBuff, ref dataSize);
386+
return SniPacketGetData(packet, _inBuff, ref dataSize);
387+
}
388+
389+
private uint SniPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize)
390+
{
391+
Debug.Assert(packet.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
392+
return SniNativeWrapper.SniPacketGetData(packet.NativePointer, _inBuff, ref dataSize);
379393
}
380394

381395
public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
@@ -409,7 +423,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
409423
bool processFinallyBlock = true;
410424
try
411425
{
412-
Debug.Assert(IntPtr.Zero == packet || IntPtr.Zero != packet && source != null, "AsyncResult null on callback");
426+
Debug.Assert(CheckPacket(packet, source), "AsyncResult null on callback");
413427

414428
if (_parser.MARSOn)
415429
{
@@ -479,6 +493,13 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
479493
}
480494
}
481495

496+
private bool CheckPacket(PacketHandle packet, TaskCompletionSource<object> source)
497+
{
498+
Debug.Assert(packet.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
499+
IntPtr ptr = packet.NativePointer;
500+
return IntPtr.Zero == ptr || IntPtr.Zero != ptr && source != null;
501+
}
502+
482503
#pragma warning disable 420 // a reference to a volatile field will not be treated as volatile
483504

484505
public void WriteAsyncCallback(IntPtr key, PacketHandle packet, uint sniError)
@@ -652,7 +673,7 @@ internal Task WritePacket(byte flushMode, bool canAccumulate = false)
652673

653674
#pragma warning disable 420 // a reference to a volatile field will not be treated as volatile
654675

655-
private Task SNIWritePacket(SNIPacket packet, out uint sniError, bool canAccumulate, bool callerHasConnectionLock, bool asyncClose = false)
676+
private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccumulate, bool callerHasConnectionLock, bool asyncClose = false)
656677
{
657678
// Check for a stored exception
658679
Exception delayedException = Interlocked.Exchange(ref _delayedWriteAsyncCallbackException, null);
@@ -694,7 +715,8 @@ private Task SNIWritePacket(SNIPacket packet, out uint sniError, bool canAccumul
694715
}
695716
finally
696717
{
697-
sniError = SniNativeWrapper.SniWritePacket(Handle, packet, sync);
718+
Debug.Assert(packet.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePacket");
719+
sniError = SniNativeWrapper.SniWritePacket(Handle, packet.NativePacket, sync);
698720
}
699721

700722
if (sniError == TdsEnums.SNI_SUCCESS_IO_PENDING)
@@ -788,7 +810,15 @@ private Task SNIWritePacket(SNIPacket packet, out uint sniError, bool canAccumul
788810

789811
#pragma warning restore 420
790812

791-
internal bool IsValidPacket(PacketHandle packetPointer) => packetPointer != default;
813+
internal bool IsValidPacket(PacketHandle packetPointer)
814+
{
815+
Debug.Assert(packetPointer.Type == PacketHandle.NativePointerType || packetPointer.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePointer");
816+
return (
817+
(packetPointer.Type == PacketHandle.NativePointerType && packetPointer.NativePointer != IntPtr.Zero)
818+
||
819+
(packetPointer.Type == PacketHandle.NativePacketType && packetPointer.NativePacket != null)
820+
);
821+
}
792822

793823
// Sends an attention signal - executing thread will consume attn.
794824
internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = false)
@@ -803,10 +833,7 @@ internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = fa
803833
return;
804834
}
805835

806-
SNIPacket attnPacket = new SNIPacket(Handle);
807-
_sniAsyncAttnPacket = attnPacket;
808-
809-
SniNativeWrapper.SniPacketSetData(attnPacket, SQL.AttentionHeader, TdsEnums.HEADER_LEN, null, null);
836+
PacketHandle attnPacket = CreateAndSetAttentionPacket();
810837

811838
RuntimeHelpers.PrepareConstrainedRegions();
812839
try
@@ -866,11 +893,20 @@ internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = fa
866893
}
867894
}
868895

896+
internal PacketHandle CreateAndSetAttentionPacket()
897+
{
898+
SNIPacket attnPacket = new SNIPacket(Handle);
899+
_sniAsyncAttnPacket = attnPacket;
900+
SniNativeWrapper.SniPacketSetData(attnPacket, SQL.AttentionHeader, TdsEnums.HEADER_LEN, null, null);
901+
return PacketHandle.FromNativePacket(attnPacket);
902+
}
903+
869904
private Task WriteSni(bool canAccumulate)
870905
{
871906
// Prepare packet, and write to packet.
872-
SNIPacket packet = GetResetWritePacket();
873-
SniNativeWrapper.SniPacketSetData(packet, _outBuff, _outBytesUsed, _securePasswords, _securePasswordOffsetsInBuffer);
907+
PacketHandle packet = GetResetWritePacket();
908+
SNIPacket nativePacket = packet.NativePacket;
909+
SniNativeWrapper.SniPacketSetData(nativePacket, _outBuff, _outBytesUsed, _securePasswords, _securePasswordOffsetsInBuffer);
874910

875911
Debug.Assert(Parser.Connection._parserLock.ThreadMayHaveLock(), "Thread is writing without taking the connection lock");
876912
Task task = SNIWritePacket(packet, out _, canAccumulate, callerHasConnectionLock: true);
@@ -921,7 +957,7 @@ private Task WriteSni(bool canAccumulate)
921957
return task;
922958
}
923959

924-
internal SNIPacket GetResetWritePacket()
960+
internal PacketHandle GetResetWritePacket()
925961
{
926962
if (_sniPacket != null)
927963
{
@@ -934,7 +970,7 @@ internal SNIPacket GetResetWritePacket()
934970
_sniPacket = _writePacketCache.Take(Handle);
935971
}
936972
}
937-
return _sniPacket;
973+
return PacketHandle.FromNativePacket(_sniPacket);
938974
}
939975

940976
internal void ClearAllWritePackets()
@@ -951,8 +987,10 @@ internal void ClearAllWritePackets()
951987
}
952988
}
953989

954-
private IntPtr AddPacketToPendingList(SNIPacket packet)
990+
private PacketHandle AddPacketToPendingList(PacketHandle packetToAdd)
955991
{
992+
Debug.Assert(packetToAdd.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePacket");
993+
SNIPacket packet = packetToAdd.NativePacket;
956994
Debug.Assert(packet == _sniPacket, "Adding a packet other than the current packet to the pending list");
957995
_sniPacket = null;
958996
IntPtr pointer = packet.DangerousGetHandle();
@@ -962,16 +1000,17 @@ private IntPtr AddPacketToPendingList(SNIPacket packet)
9621000
_pendingWritePackets.Add(pointer, packet);
9631001
}
9641002

965-
return pointer;
1003+
return PacketHandle.FromNativePointer(pointer);
9661004
}
9671005

968-
private void RemovePacketFromPendingList(IntPtr pointer)
1006+
private void RemovePacketFromPendingList(PacketHandle ptr)
9691007
{
970-
SNIPacket recoveredPacket;
1008+
Debug.Assert(ptr.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
1009+
IntPtr pointer = ptr.NativePointer;
9711010

9721011
lock (_writePacketLockObject)
9731012
{
974-
if (_pendingWritePackets.TryGetValue(pointer, out recoveredPacket))
1013+
if (_pendingWritePackets.TryGetValue(pointer, out SNIPacket recoveredPacket))
9751014
{
9761015
_pendingWritePackets.Remove(pointer);
9771016
_writePacketCache.Add(recoveredPacket);
@@ -1031,6 +1070,6 @@ private void SniWriteStatisticsAndTracing()
10311070
SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParser.WritePacket | INFO | ADV | State Object Id {0}, Packet sent. Out buffer: {1}, Out Bytes Used: {2}", ObjectID, _outBuff, _outBytesUsed);
10321071
}
10331072

1034-
protected PacketHandle EmptyReadPacket => default;
1073+
protected PacketHandle EmptyReadPacket => PacketHandle.FromNativePointer(default);
10351074
}
10361075
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ private static void ReadDispatcher(IntPtr key, IntPtr packet, uint error)
107107

108108
if (stateObj != null)
109109
{
110-
#if NETFRAMEWORK
111-
stateObj.ReadAsyncCallback(IntPtr.Zero, packet, error);
112-
#else
113110
stateObj.ReadAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error);
114-
#endif // NETFRAMEWORK
115111
}
116112
}
117113
}
@@ -132,11 +128,7 @@ private static void WriteDispatcher(IntPtr key, IntPtr packet, uint error)
132128

133129
if (stateObj != null)
134130
{
135-
#if NETFRAMEWORK
136-
stateObj.WriteAsyncCallback(IntPtr.Zero, packet, error);
137-
#else
138131
stateObj.WriteAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error);
139-
#endif // NETFRAMEWORK
140132
}
141133
}
142134
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,7 @@ public void ProcessSniPacketCompat(PacketHandle packet, uint error)
510510
else
511511
{
512512
uint dataSize = 0;
513-
514-
#if NETFRAMEWORK
515-
uint getDataError = SniNativeWrapper.SniPacketGetData(packet, _inBuff, ref dataSize);
516-
#else
517513
uint getDataError = SniPacketGetData(packet, _inBuff, ref dataSize);
518-
#endif
519514

520515
if (getDataError == TdsEnums.SNI_SUCCESS)
521516
{

src/Microsoft.Data.SqlClient/tests/FunctionalTests/TdsParserStateObject.TestHarness.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010

1111
namespace Microsoft.Data.SqlClient
1212
{
13-
#if NETFRAMEWORK
14-
using PacketHandle = IntPtr;
15-
#elif NETCOREAPP
1613
internal struct PacketHandle
1714
{
1815
}
19-
#endif
16+
2017
internal partial class TdsParserStateObject
2118
{
2219
internal int ObjectID = 1;

0 commit comments

Comments
 (0)