Пример #1
0
        internal void AddFrind(string xmlString)
        {
            XDocument dataXml = XDocument.Parse(xmlString);

            var friend = dataXml.Root;
            XElement address = friend.Element("last_address_used");
            XElement status = friend.Element("status");
            Friend friendItem = new Friend(friend.Attribute("username").Value, address.Attribute("ip_address").Value,
                                           Int32.Parse(address.Attribute("port").Value), status.Attribute("state").Value,
                                           status.Value);
            if (friend.Element("img") != null)
                friendItem.LogoFileName = friend.Element("img").Value;
            friends.Add(friendItem);

            if (Notify != null)
                Notify(2, null);
        }
Пример #2
0
        public int SignIn(string username, string password)
        {
            if (username == null || password == null)
                throw new NullReferenceException("Invalid Username or Password");

            Connect(); //TODO need errors on connection
            //TODO rethink need to return int as response

            Send(ServerOperation.SignIn, username, password, clientPort);

            byte response = ReadResponse();
            Console.WriteLine("Response: " + response);

            if (response != 0)
            {
                //TODO failure
                return response;
            }

            //TODO save status message
            this._username = username;
            //            this._statusMessage = "My Status Message";

            //Read friends string
            int length = ReadInt();

            byte[] ba = new byte[length];
            _connStream.Read(ba, 0, length);

            string tmp = Encoding.UTF8.GetString(ba);
            Console.WriteLine(tmp);
            XDocument dataXml = XDocument.Parse(tmp);

            this._statusMessage = dataXml.Root.Element("myself").Element("status").Value;

            if (dataXml.Root.Element("myself").Element("img")!= null);
            this._logoFileName = dataXml.Root.Element("myself").Element("img").Value;

            friends = new List<Friend>();
            foreach (var friend in dataXml.Root.Element("friends").Elements())
            {
                XElement address = friend.Element("last_address_used");
                XElement status = friend.Element("status");
                Friend friendItem = new Friend(friend.Attribute("username").Value, address.Attribute("ip_address").Value,
                                            Int32.Parse(address.Attribute("port").Value),
                                            status.Attribute("state").Value,
                                            status.Value);
                if (friend.Element("img") != null)
                    friendItem.LogoFileName = friend.Element("img").Value;
                friends.Add(friendItem);
            }

            if (dataXml.Root.Element("offline_messages") != null && dataXml.Root.Element("offline_messages").Elements().Count() > 0)
            {
                new Thread(delegate()
                {
                    Thread.Sleep(100);
                    try
                    {
                        if (ReceiveMessage != null)
                            foreach (var element in dataXml.Root.Element("offline_messages").Elements())
                            {
                                ReceiveMessage(element.Attribute("from").Value, element.Value, DateTime.Parse(element.Attribute("date").Value));
                            }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exc: " + ex);
            //                        throw;
                    }
                }).Start();
            }

            if (dataXml.Root.Element("friend_requests") != null && dataXml.Root.Element("friend_requests").Elements().Count() > 0)
            {
                new Thread(delegate()
                {
                    Thread.Sleep(100);
                    string friend = "";
                    try
                    {
                        foreach (var element in dataXml.Root.Element("friend_requests").Elements())
                        {
                            friend = element.Attribute("username").Value;
                            if (ConfirmFriendRequest != null && !ConfirmFriendRequest(friend, element.Value))
                                DenyFriend(friend);
                            else
                            {
                                AcceptFriend(friend);
                            }
            //                            ReceiveMessage(element.Attribute("from").Value, element.Value, DateTime.Parse(element.Attribute("date").Value));
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exc: " + ex);
                        //                        throw;
                    }
                }).Start();
            }

            //TODO implement
            //            foreach (var message in dataXml.Root.Element("offline_messages").Elements())
            //            {
            //                if (ReceiveMessage != null)
            //                {
            //                    ReceiveMessage(message.Attribute("from").Value, message.Value);
            //                }
            //            }

            return 0;
        }