Esempio n. 1
0
        /// <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);
            }
        }
Esempio n. 2
0
        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);
        }