Skip to content

Commit

Permalink
Avoid multiple exceptions at startup from MsQuic support tests (#49973)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Mar 24, 2021
1 parent 561274a commit 18ddd57
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 39 deletions.
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

0 comments on commit 18ddd57

Please sign in to comment.