< Summary

Information
Class: LiveKit.Rtc.RpcInvocationData
Assembly: LivekitRtc
File(s): /home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitRtc/Rpc.cs
Line coverage
100%
Covered lines: 12
Uncovered lines: 0
Coverable lines: 12
Total lines: 237
Line coverage: 100%
Branch coverage
50%
Covered branches: 3
Total branches: 6
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%66100%

File(s)

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

#LineLine coverage
 1// author: https://github.com/pabloFuente
 2
 3using System;
 4using System.Collections.Generic;
 5using System.Threading.Tasks;
 6
 7namespace LiveKit.Rtc
 8{
 9    /// <summary>
 10    /// Data passed to method handler for incoming RPC invocations.
 11    /// </summary>
 12    public class RpcInvocationData
 13    {
 14        /// <summary>
 15        /// Gets the unique request ID. Will match at both sides of the call, useful for debugging or logging.
 16        /// </summary>
 17        public string RequestId { get; }
 18
 19        /// <summary>
 20        /// Gets the unique participant identity of the caller.
 21        /// </summary>
 22        public string CallerIdentity { get; }
 23
 24        /// <summary>
 25        /// Gets the payload of the request. User-definable format, typically JSON.
 26        /// </summary>
 27        public string Payload { get; }
 28
 29        /// <summary>
 30        /// Gets the maximum time the caller will wait for a response in seconds.
 31        /// </summary>
 32        public double ResponseTimeout { get; }
 33
 34        /// <summary>
 35        /// Initializes a new RpcInvocationData.
 36        /// </summary>
 37        /// <param name="requestId">The request ID.</param>
 38        /// <param name="callerIdentity">The caller identity.</param>
 39        /// <param name="payload">The payload.</param>
 40        /// <param name="responseTimeout">The response timeout in seconds.</param>
 6441        public RpcInvocationData(
 6442            string requestId,
 6443            string callerIdentity,
 6444            string payload,
 6445            double responseTimeout
 6446        )
 6447        {
 6448            RequestId = requestId ?? throw new ArgumentNullException(nameof(requestId));
 6449            CallerIdentity =
 6450                callerIdentity ?? throw new ArgumentNullException(nameof(callerIdentity));
 6451            Payload = payload ?? string.Empty;
 52            ResponseTimeout = responseTimeout;
 6453        }
 54    }
 55
 56    /// <summary>
 57    /// RPC error codes.
 58    /// </summary>
 59    public enum RpcErrorCode
 60    {
 61        /// <summary>
 62        /// Application error in method handler.
 63        /// </summary>
 64        ApplicationError = 1500,
 65
 66        /// <summary>
 67        /// Connection timeout.
 68        /// </summary>
 69        ConnectionTimeout = 1501,
 70
 71        /// <summary>
 72        /// Response timeout.
 73        /// </summary>
 74        ResponseTimeout = 1502,
 75
 76        /// <summary>
 77        /// Recipient disconnected.
 78        /// </summary>
 79        RecipientDisconnected = 1503,
 80
 81        /// <summary>
 82        /// Response payload too large.
 83        /// </summary>
 84        ResponsePayloadTooLarge = 1504,
 85
 86        /// <summary>
 87        /// Failed to send.
 88        /// </summary>
 89        SendFailed = 1505,
 90
 91        /// <summary>
 92        /// Method not supported at destination.
 93        /// </summary>
 94        UnsupportedMethod = 1400,
 95
 96        /// <summary>
 97        /// Recipient not found.
 98        /// </summary>
 99        RecipientNotFound = 1401,
 100
 101        /// <summary>
 102        /// Request payload too large.
 103        /// </summary>
 104        RequestPayloadTooLarge = 1402,
 105
 106        /// <summary>
 107        /// RPC not supported by server.
 108        /// </summary>
 109        UnsupportedServer = 1403,
 110
 111        /// <summary>
 112        /// Unsupported RPC version.
 113        /// </summary>
 114        UnsupportedVersion = 1404,
 115    }
 116
 117    /// <summary>
 118    /// Specialized error handling for RPC methods.
 119    /// Instances of this type, when thrown in a method handler, will have their message
 120    /// serialized and sent across the wire.
 121    /// </summary>
 122    public class RpcError : Exception
 123    {
 124        private static readonly Dictionary<RpcErrorCode, string> ErrorMessages = new Dictionary<
 125            RpcErrorCode,
 126            string
 127        >
 128        {
 129            { RpcErrorCode.ApplicationError, "Application error in method handler" },
 130            { RpcErrorCode.ConnectionTimeout, "Connection timeout" },
 131            { RpcErrorCode.ResponseTimeout, "Response timeout" },
 132            { RpcErrorCode.RecipientDisconnected, "Recipient disconnected" },
 133            { RpcErrorCode.ResponsePayloadTooLarge, "Response payload too large" },
 134            { RpcErrorCode.SendFailed, "Failed to send" },
 135            { RpcErrorCode.UnsupportedMethod, "Method not supported at destination" },
 136            { RpcErrorCode.RecipientNotFound, "Recipient not found" },
 137            { RpcErrorCode.RequestPayloadTooLarge, "Request payload too large" },
 138            { RpcErrorCode.UnsupportedServer, "RPC not supported by server" },
 139            { RpcErrorCode.UnsupportedVersion, "Unsupported RPC version" },
 140        };
 141
 142        /// <summary>
 143        /// Gets the error code.
 144        /// </summary>
 145        public int Code { get; }
 146
 147        /// <summary>
 148        /// Gets optional additional data associated with the error (JSON recommended).
 149        /// </summary>
 150        public string? RpcData { get; }
 151
 152        /// <summary>
 153        /// Creates an error object with the given code and message, plus an optional data payload.
 154        /// </summary>
 155        /// <param name="code">Your error code (Error codes 1001-1999 are reserved for built-in errors).</param>
 156        /// <param name="message">A readable error message.</param>
 157        /// <param name="data">Optional additional data associated with the error (JSON recommended).</param>
 158        public RpcError(int code, string message, string? data = null)
 159            : base(message)
 160        {
 161            Code = code;
 162            RpcData = data;
 163        }
 164
 165        /// <summary>
 166        /// Creates an error from an error code enum.
 167        /// </summary>
 168        /// <param name="code">The error code.</param>
 169        /// <param name="message">Optional custom message.</param>
 170        /// <param name="data">Optional additional data.</param>
 171        public RpcError(RpcErrorCode code, string? message = null, string? data = null)
 172            : base(message ?? GetDefaultMessage(code))
 173        {
 174            Code = (int)code;
 175            RpcData = data;
 176        }
 177
 178        /// <summary>
 179        /// Creates a built-in error from an error code.
 180        /// </summary>
 181        /// <param name="code">The error code.</param>
 182        /// <param name="data">Optional additional data.</param>
 183        /// <returns>A new RpcError instance.</returns>
 184        public static RpcError BuiltIn(RpcErrorCode code, string? data = null)
 185        {
 186            return new RpcError(code, null, data);
 187        }
 188
 189        /// <summary>
 190        /// Creates an RpcError from a proto message.
 191        /// </summary>
 192        /// <param name="proto">The proto error.</param>
 193        /// <returns>A new RpcError instance.</returns>
 194        internal static RpcError FromProto(LiveKit.Proto.RpcError proto)
 195        {
 196            // The FFI always sets the field, as "" when the remote sent no data
 197            var data = proto.HasData && proto.Data.Length > 0 ? proto.Data : null;
 198            return new RpcError((int)proto.Code, proto.Message, data);
 199        }
 200
 201        /// <summary>
 202        /// Converts to a proto message.
 203        /// </summary>
 204        /// <returns>The proto error.</returns>
 205        internal LiveKit.Proto.RpcError ToProto()
 206        {
 207            var error = new LiveKit.Proto.RpcError
 208            {
 209                Code = (uint)Code,
 210                Message = Message ?? string.Empty,
 211            };
 212
 213            if (RpcData != null)
 214            {
 215                error.Data = RpcData;
 216            }
 217
 218            return error;
 219        }
 220
 221        private static string GetDefaultMessage(RpcErrorCode code)
 222        {
 223            if (ErrorMessages.TryGetValue(code, out var message))
 224            {
 225                return message;
 226            }
 227            return "Unknown error";
 228        }
 229    }
 230
 231    /// <summary>
 232    /// Delegate for RPC method handlers.
 233    /// </summary>
 234    /// <param name="data">The invocation data.</param>
 235    /// <returns>The response payload as a string.</returns>
 236    public delegate Task<string> RpcMethodHandler(RpcInvocationData data);
 237}