示例#1
0
 public void UpdateUser(User user)
 {
     using (SQLiteConnection con = fac.GetConnection(path))
     {
         con.Open();
         SQLiteCommand cmd = fac.GetCommand("update users_table set UserName=@uname,NickName=@nickname,PassWord=@password,Phone=@phone,Address=@address where ID=@id", new { password = user.PassWord, uname = user.UserName, id = user.ID, nickname = user.NickName, phone = user.Phone, address = user.Address }, con);
         cmd.ExecuteNonQuery();
         con.Close();
     }
 }
示例#2
0
 public void Registy(User newuser)
 {
     using (SQLiteConnection con = fac.GetConnection(path))
     {
         con.Open();
         SQLiteCommand cmd = fac.GetCommand("insert into  users_table(ID,UserName,NickName,PassWord,Phone,Address) values(@id,@uname,@nickname,@password,@phone,@address)", new { password = newuser.PassWord, uname = newuser.UserName, id = newuser.ID, nickname = newuser.NickName, phone = newuser.Phone, address = newuser.Address }, con);
         try
         {
             cmd.ExecuteNonQuery();
         }
         catch (Exception ex)
         {
             con.Close();
             //throw new Exception("经过进一步包装的异常", ex);
             throw ex;
         }
         con.Close();
     }
 }
示例#3
0
        public IList<User> GetAllUsers()
        {
            List<User> lis = new List<User>();
            using (SQLiteConnection con = fac.GetConnection(path))
            {
                SQLiteCommand cmd = fac.GetCommand("select * from users_table", null, con);
                con.Open();
                var dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    var user = new User();
                    user.ID = new Guid(dr["ID"].ToString());
                    user.UserName = dr["UserName"].ToString();
                    user.NickName = dr["NickName"].ToString();
                    user.PassWord = dr["PassWord"].ToString();
                    user.Address = dr["Address"].ToString();
                    user.Phone = dr["Phone"].ToString();
                    lis.Add(user);
                }
                dr.Close();
            }
            return lis;
        }
示例#4
0
        public bool ValidateUser(string uname, string password, out User user)
        {
            using (SQLiteConnection con = fac.GetConnection(path))
            {
                con.Open();
                SQLiteCommand cmd = fac.GetCommand("select * from users_table where UserName=@uname and PassWord=@pword", new { uname = uname, pword = password }, con);
                var dr = cmd.ExecuteReader(CommandBehavior.SingleRow);
                if (dr.Read())
                {
                    user = new User();
                    user.ID = new Guid(dr["ID"].ToString());
                    user.UserName = dr["UserName"].ToString();
                    user.NickName = dr["NickName"].ToString();
                    user.PassWord = dr["PassWord"].ToString();
                    user.Address = dr["Address"].ToString();
                    user.Phone = dr["Phone"].ToString();
                    dr.Close();
                    return true;
                }
                else
                {
                    user = null;
                    return false;
                }

            }
        }
示例#5
0
        static void Main(string[] args)
        {
            UserDAL userdal = new UserDAL();
            userdal.Path = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName, "serverdb.db");
            List<User> loginUsers = new List<User>();
            List<Socket> clients = new List<Socket>();
            List<User> cacheUser = new List<User>(userdal.GetAllUsers());
            Func<string, bool> isOnline = u => loginUsers.Any(p => p.UserName == u);
            Dictionary<string, Socket> UserSocketMap = new Dictionary<string, Socket>();
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            EndPoint ip = new IPEndPoint(IPAddress.Loopback, 9000);
            s.Bind(ip);
            s.Listen(100);
            Queue<Unit> que = new Queue<Unit>(10000);

            Thread listen = new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    Socket socket = s.Accept();
                    lock (obj)
                    {
                        clients.Add(socket);
                    }
                }
            }));
            Thread receive = new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    Thread.Sleep(100);
                    lock (obj)
                    {
                        foreach (Socket sss in clients)
                        {
                            Request request = sss.SocketGet<Request>();
                            if (request == null) continue;
                            que.Enqueue(new Unit()
                            {
                                request = request,
                                socket = sss
                            });
                        }
                    }
                }
            }));

            #region  处理函数
            Action<Request, Socket> act1 = (request, sss) =>
               {
                   if (request == null) return;
                   switch (request.ProcessType)
                   {
                       case ProcessType.Chatting:
                           #region 聊天
                           if (isOnline(request.TargetUserName))
                           {
                               Socket ss = UserSocketMap[request.TargetUserName];
                               ss.SocketSend(new Response() { ProcessType = ProcessType.Chatting, Data = request, Success = true });
                           }
                           #endregion
                           break;
                       case ProcessType.AddNewFriend:
                           #region 添加好友
                           if (isOnline(request.TargetUserName))
                           {

                               if (request.Data is NewFriendResponse)
                               {
                                   var re = request.Data as NewFriendResponse;
                                   if (re.Accept)
                                   {
                                       var resp = new Response();
                                       var u = loginUsers.Single(p => p.UserName == request.TargetUserName);
                                       StringBuilder sb = new StringBuilder();
                                       sb.Append(u.UserName).Append(";").Append(u.NickName);
                                       resp.ProcessType = ProcessType.UpdateFriendInfo;
                                       resp.Data = sb.ToString();
                                       resp.Success = true;
                                       sss.SocketSend(resp);
                                   }
                               }
                               Socket ss = UserSocketMap[request.TargetUserName];
                               ss.SocketSend(new Response() { ProcessType = ProcessType.AddNewFriend, Data = request, Success = true });
                           }
                           else
                           {
                               sss.SocketSend(new Response() { ProcessType = ProcessType.AddNewFriend, Data = "对方不在线", Success = false });
                           }
                           #endregion
                           break;
                       case ProcessType.DeleteFriend:
                           var res = new Response() { Data = request, ProcessType = ProcessType.DeleteFriend, Success = true };
                           if (isOnline(request.TargetUserName))
                           {
                               var p = UserSocketMap[request.TargetUserName];
                               p.SocketSend(res);
                           }
                           break;
                       case ProcessType.Login:
                           #region 登录
                           string password = request.Data.ToString();
                           User user = null;
                           string uname = request.SourceUserName;
                           if (cacheUser.Any(p => p.UserName == uname && p.PassWord == password))
                           {
                               user = cacheUser.Single(p => p.UserName == uname && p.PassWord == password);
                           }
                           if (user != null)
                           {
                               lock (obj)
                               {
                                   if (!isOnline(user.UserName))
                                   {
                                       loginUsers.Add(user);
                                       try
                                       {
                                           UserSocketMap.Add(user.UserName, sss);
                                       }
                                       catch
                                       {

                                       }

                                       sss.SocketSend(new Response() { ProcessType = ProcessType.Login, Success = true, Data = user });
                                   }
                                   else
                                   {
                                       sss.SocketSend(new Response() { ProcessType = ProcessType.Login, Success = false, Data = "已经登陆" });
                                   }
                               }
                           }
                           else
                           {
                               sss.SocketSend(new Response() { ProcessType = ProcessType.Login, Success = false, Data = "用户名密码错误" });
                           }
                           #endregion
                           break;
                       case ProcessType.Logout:
                           break;
                       case ProcessType.UpdateUserInfo:
                           #region 更新信息
                           var usup = request.Data as User;
                           User usold = null;
                           usold = loginUsers.Single(p => p.UserName == request.SourceUserName);
                           if (usold == null)
                               break;
                           usold.NickName = usup.NickName;
                           usold.Phone = usup.Phone;
                           usold.Address = usup.Address;
                           userdal.UpdateUser(usold);
                           #endregion
                           break;
                       case ProcessType.UpdateFriendInfo:
                           #region 更新好友信息
                           {
                               var resp = new Response();
                               resp.ProcessType = ProcessType.UpdateFriendInfo;

                               if (loginUsers.Any(p => p.UserName == request.TargetUserName))
                               {
                                   var u = loginUsers.Single(p => p.UserName == request.TargetUserName);
                                   StringBuilder sb = new StringBuilder();
                                   sb.Append(u.UserName).Append(";").Append(u.NickName);
                                   resp.Data = sb.ToString();
                                   resp.Success = true;
                               }
                               else
                               {
                                   resp.Data = "无此人信息";
                                   resp.Success = false;
                               }
                               Socket ups = UserSocketMap[request.SourceUserName];
                               ups.SocketSend(resp);
                           }
                           #endregion
                           break;
                       case ProcessType.Register:
                           #region 注册
                           string newuname = request.SourceUserName;
                           string pswd = request.Data.ToString();
                           if (cacheUser.Any(p => p.UserName == newuname))
                           {

                               sss.SocketSend(new Response() { Success = false, Data = "用户名已经存在了!", ProcessType = ProcessType.Register });
                               break;
                           }
                           if (!userdal.UserNameExist(newuname))
                           {
                               var newuser = new User() { UserName = newuname, PassWord = pswd };
                               userdal.Registy(newuser);
                               cacheUser.Add(newuser);
                               sss.SocketSend(new Response() { Success = true, Data = newuser, ProcessType = ProcessType.Register });
                           }
                           else
                           {
                               sss.SocketSend(new Response() { Success = false, Data = "用户名已经存在了!", ProcessType = ProcessType.Register });
                           }
                           break;
                       case ProcessType.File:
                           if (loginUsers.Any(p => p.UserName == request.TargetUserName))
                           {
                               Socket filesocket = UserSocketMap[request.TargetUserName];
                               filesocket.SocketSend(new Response() { Success = true, Data = request.Data, ProcessType = ProcessType.File });

                           }
                           #endregion
                           break;
                       case ProcessType.GroupChatting:
                           #region 群聊
                           string sc = request.SourceUserName;
                           string[] tars = request.TargetUserName.Split('#');
                           string ct = request.Data.ToString();
                           var req = new Request() { SourceUserName = sc, Data = ct, ProcessType = ProcessType.GroupChatting };
                           var respss = new Response() { Data = req, ProcessType = ProcessType.GroupChatting };
                           foreach (var tar in tars)
                           {
                               if (isOnline(tar))
                               {
                                   Socket st = UserSocketMap[tar];
                                   st.SocketSend(respss);
                               }
                           }
                           break;
                           #endregion
                       default:
                           break;
                   }
               };
            #endregion
            Action act2 = () =>
            {
                while (true)
                {
                    Thread.Sleep(100);
                    lock (obj)
                    {
                        if (que.Count > 0)
                        {
                            var item = que.Dequeue();
                            act1.BeginInvoke(item.request, item.socket, null, null);
                        }

                    }
                }
            };
            listen.Start();
            Console.WriteLine("start");
            listen.IsBackground = true;
            receive.Start();
            Console.WriteLine("listen");
            receive.IsBackground = true;
            Console.WriteLine("process");
            act2.BeginInvoke(null, null);
            Console.ReadLine();
        }