Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid multiple exceptions at startup from MsQuic support tests #49973

Merged
merged 1 commit into from
Mar 24, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,8 @@ internal class MsQuicApi : IDisposable

private readonly IntPtr _registrationContext;

private unsafe MsQuicApi()
private unsafe MsQuicApi(MsQuicNativeMethods.NativeApi* registration)
{
MsQuicNativeMethods.NativeApi* registration;

try
{
uint status = Interop.MsQuic.MsQuicOpen(out registration);
if (!MsQuicStatusHelper.SuccessfulStatusCode(status))
{
throw new NotSupportedException(SR.net_quic_notsupported);
}
}
catch (DllNotFoundException)
{
throw new NotSupportedException(SR.net_quic_notsupported);
}

MsQuicNativeMethods.NativeApi nativeRegistration = *registration;

RegistrationOpenDelegate =
Expand Down Expand Up @@ -138,7 +123,7 @@ private unsafe MsQuicApi()

internal static bool IsQuicSupported { get; }

static MsQuicApi()
static unsafe MsQuicApi()
{
// MsQuicOpen will succeed even if the platform will not support it. It will then fail with unspecified
// platform-specific errors in subsequent callbacks. For now, check for the minimum build we've tested it on.
Expand All @@ -150,14 +135,30 @@ static MsQuicApi()

// TODO: try to initialize TLS 1.3 in SslStream.

try
{
Api = new MsQuicApi();
IsQuicSupported = true;
}
catch (NotSupportedException)
// TODO: Consider updating all of these delegates to instead use function pointers.

if (NativeLibrary.TryLoad(Interop.Libraries.MsQuic, out IntPtr msQuicHandle))
{
IsQuicSupported = false;
try
{
if (NativeLibrary.TryGetExport(msQuicHandle, "MsQuicOpen", out IntPtr msQuicOpenAddress))
{
MsQuicNativeMethods.MsQuicOpenDelegate msQuicOpen = Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.MsQuicOpenDelegate>(msQuicOpenAddress);
uint status = msQuicOpen(out MsQuicNativeMethods.NativeApi* registration);
if (MsQuicStatusHelper.SuccessfulStatusCode(status))
{
IsQuicSupported = true;
Api = new MsQuicApi(registration);
}
}
}
finally
{
if (!IsQuicSupported)
{
NativeLibrary.Free(msQuicHandle);
}
}
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ internal struct NativeApi
internal IntPtr DatagramSend;
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate uint MsQuicOpenDelegate(
out NativeApi* registration);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate uint SetContextDelegate(
IntPtr handle,
Expand Down