Ejemplo n.º 1
0
        /*
         * public TypeInfo GetTypeDefinition(Type t)
         * {
         *  return ClassWrapper.GetFromType(t);
         * }*/

        public TypeInfo GetTypeDefinition(object def)
        {
            Type t = GetType(def);

            return(ClassWrapper.GetFromType(t));
        }
Ejemplo n.º 2
0
        public async void CommandReceived(CrossSocket client, string text)
        {
            //Console.WriteLine(text);
            Newtonsoft.Json.Linq.JObject command = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(text);

            var answer = command.Property("answer");

            if (answer != null && answer.ToObject <bool>())
            {
                // is an answer ...

                // get the response
                var store = GetStoreForSocket(client, false);
                if (store != null)
                {
                    TaskCompletionSource <object> taskPromise = null;
                    var taskid = command.Value <int>("taskid");
                    if (store.tasks.TryGetValue(taskid, out taskPromise))
                    {
                        store.tasks.Remove(taskid);
                        JObject Result = command.Value <JObject>("result");
                        if (Result != null)
                        {
                            var dict = (IDictionary <string, object>)GetArgument(client, Result);
                            if (dict.ContainsKey("error"))
                            {
                                var error = new RemoteException((string)((IDictionary <string, object>)dict["error"])["message"]);
                                var Ex    = (IDictionary <string, object>)dict["error"];
                                error.Code = (string)Ex["code"];
                                if (Ex.ContainsKey("stack"))
                                {
                                    error.Stack = (string)Ex["stack"];
                                }
                                taskPromise.SetException(error);
                            }
                            else
                            {
                                taskPromise.SetResult(GetArgument(client, Result["data"]));
                            }
                        }
                    }
                }
            }
            else
            {
                object   result = null;
                object[] args2  = null;
                try{
                    string   targetid = command.Property("target").ToObject <string>();
                    object   o        = GetTarget(targetid);
                    TypeInfo t        = null;
                    if (o is TypeInfo)
                    {
                        t = (TypeInfo)o;
                        if (!t.noninstance)
                        {
                            t = ClassWrapper.GetFromObject(o);
                        }
                    }
                    else
                    {
                        t = ClassWrapper.GetFromObject(o);
                    }


                    string method = command.Property("method").ToObject <string>();

                    if (!t.methods.ContainsKey(method))
                    {
                        if (method == "rpa_run")
                        {
                            method = "Invoke";
                            if (!t.methods.ContainsKey(method))
                            {
                                throw new Exception("Method " + method + " not found in " + targetid);
                            }
                        }
                        else
                        {
                            throw new Exception("Method " + method + " not found in " + targetid);
                        }
                    }


                    JArray args1 = (JArray)command["arguments"];
                    args2 = GetArguments(client, args1);
                    if (t.noninstance)
                    {
                        result = t.methods[method].Invoke(null, args2);
                    }
                    else
                    {
                        result = t.methods[method].Invoke(o, args2);
                    }
                    if (result is System.Threading.Tasks.Task)
                    {
                        System.Threading.Tasks.Task task = (System.Threading.Tasks.Task)result;
                        await task;

                        if (task.Exception != null)
                        {
                            SendAnswerError(client, command, task.Exception);
                        }
                        else
                        {
                            Type   taskType = result.GetType();
                            Type[] generic  = taskType.GetGenericArguments();
                            if (generic.Length > 0)
                            {
                                TypeInfo taskTypeInfo = ClassWrapper.GetFromObject(result);
                                result = taskTypeInfo.methods["get_Result"].Invoke(result, new object[] {});
                            }
                            else
                            {
                                result = null;
                            }
                            SendAnswer(client, command, result);
                        }
                    }
                    else
                    {
                        SendAnswer(client, command, result);
                    }
                }catch (Exception e) {
                    SendAnswerError(client, command, e);
                }
                finally{
                    try{
                        if (args2 != null && Autounref)
                        {
                            foreach (object arg in args2)
                            {
                                if (arg is DynamicRemoteObject)
                                {
                                    var remote = (DynamicRemoteObject)arg;
                                    if (remote.preserved <= 0)
                                    {
                                        await remote.UnRef();
                                    }
                                }
                            }
                        }
                    }catch (Exception e) {
                        Console.WriteLine("Failed unref: " + e.Message);
                    }
                }
                //Console.WriteLine(result);
                // send the result
            }
        }
Ejemplo n.º 3
0
 public TypeInfo GetTypeDefinition(Type t, Type[] generic)
 {
     return(ClassWrapper.GetFromType(t.MakeGenericType(generic)));
 }