Ejemplo n.º 1
0
 public object ConvertTo(Type t)
 {
     if (t == typeof(Func <object, Task <object> >) || t == typeof(Func <object[], Task <object> >))
     {
         if (dictionary.ContainsKey("rpa_function") && (bool)dictionary["rpa_function"])
         {
             if (t == typeof(Func <object, Task <object> >))
             {
                 return(new Func <object, Task <object> >((object arg) =>
                 {
                     return RemoteSender.Invoke(server, socket, (string)dictionary["rpa_id"], new object[] { arg });
                 }));
             }
             else if (t == typeof(Func <object[], Task <object> >))
             {
                 return(new Func <object[], Task <object> >((object[] args) =>
                 {
                     return RemoteSender.Invoke(server, socket, (string)dictionary["rpa_id"], args);
                 }));
             }
         }
         else
         {
             var ex = new RemoteException("Cannot cast this object as a Function");
             ex.Code = "INVALID_CAST_EXCEPTION";
             throw ex;
         }
     }
     else if (t == typeof(Dictionary <string, object>))
     {
         return(dictionary);
     }
     return(this);
 }
Ejemplo n.º 2
0
        public async Task AttachDisconnect(CrossSocket client)
        {
            await client.WaitDisconnect();

            var store = GetStoreForSocket(client, false);

            if (store != null)
            {
                var tasks = store.tasks;
                if (tasks != null)
                {
                    var ex = new RemoteException("RPA connection was destroyed");
                    ex.Code = "RPA_DESTROYED";
                    foreach (var Item in tasks)
                    {
                        Item.Value.SetException(ex);
                    }
                    store.tasks = null;
                }

                if (store.refs != null)
                {
                    foreach (var Item in store.refs)
                    {
                        int count = Item.Value;
                        for (int i = 0; i < count; i++)
                        {
                            this.UnRef(Item.Key, client);
                        }
                    }
                    store.refs = null;
                }
                socketStore.Remove(client);
            }
        }
Ejemplo n.º 3
0
        public async Task <CrossSocket> Create()
        {
            if (!IsUnix)
            {
                var csocket = new CrossSocket();
                csocket.pipeid = GetSha1Id();
                return(csocket);
            }
            else
            {
                var home = Environment.GetEnvironmentVariable("HOME");
                if (!((new DirectoryInfo(home)).Exists))
                {
                    home = "/Users/" + Environment.GetEnvironmentVariable("USER");
                }
                var kawi    = home + "/.kawi";
                var kawidir = new DirectoryInfo(kawi);
                if (!kawidir.Exists)
                {
                    kawidir.Create();
                }
                kawi    = kawi + "/rpa";
                kawidir = new DirectoryInfo(kawi);
                if (!kawidir.Exists)
                {
                    kawidir.Create();
                }

                var socket       = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
                var file         = kawi + "/" + GetSha1Id();
                var unixEndPoint = new UnixEndPoint(file);
                //Console.WriteLine(file);


                try{
                    socket.Bind(unixEndPoint);
                    socket.Listen(100);
                }catch (Exception) {
                    bool isactive = await this.IsActive();

                    if (!isactive)
                    {
                        (new FileInfo(file)).Delete();
                        socket.Bind(unixEndPoint);
                        socket.Listen(100);
                    }
                    else
                    {
                        var ex = new RemoteException("RPA cannot register, id " + id + " is already used");
                        ex.Code = "RPA_ID_USED";
                        throw ex;
                    }
                }

                var csocket = new CrossSocket();
                csocket.socket = socket;
                return(csocket);
            }
        }
Ejemplo n.º 4
0
        public Type GetType(object def)
        {
            if (def is string)
            {
                Type type = null;
                if (loadedTypes.TryGetValue(def.ToString(), out type))
                {
                    return(type);
                }


                var ex = new RemoteException("Type " + def.ToString() + " was not found");
                ex.Code = "TYPE_NOT_FOUND";
                throw ex;
            }
            else
            {
                if (def is IDictionary <string, object> )
                {
                    var  dict = (IDictionary <string, object>)def;
                    var  ts   = dict["type"];
                    Type t    = GetType(ts);
                    if (dict.ContainsKey("generic"))
                    {
                        System.Collections.IList generic = (System.Collections.IList)dict["generic"];
                        Type[] rgeneric = new Type[generic.Count];
                        for (int i = 0; i < generic.Count; i++)
                        {
                            rgeneric[i] = GetType(generic[i]);
                        }
                        t = t.MakeGenericType(rgeneric);
                    }
                    else
                    {
                        if (t.GetGenericArguments().Length > 0)
                        {
                            var ex = new RemoteException("You need pass generic type arguments");
                            ex.Code = "INVALID_ARGUMENTS";
                            throw ex;
                        }
                    }
                    return(t);
                }
                return(null);
            }
        }
Ejemplo n.º 5
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
            }
        }