Ejemplo n.º 1
0
        public static void LoadUsers(SqlDataReader reader)
        {
            while (reader.Read())
            {
                User user = new User();

                user.UserID = (int)reader["UserID"];
                user.NickName = reader["NickName"].ToString();
                user.Password = reader["Pass"].ToString();
                user.Ban = (DateTime)reader["Ban"];
                user.Status = (int)Statuses.unchanged;

                users.Add(user);
            }
        }
Ejemplo n.º 2
0
 public MessageQueue(ChatInfoDAL.User us, string mess)
 {
     User    = us;
     Message = mess;
 }
        void Listen_Reg(object reg_socket)
        {
            Console.WriteLine("Reg Connect");
            Socket connection_socket = reg_socket as Socket;
            if (connection_socket == null)
                return;

            byte[] regInfo = new byte[1024];
            ////int byteCount = client_socket.Receive(firstAnswer);
            ////Stream stream = new MemoryStream(firstAnswer);
            Stream stream = new MemoryStream();
            IFormatter formatter = new BinaryFormatter();

            int resCount = -1;
            connection_socket.ReceiveTimeout = 500;
            while (resCount != 0)
            {
                try
                {
                    resCount = connection_socket.Receive(regInfo);
                    stream.Write(regInfo, 0, resCount);
                }
                catch (SocketException exp) { resCount = 0; }
            }
            //stream.Flush();
            stream.Position = 0;
            if (stream.Length > 0)
            {
                RegInfo reg = (RegInfo)(formatter.Deserialize(stream));
                Console.WriteLine(reg.Name);
                if (reg.Pass1 != reg.Pass2)
                {
                    byte[] answer = Encoding.UTF8.GetBytes("Пароли не совпадают");
                    connection_socket.Send(answer);
                }
                else
                {
                    string query = string.Format("select * from Users where NickName='{0}'", reg.Name);
                    SqlCommand command = new SqlCommand(query, DataBase.Sql);
                    SqlDataReader reader = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        byte[] answer = Encoding.UTF8.GetBytes("Пользователь уже существует");
                        connection_socket.Send(answer);
                        reader.Close();
                    }
                    else
                    {
                        reader.Close();
                        string queryIns = string.Format("INSERT INTO Users (NickName, Pass, Ban) VALUES ('{0}', {1}, '1900-01-01')", reg.Name, reg.Pass1);
                        SqlCommand commandIns = new SqlCommand(queryIns, DataBase.Sql);
                        commandIns.ExecuteNonQuery();

                        string queryNewID = string.Format("select IDENT_CURRENT('Users') as ID_cur");
                        SqlCommand commandNewID = new SqlCommand(queryNewID, DataBase.Sql);
                        SqlDataReader readerNewID  = commandNewID.ExecuteReader();
                        readerNewID.Read();
                        //Console.WriteLine(readerNewID["ID_cur"]);
                        int id = int.Parse(readerNewID["ID_cur"].ToString());
                        readerNewID.Close();

                        ChatInfoDAL.User newUser = new ChatInfoDAL.User(id, reg.Name, reg.Pass1, new DateTime(1900, 1, 1));
                        ChatInfoDAL.UserManager.Users.Add(newUser);

                        string queryInsGroup = string.Format("INSERT INTO MemeberOfGroups (GroupID, UserID) VALUES (6, {0})", id);
                        SqlCommand commandInsGroup = new SqlCommand(queryInsGroup, DataBase.Sql);
                        commandInsGroup.ExecuteNonQuery();

                        //Func<ComboBox, string> getGroupValue = GetStringvalueDispatch;
                        //string first = (string)groupCombo.Dispatcher.Invoke(getGroupValue, groupCombo);
                        //string second = (string)BanCombo.Dispatcher.Invoke(getGroupValue, BanCombo);
                        //FillListUser(first, second);

                        ChatInfoDAL.MemberOfGroupManager.MembersOfGroup.Add(new ChatInfoDAL.MemberOfGroup(id, 6));

                        byte[] answer = Encoding.UTF8.GetBytes("Добро пожаловать!");
                        connection_socket.Send(answer);

                        Action<string> action = MessageToInfoStackAdd;
                        string messa = String.Format("{0} зарегестрирован", newUser.NickName);
                        infoStack.Dispatcher.Invoke(action, messa);

                        foreach (Socket sct in dictSocketUser.Keys)
                        {
                            byte[] code = Encoding.UTF8.GetBytes("09");
                            sct.Send(code);

                            IFormatter formatter2 = new BinaryFormatter();
                            Stream stream2 = new MemoryStream();

                            formatter2.Serialize(stream2, newUser);

                            byte[] buffer2 = new byte[1024];
                            stream2.Position = 0;
                            while (stream2.Position < stream2.Length)
                            {
                                int readCount = stream2.Read(buffer2, 0, 1024);
                                sct.Send(buffer2, readCount, 0);
                            }
                        }
                    }

                }
                connection_socket.Close();
            }
            else { connection_socket.Close(); }
        }