Skip to content

Commit

Permalink
Allocate MsQuicSafeHandle._traceId lazily (#72360)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzikm authored Jul 18, 2022
1 parent 5cbbdba commit 86a4ec3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,37 @@ internal static unsafe QuicAddr ToQuicAddr(this IPEndPoint iPEndPoint)
}

internal static unsafe T GetMsQuicParameter<T>(MsQuicSafeHandle handle, uint parameter)
where T: unmanaged
where T : unmanaged
{
T value;
uint length = (uint)sizeof(T);

ThrowHelper.ThrowIfMsQuicError(MsQuicApi.Api.ApiTable->GetParam(
int status = MsQuicApi.Api.ApiTable->GetParam(
handle.QuicHandle,
parameter,
&length,
(byte*)&value),
$"GetParam({handle}, {parameter}) failed");
(byte*)&value);

if (StatusFailed(status))
{
throw ThrowHelper.GetExceptionForMsQuicStatus(status, $"GetParam({handle}, {parameter}) failed");
}

return value;
}

internal static unsafe void SetMsQuicParameter<T>(MsQuicSafeHandle handle, uint parameter, T value)
where T: unmanaged
where T : unmanaged
{
ThrowHelper.ThrowIfMsQuicError(MsQuicApi.Api.ApiTable->SetParam(
int status = MsQuicApi.Api.ApiTable->SetParam(
handle.QuicHandle,
parameter,
(uint)sizeof(T),
(byte*)&value),
$"SetParam({handle}, {parameter}) failed");
(byte*)&value);

if (StatusFailed(status))
{
throw ThrowHelper.GetExceptionForMsQuicStatus(status, $"SetParam({handle}, {parameter}) failed");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ internal unsafe class MsQuicSafeHandle : SafeHandle
};

private readonly delegate* unmanaged[Cdecl]<QUIC_HANDLE*, void> _releaseAction;
private readonly string _traceId;
private string? _traceId;
private SafeHandleType _type;

public override bool IsInvalid => handle == IntPtr.Zero;

Expand All @@ -30,7 +31,7 @@ public MsQuicSafeHandle(QUIC_HANDLE* handle, delegate* unmanaged[Cdecl]<QUIC_HAN
: base((IntPtr)handle, ownsHandle: true)
{
_releaseAction = releaseAction;
_traceId = $"[{s_typeName[(int)safeHandleType]}][0x{DangerousGetHandle():X11}]";
_type = safeHandleType;

if (NetEventSource.Log.IsEnabled())
{
Expand All @@ -51,7 +52,7 @@ protected override bool ReleaseHandle()
return true;
}

public override string ToString() => _traceId;
public override string ToString() => _traceId ??= $"[{s_typeName[(int)_type]}][0x{DangerousGetHandle():X11}]";
}

internal enum SafeHandleType
Expand Down

0 comments on commit 86a4ec3

Please sign in to comment.