/// <summary> /// 客户登录 /// </summary> public void Login(string userName, string password) { _LocalUserName = userName; // 发送登录消息到服务器 C2S_LoginMessage loginMsg = new C2S_LoginMessage(userName, password); SendMessage(loginMsg, _hostPoint); }
/// <summary> /// 验证信息 登陆 /// 这是我们规定的硬登陆 无论服务器有无应用层的现场关闭否,都将覆盖之 /// </summary> /// <returns></returns> public async Task OnAuthByLogin(C2S_LoginMessage message, Action <S2C_LoginMessage> reply) { string account = message.Account; string password = message.Password; S2C_LoginMessage response; S2C_LoginMessage.Types.State result = S2C_LoginMessage.Types.State.Ok; //设置现场状态 if (m_csm.SetStateCheck(PlayerContextStateMachine.EventOnAuthLoginReq) == -1) { Log.Trace("OnAuthByLogin::PlayerContextStateMachine switch Fail to LoginReq "); result = S2C_LoginMessage.Types.State.Fail; //PostLocalMessage(new LocalMessageShutdownContext { m_shutdownRightNow = true }); goto RETURN; } // 进行数据库验证 var authDB = await ValidateAuthLogin(account, password); //验证失败则通知客户端 if (!authDB) { //设置现场状态 if (m_csm.SetStateCheck(PlayerContextStateMachine.EventOnAuthLoginFail) == -1) { Log.Info("OnAuthByLogin::PlayerContextStateMachine switch Fail to LoginFail"); return; } Log.Info(account + " 验证失败"); result = S2C_LoginMessage.Types.State.Fail; goto RETURN; } //获取数据库player的实体 m_gameServerDBPlayer = await GetPlayerFromDBAsync(account); if (m_gameServerDBPlayer == null) { Log.Info("gameServerDBPlayer is NULL"); result = S2C_LoginMessage.Types.State.Fail; goto RETURN; } //DB的唯一标识符代表着这个玩家应用层的Id m_gameUserId = m_gameServerDBPlayer.userName; //将玩家现场注册到应用层上 var brpc = GameServer.Instance.PlayerCtxManager.RegisterPlayerContextByString(m_gameUserId, this); //表示已有旧的玩家现场在服务器中 需要对旧玩家现场的状态进行迁移和销毁 if (!brpc) { var oldCtx = GameServer.Instance.PlayerCtxManager.FindPlayerContextByString(m_gameUserId); if (oldCtx != null) { oldCtx.PostLocalMessage(new LocalMessageShutdownContext { m_shutdownRightNow = true }); var loginMsg = "用户已登录,服务器关闭旧用户,请重新登录"; result = S2C_LoginMessage.Types.State.Fail; Log.Debug("userName = "******"\nMessage = " + loginMsg); goto RETURN; } } //修改玩家现场状态机 if (m_csm.SetStateCheck(PlayerContextStateMachine.EventOnAuthLoginOK) == -1) { Log.Info("OnAuthByLogin::PlayerContextStateMachine switch Fail To LoginOk"); result = S2C_LoginMessage.Types.State.Fail; goto RETURN; } //由于目前是单服 所以直接将玩家现场状态设置成验证成功状态 if (m_csm.SetStateCheck(PlayerContextStateMachine.EventOnSessionLoginOK) == -1) { Log.Info("OnAuthByLogin::PlayerContextStateMachine switch Fail To SessuibLoginOk"); result = S2C_LoginMessage.Types.State.Fail; goto RETURN; } //向客户端发送登陆验证成功 RETURN: if (result == S2C_LoginMessage.Types.State.Ok) { response = new S2C_LoginMessage { PlayerGameId = this.m_gameUserId, State = result }; await OnLoginOk();//TODO:通知应用层登录流程完成 } else { response = new S2C_LoginMessage { PlayerGameId = message.Account, State = result } }; reply(response); }