Exemple #1
0
        /// <summary>
        /// Load users from xml file
        /// </summary>
        public void Load()
        {
            lock (_users)
            {
                if (!File.Exists(_fileName))
                {
                    return;
                }

                XmlDocument doc = new XmlDocument();

                doc.Load(_fileName);

                XmlNodeList xmlNodeList = doc.SelectNodes("users/user");

                if (xmlNodeList == null)
                {
                    return;
                }

                foreach (XmlNode selectNode in xmlNodeList)
                {
                    XmlAttribute loginAttribute = selectNode.Attributes?["login"];
                    string       login          = loginAttribute.Value;
                    string       pass           = selectNode.SelectSingleNode("password").InnerText;
                    string       nick           = selectNode.SelectSingleNode("nick").InnerText;
                    Sex          sex;
                    Enum.TryParse(selectNode.SelectSingleNode("sex").InnerText, out sex);
                    DateTime birthdate = DateTime.ParseExact(selectNode.SelectSingleNode("birthdate").InnerText, "dd.MM.yyyy", null);
                    _users[login] = new UserInfoWithPrivateInfo(pass, nick, sex, birthdate);
                }
            }
        }
Exemple #2
0
 public Task <RequestToServerResult> UpdateUser(string login, UserInfoWithPrivateInfo userInfoWithPrivateInfo)
 {
     return(Task <RequestToServerResult> .Factory.StartNew(() =>
     {
         new UpdateUserPacket(_streamWriter.BaseStream, login, userInfoWithPrivateInfo).Send();
         lock (_users)
         {
             _users[Login] = userInfoWithPrivateInfo;
         }
         _context.Post(p => UserUpdated?.Invoke(this, EventArgs.Empty), null);
         return RequestToServerResult.OK;
     }));
 }
Exemple #3
0
        public bool TryGetUserInfoWithPrivateInfo(string login, out UserInfoWithPrivateInfo userInfoWithPrivateInfo)
        {
            if (string.IsNullOrWhiteSpace(login))
            {
                userInfoWithPrivateInfo = null;
                return(false);
            }

            lock (_users)
            {
                return(_users.TryGetValue(login, out userInfoWithPrivateInfo));
            }
        }
Exemple #4
0
 public void Update(string login, UserInfoWithPrivateInfo userInfoWithPrivateInfo)
 {
     lock (_users)
     {
         XElement doc   = XElement.Load(_fileName);
         var      items = from item in doc.Descendants("user")
                          where item.Attribute("login").Value == login
                          select item;
         XElement xElement = items.First();
         xElement.Element("password").ReplaceNodes(new XCData(userInfoWithPrivateInfo.Password));
         xElement.Element("nick").ReplaceNodes(new XCData(userInfoWithPrivateInfo.Nick));
         xElement.Element("sex").ReplaceNodes(new XCData(userInfoWithPrivateInfo.Sex.ToString()));
         xElement.Element("birthdate").ReplaceNodes(new XCData(userInfoWithPrivateInfo.Birthdate.ToString("dd.MM.yyyy")));
         doc.Save(_fileName);
     }
 }
Exemple #5
0
 public void Add(string login, UserInfoWithPrivateInfo userInfoWithPrivateInfo)
 {
     lock (_users)
     {
         _users.Add(login, userInfoWithPrivateInfo);
         XDocument doc  = XDocument.Load(_fileName);
         XElement  user = new XElement("user");
         user.Add(new XAttribute("login", login));
         user.Add(new XElement("password", new XCData(userInfoWithPrivateInfo.Password)));
         user.Add(new XElement("nick", new XCData(userInfoWithPrivateInfo.Nick)));
         user.Add(new XElement("sex", new XCData(userInfoWithPrivateInfo.Sex.ToString())));
         user.Add(new XElement("birthdate", new XCData(userInfoWithPrivateInfo.Birthdate.ToString("dd.MM.yyyy"))));
         doc.Element("users")?.Add(user);
         doc.Save(_fileName);
     }
 }
Exemple #6
0
 public Task <RequestToServerResult> RegisterNewUser(string login, UserInfoWithPrivateInfo userInfoWithPrivateInfo)
 {
     return(Task <RequestToServerResult> .Factory.StartNew(() =>
     {
         if (IsConnected)
         {
             _registerEvents[0].Reset();
             _registerEvents[1].Reset();
             new RegistryPacket(_streamWriter.BaseStream, login, userInfoWithPrivateInfo).Send();
             var result = WaitHandle.WaitAny(_registerEvents, 60000);
             if (result == 0)
             {
                 return RequestToServerResult.OK;
             }
             else
             {
                 return RequestToServerResult.Failed;
             }
         }
         return RequestToServerResult.Disconnect;
     }));
 }