Skip to content

Commit c21d451

Browse files
committed
Merge ClearAllWritePackets, AddPacketToPendingList, RemovePacketFromPendingList
1 parent 95c8414 commit c21d451

File tree

4 files changed

+62
-60
lines changed

4 files changed

+62
-60
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,6 @@ internal abstract void CreatePhysicalSNIHandle(
100100

101101
internal abstract uint CheckConnection();
102102

103-
internal abstract void ClearAllWritePackets();
104-
105-
internal abstract PacketHandle AddPacketToPendingList(PacketHandle packet);
106-
107-
protected abstract void RemovePacketFromPendingList(PacketHandle pointer);
108-
109103
internal int DecrementPendingCallbacks(bool release)
110104
{
111105
int remaining = Interlocked.Decrement(ref _pendingCallbacks);

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

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ internal partial class TdsParserStateObject
2323
protected SNIHandle _sessionHandle = null; // the SNI handle we're to work on
2424

2525
// SNI variables // multiple resultsets in one batch.
26-
private SNIPacket _sniPacket = null; // Will have to re-vamp this for MARS
26+
protected SNIPacket _sniPacket = null; // Will have to re-vamp this for MARS
2727
internal SNIPacket _sniAsyncAttnPacket = null; // Packet to use to send Attn
28-
private readonly WritePacketCache _writePacketCache = new WritePacketCache(); // Store write packets that are ready to be re-used
29-
private readonly Dictionary<IntPtr, SNIPacket> _pendingWritePackets = new Dictionary<IntPtr, SNIPacket>(); // Stores write packets that have been sent to SNI, but have not yet finished writing (i.e. we are waiting for SNI's callback)
28+
protected readonly WritePacketCache _writePacketCache = new WritePacketCache(); // Store write packets that are ready to be re-used
3029

3130
// Async variables
3231
private GCHandle _gcHandle; // keeps this object alive until we're closed.
@@ -906,57 +905,6 @@ private Task WriteSni(bool canAccumulate)
906905
return task;
907906
}
908907

909-
internal void ClearAllWritePackets()
910-
{
911-
if (_sniPacket != null)
912-
{
913-
_sniPacket.Dispose();
914-
_sniPacket = null;
915-
}
916-
lock (_writePacketLockObject)
917-
{
918-
Debug.Assert(_pendingWritePackets.Count == 0 && _asyncWriteCount == 0, "Should not clear all write packets if there are packets pending");
919-
_writePacketCache.Clear();
920-
}
921-
}
922-
923-
private PacketHandle AddPacketToPendingList(PacketHandle packetToAdd)
924-
{
925-
Debug.Assert(packetToAdd.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePacket");
926-
SNIPacket packet = packetToAdd.NativePacket;
927-
Debug.Assert(packet == _sniPacket, "Adding a packet other than the current packet to the pending list");
928-
_sniPacket = null;
929-
IntPtr pointer = packet.DangerousGetHandle();
930-
931-
lock (_writePacketLockObject)
932-
{
933-
_pendingWritePackets.Add(pointer, packet);
934-
}
935-
936-
return PacketHandle.FromNativePointer(pointer);
937-
}
938-
939-
private void RemovePacketFromPendingList(PacketHandle ptr)
940-
{
941-
Debug.Assert(ptr.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
942-
IntPtr pointer = ptr.NativePointer;
943-
944-
lock (_writePacketLockObject)
945-
{
946-
if (_pendingWritePackets.TryGetValue(pointer, out SNIPacket recoveredPacket))
947-
{
948-
_pendingWritePackets.Remove(pointer);
949-
_writePacketCache.Add(recoveredPacket);
950-
}
951-
#if DEBUG
952-
else
953-
{
954-
Debug.Assert(false, "Removing a packet from the pending list that was never added to it");
955-
}
956-
#endif
957-
}
958-
}
959-
960908
//////////////////////////////////////////////
961909
// Statistics, Tracing, and related methods //
962910
//////////////////////////////////////////////

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Generic;
67
using System.Diagnostics;
78
using System.Threading.Tasks;
89
using Interop.Windows.Sni;
@@ -12,6 +13,8 @@ namespace Microsoft.Data.SqlClient
1213
{
1314
internal class TdsParserStateObjectNative : TdsParserStateObject
1415
{
16+
private readonly Dictionary<IntPtr, SNIPacket> _pendingWritePackets = new Dictionary<IntPtr, SNIPacket>(); // Stores write packets that have been sent to SNI, but have not yet finished writing (i.e. we are waiting for SNI's callback)
17+
1518
internal TdsParserStateObjectNative(TdsParser parser, TdsParserStateObject physicalConnection, bool async)
1619
: base(parser, physicalConnection.Handle, async)
1720
{
@@ -47,6 +50,27 @@ protected override bool CheckPacket(PacketHandle packet, TaskCompletionSource<ob
4750
return IntPtr.Zero == ptr || IntPtr.Zero != ptr && source != null;
4851
}
4952

53+
protected override void RemovePacketFromPendingList(PacketHandle ptr)
54+
{
55+
Debug.Assert(ptr.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
56+
IntPtr pointer = ptr.NativePointer;
57+
58+
lock (_writePacketLockObject)
59+
{
60+
if (_pendingWritePackets.TryGetValue(pointer, out SNIPacket recoveredPacket))
61+
{
62+
_pendingWritePackets.Remove(pointer);
63+
_writePacketCache.Add(recoveredPacket);
64+
}
65+
#if DEBUG
66+
else
67+
{
68+
Debug.Fail("Removing a packet from the pending list that was never added to it");
69+
}
70+
#endif
71+
}
72+
}
73+
5074
internal override bool IsFailedHandle() => _sessionHandle.Status != TdsEnums.SNI_SUCCESS;
5175

5276
internal override bool IsPacketEmpty(PacketHandle readPacket)
@@ -82,6 +106,22 @@ internal override uint WritePacket(PacketHandle packet, bool sync)
82106
return SniNativeWrapper.SniWritePacket(Handle, packet.NativePacket, sync);
83107
}
84108

109+
internal override PacketHandle AddPacketToPendingList(PacketHandle packetToAdd)
110+
{
111+
Debug.Assert(packetToAdd.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePacket");
112+
SNIPacket packet = packetToAdd.NativePacket;
113+
Debug.Assert(packet == _sniPacket, "Adding a packet other than the current packet to the pending list");
114+
_sniPacket = null;
115+
IntPtr pointer = packet.DangerousGetHandle();
116+
117+
lock (_writePacketLockObject)
118+
{
119+
_pendingWritePackets.Add(pointer, packet);
120+
}
121+
122+
return PacketHandle.FromNativePointer(pointer);
123+
}
124+
85125
internal override bool IsValidPacket(PacketHandle packetPointer)
86126
{
87127
Debug.Assert(packetPointer.Type == PacketHandle.NativePointerType || packetPointer.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePointer");
@@ -108,6 +148,20 @@ internal override PacketHandle GetResetWritePacket(int dataSize)
108148
return PacketHandle.FromNativePacket(_sniPacket);
109149
}
110150

151+
internal override void ClearAllWritePackets()
152+
{
153+
if (_sniPacket != null)
154+
{
155+
_sniPacket.Dispose();
156+
_sniPacket = null;
157+
}
158+
lock (_writePacketLockObject)
159+
{
160+
Debug.Assert(_pendingWritePackets.Count == 0 && _asyncWriteCount == 0, "Should not clear all write packets if there are packets pending");
161+
_writePacketCache.Clear();
162+
}
163+
}
164+
111165
internal override uint SniGetConnectionId(ref Guid clientConnectionId)
112166
=> SniNativeWrapper.SniGetConnectionId(Handle, ref clientConnectionId);
113167

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@ internal long TimeoutTime
499499

500500
internal abstract uint WritePacket(PacketHandle packet, bool sync);
501501

502+
internal abstract PacketHandle AddPacketToPendingList(PacketHandle packet);
503+
504+
protected abstract void RemovePacketFromPendingList(PacketHandle pointer);
505+
506+
internal abstract void ClearAllWritePackets();
507+
502508
internal abstract bool IsValidPacket(PacketHandle packetPointer);
503509

504510
internal abstract PacketHandle ReadAsync(SessionHandle handle, out uint error);

0 commit comments

Comments
 (0)