Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/System.Data.SqlClient/src/System.Data.SqlClient.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ProjectGuid>{D4550556-4745-457F-BA8F-3EBF3836D6B4}</ProjectGuid>
<AssemblyName>System.Data.SqlClient</AssemblyName>
Expand Down Expand Up @@ -276,6 +276,8 @@
<!-- Manage the SNI toggle for Windows netstandard and UWP -->
<Compile Include="System\Data\SqlClient\SNI\SNITcpHandle.Windows.cs" />
<Compile Include="System\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs" />
<Compile Include="System\Data\SqlClient\PacketHandle.Windows.cs" />
<Compile Include="System\Data\SqlClient\SessionHandle.Windows.cs" />
<AdditionalFiles Include="$(MSBuildProjectDirectory)/*.analyzerdata.windows" />
</ItemGroup>
<ItemGroup Condition="'$(IsUAPAssembly)' == 'true'">
Expand All @@ -285,6 +287,8 @@
<Compile Include="System\Data\SqlClient\SNI\LocalDB.uap.cs" />
<Compile Include="System\Data\ProviderBase\DbConnectionPoolIdentity.Unix.cs" />
<Compile Include="System\Data\SqlClient\TdsParser.Unix.cs" />
<Compile Include="System\Data\SqlClient\PacketHandle.Unix.cs" />
<Compile Include="System\Data\SqlClient\SessionHandle.Unix.cs" />
</ItemGroup>
<!-- Assets needed on Windows but should be avoided on UAP to avoid sni.dll -->
<ItemGroup Condition=" '$(TargetsWindows)' == 'true' And '$(IsPartialFacadeAssembly)' != 'true' and '$(IsUAPAssembly)' != 'true'">
Expand Down Expand Up @@ -479,6 +483,8 @@
<Compile Include="System\Data\SqlClient\LocalDBAPI.Unix.cs" />
<Compile Include="System\Data\SqlClient\SNI\LocalDB.Unix.cs" />
<Compile Include="System\Data\SqlClient\SNI\SNITcpHandle.Unix.cs" />
<Compile Include="System\Data\SqlClient\PacketHandle.Unix.cs" />
<Compile Include="System\Data\SqlClient\SessionHandle.Unix.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true' And '$(IsPartialFacadeAssembly)' != 'true' and '$(IsUAPAssembly)' != 'true'">
<Reference Include="Microsoft.Win32.Registry" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


namespace System.Data.SqlClient
{
// this structure is used for transporting packet handle references between the TdsParserStateObject
// base class and Managed or Native implementations.
// It prevents the native IntPtr type from being boxed and prevents the need to cast from object which loses compile time type safety
// It carries type information so that assertions about the type of handle can be made in the implemented abstract methods
// it is a ref struct so that it can only be used to transport the handles and not store them

// N.B. If you change this type you must also change the version for the other platform

internal readonly ref struct PacketHandle
{
public const int NativePointerType = 1;
public const int NativePacketType = 2;
public const int ManagedPacketType = 3;

public readonly SNI.SNIPacket ManagedPacket;
public readonly int Type;

private PacketHandle(SNI.SNIPacket managedPacket, int type)
{
Type = type;
ManagedPacket = managedPacket;
}

public static PacketHandle FromManagedPacket(SNI.SNIPacket managedPacket) => new PacketHandle(managedPacket, ManagedPacketType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


namespace System.Data.SqlClient
{
// this structure is used for transporting packet handle references between the TdsParserStateObject
// base class and Managed or Native implementations.
// It prevents the native IntPtr type from being boxed and prevents the need to cast from object which loses compile time type safety
// It carries type information so that assertions about the type of handle can be made in the implemented abstract methods
// it is a ref struct so that it can only be used to transport the handles and not store them

// N.B. If you change this type you must also change the version for the other platform

internal readonly ref struct PacketHandle
{
public const int NativePointerType = 1;
public const int NativePacketType = 2;
public const int ManagedPacketType = 3;

public readonly IntPtr NativePointer;
public readonly SNIPacket NativePacket;

public readonly SNI.SNIPacket ManagedPacket;
public readonly int Type;

private PacketHandle(IntPtr nativePointer, SNIPacket nativePacket, SNI.SNIPacket managedPacket, int type)
{
Type = type;
ManagedPacket = managedPacket;
NativePointer = nativePointer;
NativePacket = nativePacket;
}

public static PacketHandle FromManagedPacket(SNI.SNIPacket managedPacket) => new PacketHandle(default, default, managedPacket, ManagedPacketType);

public static PacketHandle FromNativePointer(IntPtr nativePointer) => new PacketHandle(nativePointer, default, default, NativePointerType);

public static PacketHandle FromNativePacket(SNIPacket nativePacket) => new PacketHandle(default, nativePacket, default, NativePacketType);


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public void HandleReceiveError(SNIPacket packet)
_packetEvent.Set();
}

((TdsParserStateObject)_callbackObject).ReadAsyncCallback(packet, 1);
((TdsParserStateObject)_callbackObject).ReadAsyncCallback(PacketHandle.FromManagedPacket(packet), 1);
}

/// <summary>
Expand All @@ -332,7 +332,7 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode)
{
Debug.Assert(_callbackObject != null);

((TdsParserStateObject)_callbackObject).WriteAsyncCallback(packet, sniErrorCode);
((TdsParserStateObject)_callbackObject).WriteAsyncCallback(PacketHandle.FromManagedPacket(packet), sniErrorCode);
}
}

Expand Down Expand Up @@ -378,7 +378,7 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header)
_asyncReceives--;
Debug.Assert(_callbackObject != null);

((TdsParserStateObject)_callbackObject).ReadAsyncCallback(packet, 0);
((TdsParserStateObject)_callbackObject).ReadAsyncCallback(PacketHandle.FromManagedPacket(packet), 0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ internal class SNIPacket : IDisposable, IEquatable<SNIPacket>
private int _offset;
private string _description;
private SNIAsyncCallback _completionCallback;

private ArrayPool<byte> _arrayPool = ArrayPool<byte>.Shared;
private bool _isBufferFromArrayPool = false;

public SNIPacket() { }
Expand Down Expand Up @@ -98,14 +96,14 @@ public void Allocate(int capacity)
{
if (_isBufferFromArrayPool)
{
_arrayPool.Return(_data);
ArrayPool<byte>.Shared.Return(_data);
}
_data = null;
}

if (_data == null)
{
_data = _arrayPool.Rent(capacity);
_data = ArrayPool<byte>.Shared.Rent(capacity);
_isBufferFromArrayPool = true;
}

Expand Down Expand Up @@ -221,7 +219,7 @@ public void Release()
{
if(_isBufferFromArrayPool)
{
_arrayPool.Return(_data);
ArrayPool<byte>.Shared.Return(_data);
}
_data = null;
_capacity = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


namespace System.Data.SqlClient
{
// this structure is used for transporting packet handle references between the TdsParserStateObject
// base class and Managed or Native implementations.
// It carries type information so that assertions about the type of handle can be made in the
// implemented abstract methods
// it is a ref struct so that it can only be used to transport the handles and not store them

// N.B. If you change this type you must also change the version for the other platform

internal readonly ref struct SessionHandle
{
public const int NativeHandleType = 1;
public const int ManagedHandleType = 2;

public readonly SNI.SNIHandle ManagedHandle;
public readonly int Type;

public SessionHandle(SNI.SNIHandle managedHandle, int type)
{
Type = type;
ManagedHandle = managedHandle;
}

public bool IsNull => ManagedHandle is null;

public static SessionHandle FromManagedSession(SNI.SNIHandle managedSessionHandle) => new SessionHandle(managedSessionHandle, ManagedHandleType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


namespace System.Data.SqlClient
{
// this structure is used for transporting packet handle references between the TdsParserStateObject
// base class and Managed or Native implementations.
// It carries type information so that assertions about the type of handle can be made in the
// implemented abstract methods
// it is a ref struct so that it can only be used to transport the handles and not store them

// N.B. If you change this type you must also change the version for the other platform

internal readonly ref struct SessionHandle
{
public const int NativeHandleType = 1;
public const int ManagedHandleType = 2;

public readonly SNI.SNIHandle ManagedHandle;
public readonly SNIHandle NativeHandle;

public readonly int Type;

public SessionHandle(SNI.SNIHandle managedHandle, SNIHandle nativeHandle, int type)
{
Type = type;
ManagedHandle = managedHandle;
NativeHandle = nativeHandle;
}

public bool IsNull => (Type == NativeHandleType) ? NativeHandle is null : ManagedHandle is null;

public static SessionHandle FromManagedSession(SNI.SNIHandle managedSessionHandle) => new SessionHandle(managedSessionHandle, default, ManagedHandleType);

public static SessionHandle FromNativeHandle(SNIHandle nativeSessionHandle) => new SessionHandle(default, nativeSessionHandle, NativeHandleType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ internal void PostReadAsyncForMars()
// Have to post read to initialize MARS - will get callback on this when connection goes
// down or is closed.

IntPtr temp = IntPtr.Zero;
PacketHandle temp = default;
uint error = TdsEnums.SNI_SUCCESS;

_pMarsPhysicalConObj.IncrementPendingCallbacks();
object handle = _pMarsPhysicalConObj.SessionHandle;
temp = (IntPtr)_pMarsPhysicalConObj.ReadAsync(out error, ref handle);
SessionHandle handle = _pMarsPhysicalConObj.SessionHandle;
temp = _pMarsPhysicalConObj.ReadAsync(handle, out error);

if (temp != IntPtr.Zero)
Debug.Assert(temp.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");

if (temp.NativePointer != IntPtr.Zero)
{
// Be sure to release packet, otherwise it will be leaked by native.
_pMarsPhysicalConObj.ReleasePacket(temp);
}
Debug.Assert(IntPtr.Zero == temp, "unexpected syncReadPacket without corresponding SNIPacketRelease");

Debug.Assert(IntPtr.Zero == temp.NativePointer, "unexpected syncReadPacket without corresponding SNIPacketRelease");
if (TdsEnums.SNI_SUCCESS_IO_PENDING != error)
{
Debug.Assert(TdsEnums.SNI_SUCCESS != error, "Unexpected successful read async on physical connection before enabling MARS!");
Expand Down Expand Up @@ -118,4 +120,4 @@ private SNIErrorDetails GetSniErrorDetails()
}

} // tdsparser
}//namespace
}//namespace
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static void ReadDispatcher(IntPtr key, IntPtr packet, uint error)

if (null != stateObj)
{
stateObj.ReadAsyncCallback(IntPtr.Zero, packet, error);
stateObj.ReadAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error);
}
}
}
Expand All @@ -125,7 +125,7 @@ private static void WriteDispatcher(IntPtr key, IntPtr packet, uint error)

if (null != stateObj)
{
stateObj.WriteAsyncCallback(IntPtr.Zero, packet, error);
stateObj.WriteAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error);
}
}
}
Expand Down Expand Up @@ -296,4 +296,4 @@ public void Dispose()
}
}
}
}
}
Loading