Пример #1
0
        public Object JsonCommand(string cmd, Object parameter)
        {
            if (!_configured)
            {
                return(null);
            }

            var client = new JsonRpcClient {
                Url = GetJsonPath(), Timeout = GetTimeout()
            };
            var creds = GetCredentials();

            if (creds != null)
            {
                client.Credentials = creds;
            }
            object retval   = null;
            var    logparam = "";

            if (parameter != null)
            {
                logparam = parameter.GetType().ToString() == "System.String[]" ? String.Join(",", (string[])parameter) : parameter.ToString();
            }
            try
            {
                lock (Locker)
                {
                    retval = JsonConvert.Import(client.Invoke(cmd, parameter).ToString());
                }
                if (cmd != "JSONRPC.Ping" && cmd != "VideoPlayer.GetTime" && cmd != "System.GetInfoLabels" && cmd != "AudioPlayer.GetTime")
                {
                    Log("JSONCMD : " + cmd + ((parameter == null) ? "" : " - " + logparam));
                }
                else
                {
                    Trace("JSONCMD : " + cmd + ((parameter == null) ? "" : " - " + logparam));
                }
            }
            catch (JsonException e)
            {
                if (cmd != "JSONRPC.Ping")
                {
                    Log("JSONCMD : " + cmd + ((parameter == null) ? "" : " : " + logparam) + " - " + e.Message);
                }
                else
                {
                    Trace("JSONCMD : " + cmd + ((parameter == null) ? "" : " : " + logparam) + " - " + e.Message);
                }
            }
            finally
            {
                client.Dispose();
            }
            if (retval != null)
            {
                Trace(retval.ToString());
            }
            return(retval);
        }
Пример #2
0
 public void Dispose()
 {
     if (!proc.HasExited)
     {
         Shutdown();
         client.Dispose();
         proc.WaitForExit(5000);
         if (proc.HasExited == false)
         {
             logger.Error("Butler didn't shutdown gracefully, going to kill it.");
             proc.Kill();
         }
     }
     else
     {
         client.Dispose();
     }
 }
        /// <summary>
        /// Extension method that Wraps an existing JsonRpcClient object with an explicit
        ///  interface definition (Service Contract)
        /// </summary>
        /// <typeparam name="T">The type of the interface (Service Contract).</typeparam>
        /// <param name="client">A JSON-RPC 2.0 service client</param>
        /// <returns></returns>
        public static T AsServiceContract <T>(this JsonRpcClient client)
            where T : class
        {
            _ = client ?? throw new ArgumentNullException(nameof(client));

            var svcCntr = _svcCtrInfoDict.Value.GetOrAdd(typeof(T), svcCntrType => new Lazy <ServiceContractInfo>(() =>
            {
                if (!svcCntrType.IsInterface)
                {
                    throw new InvalidOperationException($"{svcCntrType.Name}: is not an interface.");
                }

                var res = new ServiceContractInfo
                {
                    OperContracts = svcCntrType.GetMethodsFromInterface().Select(mi => new OperationContractInfo
                    {
                        MethodInfo = mi,
                        MethodName = mi.GetCustomAttribute <AliasAttribute>()?.Name ?? mi.Name,
                        RpcName    = mi.GetCustomAttribute <JsonRpcNameAttribute>()?.Value
                    }).Where(x => x.RpcName != null).ToArray(),
                    Disposable = typeof(IDisposable).IsAssignableFrom(svcCntrType)
                };

                if (res.OperContracts.Count == 0)
                {
                    throw new InvalidOperationException($"{svcCntrType.Name}: no operation contracts found.");
                }

                return(res);
            })).Value;

            IDictionary <string, object> expando = new ExpandoObject();

            foreach (var oc in svcCntr.OperContracts)
            {
                expando.Add(oc.MethodName, GetOrAddMapFunc(oc.MethodInfo, oc.RpcName, client));
            }

            if (svcCntr.Disposable)
            {
                expando.Add("Dispose", new Action(() => client.Dispose()));
            }

            return(expando.ActLike <T>());
        }