Beispiel #1
0
            public CommunicatorItem addchild(string key)
            {
                var ci = new CommunicatorItem(socket, password);

                ci.path = path + "/" + key;
                items.Add(key, ci);
                return(ci);
            }
Beispiel #2
0
 public static CommunicatorItem FindNotReady(CommunicatorItem item)
 {
     if (!item.IsReady)
     {
         return(item);
     }
     else
     {
         foreach (var ci in item.items)
         {
             CommunicatorItem nr = FindNotReady(ci.Value);
             if (nr != null)
             {
                 return(nr);
             }
         }
     }
     return(null);
 }
Beispiel #3
0
        public void listen(int Port)
        {
            listening = true;
            new Task(() =>
            {
                IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

                TcpListener listener = new TcpListener(ipAddress, Port);

                listener.Start();
                while (listening)
                {
                    Socket client = listener.AcceptSocket();

                    var childSocketThread = new Thread(() =>
                    {
                        string key = RandomString(12);
                        CommunicatorConnection Connection = new CommunicatorConnection();
                        Connection.iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                        connections.Add(key, Connection);
                        int connectionsindex = connections.Count - 1;

                        CommunicatorItem mainitem = new CommunicatorItem(client, options.password);
                        byte[] data = new byte[8192];
                        string res  = "";

                        res      = "";
                        int size = client.Receive(data);

                        for (int i = 0; i < size; i++)
                        {
                            res += Convert.ToChar(data[i]);
                        }

                        res = Connection.Decrypt(res, options.password, false);

                        //Console.WriteLine(res);
                        CommunicatorItem notreadyitem = FindNotReady(mainitem);
                        do
                        {
                            notreadyitem.getitem(Connection);
                            notreadyitem = FindNotReady(mainitem);
                        }while (notreadyitem != null);
                        data           = null;
                        dynamic result = mainitem.ToObject();
                        for (int i = 0; i < Callbacks.Count; i++)
                        {
                            if (Callbacks.ElementAt(i).Key == ((IDictionary <string, object>)result)["type"].ToString())
                            {
                                Action <object> cb = null;
                                Callbacks.TryGetValue((string)((IDictionary <string, object>)result)["type"], out cb);

                                cb((ExpandoObject)result.val);
                            }
                        }
                        client.Send(Encoding.UTF8.GetBytes(Connection.Encrypt("close", options.password, false)));
                        client.Close();
                        client.Dispose();

                        connections.Remove(key);
                    });
                    childSocketThread.Start();
                }
            }).Start();
        }