< Summary

Information
Class: LiveKit.Rtc.Internal.FfiHandle
Assembly: LivekitRtc
File(s): /home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitRtc/Internal/FfiHandle.cs
Line coverage
75%
Covered lines: 28
Uncovered lines: 9
Coverable lines: 37
Total lines: 149
Line coverage: 75.6%
Branch coverage
50%
Covered branches: 9
Total branches: 18
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_LiveCount()100%11100%
get_FailedReleaseCount()100%11100%
.ctor(...)50%22100%
.ctor(...)0%620%
get_IsInvalid()50%22100%
get_HandleId()100%11100%
ReleaseHandle()50%6680%
MarkConsumed()66.66%6685.71%
FromId(...)100%11100%

File(s)

/home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitRtc/Internal/FfiHandle.cs

#LineLine coverage
 1// author: https://github.com/pabloFuente
 2
 3using System;
 4using System.Runtime.ConstrainedExecution;
 5using System.Runtime.InteropServices;
 6using System.Threading;
 7
 8namespace LiveKit.Rtc.Internal
 9{
 10    /// <summary>
 11    /// SafeHandle wrapper for FFI handles returned by the native library.
 12    /// Ensures proper cleanup of native resources.
 13    /// </summary>
 14    public class FfiHandle : SafeHandle
 15    {
 16        /// <summary>
 17        /// Invalid handle constant.
 18        /// </summary>
 19        public const ulong InvalidHandle = 0;
 20
 21        // Process-wide counters read by the reliability tests (internal, not part of the public
 22        // API): a handle that is never released, or released twice, is a native leak or a
 23        // double free that no exception reveals
 24        private static long s_wrapped;
 25        private static long s_released;
 26        private static long s_failedReleases;
 27
 28        private readonly ulong _handleId;
 29        private bool _disposed;
 30
 31        /// <summary>
 32        /// Valid handles wrapped and not released yet, in the whole process.
 33        /// </summary>
 34        internal static long LiveCount =>
 12935            Interlocked.Read(ref s_wrapped) - Interlocked.Read(ref s_released);
 36
 37        /// <summary>
 38        /// Releases the native library rejected because it did not know the handle: a handle
 39        /// released twice, or wrapped by two owners.
 40        /// </summary>
 14941        internal static long FailedReleaseCount => Interlocked.Read(ref s_failedReleases);
 42
 43        /// <summary>
 44        /// Creates a new FfiHandle from a native handle ID.
 45        /// </summary>
 46        /// <param name="handleId">The native handle ID.</param>
 47        internal FfiHandle(ulong handleId)
 1040548            : base(IntPtr.Zero, true)
 1040549        {
 1040550            _handleId = handleId;
 1040551            SetHandle(new IntPtr((long)handleId));
 1040552            if (handleId != InvalidHandle)
 1040553                Interlocked.Increment(ref s_wrapped);
 1040554        }
 55
 56        /// <summary>
 57        /// Creates an FfiHandle from an IntPtr.
 58        /// </summary>
 59        /// <param name="ptr">The pointer value.</param>
 60        internal FfiHandle(IntPtr ptr)
 061            : base(ptr, true)
 062        {
 063            _handleId = (ulong)ptr.ToInt64();
 064            if (!IsInvalid)
 065                Interlocked.Increment(ref s_wrapped);
 066        }
 67
 68        /// <summary>
 69        /// Gets whether this handle is invalid.
 70        /// </summary>
 2195571        public override bool IsInvalid => handle == IntPtr.Zero || _handleId == InvalidHandle;
 72
 73        /// <summary>
 74        /// Gets the underlying handle ID.
 75        /// </summary>
 5915676        public ulong HandleId => _handleId;
 77
 78        /// <summary>
 79        /// Releases the native handle.
 80        /// </summary>
 81        /// <returns>True if the handle was released successfully.</returns>
 82        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
 83        protected override bool ReleaseHandle()
 1030384        {
 1030385            if (_disposed || IsInvalid)
 086                return true;
 87
 1030388            _disposed = true;
 1030389            var dropped = NativeMethods.DropHandle(_handleId);
 1030390            Interlocked.Increment(ref s_released);
 1030391            if (!dropped)
 092                Interlocked.Increment(ref s_failedReleases);
 1030393            return dropped;
 1030394        }
 95
 96        /// <summary>
 97        /// Records that a request handed the handle over to the native library, which now owns it
 98        /// (the incremental read of a data stream reader takes its reader): it counts as released
 99        /// and is never dropped. Only the sole owner of the handle may call it.
 100        /// </summary>
 101        internal void MarkConsumed()
 69102        {
 69103            if (_disposed || IsInvalid || IsClosed)
 0104                return;
 69105            _disposed = true;
 69106            Interlocked.Increment(ref s_released);
 69107            SetHandleAsInvalid();
 69108        }
 109
 110        /// <summary>
 111        /// Creates an FfiHandle from an owned handle proto message.
 112        /// </summary>
 113        /// <param name="id">The handle ID.</param>
 114        /// <returns>A new FfiHandle instance.</returns>
 115        public static FfiHandle FromId(ulong id)
 9132116        {
 9132117            return new FfiHandle(id);
 9132118        }
 119    }
 120
 121    /// <summary>
 122    /// Represents an owned handle that can be converted to an FfiHandle.
 123    /// </summary>
 124    public readonly struct OwnedHandle
 125    {
 126        /// <summary>
 127        /// The handle ID.
 128        /// </summary>
 129        public ulong Id { get; }
 130
 131        /// <summary>
 132        /// Creates a new OwnedHandle.
 133        /// </summary>
 134        /// <param name="id">The handle ID.</param>
 135        public OwnedHandle(ulong id)
 136        {
 137            Id = id;
 138        }
 139
 140        /// <summary>
 141        /// Converts this OwnedHandle to an FfiHandle.
 142        /// </summary>
 143        /// <returns>A new FfiHandle instance.</returns>
 144        public FfiHandle ToFfiHandle()
 145        {
 146            return new FfiHandle(Id);
 147        }
 148    }
 149}