示例#1
0
        /// <summary>
        /// 客户登录
        /// </summary>
        public void Login(string userName, string password)
        {
            _LocalUserName = userName;

            // 发送登录消息到服务器
            C2S_LoginMessage loginMsg = new C2S_LoginMessage(userName, password);

            this.SendMessage(loginMsg, _hostPoint);
        }
示例#2
0
        //线程主方法
        private void Run()
        {
            byte[] msgBuffer = null;

            while (true)
            {
                msgBuffer = _server.Receive(ref _remotePoint); //接受消息
                try
                {
                    //将消息转换为对象
                    object msgObject = ObjectSerializer.Deserialize(msgBuffer);
                    if (msgObject == null)
                    {
                        continue;
                    }

                    Type msgType = msgObject.GetType();
                    DoWriteLog("========Recevied:======");
                    DoWriteLog("Message:" + msgType.ToString());
                    DoWriteLog("From:" + _remotePoint.ToString());
                    DoWriteLog("=======================");

                    //新用户登录
                    if (msgType == typeof(C2S_LoginMessage))
                    {
                        C2S_LoginMessage lginMsg = (C2S_LoginMessage)msgObject;
                        DoWriteLog(string.Format("User:'******' Logined!", lginMsg.FromUserName));

                        // 添加用户到列表
                        IPEndPoint userEndPoint = new IPEndPoint(_remotePoint.Address, _remotePoint.Port);
                        User       user         = new User(lginMsg.FromUserName, userEndPoint);
                        _userList.Add(user);

                        this.DoUserChanged(_userList);

                        //通知所有人,有新用户登录
                        S2C_UserAction msgNewUser = new S2C_UserAction(user, UserAction.Login);
                        foreach (User u in _userList)
                        {
                            if (u.UserName == user.UserName) //如果是自己,发送所有在线用户列表
                            {
                                this.SendMessage(new S2C_UserListMessage(_userList), u.NetPoint);
                            }
                            else
                            {
                                this.SendMessage(msgNewUser, u.NetPoint);
                            }
                        }
                    }
                    else if (msgType == typeof(C2S_LogoutMessage))
                    {
                        C2S_LogoutMessage lgoutMsg = (C2S_LogoutMessage)msgObject;
                        DoWriteLog(string.Format("User:'******' Logoff!", lgoutMsg.FromUserName));

                        // 从列表中删除用户
                        User logoutUser = _userList.Find(lgoutMsg.FromUserName);
                        if (logoutUser != null)
                        {
                            _userList.Remove(logoutUser);
                        }

                        this.DoUserChanged(_userList);

                        //通知所有人,有用户登出
                        S2C_UserAction msgNewUser = new S2C_UserAction(logoutUser, UserAction.Logout);
                        foreach (User u in _userList)
                        {
                            this.SendMessage(msgNewUser, u.NetPoint);
                        }
                    }

                    else if (msgType == typeof(C2S_HolePunchingRequestMessage))
                    {
                        //接收到A给B打洞的消息,打洞请求,由客户端发送给服务器端
                        C2S_HolePunchingRequestMessage msgHoleReq = (C2S_HolePunchingRequestMessage)msgObject;

                        User userA = _userList.Find(msgHoleReq.FromUserName);
                        User userB = _userList.Find(msgHoleReq.ToUserName);

                        // 发送打洞(Punching Hole)消息
                        DoWriteLog(string.Format("User:[{0} IP:{1}] want to create session tunnel with [{2} IP:{3}].",
                                                 userA.UserName, userA.NetPoint.ToString(),
                                                 userB.UserName, userB.NetPoint.ToString()));

                        //由Server发送消息给B,将A的IP的IP地址信息告诉B,然后由B发送一个测试消息给A.
                        S2C_HolePunchingMessage msgHolePunching = new S2C_HolePunchingMessage(_remotePoint);
                        this.SendMessage(msgHolePunching, userB.NetPoint); //Server->B
                    }
                    else if (msgType == typeof(C2S_GetUsersMessage))
                    {
                        // 发送当前用户信息
                        S2C_UserListMessage srvResMsg = new S2C_UserListMessage(_userList);
                        this.SendMessage(srvResMsg, _remotePoint);
                    }
                }
                catch (Exception ex)
                {
                    DoWriteLog(ex.Message);
                }
            }
        }