1818
1919namespace Microsoft . Data . SqlClient
2020{
21- using PacketHandle = IntPtr ;
22-
2321 internal partial class TdsParserStateObject
2422 {
2523 protected SNIHandle _sessionHandle = null ; // the SNI handle we're to work on
@@ -138,27 +136,35 @@ internal void CreatePhysicalSNIHandle(
138136 ipPreference , cachedDNSInfo , hostNameInCertificate ) ;
139137 }
140138
141- 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+ }
142144
143145 internal PacketHandle ReadSyncOverAsync ( int timeoutRemaining , out uint error )
144146 {
145147 SNIHandle handle = Handle ?? throw ADP . ClosedConnectionError ( ) ;
146- PacketHandle readPacket = default ;
147- error = SniNativeWrapper . SniReadSyncOverAsync ( handle , ref readPacket , timeoutRemaining ) ;
148- return readPacket ;
148+ IntPtr readPacketPtr = IntPtr . Zero ;
149+ error = SniNativeWrapper . SniReadSyncOverAsync ( handle , ref readPacketPtr , timeoutRemaining ) ;
150+ return PacketHandle . FromNativePointer ( readPacketPtr ) ;
149151 }
150152
151153 internal PacketHandle ReadAsync ( SessionHandle handle , out uint error )
152154 {
153- PacketHandle readPacket = default ;
154- error = SniNativeWrapper. SniReadAsync( handle . NativeHandle , ref readPacket ) ;
155- return readPacket ;
155+ IntPtr readPacketPtr = IntPtr . Zero ;
156+ error = SniNativeWrapper . SniReadAsync ( handle . NativeHandle , ref readPacketPtr ) ;
157+ return PacketHandle . FromNativePointer ( readPacketPtr ) ;
156158 }
157159
158160 internal uint CheckConnection ( ) => SniNativeWrapper . SniCheckConnection ( Handle ) ;
159161
160- internal void ReleasePacket ( PacketHandle syncReadPacket ) => SniNativeWrapper . SniPacketRelease ( syncReadPacket ) ;
161-
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+
162168 [ ReliabilityContract ( Consistency . WillNotCorruptState , Cer . Success ) ]
163169 internal int DecrementPendingCallbacks ( bool release )
164170 {
@@ -377,7 +383,13 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error)
377383
378384 private uint GetSniPacket ( PacketHandle packet , ref uint dataSize )
379385 {
380- 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 ) ;
381393 }
382394
383395 public void ReadAsyncCallback ( IntPtr key , PacketHandle packet , uint error )
@@ -411,7 +423,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
411423 bool processFinallyBlock = true ;
412424 try
413425 {
414- Debug . Assert ( IntPtr . Zero == packet || IntPtr . Zero != packet && source != null , "AsyncResult null on callback" ) ;
426+ Debug . Assert ( CheckPacket ( packet , source ) , "AsyncResult null on callback" ) ;
415427
416428 if ( _parser . MARSOn )
417429 {
@@ -481,6 +493,13 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
481493 }
482494 }
483495
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+
484503#pragma warning disable 420 // a reference to a volatile field will not be treated as volatile
485504
486505 public void WriteAsyncCallback ( IntPtr key , PacketHandle packet , uint sniError )
@@ -654,7 +673,7 @@ internal Task WritePacket(byte flushMode, bool canAccumulate = false)
654673
655674#pragma warning disable 420 // a reference to a volatile field will not be treated as volatile
656675
657- 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 )
658677 {
659678 // Check for a stored exception
660679 Exception delayedException = Interlocked . Exchange ( ref _delayedWriteAsyncCallbackException , null ) ;
@@ -696,7 +715,8 @@ private Task SNIWritePacket(SNIPacket packet, out uint sniError, bool canAccumul
696715 }
697716 finally
698717 {
699- 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 ) ;
700720 }
701721
702722 if ( sniError == TdsEnums . SNI_SUCCESS_IO_PENDING )
@@ -790,7 +810,15 @@ private Task SNIWritePacket(SNIPacket packet, out uint sniError, bool canAccumul
790810
791811#pragma warning restore 420
792812
793- 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+ }
794822
795823 // Sends an attention signal - executing thread will consume attn.
796824 internal void SendAttention ( bool mustTakeWriteLock = false , bool asyncClose = false )
@@ -805,10 +833,7 @@ internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = fa
805833 return ;
806834 }
807835
808- SNIPacket attnPacket = new SNIPacket ( Handle ) ;
809- _sniAsyncAttnPacket = attnPacket ;
810-
811- SniNativeWrapper . SniPacketSetData ( attnPacket , SQL . AttentionHeader , TdsEnums . HEADER_LEN , null , null ) ;
836+ PacketHandle attnPacket = CreateAndSetAttentionPacket ( ) ;
812837
813838 RuntimeHelpers . PrepareConstrainedRegions ( ) ;
814839 try
@@ -868,11 +893,20 @@ internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = fa
868893 }
869894 }
870895
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+
871904 private Task WriteSni ( bool canAccumulate )
872905 {
873906 // Prepare packet, and write to packet.
874- SNIPacket packet = GetResetWritePacket ( ) ;
875- SniNativeWrapper . SniPacketSetData ( packet , _outBuff , _outBytesUsed , _securePasswords , _securePasswordOffsetsInBuffer ) ;
907+ PacketHandle packet = GetResetWritePacket ( ) ;
908+ SNIPacket nativePacket = packet . NativePacket ;
909+ SniNativeWrapper . SniPacketSetData ( nativePacket , _outBuff , _outBytesUsed , _securePasswords , _securePasswordOffsetsInBuffer ) ;
876910
877911 Debug . Assert ( Parser . Connection . _parserLock . ThreadMayHaveLock ( ) , "Thread is writing without taking the connection lock" ) ;
878912 Task task = SNIWritePacket ( packet , out _ , canAccumulate , callerHasConnectionLock : true ) ;
@@ -923,7 +957,7 @@ private Task WriteSni(bool canAccumulate)
923957 return task ;
924958 }
925959
926- internal SNIPacket GetResetWritePacket ( )
960+ internal PacketHandle GetResetWritePacket ( )
927961 {
928962 if ( _sniPacket != null )
929963 {
@@ -936,7 +970,7 @@ internal SNIPacket GetResetWritePacket()
936970 _sniPacket = _writePacketCache . Take ( Handle ) ;
937971 }
938972 }
939- return _sniPacket ;
973+ return PacketHandle . FromNativePacket ( _sniPacket ) ;
940974 }
941975
942976 internal void ClearAllWritePackets ( )
@@ -953,8 +987,10 @@ internal void ClearAllWritePackets()
953987 }
954988 }
955989
956- private IntPtr AddPacketToPendingList ( SNIPacket packet )
990+ private PacketHandle AddPacketToPendingList ( PacketHandle packetToAdd )
957991 {
992+ Debug . Assert ( packetToAdd . Type == PacketHandle . NativePacketType , "unexpected packet type when requiring NativePacket" ) ;
993+ SNIPacket packet = packetToAdd . NativePacket ;
958994 Debug . Assert ( packet == _sniPacket , "Adding a packet other than the current packet to the pending list" ) ;
959995 _sniPacket = null ;
960996 IntPtr pointer = packet . DangerousGetHandle ( ) ;
@@ -964,16 +1000,17 @@ private IntPtr AddPacketToPendingList(SNIPacket packet)
9641000 _pendingWritePackets . Add ( pointer , packet ) ;
9651001 }
9661002
967- return pointer ;
1003+ return PacketHandle . FromNativePointer ( pointer ) ;
9681004 }
9691005
970- private void RemovePacketFromPendingList ( IntPtr pointer )
1006+ private void RemovePacketFromPendingList ( PacketHandle ptr )
9711007 {
972- SNIPacket recoveredPacket ;
1008+ Debug . Assert ( ptr . Type == PacketHandle . NativePointerType , "unexpected packet type when requiring NativePointer" ) ;
1009+ IntPtr pointer = ptr . NativePointer ;
9731010
9741011 lock ( _writePacketLockObject )
9751012 {
976- if ( _pendingWritePackets . TryGetValue ( pointer , out recoveredPacket ) )
1013+ if ( _pendingWritePackets . TryGetValue ( pointer , out SNIPacket recoveredPacket ) )
9771014 {
9781015 _pendingWritePackets . Remove ( pointer ) ;
9791016 _writePacketCache . Add ( recoveredPacket ) ;
@@ -1033,6 +1070,6 @@ private void SniWriteStatisticsAndTracing()
10331070 SqlClientEventSource . Log . TryAdvancedTraceBinEvent ( "TdsParser.WritePacket | INFO | ADV | State Object Id {0}, Packet sent. Out buffer: {1}, Out Bytes Used: {2}" , ObjectID , _outBuff , _outBytesUsed ) ;
10341071 }
10351072
1036- protected PacketHandle EmptyReadPacket => default ;
1073+ protected PacketHandle EmptyReadPacket => PacketHandle . FromNativePointer ( default ) ;
10371074 }
10381075}
0 commit comments