Exemplo n.º 1
0
        /// <summary>
        /// Call json-rpc method
        /// </summary>
        /// <param name="returnType"></param>
        /// <param name="request"></param>
        /// <returns>null or object of requested type</returns>
        public object Invoke(Type returnType, IJsonRequest request)
        {
            if (!IsActive)
            {
                return(null);
            }

            if (request == null)
            {
                return(null);
            }

            lock (mSyncInvoke)
            {
                if (mRemoteMethods.Contains(request.Method))
                {
                    try
                    {
                        return(JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                                    returnType,
                                                    request));
                    }
                    catch (Exception)
                    {
                        Dispose();
                        return(null);
                    }
                }

                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Call json-rpc method
        /// </summary>
        /// <param name="request"></param>
        /// <returns>raw json response</returns>
        public string JsonInvoke(IJsonRequest request)
        {
            if (!IsActive)
            {
                return("{\"id\":" + "-" + ",\"error\":{\"name\":\"JSONRPCError\",\"message\":\"Connection disactive\",\"errors\":\"\"}}");
            }

            if (request == null)
            {
                return("{\"id\":" + "-" + ",\"error\":{\"name\":\"JSONRPCError\",\"message\":\"JsonRequest cannot be null\",\"errors\":\"\"}}");
            }

            lock (mSyncInvoke)
            {
                if (mRemoteMethods.Contains(request.Method))
                {
                    try
                    {
                        return((string)JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                                            typeof(JsonBuffer),
                                                            request));
                    }
                    catch (Exception)
                    {
                        Dispose();
                        return("{\"id\":" + request.Id +
                               ",\"error\":{\"name\":\"JSONRPCError\",\"message\":\"Timeout method call\",\"errors\":\"\"}}");
                    }
                }

                return("{\"id\":" + request.Id +
                       ",\"error\":{\"name\":\"JSONRPCError\",\"message\":\"Method unavailable\",\"errors\":\"\"}}");
            }
        }
Exemplo n.º 3
0
        IEnumerable <string> RequestMethodsList()
        {
            if (!IsActive)
            {
                return(null);
            }


            return((string[])JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                                  typeof(string[]),
                                                  new JsonRequest(0, "proxy.methods", null)));
        }
Exemplo n.º 4
0
        string RequestId()
        {
            if (!IsActive)
            {
                return("");
            }


            return((string)JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                                typeof(string),
                                                new JsonRequest(0, "smart.id", null)));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Call json-rpc method
        /// </summary>
        /// <param name="returnType"></param>
        /// <param name="request"></param>
        /// <returns>null or object of requested type</returns>
        public object Invoke(Type returnType, IJsonRequest request)
        {
            if (!IsActive)
            {
                return(null);
            }

            try
            {
                return(JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                            returnType,
                                            request));
            }
            catch (Exception)
            {
                Dispose();
                return(null);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Call json-rpc method
        /// </summary>
        /// <param name="request"></param>
        /// <returns>raw json response</returns>
        public string JsonInvoke(IJsonRequest request)
        {
            if (!IsActive)
            {
                return(null);
            }

            try
            {
                return((string)JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                                    typeof(JsonBuffer),
                                                    request));
            }
            catch (Exception)
            {
                Dispose();
                return("{\"id\":" + request.Id + ",\"error\":{\"name\":\"JSONRPCError\",\"message\":\"Timeout method call\",\"errors\":\"\"}}");
            }
        }
Exemplo n.º 7
0
        public object Invoke(Type returnType, string method, IDictionary args)
        {
            try
            {
                JsonRpcDispatcher internalDispatcher;
                if (mRpcMethods.TryGetValue(method, out internalDispatcher))
                {
                    // internal method
                    return((new ResponseParser(internalDispatcher.Process((new JsonRequest(0, method, args)).ToString()), returnType)).Result());
                }

                // Heardbeat test
                // try to get updated state of connection. It's hack
                try
                {
                    var timeout = GetJsonRpc().WriteTimeout;
                    GetJsonRpc().WriteTimeout = 2;

                    GetJsonRpc().WriteByte(0);
                    GetJsonRpc().WriteByte(0);
                    GetJsonRpc().WriteByte(0);
                    GetJsonRpc().WriteByte(0);
                    GetJsonRpc().WriteByte(0);

                    Thread.Sleep(5);
                    GetJsonRpc().WriteTimeout = timeout;
                }
                catch (IOException)
                {
                    //Debug.WriteLine("remote server connection test");
                }


                // external method
                return(JsonRpcCaller.Invoke(GetJsonRpc(),
                                            returnType,
                                            (++mIdRequest), method, args));
            }
            catch (Exception)
            {
                return(null);
            }
        }