コード例 #1
0
        public int HandlePacket(ClientBase client, GamePacket packet)
        {
            CenterClient center = client as CenterClient;

            ServerType type = (ServerType)packet.ReadUnsignedByte();

            string ip = packet.ReadUTF();

            int port = packet.ReadInt();

            uint id = 0;

            switch (type)
            {
            //case ServerType.UnKnowServer:
            //    break;
            //case ServerType.CenterServer:
            //    break;
            case ServerType.CacheServer:
                id = 1;
                if (CenterServerConfig.Configuration.CacheIP == ip && CenterServerConfig.Configuration.CachePort == port)
                {
                    CenterGlobal.CacheServer = center;

                    CenterGlobal.GatewayServerMgr.ForEach((c) =>
                    {
                        ConnectCacheServerPacket pkg = new ConnectCacheServerPacket();
                        pkg.IP   = ip;
                        pkg.Port = port;
                        c.SendTcp(pkg);
                    });
                }
                break;

            case ServerType.LogicServer:
                id = GameGlobal.GameLogicMgr.GetID(info => info.IP == ip && info.Port == port);
                if (id > 0)
                {
                    CenterGlobal.GameLogicServerMgr.Add(id, center);
                    CenterGlobal.GatewayServerMgr.ForEach((c) => {
                        ConnectLogicServerPacket pkg = new ConnectLogicServerPacket();
                        pkg.ID   = id;
                        pkg.IP   = ip;
                        pkg.Port = port;
                        c.SendTcp(pkg);
                    });
                }
                break;

            case ServerType.BattleServer:
                id = GameGlobal.BattleMgr.GetID(info => info.IP == ip && info.Port == port);
                if (id > 0)
                {
                    CenterGlobal.BattleServerMgr.Add(id, center);
                    CenterGlobal.GatewayServerMgr.ForEach((c) =>
                    {
                        ConnectBattleServerPacket pkg = new ConnectBattleServerPacket();
                        pkg.ID   = id;
                        pkg.IP   = ip;
                        pkg.Port = port;
                        c.SendTcp(pkg);
                    });
                }
                break;

            case ServerType.GatewayServer:
                id = GameGlobal.GatewayMgr.GetID(info => info.IP == ip && info.Port == port);

                UpdateServerIDPacket pkt = new UpdateServerIDPacket();
                pkt.ID = (byte)id;
                center.SendTcp(pkt);

                if (id > 0)
                {
                    CenterGlobal.GatewayServerMgr.Add(id, center);

                    uint[] keys = CenterGlobal.GameLogicServerMgr.GetKeys();

                    foreach (uint key in keys)
                    {
                        GameLogicInfo            info = GameGlobal.GameLogicMgr.FindTemplate(key);
                        ConnectLogicServerPacket pkg  = new ConnectLogicServerPacket();
                        pkg.ID   = key;
                        pkg.IP   = info.IP;
                        pkg.Port = info.Port;
                        center.SendTcp(pkg);
                    }

                    keys = CenterGlobal.BattleServerMgr.GetKeys();
                    foreach (uint key in keys)
                    {
                        BattleInfo info = GameGlobal.BattleMgr.FindTemplate(key);
                        ConnectBattleServerPacket pkg = new ConnectBattleServerPacket();
                        pkg.ID   = key;
                        pkg.IP   = info.IP;
                        pkg.Port = info.Port;
                        center.SendTcp(pkg);
                    }

                    if (CenterGlobal.CacheServer != null && CenterGlobal.CacheServer.Connected)
                    {
                        ConnectCacheServerPacket cachePacket = new ConnectCacheServerPacket();
                        cachePacket.IP   = CenterServerConfig.Configuration.CacheIP;
                        cachePacket.Port = CenterServerConfig.Configuration.CachePort;
                        center.SendTcp(cachePacket);
                    }
                }
                break;

            default:
                center.Disconnect();
                return(0);
            }

            if (id > 0)
            {
                center.ID        = id;
                center.Type      = type;
                center.Validated = true;
            }
            else
            {
                center.Disconnect();
            }

            return(0);
        }
コード例 #2
0
        void HandlePacket(CenterClient client, GamePacket pkg)
        {
            //服务器类型
            ServerType type = (ServerType)pkg.ReadUnsignedByte();

            //服务器监听地址和端口
            string ip   = pkg.ReadUTF();
            int    port = pkg.ReadInt();

            int id = 0;

            switch (type)
            {
            case ServerType.LogicServer:
            {
                id = CenterGlobal.LogicMgr.GetID(info => info.IP == ip && info.Port == port);

                if (id > 0)
                {
                    CenterGlobal.LogicServerMgr.Add(id, client);
                    client.Disconnected += new DisconnectDelegate(logic_Client_Disconnected);

                    //通知所有网关服务器连接逻辑服务器
                    CenterGlobal.GatewayServerMgr.ForEach(gateway =>
                        {
                            ConnectLogicServerPacket p = new ConnectLogicServerPacket();

                            p.ID   = id;
                            p.IP   = ip;
                            p.Port = port;

                            gateway.SendTcp(p);
                        });
                }
                break;
            }

            case ServerType.BattleServer:
            {
                id = CenterGlobal.BattleMgr.GetID(info => info.IP == ip && info.Port == port);

                if (id > 0)
                {
                    CenterGlobal.BattleServerMgr.Add(id, client);
                    client.Disconnected += new DisconnectDelegate(battle_Client_Disconnected);

                    //通知所有网关服务器连接战斗服务器
                    CenterGlobal.GatewayServerMgr.ForEach(gateway =>
                        {
                            ConnectBattleServerPacket p = new ConnectBattleServerPacket();

                            p.ID   = id;
                            p.IP   = ip;
                            p.Port = port;

                            gateway.SendTcp(p);
                        });
                }
                break;
            }

            case ServerType.GatewayServer:
            {
                id = CenterGlobal.GatewayMgr.GetID(info => info.IP == ip && info.Port == port);

                if (id > 0)
                {
                    CenterGlobal.GatewayServerMgr.Add(id, client);
                    client.Disconnected += new DisconnectDelegate(gateway_Client_Disconnected);

                    //更新网关ID
                    UpdateServerIDPacket pkt = new UpdateServerIDPacket();
                    pkt.ID = (byte)id;
                    client.SendTcp(pkt);

                    //遍历所有逻辑服务器,通知网关连接.
                    CenterGlobal.LogicServerMgr.ForEachKey(lid =>
                        {
                            GameLogicInfo info = CenterGlobal.LogicMgr.FindTemplate(lid);

                            ConnectLogicServerPacket p = new ConnectLogicServerPacket();

                            p.ID   = info.TemplateID;
                            p.IP   = info.IP;
                            p.Port = info.Port;

                            client.SendTcp(p);
                        });

                    //遍历所有战斗服务器,通知网关连接.
                    CenterGlobal.BattleServerMgr.ForEachKey(tid =>
                        {
                            BattleInfo info = CenterGlobal.BattleMgr.FindTemplate(tid);

                            ConnectBattleServerPacket p = new ConnectBattleServerPacket();

                            p.ID   = info.TemplateID;
                            p.IP   = info.IP;
                            p.Port = info.Port;

                            client.SendTcp(p);
                        });
                }
                break;
            }
            }

            //有效连接时设置服务器信息
            if (id > 0)
            {
                client.ServerID  = id;
                client.Type      = type;
                client.Validated = true;
            }
            else
            {
                log.ErrorFormat("服务器验证无效! IP:{0}  Port:{1}", ip, port);
                client.Disconnect();
            }
        }