| | | 1 | | // author: https://github.com/pabloFuente |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | |
| | | 7 | | namespace 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> |
| | | 41 | | public RpcInvocationData( |
| | | 42 | | string requestId, |
| | | 43 | | string callerIdentity, |
| | | 44 | | string payload, |
| | | 45 | | double responseTimeout |
| | | 46 | | ) |
| | | 47 | | { |
| | | 48 | | RequestId = requestId ?? throw new ArgumentNullException(nameof(requestId)); |
| | | 49 | | CallerIdentity = |
| | | 50 | | callerIdentity ?? throw new ArgumentNullException(nameof(callerIdentity)); |
| | | 51 | | Payload = payload ?? string.Empty; |
| | | 52 | | ResponseTimeout = responseTimeout; |
| | | 53 | | } |
| | | 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 | | { |
| | 1 | 124 | | private static readonly Dictionary<RpcErrorCode, string> ErrorMessages = new Dictionary< |
| | 1 | 125 | | RpcErrorCode, |
| | 1 | 126 | | string |
| | 1 | 127 | | > |
| | 1 | 128 | | { |
| | 1 | 129 | | { RpcErrorCode.ApplicationError, "Application error in method handler" }, |
| | 1 | 130 | | { RpcErrorCode.ConnectionTimeout, "Connection timeout" }, |
| | 1 | 131 | | { RpcErrorCode.ResponseTimeout, "Response timeout" }, |
| | 1 | 132 | | { RpcErrorCode.RecipientDisconnected, "Recipient disconnected" }, |
| | 1 | 133 | | { RpcErrorCode.ResponsePayloadTooLarge, "Response payload too large" }, |
| | 1 | 134 | | { RpcErrorCode.SendFailed, "Failed to send" }, |
| | 1 | 135 | | { RpcErrorCode.UnsupportedMethod, "Method not supported at destination" }, |
| | 1 | 136 | | { RpcErrorCode.RecipientNotFound, "Recipient not found" }, |
| | 1 | 137 | | { RpcErrorCode.RequestPayloadTooLarge, "Request payload too large" }, |
| | 1 | 138 | | { RpcErrorCode.UnsupportedServer, "RPC not supported by server" }, |
| | 1 | 139 | | { RpcErrorCode.UnsupportedVersion, "Unsupported RPC version" }, |
| | 1 | 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) |
| | 10 | 159 | | : base(message) |
| | 10 | 160 | | { |
| | | 161 | | Code = code; |
| | | 162 | | RpcData = data; |
| | 10 | 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) |
| | 1 | 172 | | : base(message ?? GetDefaultMessage(code)) |
| | 1 | 173 | | { |
| | | 174 | | Code = (int)code; |
| | | 175 | | RpcData = data; |
| | 1 | 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) |
| | 1 | 185 | | { |
| | 1 | 186 | | return new RpcError(code, null, data); |
| | 1 | 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) |
| | 9 | 195 | | { |
| | | 196 | | // The FFI always sets the field, as "" when the remote sent no data |
| | 9 | 197 | | var data = proto.HasData && proto.Data.Length > 0 ? proto.Data : null; |
| | 9 | 198 | | return new RpcError((int)proto.Code, proto.Message, data); |
| | 9 | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Converts to a proto message. |
| | | 203 | | /// </summary> |
| | | 204 | | /// <returns>The proto error.</returns> |
| | | 205 | | internal LiveKit.Proto.RpcError ToProto() |
| | 2 | 206 | | { |
| | 2 | 207 | | var error = new LiveKit.Proto.RpcError |
| | 2 | 208 | | { |
| | 2 | 209 | | Code = (uint)Code, |
| | 2 | 210 | | Message = Message ?? string.Empty, |
| | 2 | 211 | | }; |
| | | 212 | | |
| | 2 | 213 | | if (RpcData != null) |
| | 1 | 214 | | { |
| | 1 | 215 | | error.Data = RpcData; |
| | 1 | 216 | | } |
| | | 217 | | |
| | 2 | 218 | | return error; |
| | 2 | 219 | | } |
| | | 220 | | |
| | | 221 | | private static string GetDefaultMessage(RpcErrorCode code) |
| | 1 | 222 | | { |
| | 1 | 223 | | if (ErrorMessages.TryGetValue(code, out var message)) |
| | 1 | 224 | | { |
| | 1 | 225 | | return message; |
| | | 226 | | } |
| | 0 | 227 | | return "Unknown error"; |
| | 1 | 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 | | } |