public void AddSession(ClientUID client_uid) { if (!m_client_sessions.Contains(client_uid)) { m_client_sessions.Add(client_uid); } }
public void Setup(ClientUID client_uid, InterServerID srv_uid, PlayerInfoForWS info) { m_char_idx = info.char_idx; m_client_uid = client_uid; m_srv_uid = srv_uid; m_player_data.Copy(info); }
public void Set(ClientUID client_uid, ushort header, ByteArray by) { data.WriteUShort(header); //必须填入id client_uid.Write(data); by.Skip(ClientUID.Size()); //由于by是C2S消息,带ClientUID,所以跳过 by.Read(data, by.Available); }
/// <summary> /// 登陆 /// </summary> private void OnClientLogin(PacketBase packet) { gs2ws.ClientLogin msg = packet as gs2ws.ClientLogin; string account_name = msg.name; string account_psw = msg.psw; ClientUID client_uid = msg.client_uid; //查询是否在缓存 AccountData account_data = new AccountData(); if (AccountCacheManager.Instance.GetAccountData(account_name, ref account_data)) { CheckAccountLogin(client_uid, account_name, account_psw, account_data); } else { SQLLoginHandle.QueryAccountData(account_name, (data) => { AccountCacheManager.Instance.AddAccountData(account_name, data); CheckAccountLogin(client_uid, account_name, account_psw, data); } ); } }
public void Set(ClientUID client_uid, PackBaseS2C packet) { if (!is_broadcast && (client_uid.srv_uid == 0 || client_uid.conn_idx == 0)) { Console.WriteLine("未设置转发目标服务器数据"); } packet.client_uid = client_uid; packet.Write(data); }
public Player GetPlayerByClientUID(ClientUID uid) { long char_idx = 0; if (m_client_players.TryGetValue(uid, out char_idx)) { return(GetUnitByIdx(char_idx) as Player); } return(null); }
/// <summary> /// 进入游戏 /// </summary> private void OnEnterGame(PacketBase packet) { gs2ss.EnterGame msg = packet as gs2ss.EnterGame; ClientUID client_uid = msg.client_uid; InterServerID server_uid = msg.server_uid; UnitManager.Instance.AddSession(client_uid); if (!UnitManager.Instance.HasUnit(msg.char_idx)) { DBID db_id = new DBID(); db_id.game_id = ServerConfig.GetDBByAccountIdx(msg.account_idx, eDBType.Game); PlayerInfoForSS ss_data = CommonObjectPools.Spawn <PlayerInfoForSS>(); SQLCharHandle.QueryCharacterInfo(msg.char_idx, db_id, ss_data, is_load => { if (is_load && UnitManager.Instance.HadSession(client_uid)) {//读取玩数据,有可能已经退出 //创建玩家 Player player = new Player(); player.client_uid = client_uid; player.LoadData(ss_data); UnitManager.Instance.AddUnit(player); //告诉gs成功进入游戏 ss2gs.EnterGame rep_gs_msg = PacketPools.Get(ss2gs.msg.ENTER_GAME) as ss2gs.EnterGame; rep_gs_msg.server_uid = server_uid; rep_gs_msg.client_uid = client_uid; rep_gs_msg.char_idx = ss_data.char_idx; ServerNetManager.Instance.Send(server_uid.gs_uid, rep_gs_msg); //告诉ws ss2ws.LoginClient rep_ws_msg = PacketPools.Get(ss2ws.msg.LOGIN_CLIENT) as ss2ws.LoginClient; rep_ws_msg.server_uid = server_uid; rep_ws_msg.client_uid = client_uid; rep_ws_msg.data.Copy(ss_data); ServerNetManager.Instance.Send2WS(rep_ws_msg); //告诉gl ss2gl.LoginClient rep_gl_msg = PacketPools.Get(ss2gl.msg.LOGIN_CLIENT) as ss2gl.LoginClient; rep_gl_msg.server_uid = server_uid; rep_gl_msg.data.Copy(ss_data); ServerNetManager.Instance.Send2GL(rep_gl_msg); //告诉客户端角色基础信息 ss2c.CharacterInfo rep_msg = PacketPools.Get(ss2c.msg.CHARACTER_INFO) as ss2c.CharacterInfo; rep_msg.data.Copy(ss_data); ServerNetManager.Instance.SendProxy(client_uid, rep_msg, false); //初始化内部逻辑 player.OnFirstEnter(); } CommonObjectPools.Despawn(ss_data); }); } }
private void OnClientLogin(PacketBase packet) { gs2db.ClientLogin msg = packet as gs2db.ClientLogin; ClientUID client_uid = msg.client_uid; string account_name = msg.name; string account_psw = msg.psw; SQLLoginHandle.QueryAccountData(account_name, (data) => { ///1.验证账号密码 eLoginResult result = eLoginResult.E_FAILED_UNKNOWNERROR; if (data.account_idx > 0) { string md5_msg = StringUtils.GetMD5(account_psw); if (data.password_md5 == md5_msg) { result = eLoginResult.E_SUCCESS; } else { result = eLoginResult.E_FAILED_INVALIDPASSWORD; } } else { result = eLoginResult.E_FAILED_INVALIDACCOUNT; } //if(result != eLoginResult.E_SUCCESS) { db2gs.ClientLogin rep_msg = PacketPools.Get(db2gs.msg.CLIENT_LOGIN) as db2gs.ClientLogin; rep_msg.client_uid = client_uid; rep_msg.result = result; rep_msg.account_idx = data.account_idx; rep_msg.spid = data.spid; this.Send(rep_msg); return; } /////2.验证登录状态 //DBHandle.QueryLoginStatus(data.account_idx, (login_res) => //{ // db2gs.ClientLogin rep_msg = PacketPools.Get(db2gs.msg.CLIENT_LOGIN) as db2gs.ClientLogin; // rep_msg.client_uid = msg.client_uid; // rep_msg.result = login_res; // rep_msg.account_idx = data.account_idx; // this.Send(rep_msg); //} //); } ); }
/// <summary> /// 发送验证结果 /// </summary> private void SendLoginResult(ClientUID client_uid, eLoginResult result, long account_idx, ushort spid) { ws2gs.ClientLogin msg = PacketPools.Get(ws2gs.msg.CLIENT_LOGIN) as ws2gs.ClientLogin; msg.client_uid = client_uid; msg.login_result = result; if (result == eLoginResult.E_SUCCESS) { msg.account_idx = account_idx; msg.spid = spid; msg.ss_uid = ServerNetManager.Instance.AllocSSForClient(); } ServerNetManager.Instance.Send(client_uid.srv_uid, msg); }
/// <summary> /// 登入处理 /// </summary> public void HandleLogin(ClientUID client_uid, InterServerID srv_uid, PlayerInfoForWS data) { Unit unit = GetUnitByIdx(data.char_idx); if (unit != null) {//可能上次退出时,没有清除数据 this.RemoveUnit(unit); } //创建玩家 unit = new Unit(); unit.Setup(client_uid, srv_uid, data); UnitManager.Instance.AddUnit(unit); unit.OnEnter(); }
/// <summary> /// 请求验证 /// </summary> private void CheckAccountLogin(ClientUID client_uid, string account_name, string account_psw, AccountData account_data) { ///1.验证账号密码 eLoginResult result = eLoginResult.E_FAILED_UNKNOWNERROR; if (account_data.account_idx > 0) { string md5_msg = StringUtils.GetMD5(account_psw); if (account_data.password_md5 == md5_msg) { result = eLoginResult.E_SUCCESS; } else { result = eLoginResult.E_FAILED_INVALIDPASSWORD; } } else { result = eLoginResult.E_FAILED_INVALIDACCOUNT; } ///2.验证结果处理 if (result == eLoginResult.E_SUCCESS) { //处理踢号:如果存在账号索引,说明已经登录成功过一次 Unit unit = UnitManager.Instance.GetUnitByAccount(account_data.account_idx); if (unit != null) { //踢号:发给账号所在的gate ServerNetManager.Instance.KickAccount(unit.account_idx); //延长几秒发送,等待踢号 long account_idx = account_data.account_idx; ushort spid = account_data.spid; TimerManager.Instance.AddOnce(3000, (timer_id, param) => { this.SendLoginResult(client_uid, result, account_idx, spid); }); } else { this.SendLoginResult(client_uid, result, account_data.account_idx, account_data.spid); } } else { this.SendLoginResult(client_uid, result, 0, 0); } }
/// <summary> /// 通过gate转发给client /// </summary> public int SendProxy(ClientUID client_uid, PackBaseS2C packet, bool is_broadcast = false) { int size = 0; ConnAppProc app_server; if (m_srv_servers.TryGetValue(client_uid.srv_uid, out app_server)) { ProxyS2CMsg msg = PacketPools.Get((ushort)ss2gs.msg.PROXY_SS_MSG) as ProxyS2CMsg; msg.is_broadcast = is_broadcast; msg.Set(client_uid, packet); size = Send(app_server.conn_idx, msg); } PacketPools.Recover(packet);//回收消息本身 return(size); }
/// <summary> /// 角色列表 /// </summary> private void OnEnumCharacterList(PacketBase packet) { gs2ss.EnumCharacter msg = packet as gs2ss.EnumCharacter; ClientUID client_uid = msg.client_uid; SQLCharHandle.QueryCharacterList(msg.account_idx, new DBID(msg.game_db_id), (data) => { ss2c.EnumCharacter rep_msg = PacketPools.Get(ss2c.msg.ENUM_CHAR) as ss2c.EnumCharacter; foreach (var char_data in data) { rep_msg.list.Add(char_data); } ServerNetManager.Instance.SendProxy(client_uid, rep_msg); } ); }
/// <summary> /// 发给客户端 /// </summary> public int SendProxy(ClientUID client_uid, PackBaseS2C packet) { int size = 0; ConnAppProc app_server; if (m_srv_servers.TryGetValue(client_uid.srv_uid, out app_server)) { ProxyS2CMsg msg = PacketPools.Get((ushort)ws2gs.msg.PROXY_WS_MSG) as ProxyS2CMsg; msg.Set(client_uid, packet); size = Send(app_server.conn_idx, msg); } else { Log.Debug("未找到服务器id:" + client_uid.srv_uid); } PacketPools.Recover(packet);//回收消息本身 return(size); }
private void OnCharacterInfo(PacketBase packet) { ss2db.CharacterInfo msg = packet as ss2db.CharacterInfo; PlayerCache member = PlayerCacheManager.Instance.GetMember(msg.char_idx); if (member == null) { //由于数据包会放入对象池,所以在这先保存临时数据 ClientUID client_uid = msg.client_uid; InterServerID server_uid = msg.server_uid; long char_idx = msg.char_idx; PlayerCacheManager.Instance.LoadPlayer(msg.char_idx, idx => { OnCharacterInfoLoaded(client_uid, server_uid, char_idx); }); } else { OnCharacterInfoLoaded(msg.client_uid, msg.server_uid, msg.char_idx); } }
/// <summary> /// 创建角色 /// </summary> private void OnCreateCharacter(PacketBase packet) { ss2db.CreateCharacter msg = packet as ss2db.CreateCharacter; InterServerID server_uid = msg.server_uid; ClientUID client_uid = msg.client_uid; CreateCharacterInfo create_info = msg.data; create_info.char_idx = ServerNetManager.Instance.GetNextCharIdx(); SQLCharHandle.CreateCharacter(msg.account_idx, create_info, (res) => { eCreateCharResult result = eCreateCharResult.E_FAILED_COMMONERROR; if (create_info.char_idx == res) { result = eCreateCharResult.E_SUCCESS; } else { switch (res) { case 0: result = eCreateCharResult.E_FAILED_INTERNALERROR; break; case 1: result = eCreateCharResult.E_FAILED_CHARCOUNTLIMIT; break; case 2: result = eCreateCharResult.E_FAILED_INVALIDPARAM_REPEATEDNAME; break; case 3: result = eCreateCharResult.E_FAILED_COMMONERROR; break; } } db2ss.CreateCharacter rep_msg = PacketPools.Get(db2ss.msg.CREATE_CHARACTER) as db2ss.CreateCharacter; rep_msg.server_uid = server_uid; rep_msg.client_uid = client_uid; rep_msg.char_idx = create_info.char_idx; rep_msg.char_name = create_info.char_name; rep_msg.result = result; this.Send(rep_msg); } ); }
/// <summary> /// 角色列表 /// </summary> private void OnEnumCharacter(PacketBase packet) { ss2db.EnumCharacter msg = packet as ss2db.EnumCharacter; InterServerID server_uid = msg.server_uid; ClientUID client_uid = msg.client_uid; SQLCharHandle.QueryCharacterList(msg.account_idx, (data) => { db2ss.EnumCharacter rep_msg = PacketPools.Get(db2ss.msg.ENUM_CHAR) as db2ss.EnumCharacter; rep_msg.server_uid = server_uid; rep_msg.client_uid = client_uid; foreach (var char_data in data) { rep_msg.list.Add(char_data); } rep_msg.result = eEnumCharResult.E_SUCCESS; this.Send(rep_msg); } ); }
/// <summary> /// 处理登出逻辑 /// </summary> private void HandleLogoutAccount(ClientUID client_uid) { Player player = UnitManager.Instance.GetPlayerByClientUID(client_uid) as Player; if (player == null) {//如果在加载完角色信息前退出,则不会有unit return; } long char_idx = player.char_idx; ushort fs_uid = player.fs_uid; //从场景移除 BaseScene scene = SceneManager.Instance.GetScene(player.scene_obj_idx); if (scene != null) { scene.RemoveUnit(player); } //从管理器移除 UnitManager.Instance.RemoveUnit(player); //告诉fs ss2fs.LogoutClient fs_msg = PacketPools.Get(ss2fs.msg.LOGOUT_CLIENT) as ss2fs.LogoutClient; fs_msg.char_idx = char_idx; ServerNetManager.Instance.Send2FS(fs_uid, fs_msg); //告诉ws ss2ws.LogoutClient ws_msg = PacketPools.Get(ss2ws.msg.LOGOUT_CLIENT) as ss2ws.LogoutClient; ws_msg.char_idx = char_idx; ServerNetManager.Instance.Send2WS(ws_msg); //告诉gl ss2gl.LogoutClient gl_msg = PacketPools.Get(ss2gl.msg.LOGOUT_CLIENT) as ss2gl.LogoutClient; gl_msg.char_idx = char_idx; ServerNetManager.Instance.Send2GL(gl_msg); }
private void OnCharacterInfoLoaded(ClientUID client_uid, InterServerID server_uid, long char_idx) { PlayerCache member = PlayerCacheManager.Instance.GetMember(char_idx); if (member == null) { Log.Warning("OnPlayerInfoLoaded - 未找到玩家缓存信息:" + char_idx); return; } //设置服务器信息 member.client_uid = client_uid; member.ss_uid = server_uid.ss_uid; member.UpdateAttribute(eUnitModType.UMT_time_last_login, Time.second_time); //返回数据给ss db2ss.CharacterInfo rep_msg = PacketPools.Get(db2ss.msg.CHARACTER_INFO) as db2ss.CharacterInfo; rep_msg.server_uid = server_uid; rep_msg.client_uid = client_uid; rep_msg.data.Copy(member.ss_data); this.Send(rep_msg); //读取邮件 MailCacheManager.Instance.LoadMailbox(member.char_idx); }
public bool HadSession(ClientUID client_uid) { return(m_client_sessions.Contains(client_uid)); }
public void RemoveSession(ClientUID client_uid) { m_client_sessions.Remove(client_uid); }