/// <summary> /// 初始化GameServer代理 /// 同一个GameServer的Service与ClientAgent是一个N:1的关系 /// </summary> /// <param name="callback"></param> /// <param name="clientInfo"></param> /// <param name="bFistInit">是否是首次初始化该client</param> /// <returns></returns> public int InitializeClient(IKuaFuClient callback, KuaFuClientContext clientInfo, out bool bFistInit) { bFistInit = false; lock (Mutex) { ClientAgent agent = null; if (!ServerId2ClientAgent.TryGetValue(clientInfo.ServerId, out agent) || !agent.IsAlive) { // clientInfo.ServerId 上的任意service都未连接过来,或者已连接,但是agent已超时死亡,需要重设agent LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器连接1.ServerId:{0},ClientId:{1},info:{2},GameType:{3} [Service首次连接过来]", clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType)); bFistInit = true; clientInfo.ClientId = KuaFuServerManager.GetUniqueClientId(); agent = new ClientAgent(clientInfo, callback); ServerId2ClientAgent[clientInfo.ServerId] = agent; } else { // clientInfo.ServerId 上的至少有一个service已经连接过来了 // token 不一致,重复 ServerId重复!!! if (clientInfo.Token != agent.ClientInfo.Token) { LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器ID重复,禁止连接.ServerId:{0},ClientId:{1},info:{2},GameType:{3}", clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType)); return(StdErrorCode.Error_Duplicate_Key_ServerId); } // clientInfo.ServerId 这个GameType的Service首次连接过来 if (clientInfo.ClientId <= 0) { bFistInit = true; LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器连接2.ServerId:{0},ClientId:{1},info:{2},GameType:{3} [Service非首次连接过来]", clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType)); } else if (clientInfo.ClientId != agent.ClientInfo.ClientId) { // GameServer不重启,中心重启后,从第二个连过来的service开始会走到这里 // tcp的虚拟连接能够使Gameserver感知不到中心的重启,这里需要修改clientInfo.ClientId bFistInit = true; LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器连接3.ServerId:{0},ClientId:{1},info:{2},GameType:{3} [中心重启]", clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType)); } } if (agent != null) { clientInfo.ClientId = agent.ClientInfo.ClientId; agent.ClientHeartTick(); agent.TryInitGameType(clientInfo.GameType); } return(clientInfo.ClientId); } }
public void AddSession(string sessionId, int gameType, IKuaFuClient callback) { lock (this.Mutex) { this.SessionId2GameTypeDict[sessionId] = gameType; this.KuaFuClientDict[gameType] = callback; } }
/// <summary> /// 初始化跨服客户端回调对象 /// </summary> public int InitializeClient(IKuaFuClient callback, KuaFuClientContext clientInfo) { try { if (clientInfo.GameType == (int)GameTypes.Spread && clientInfo.ServerId != 0) { return(ClientAgentManager.Instance().InitializeClient(callback, clientInfo)); } else { LogManager.WriteLog(LogTypes.Warning, string.Format("InitializeClient时GameType错误,禁止连接.ServerId:{0},GameType:{1}", clientInfo.ServerId, clientInfo.GameType)); return(StdErrorCode.Error_Invalid_GameType); } } catch (System.Exception ex) { LogManager.WriteExceptionUseCache(string.Format("InitializeClient服务器ID重复,禁止连接.ServerId:{0},ClientId:{1}", clientInfo.ServerId, clientInfo.ClientId)); return(StdErrorCode.Error_Server_Internal_Error); } }
private int ChangeRoleState(KuaFuRoleData kuaFuRoleData, KuaFuRoleStates state) { int result = -1; try { IKuaFuClient kuaFuClient = null; KuaFuRoleData roleData = null; int oldGameId = 0; lock (kuaFuRoleData) { kuaFuRoleData.Age++; kuaFuRoleData.State = state; if (state == KuaFuRoleStates.None && kuaFuRoleData.GameId > 0) { oldGameId = kuaFuRoleData.GameId; } roleData = (KuaFuRoleData)kuaFuRoleData; } if (oldGameId > 0) { RemoveRoleFromFuBen(oldGameId, kuaFuRoleData.RoleId); } if (null != roleData) { AsyncDataItem evItem = new AsyncDataItem(KuaFuEventTypes.RoleStateChange, kuaFuRoleData); ClientAgentManager.Instance().PostAsyncEvent(kuaFuRoleData.ServerId, GameType, evItem); } } catch (System.Exception ex) { return(-1); } return(result); }
private int ChangeRoleState(HuanYingSiYuanAgent huanYingSiYuanAgent, KuaFuRoleData kuaFuRoleData, KuaFuRoleStates state) { int result = -1; try { IKuaFuClient kuaFuClient = null; KuaFuRoleData roleData = null; int oldGameId = 0; lock (kuaFuRoleData) { kuaFuRoleData.Age++; kuaFuRoleData.State = state; if (state == KuaFuRoleStates.None && kuaFuRoleData.GameId > 0) { oldGameId = kuaFuRoleData.GameId; } kuaFuClient = huanYingSiYuanAgent.KuaFuClient; roleData = (KuaFuRoleData)kuaFuRoleData; } if (oldGameId > 0) { RemoveRoleFromFuBen(oldGameId, kuaFuRoleData.RoleId); } if (null != kuaFuClient && null != roleData) { result = huanYingSiYuanAgent.NotifyChangeState(kuaFuRoleData); } } catch (System.Exception ex) { return(-1); } return(result); }
/// <summary> /// 初始化GameServer代理 /// 同一个GameServer的Service与ClientAgent是一个N:1的关系 /// </summary> /// <param name="callback"></param> /// <param name="clientInfo"></param> /// <returns></returns> public int InitializeClient(IKuaFuClient callback, KuaFuClientContext clientInfo) { bool bPlaceHolder = false; return(InitializeClient(callback, clientInfo, out bPlaceHolder)); }
public ClientAgent(KuaFuClientContext clientInfo, IKuaFuClient callback) { ClientInfo = clientInfo; KuaFuClient = callback; ClientHeartTick(); }
private bool AssignGameFuben(KuaFuRoleData kuaFuRoleData, ServerLoadContext context, long waitSecs1, long waitSecs2, DateTime now) { int roleCount = 0; DateTime stateEndTime = now.AddSeconds(EnterGameSecs); HuanYingSiYuanAgent huanYingSiYuanAgent = null; HuanYingSiYuanFuBenData huanYingSiYuanFuBenData = null; IKuaFuClient kuaFuClient = null; KuaFuFuBenRoleData kuaFuFuBenRoleData = new KuaFuFuBenRoleData() { ServerId = kuaFuRoleData.ServerId, RoleId = kuaFuRoleData.RoleId, ZhanDouLi = HuanYingSiYuanGameData.GetZhanDouLi(kuaFuRoleData.GameData), }; List <KuaFuRoleData> updateRoleDataList = new List <KuaFuRoleData>(); try { //先检测是否有需要补充人的队伍 foreach (var fuBenData in ShotOfRolesFuBenDataDict.Values) { //分组编号要相等 if (fuBenData.CanRemove()) { RemoveGameFuBen(fuBenData); } else if (fuBenData.CanEnter(kuaFuRoleData.GroupIndex, waitSecs1, waitSecs2)) { if (ServerId2KuaFuClientAgent.TryGetValue(fuBenData.ServerId, out huanYingSiYuanAgent) && huanYingSiYuanAgent.IsAlive) { roleCount = fuBenData.AddKuaFuFuBenRoleData(kuaFuFuBenRoleData, GameFuBenRoleCountChangedHandler); if (roleCount > 0) { huanYingSiYuanFuBenData = fuBenData; break; } } } } if (null == huanYingSiYuanFuBenData) { //按负载状态在一个跨服活动服务器上分配一个新的游戏副本,并加入 LinkedListNode <KuaFuServerGameConfig> node = context.IdelActiveServerQueue.First; int count = context.IdelActiveServerQueue.Count; for (int i = 0; i < count && node != null; i++) { KuaFuServerGameConfig srv = node.Value; LinkedListNode <KuaFuServerGameConfig> next = node.Next; if (ServerId2KuaFuClientAgent.TryGetValue(srv.ServerId, out huanYingSiYuanAgent) && huanYingSiYuanAgent.IsAlive) { int serverLoad = huanYingSiYuanAgent.GetAliveGameFuBenCount(); if (serverLoad < srv.Capacity && serverLoad <= context.ServerLoadAvg) { try { huanYingSiYuanFuBenData = Persistence.CreateHysyGameFuBen(huanYingSiYuanAgent, kuaFuRoleData.GroupIndex, 1); if (huanYingSiYuanFuBenData != null) { AddGameFuBen(huanYingSiYuanFuBenData, huanYingSiYuanAgent); roleCount = huanYingSiYuanFuBenData.AddKuaFuFuBenRoleData(kuaFuFuBenRoleData, GameFuBenRoleCountChangedHandler); if (roleCount > 0) { context.AlivedGameFuBenCount++; context.CalcServerLoadAvg(); break; } } } catch (System.Exception ex) { huanYingSiYuanAgent.MaxActiveTicks = 0; } } else { context.IdelActiveServerQueue.Remove(node); if (serverLoad < srv.Capacity) { context.IdelActiveServerQueue.AddLast(node); } } } node = next; } } if (huanYingSiYuanFuBenData != null && roleCount > 0) { if (roleCount == 1) { huanYingSiYuanFuBenData.EndTime = now; //第一个人进入时,重置副本创建时间 } if (huanYingSiYuanFuBenData.State == GameFuBenState.Wait) { if (roleCount == Consts.HuanYingSiYuanRoleCountTotal) { List <KuaFuFuBenRoleData> roleList = huanYingSiYuanFuBenData.SortFuBenRoleList(); foreach (var role in roleList) { KuaFuRoleData kuaFuRoleDataTemp; KuaFuRoleKey key = KuaFuRoleKey.Get(role.ServerId, role.RoleId); if (RoleIdKuaFuRoleDataDict.TryGetValue(key, out kuaFuRoleDataTemp)) { kuaFuRoleDataTemp.UpdateStateTime(huanYingSiYuanFuBenData.GameId, KuaFuRoleStates.NotifyEnterGame, stateEndTime.Ticks); } } huanYingSiYuanFuBenData.State = GameFuBenState.Start; NotifyFuBenRoleEnterGame(huanYingSiYuanFuBenData); } else { kuaFuRoleData.UpdateStateTime(huanYingSiYuanFuBenData.GameId, KuaFuRoleStates.SignUpWaiting, kuaFuRoleData.StateEndTicks); NotifyFuBenRoleCount(huanYingSiYuanFuBenData); } } else if (huanYingSiYuanFuBenData.State == GameFuBenState.Start) { kuaFuRoleData.UpdateStateTime(huanYingSiYuanFuBenData.GameId, KuaFuRoleStates.NotifyEnterGame, stateEndTime.Ticks); NotifyFuBenRoleEnterGame(huanYingSiYuanFuBenData); } } else { //如果分配失败,则返回false,本轮不在尝试分配 return(false); } return(true); } catch (System.Exception ex) { LogManager.WriteExceptionUseCache(ex.ToString()); } return(false); }
public int RoleSignUp(int serverId, string userId, int zoneId, int roleId, int gameType, int groupIndex, IGameData gameData) { int result = (int)KuaFuRoleStates.SignUp; IKuaFuClient kuaFuClient = null; //查找对应的代理对象 KuaFuRoleData kuaFuRoleData; HuanYingSiYuanAgent huanYingSiYuanAgent; if (!ServerId2KuaFuClientAgent.TryGetValue(serverId, out huanYingSiYuanAgent)) { LogManager.WriteLog(LogTypes.Error, string.Format("RoleSignUp时ServerId错误.ServerId:{0},roleId:{1}", serverId, roleId)); return(-500); } //保持活动的角色和帐号唯一,不加锁,不严格保证 int rid; if (!UserId2RoleIdActiveDict.TryGetValue(userId, out rid)) { UserId2RoleIdActiveDict[userId] = roleId; } else { if (rid > 0 && rid != roleId) { if (RoleIdKuaFuRoleDataDict.TryGetValue(KuaFuRoleKey.Get(serverId, rid), out kuaFuRoleData)) { if (ChangeRoleState(huanYingSiYuanAgent, kuaFuRoleData, KuaFuRoleStates.None) < 0) { //return -1; } } UserId2RoleIdActiveDict[userId] = roleId; } } //添加或更新角色信息和状态 Lazy <KuaFuRoleData> lazy = new Lazy <KuaFuRoleData>(() => { return(new KuaFuRoleData() { RoleId = roleId, UserId = userId, GameType = gameType, }); }); KuaFuRoleKey key = KuaFuRoleKey.Get(serverId, roleId); kuaFuRoleData = RoleIdKuaFuRoleDataDict.GetOrAdd(key, (x) => { return(lazy.Value); }); int oldGameId = 0; lock (kuaFuRoleData) { oldGameId = kuaFuRoleData.GameId; kuaFuRoleData.GameId = 0; //更新到新的状态 kuaFuRoleData.Age++; kuaFuRoleData.State = KuaFuRoleStates.SignUp; kuaFuRoleData.ServerId = serverId; kuaFuRoleData.ZoneId = zoneId; kuaFuRoleData.GameData = gameData; kuaFuRoleData.GroupIndex = groupIndex; kuaFuRoleData.StateEndTicks = Global.NowTime.Ticks; } //WaitingKuaFuRoleDataDict[key] = kuaFuRoleData; //移除旧的游戏状态 if (oldGameId > 0) { RemoveRoleFromFuBen(oldGameId, roleId); } return(result); }
/// <summary> /// 初始化跨服客户端回调对象 /// </summary> /// <param name="callback"></param> /// <param name="age"></param> /// <param name="version"></param> /// <returns>clientId</returns> public int InitializeClient(IKuaFuClient callback, KuaFuClientContext clientInfo) { int clientId = -1; bool firstInit = false; try { if (clientInfo.GameType == (int)GameTypes.HuanYingSiYuan && clientInfo.ServerId != 0) { //如果是第一次初始化,需要清空原始的 if (clientInfo.ClientId == 0) { firstInit = true; } Lazy <HuanYingSiYuanAgent> lazy = new Lazy <HuanYingSiYuanAgent>(() => { clientInfo.ClientId = Persistence.GetUniqueClientId(clientInfo.ServerId); return(new HuanYingSiYuanAgent(clientInfo, callback)); }); HuanYingSiYuanAgent huanYingSiYuanAgent = ServerId2KuaFuClientAgent.GetOrAdd(clientInfo.ServerId, (x) => { return(lazy.Value); }); if (clientInfo.ClientId != huanYingSiYuanAgent.ClientInfo.ClientId) { if (clientInfo.ClientId > 0) { if (huanYingSiYuanAgent.IsClientAlive()) { LogManager.WriteLog(LogTypes.Error, string.Format("InitializeClient服务器ID重复,禁止连接.ServerId:{0},ClientId:{1}", clientInfo.ServerId, clientInfo.ClientId)); return(StdErrorCode.Error_Duplicate_Key_ServerId); } } clientId = huanYingSiYuanAgent.ClientInfo.ClientId; } else { clientId = clientInfo.ClientId; } if (firstInit) { huanYingSiYuanAgent.Reset(); List <int> list = huanYingSiYuanAgent.GetAliveGameFuBenList(); if (null != list && list.Count > 0) { foreach (var id in list) { HuanYingSiYuanFuBenData fuBenData; if (HuanYingSiYuanFuBenDataDict.TryGetValue(id, out fuBenData)) { RemoveGameFuBen(fuBenData, huanYingSiYuanAgent); } else { huanYingSiYuanAgent.RemoveGameFuBen(id); } } } } } else { LogManager.WriteLog(LogTypes.Error, string.Format("InitializeClient时GameType错误,禁止连接.ServerId:{0},GameType:{1}", clientInfo.ServerId, clientInfo.GameType)); return(StdErrorCode.Error_Invalid_GameType); } } catch (System.Exception ex) { LogManager.WriteExceptionUseCache(string.Format("InitializeClient服务器ID重复,禁止连接.ServerId:{0},ClientId:{1}", clientInfo.ServerId, clientInfo.ClientId)); return(StdErrorCode.Error_Server_Internal_Error); } return(clientId); }
public HuanYingSiYuanAgent(KuaFuClientContext clientInfo, IKuaFuClient callback) : base(clientInfo, callback) { }