public bool AssginKfFuben(GameTypes gameType, long gameId, int roleNum, out int kfSrvId) { kfSrvId = 0; lock (Mutex) { long payload = long.MaxValue; ClientAgent assignAgent = null; foreach (var sid in AllKfServerId) { ClientAgent agent = null; if (ServerId2ClientAgent.TryGetValue(sid, out agent) && agent.IsAlive && agent.TotalRolePayload < payload) { payload = agent.TotalRolePayload; assignAgent = agent; } } if (assignAgent != null) { assignAgent.AssginKfFuben(gameType, gameId, roleNum); kfSrvId = assignAgent.ClientInfo.ServerId; return(true); } } return(false); }
/// <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 SetMainlinePayload(int serverId, int payload) { lock (Mutex) { ClientAgent agent = null; if (ServerId2ClientAgent.TryGetValue(serverId, out agent)) { agent.SetMainlinePayload(payload); } } }
public void RemoveKfFuben(GameTypes gameType, int kfSrvId, long gameId) { lock (Mutex) { ClientAgent agent = null; if (ServerId2ClientAgent.TryGetValue(kfSrvId, out agent)) { agent.RemoveKfFuben(gameType, gameId); } } }
/// <summary> /// 给特定GameServer投递异步事件 /// </summary> /// <param name="ServerId"></param> /// <param name="gameType"></param> /// <param name="evItem"></param> public void PostAsyncEvent(int ServerId, GameTypes gameType, AsyncDataItem evItem) { lock (Mutex) { ClientAgent agent = null; if (ServerId2ClientAgent.TryGetValue(ServerId, out agent)) { agent.PostAsyncEvent(gameType, evItem); } } }
public AsyncDataItem[] PickAsyncEvent(int serverId, GameTypes gameType) { lock (this.Mutex) { ClientAgent agent = null; if (this.ServerId2ClientAgent.TryGetValue(serverId, out agent)) { return(agent.PickAsyncEvent(gameType)); } } return(null); }
public bool IsAgentAlive(int serverId) { lock (this.Mutex) { ClientAgent agent = null; if (this.ServerId2ClientAgent.TryGetValue(serverId, out agent) && agent.IsAlive) { return(true); } } return(false); }
public void UpdateKuaFuMapClientCount(int serverId, Dictionary <int, int> mapClientCountDict) { if (mapClientCountDict != null && mapClientCountDict.Count > 0) { ClientAgent agent = ClientAgentManager.Instance().GetCurrentClientAgent(serverId); if (null != agent) { KuaFuServerManager.UpdateKuaFuLineData(agent.ClientInfo.ServerId, mapClientCountDict); ClientAgentManager.Instance().SetMainlinePayload(agent.ClientInfo.ServerId, mapClientCountDict.Values.ToList <int>().Sum()); } } }
public void GetServerState(int serverId, out int state, out int load) { state = 0; load = 0; lock (this.Mutex) { ClientAgent agent = null; if (this.ServerId2ClientAgent.TryGetValue(serverId, out agent)) { if (agent.IsAlive) { state = 1; load = (int)agent.TotalRolePayload; } } } }
public bool GetClientCacheItems(int serverId) { lock (this.Mutex) { if (this.Initialized) { ClientAgent agent = ClientAgentManager.Instance().GetCurrentClientAgent(serverId); if (agent != null && agent.ClientInfo.ClientId > 0) { int clientId; if (!this.BroadcastServerIdHashSet.TryGetValue(serverId, out clientId) || clientId != agent.ClientInfo.ClientId) { this.BroadcastServerIdHashSet[serverId] = agent.ClientInfo.ClientId; return(true); } } } } return(false); }
public Dictionary <int, GameTypeStaticsData> GetGameTypeStatics() { var result = new Dictionary <int, GameTypeStaticsData>(); lock (Mutex) { foreach (var serverId in AllKfServerId) { ClientAgent agent = null; if (ServerId2ClientAgent.TryGetValue(serverId, out agent)) { foreach (var tmpKvp in agent.GetGameTypeStatics()) { GameTypeStaticsData existData = null; if (!result.TryGetValue(tmpKvp.Key, out existData)) { existData = new GameTypeStaticsData(); result[tmpKvp.Key] = existData; } existData.ServerAlived += tmpKvp.Value.ServerAlived; existData.FuBenAlived += tmpKvp.Value.FuBenAlived; existData.SingUpRoleCount += tmpKvp.Value.SingUpRoleCount; existData.StartGameRoleCount += tmpKvp.Value.StartGameRoleCount; } } } foreach (var kvp in GameTypeLoadDict) { if (result.ContainsKey(kvp.Key)) { result[kvp.Key].SingUpRoleCount = kvp.Value.SingUpRoleCount; result[kvp.Key].StartGameRoleCount = kvp.Value.StartGameRoleCount; } } } return(result); }
public bool SpecialKfFuben(GameTypes gameType, long gameId, int roleNum, out int kfSrvId) { kfSrvId = 0; ClientAgent assignAgent = null; int serverId = KuaFuServerManager.GetSpecialLineId(gameType); bool result; if (serverId <= 0) { kfSrvId = -3; result = false; } else { lock (this.Mutex) { ClientAgent agent = null; if (this.ServerId2ClientAgent.TryGetValue(serverId, out agent) && agent.IsAlive) { assignAgent = agent; } else { kfSrvId = -100; } if (assignAgent != null) { assignAgent.AssginKfFuben(gameType, gameId, roleNum); kfSrvId = assignAgent.ClientInfo.ServerId; return(true); } } result = false; } return(result); }
public int InitializeClient(KuaFuClientContext clientInfo, out bool bFistInit) { bFistInit = false; if (KuaFuServerManager.LimitIP) { bool denied = true; KuaFuServerInfo serverInfo = KuaFuServerManager.GetKuaFuServerInfo(clientInfo.ServerId); if (null == serverInfo) { LogManager.WriteLog(LogTypes.Error, string.Format("非法连接,无效的服务器编号#serverid={0},ip={1},gametype={2}", clientInfo.ServerId, clientInfo.Token, (GameTypes)clientInfo.GameType), null, true); return(-1); } if (serverInfo != null && !string.IsNullOrEmpty(clientInfo.Token)) { if (!string.IsNullOrEmpty(serverInfo.LanIp) && clientInfo.Token.Contains(serverInfo.LanIp)) { denied = false; } else if (clientInfo.Token.Contains(serverInfo.Ip)) { denied = false; } } if (denied) { LogManager.WriteLog(LogTypes.Error, string.Format("非法连接#serverid={0},ip={1},ip={2},lanip={3},gametype={4}", new object[] { clientInfo.ServerId, clientInfo.Token, serverInfo.Ip, serverInfo.LanIp, (GameTypes)clientInfo.GameType }), null, true); return(-1); } } int clientId; lock (this.Mutex) { ClientAgent agent = null; if (!this.ServerId2ClientAgent.TryGetValue(clientInfo.ServerId, out agent)) { LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器连接1.ServerId:{0},ClientId:{1},info:{2},GameType:{3} [Service首次连接过来]", new object[] { clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType }), null, true); bFistInit = true; clientInfo.ClientId = KuaFuServerManager.GetUniqueClientId(); agent = new ClientAgent(clientInfo); this.ServerId2ClientAgent[clientInfo.ServerId] = agent; } else if (clientInfo.Token != agent.ClientInfo.Token) { if (clientInfo.ClientId == agent.ClientInfo.ClientId) { LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器IP变化.ServerId:{0},ClientId:{1},info:{2},GameType:{3}", new object[] { clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType }), null, true); } else { if (agent.IsAlive) { LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器ID重复,禁止连接.ServerId:{0},ClientId:{1},info:{2},GameType:{3}", new object[] { clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType }), null, true); return(-11002); } bFistInit = true; clientInfo.ClientId = KuaFuServerManager.GetUniqueClientId(); agent.Reset(clientInfo); LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器IP变化.ServerId:{0},ClientId:{1},info:{2},GameType:{3}", new object[] { clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType }), null, true); } } else if (clientInfo.ClientId != agent.ClientInfo.ClientId) { if (clientInfo.ClientId <= 0) { clientInfo.ClientId = agent.ClientInfo.ClientId; agent.Reset(clientInfo); LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器重连.ServerId:{0},ClientId:{1},info:{2},GameType:{3} [首次连接过来]", new object[] { clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType }), null, true); } else { LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器重连.ServerId:{0},ClientId:{1},info:{2},GameType:{3} [非首次连接过来]", new object[] { clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType }), null, true); } } else if (!Global.TestMode && !agent.IsAlive) { LogManager.WriteLog(LogTypes.Info, string.Format("InitializeClient服务器连接和上次心跳时间间隔过长.ServerId:{0},ClientId:{1},info:{2},GameType:{3},time={4}", new object[] { clientInfo.ServerId, clientInfo.ClientId, clientInfo.Token, (GameTypes)clientInfo.GameType, agent.DeltaTime }), null, true); } if (agent != null) { clientInfo.ClientId = agent.ClientInfo.ClientId; agent.ClientHeartTick(); agent.TryInitGameType(clientInfo.GameType); } clientId = clientInfo.ClientId; } return(clientId); }