@@ -43,23 +43,34 @@ private enum NativeProtocols
43
43
internal SNIPacket _sniAsyncAttnPacket = null ; // Packet to use to send Attn
44
44
private readonly WritePacketCache _writePacketCache = new WritePacketCache ( ) ; // Store write packets that are ready to be re-used
45
45
46
- public TdsParserStateObjectNative ( TdsParser parser ) : base ( parser ) { }
47
-
48
46
private GCHandle _gcHandle ; // keeps this object alive until we're closed.
49
47
50
- private 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)
48
+ 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)
51
49
52
- internal TdsParserStateObjectNative ( TdsParser parser , TdsParserStateObject physicalConnection , bool async ) :
53
- base ( parser , physicalConnection , async)
50
+ internal TdsParserStateObjectNative ( TdsParser parser , TdsParserStateObject physicalConnection , bool async )
51
+ : base ( parser , physicalConnection , async)
54
52
{
55
53
}
56
54
55
+ public TdsParserStateObjectNative ( TdsParser parser )
56
+ : base ( parser )
57
+ {
58
+ }
59
+
60
+ ////////////////
61
+ // Properties //
62
+ ////////////////
63
+
57
64
internal SNIHandle Handle => _sessionHandle ;
58
65
59
66
internal override uint Status => _sessionHandle != null ? _sessionHandle . Status : TdsEnums . SNI_UNINITIALIZED ;
60
67
61
68
internal override SessionHandle SessionHandle => SessionHandle . FromNativeHandle ( _sessionHandle ) ;
62
69
70
+ protected override PacketHandle EmptyReadPacket => PacketHandle . FromNativePointer ( default ) ;
71
+
72
+ internal override Guid ? SessionId => default ;
73
+
63
74
protected override void CreateSessionHandle ( TdsParserStateObject physicalConnection , bool async )
64
75
{
65
76
Debug . Assert ( physicalConnection is TdsParserStateObjectNative , "Expected a stateObject of type " + this . GetType ( ) ) ;
@@ -256,22 +267,6 @@ protected override void FreeGcHandle(int remaining, bool release)
256
267
257
268
internal override bool IsFailedHandle ( ) => _sessionHandle . Status != TdsEnums . SNI_SUCCESS ;
258
269
259
- internal override PacketHandle ReadSyncOverAsync ( int timeoutRemaining , out uint error )
260
- {
261
- SNIHandle handle = Handle ;
262
- if ( handle == null )
263
- {
264
- throw ADP . ClosedConnectionError ( ) ;
265
- }
266
- IntPtr readPacketPtr = IntPtr . Zero ;
267
- error = SniNativeWrapper . SniReadSyncOverAsync ( handle , ref readPacketPtr , GetTimeoutRemaining ( ) ) ;
268
- return PacketHandle . FromNativePointer ( readPacketPtr ) ;
269
- }
270
-
271
- protected override PacketHandle EmptyReadPacket => PacketHandle. FromNativePointer( default ) ;
272
-
273
- internal override Guid ? SessionId => default ;
274
-
275
270
internal override bool IsPacketEmpty ( PacketHandle readPacket )
276
271
{
277
272
Debug . Assert ( readPacket . Type == PacketHandle . NativePointerType || readPacket . Type == 0 , "unexpected packet type when requiring NativePointer" ) ;
@@ -298,6 +293,14 @@ internal override PacketHandle ReadAsync(SessionHandle handle, out uint error)
298
293
return PacketHandle . FromNativePointer ( readPacketPtr ) ;
299
294
}
300
295
296
+ internal override PacketHandle ReadSyncOverAsync ( int timeoutRemaining , out uint error )
297
+ {
298
+ SNIHandle handle = Handle ?? throw ADP . ClosedConnectionError ( ) ;
299
+ IntPtr readPacketPtr = IntPtr . Zero ;
300
+ error = SniNativeWrapper . SniReadSyncOverAsync ( handle , ref readPacketPtr , GetTimeoutRemaining ( ) ) ;
301
+ return PacketHandle . FromNativePointer ( readPacketPtr ) ;
302
+ }
303
+
301
304
internal override PacketHandle CreateAndSetAttentionPacket ( )
302
305
{
303
306
SNIHandle handle = Handle ;
@@ -332,11 +335,9 @@ internal override PacketHandle AddPacketToPendingList(PacketHandle packetToAdd)
332
335
internal override bool IsValidPacket ( PacketHandle packetPointer )
333
336
{
334
337
Debug . Assert ( packetPointer . Type == PacketHandle . NativePointerType || packetPointer . Type == PacketHandle . NativePacketType , "unexpected packet type when requiring NativePointer" ) ;
335
- return (
336
- ( packetPointer . Type == PacketHandle . NativePointerType && packetPointer . NativePointer != IntPtr . Zero )
337
- ||
338
- ( packetPointer . Type == PacketHandle . NativePacketType && packetPointer . NativePacket != null )
339
- ) ;
338
+
339
+ return ( packetPointer . Type == PacketHandle . NativePointerType && packetPointer . NativePointer != IntPtr . Zero )
340
+ || ( packetPointer . Type == PacketHandle . NativePacketType && packetPointer . NativePacket != null ) ;
340
341
}
341
342
342
343
internal override PacketHandle GetResetWritePacket ( int dataSize )
@@ -444,8 +445,16 @@ internal override void DisposePacketCache()
444
445
{
445
446
lock ( _writePacketLockObject )
446
447
{
447
- _writePacketCache . Dispose ( ) ;
448
- // Do not set _writePacketCache to null, just in case a WriteAsyncCallback completes after this point
448
+ #if NETFRAMEWORK
449
+ RuntimeHelpers . PrepareConstrainedRegions ( ) ;
450
+ #endif
451
+ try
452
+ { }
453
+ finally
454
+ {
455
+ _writePacketCache . Dispose ( ) ;
456
+ // Do not set _writePacketCache to null, just in case a WriteAsyncCallback completes after this point
457
+ }
449
458
}
450
459
}
451
460
0 commit comments