@@ -136,27 +136,35 @@ internal void CreatePhysicalSNIHandle(
136
136
ipPreference , cachedDNSInfo , hostNameInCertificate ) ;
137
137
}
138
138
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
+ }
140
144
141
145
internal PacketHandle ReadSyncOverAsync ( int timeoutRemaining , out uint error )
142
146
{
143
147
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 ) ;
147
151
}
148
152
149
153
internal PacketHandle ReadAsync ( SessionHandle handle , out uint error )
150
154
{
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 ) ;
154
158
}
155
159
156
160
internal uint CheckConnection ( ) => SniNativeWrapper . SniCheckConnection ( Handle ) ;
157
161
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
+
160
168
[ ReliabilityContract ( Consistency . WillNotCorruptState , Cer . Success ) ]
161
169
internal int DecrementPendingCallbacks ( bool release )
162
170
{
@@ -375,7 +383,13 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error)
375
383
376
384
private uint GetSniPacket ( PacketHandle packet , ref uint dataSize )
377
385
{
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 ) ;
379
393
}
380
394
381
395
public void ReadAsyncCallback ( IntPtr key , PacketHandle packet , uint error )
@@ -409,7 +423,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
409
423
bool processFinallyBlock = true ;
410
424
try
411
425
{
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" ) ;
413
427
414
428
if ( _parser . MARSOn )
415
429
{
@@ -479,6 +493,13 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
479
493
}
480
494
}
481
495
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
+
482
503
#pragma warning disable 420 // a reference to a volatile field will not be treated as volatile
483
504
484
505
public void WriteAsyncCallback ( IntPtr key , PacketHandle packet , uint sniError )
@@ -652,7 +673,7 @@ internal Task WritePacket(byte flushMode, bool canAccumulate = false)
652
673
653
674
#pragma warning disable 420 // a reference to a volatile field will not be treated as volatile
654
675
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 )
656
677
{
657
678
// Check for a stored exception
658
679
Exception delayedException = Interlocked . Exchange ( ref _delayedWriteAsyncCallbackException , null ) ;
@@ -694,7 +715,8 @@ private Task SNIWritePacket(SNIPacket packet, out uint sniError, bool canAccumul
694
715
}
695
716
finally
696
717
{
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 ) ;
698
720
}
699
721
700
722
if ( sniError == TdsEnums . SNI_SUCCESS_IO_PENDING )
@@ -788,7 +810,15 @@ private Task SNIWritePacket(SNIPacket packet, out uint sniError, bool canAccumul
788
810
789
811
#pragma warning restore 420
790
812
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
+ }
792
822
793
823
// Sends an attention signal - executing thread will consume attn.
794
824
internal void SendAttention ( bool mustTakeWriteLock = false , bool asyncClose = false )
@@ -803,10 +833,7 @@ internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = fa
803
833
return ;
804
834
}
805
835
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 ( ) ;
810
837
811
838
RuntimeHelpers . PrepareConstrainedRegions ( ) ;
812
839
try
@@ -866,11 +893,20 @@ internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = fa
866
893
}
867
894
}
868
895
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
+
869
904
private Task WriteSni ( bool canAccumulate )
870
905
{
871
906
// 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 ) ;
874
910
875
911
Debug . Assert ( Parser . Connection . _parserLock . ThreadMayHaveLock ( ) , "Thread is writing without taking the connection lock" ) ;
876
912
Task task = SNIWritePacket ( packet , out _ , canAccumulate , callerHasConnectionLock : true ) ;
@@ -921,7 +957,7 @@ private Task WriteSni(bool canAccumulate)
921
957
return task ;
922
958
}
923
959
924
- internal SNIPacket GetResetWritePacket ( )
960
+ internal PacketHandle GetResetWritePacket ( )
925
961
{
926
962
if ( _sniPacket != null )
927
963
{
@@ -934,7 +970,7 @@ internal SNIPacket GetResetWritePacket()
934
970
_sniPacket = _writePacketCache . Take ( Handle ) ;
935
971
}
936
972
}
937
- return _sniPacket ;
973
+ return PacketHandle . FromNativePacket ( _sniPacket ) ;
938
974
}
939
975
940
976
internal void ClearAllWritePackets ( )
@@ -951,8 +987,10 @@ internal void ClearAllWritePackets()
951
987
}
952
988
}
953
989
954
- private IntPtr AddPacketToPendingList ( SNIPacket packet )
990
+ private PacketHandle AddPacketToPendingList ( PacketHandle packetToAdd )
955
991
{
992
+ Debug . Assert ( packetToAdd . Type == PacketHandle . NativePacketType , "unexpected packet type when requiring NativePacket" ) ;
993
+ SNIPacket packet = packetToAdd . NativePacket ;
956
994
Debug . Assert ( packet == _sniPacket , "Adding a packet other than the current packet to the pending list" ) ;
957
995
_sniPacket = null ;
958
996
IntPtr pointer = packet . DangerousGetHandle ( ) ;
@@ -962,16 +1000,17 @@ private IntPtr AddPacketToPendingList(SNIPacket packet)
962
1000
_pendingWritePackets . Add ( pointer , packet ) ;
963
1001
}
964
1002
965
- return pointer ;
1003
+ return PacketHandle . FromNativePointer ( pointer ) ;
966
1004
}
967
1005
968
- private void RemovePacketFromPendingList ( IntPtr pointer )
1006
+ private void RemovePacketFromPendingList ( PacketHandle ptr )
969
1007
{
970
- SNIPacket recoveredPacket ;
1008
+ Debug . Assert ( ptr . Type == PacketHandle . NativePointerType , "unexpected packet type when requiring NativePointer" ) ;
1009
+ IntPtr pointer = ptr . NativePointer ;
971
1010
972
1011
lock ( _writePacketLockObject )
973
1012
{
974
- if ( _pendingWritePackets . TryGetValue ( pointer , out recoveredPacket ) )
1013
+ if ( _pendingWritePackets . TryGetValue ( pointer , out SNIPacket recoveredPacket ) )
975
1014
{
976
1015
_pendingWritePackets . Remove ( pointer ) ;
977
1016
_writePacketCache . Add ( recoveredPacket ) ;
@@ -1031,6 +1070,6 @@ private void SniWriteStatisticsAndTracing()
1031
1070
SqlClientEventSource . Log . TryAdvancedTraceBinEvent ( "TdsParser.WritePacket | INFO | ADV | State Object Id {0}, Packet sent. Out buffer: {1}, Out Bytes Used: {2}" , ObjectID , _outBuff , _outBytesUsed ) ;
1032
1071
}
1033
1072
1034
- protected PacketHandle EmptyReadPacket => default ;
1073
+ protected PacketHandle EmptyReadPacket => PacketHandle . FromNativePointer ( default ) ;
1035
1074
}
1036
1075
}
0 commit comments