Exemple #1
0
    public override void OnPlayerLogin(Player player)
    {
        if (m_disConnectDict.ContainsKey(player.playerID))
        {
            //重连吧少年
            PlayerMatchMsg_c msg = new PlayerMatchMsg_c();
            msg.isMatched   = true;
            msg.predictTime = 0;

            ConnectionComponent conn = m_disConnectDict[player.playerID];
            conn.m_session = player.session;

            conn.Entity.World.eventSystem.DispatchEvent(ServiceEventDefine.c_playerJoin, conn.Entity);

            ProtocolAnalysisService.SendMsg(player.session, msg);
        }
    }
    public void ReceviceSelectCharacter(SyncSession session, PlayerSelectCharacter_s msg)
    {
        if (session.player == null)
        {
            Debug.LogError("玩家未登录");
        }

        //判断该角色是否在玩家的拥有角色列表

        //如果是,替换玩家角色

        session.player.characterID = msg.characterID;
        PlayerSelectCharacter_c m = new PlayerSelectCharacter_c();


        ProtocolAnalysisService.SendMsg(session, m);
    }
Exemple #3
0
    void SendReconnectMsg(ConnectionComponent comp)
    {
        float time = ServiceTime.GetServiceTime();

        //发送游戏全部数据
        PushAllEnityData(comp);

        //发送单例数据
        PushSingleComponentData(comp);

        //发送游戏开始消息
        StartSyncMsg startMsg = CreateStartMsg(m_world);

        ProtocolAnalysisService.SendMsg(comp.m_session, startMsg);

        Debug.Log("重连时间 " + (ServiceTime.GetServiceTime() - time));
    }
    static void BroadcastCommand(WorldBase world, ConnectionComponent connectComp, T cmd, bool includeSelf)
    {
        cmd.time = ServiceTime.GetServiceTime();

        //TODO 与预测一致不广播节约带宽;
        List <EntityBase> list = world.GetEntityList(new string[] { "ConnectionComponent" });

        for (int i = 0; i < list.Count; i++)
        {
            ConnectionComponent cp = list[i].GetComp <ConnectionComponent>();
            if (cp.m_session != null)
            {
                ProtocolAnalysisService.SendMsg(cp.m_session, cmd);
                //Debug.Log("BroadcastCommand " + cmd.frame);
            }
        }
    }
Exemple #5
0
    public void PushAllEnityData(ConnectionComponent connect)
    {
        if (connect.m_session != null &&
            !connect.m_session.Connected)
        {
            return;
        }

        SyncEntityMsg msg = new SyncEntityMsg();

        msg.frame = m_world.FrameCount;
        msg.infos = new List <EntityInfo>();

        List <EntityBase> waitSyncList = GetEntityList();

        int count = 0;

        for (int i = 0; i < waitSyncList.Count; i++)
        {
            count++;
            msg.infos.Add(CreateEntityInfo(waitSyncList[i], connect.m_session));

            if (count > 5)
            {
                count = 0;
                if (msg.infos.Count > 0)
                {
                    ProtocolAnalysisService.SendMsg(connect.m_session, msg);
                }

                msg       = new SyncEntityMsg();
                msg.frame = m_world.FrameCount;

                msg.infos = new List <EntityInfo>();
            }
        }

        if (msg.infos.Count > 0)
        {
            ProtocolAnalysisService.SendMsg(connect.m_session, msg);
        }

        connect.m_waitDestroyEntity.Clear();
        connect.m_waitSyncEntity.Clear();
    }
    public override void NoRecalcBeforeFixedUpdate(int deltaTime)
    {
        List <EntityBase> list = GetEntityList();

        for (int i = 0; i < list.Count; i++)
        {
            ConnectionComponent comp = list[i].GetComp <ConnectionComponent>();
            CommandComponent    cmd  = (CommandComponent)comp.GetCommand(m_world.FrameCount);
            cmd.id    = list[i].ID;
            cmd.frame = m_world.FrameCount;

            list[i].ChangeComp(cmd);

            //Debug.Log("USE cmd id "+ list[i].ID + " frame " + cmd.frame + " content " + Serializer.Serialize(cmd));

            //到了这一帧还没有发送命令的,给预测一个并广播给所有前端
            if (comp.LastInputFrame < m_world.FrameCount)
            {
                for (int j = 0; j < list.Count; j++)
                {
                    ConnectionComponent conn = list[j].GetComp <ConnectionComponent>();

                    //lock(conn.m_unConfirmFrame)
                    {
                        ////补发未确认的帧
                        //foreach (var item in conn.m_unConfirmFrame)
                        //{
                        //    CommandMsg info = item.Value;
                        //    ProtocolAnalysisService.SendMsg(conn.m_session, info);
                        //}

                        //cmsg.index = conn.GetSendIndex();

                        //conn.m_unConfirmFrame.Add(cmsg.index, cmd);

                        ProtocolAnalysisService.SendMsg(conn.m_session, cmd);

                        //Debug.Log("消息落后或掉线 预测一个 输入并派发 comp.lastInputFrame " + comp.lastInputFrame  + " m_world.FrameCount " + m_world.FrameCount +" ID " + comp.Entity.ID);
                    }
                }
            }
        }
    }
    static void ReceviceSyncMsg(SyncSession session, T msg)
    {
        //Debug.Log("ReceviceSyncMsg " + msg.id);

        ConnectionComponent connectComp = session.m_connect;
        WorldBase           world       = session.m_connect.Entity.World;

        if (connectComp != null)
        {
            if (msg.frame > world.FrameCount)
            {
                //消息确认
                AffirmMsg amsg = new AffirmMsg();
                amsg.frame = msg.frame;
                amsg.time  = msg.time;

                BroadcastCommand(world, connectComp, msg, false);

                ProtocolAnalysisService.SendMsg(session, amsg);

                connectComp.m_commandList.Add(msg);
                connectComp.lastInputFrame = msg.frame;
            }
            else
            {
                //把玩家的这次上报当做最新的操作并转发
                Debug.Log("帧数落后  world.FrameCount: " + world.FrameCount + " msg frame:" + msg.frame + " 预测列表计数 " + connectComp.m_forecastList.Count);
                //Debug.Log("接收玩家数据 " + Serializer.Serialize(msg));
                connectComp.m_lastInputCache = msg;
                connectComp.lastInputFrame   = world.FrameCount;

                //并且让这个玩家提前
                PursueMsg pmsg = new PursueMsg();
                pmsg.id           = msg.id;
                pmsg.recalcFrame  = msg.frame;
                pmsg.frame        = world.FrameCount;
                pmsg.advanceCount = CalcAdvanceFrame(connectComp);
                pmsg.serverTime   = ServiceTime.GetServiceTime();

                ProtocolAnalysisService.SendMsg(session, pmsg);
            }
        }
    }
Exemple #8
0
    static void ReceviceSameCmdMsg(SyncSession session, SameCommand msg)
    {
        //消息确认
        AffirmMsg amsg = new AffirmMsg();

        amsg.index = msg.frame;
        amsg.time  = msg.time;
        ProtocolAnalysisService.SendMsg(session, amsg);

        ConnectionComponent connectComp = session.m_connect;

        if (connectComp != null)
        {
            WorldBase world = connectComp.Entity.World;

            if (world.FrameCount <= msg.frame + 4)
            {
                //取上一帧的数据
                T scmd = (T)connectComp.GetCommand(msg.frame - 1).DeepCopy();
                scmd.frame = world.FrameCount + 1;
                connectComp.AddCommand(scmd);
            }

            //    //取上一帧的数据
            //    T scmd = (T)connectComp.GetCommand(msg.frame - 1).DeepCopy();
            ////msg.frame = world.FrameCount + 1;
            //    scmd.frame = world.FrameCount + 1;
            //if (connectComp.AddCommand(scmd))
            //    {
            //        //BroadcastSameCommand(world, connectComp, msg, true);
            //    }
            //}
            //else
            //{
            //    //Debug.Log("Same frame " + world.FrameCount);
            //    //scmd.frame = world.FrameCount + 1;
            //    //connectComp.AddCommand(scmd);
            //}

            ControlSpeed(connectComp, world, msg.frame);
        }
    }
    public void RecevicePlayerLogin(SyncSession session, PlayerLoginMsg_s e)
    {
        Debug.Log("RecevicePlayerLogin");

        if (session.player != null)
        {
            Debug.Log("" + session.player.playerID + " 已经登录,不需要重复登录! ");
        }

        string clauseContent = "ID ='" + e.playerID + "'";
        var    result        = DataBaseService.database.Query(c_playerTableName, null, clauseContent, null, null, null, null);

        if (result.MoveToNext())
        {
            Debug.Log("查询到记录! ");

            session.player = GetOldPlayer(result);

            result.Close();
        }
        else
        {
            result.Close();
            Debug.Log("未查询到记录!");

            session.player = GetNewPlayer();

            Dictionary <string, string> value = new Dictionary <string, string>();
            value.Add("ID", e.playerID);
            DataBaseService.database.Insert(c_playerTableName, null, value);
        }

        session.player.playerID = e.playerID;
        session.player.session  = session;

        PlayerLoginMsg_c msg = new PlayerLoginMsg_c();

        ProtocolAnalysisService.SendMsg(session, msg);

        //派发玩家登陆事件
        m_service.OnPlayerLogin(session.player);
    }
Exemple #10
0
    protected override void OnPlayerLogin(Player player)
    {
        if (m_disConnectDict.ContainsKey(player.playerID))
        {
            //重连吧少年
            PlayerMatchMsg_c msg = new PlayerMatchMsg_c();
            msg.isMatched   = true;
            msg.predictTime = 0;
            msg.matchInfo   = new List <MatchPlayerInfo>();
            ProtocolAnalysisService.SendMsg(player.session, msg);

            ConnectionComponent conn = m_disConnectDict[player.playerID];
            conn.m_session           = player.session;
            conn.m_session.m_connect = conn;

            m_disConnectDict.Remove(player.playerID);

            conn.Entity.World.eventSystem.DispatchEvent(ServiceEventDefine.c_playerReconnect, conn.Entity);
        }
    }
    void Match(Player player)
    {
        if (!matchList.Contains(player))
        {
            matchList.Add(player);

            PlayerMatchMsg_c msg = new PlayerMatchMsg_c();
            msg.predictTime = 300;
            msg.isMatched   = false;

            ProtocolAnalysisService.SendMsg(player.session, msg);

            //TODO 潜在的线程不安全隐患 ---> 公共的matchList
            if (matchList.Count >= roomPeopleNum)
            {
                //构造玩家列表
                Player[] tmp = new Player[roomPeopleNum];

                for (int i = 0; i < roomPeopleNum; i++)
                {
                    tmp[i] = matchList[i];
                }

                //将已匹配到的玩家从列表中删除
                for (int i = 0; i < roomPeopleNum; i++)
                {
                    matchList.Remove(tmp[i]);
                }

                StartGame(tmp);
            }
        }
        else
        {
            Debug.Log("已经在匹配队列了!");
        }
    }
Exemple #12
0
    public override void NoRecalcBeforeFixedUpdate(int deltaTime)
    {
        List <EntityBase> list = GetEntityList();

        for (int i = 0; i < list.Count; i++)
        {
            ConnectionComponent comp = list[i].GetComp <ConnectionComponent>();
            CommandComponent    cmd  = (CommandComponent)comp.GetCommand(m_world.FrameCount);
            cmd.id    = list[i].ID;
            cmd.frame = m_world.FrameCount;

            list[i].ChangeComp(cmd);

            //到了这一帧还没有发送命令的,给预测一个并广播给所有前端
            if (comp.lastInputFrame < m_world.FrameCount)
            {
                for (int j = 0; j < list.Count; j++)
                {
                    ConnectionComponent conn = list[j].GetComp <ConnectionComponent>();
                    ProtocolAnalysisService.SendMsg(comp.m_session, cmd);
                }
            }
        }
    }
Exemple #13
0
    public override void EndFrame(int deltaTime)
    {
        if (!isDebug)
        {
            return;
        }

        DebugMsg msg = new DebugMsg();

        msg.frame          = m_world.FrameCount;
        msg.seed           = m_world.m_RandomSeed;
        msg.infos          = new List <EntityInfo>();
        msg.singleCompInfo = new List <ComponentInfo>();

        for (int i = 0; i < m_world.m_entityList.Count; i++)
        {
            msgCache.Append(m_world.m_entityList[i].ID + "\n");
        }

        msg.msg = msgCache.ToString();

        msgCache.Clear();

        for (int i = 0; i < m_world.m_entityList.Count; i++)
        {
            EntityBase eb = m_world.m_entityList[i];

            bool isFilter = false;
            if (isPlayerOnly &&
                !eb.GetExistComp(ComponentType.ConnectionComponent))
            {
                isFilter = true;
            }

            if (isFlyObject &&
                !eb.GetExistComp(ComponentType.FlyObjectComponent))
            {
                isFilter = true;
            }

            if (isFilter)
            {
                continue;
            }

            EntityInfo einfo = new EntityInfo();
            einfo.id = eb.ID;

            einfo.infos = new List <ComponentInfo>();

            foreach (var item in eb.comps)
            {
                if (item == null)
                {
                    continue;
                }

                if (item.GetType().IsSubclassOf(typeof(PlayerCommandBase)))
                {
                    CommandComponent cc   = (CommandComponent)item;
                    ComponentInfo    info = new ComponentInfo();
                    cc.time         = 0;
                    cc.id           = eb.ID;
                    cc.frame        = m_world.FrameCount;
                    info.m_compName = item.GetType().Name;
                    info.content    = Serializer.Serialize(item);

                    einfo.infos.Add(info);
                }
                else if (IsFilter(item.GetType().Name))
                {
                    ComponentInfo info = new ComponentInfo();
                    info.m_compName = item.GetType().Name;
                    info.content    = Serializer.Serialize(item);

                    einfo.infos.Add(info);
                }
            }

            if (einfo.infos.Count > 0)
            {
                msg.infos.Add(einfo);
            }
        }

        for (int i = 0; i < SingleCompFilter.Length; i++)
        {
            SingletonComponent sc   = m_world.GetSingletonComp(SingleCompFilter[i]);
            ComponentInfo      info = new ComponentInfo();

            info.m_compName = SingleCompFilter[i];
            info.content    = Serializer.Serialize(sc);

            if (info.m_compName == "MapGridStateComponent")
            {
                MapGridStateComponent lmsc = (MapGridStateComponent)sc;
                MapGridStateComponent msc  = des.Deserialize <MapGridStateComponent>(info.content);

                if (!JudgeDict(msc.globalRandomCellHaveItemList, lmsc.globalRandomCellHaveItemList))
                {
                    Debug.Log("验证错误" + m_world.FrameCount);
                }
                else
                {
                    Debug.Log("验证通过 " + m_world.FrameCount);
                }
            }

            msg.singleCompInfo.Add(info);
        }

        List <EntityBase> list = GetEntityList();

        for (int i = 0; i < list.Count; i++)
        {
            ConnectionComponent cc = list[i].GetComp <ConnectionComponent>();
            ProtocolAnalysisService.SendMsg(cc.m_session, msg);
        }
    }
    public override void EndFrame(int deltaTime)
    {
        if (!isDebug)
        {
            return;
        }

        DebugMsg msg = new DebugMsg();

        msg.frame          = m_world.FrameCount;
        msg.seed           = m_world.m_RandomSeed;
        msg.infos          = new List <EntityInfo>();
        msg.singleCompInfo = new List <ComponentInfo>();

        for (int i = 0; i < m_world.m_entityList.Count; i++)
        {
            EntityBase eb = m_world.m_entityList[i];

            if (isPlayerOnly &&
                !eb.GetExistComp <ConnectionComponent>())
            {
                continue;
            }

            EntityInfo einfo = new EntityInfo();
            einfo.id = eb.ID;

            einfo.infos = new List <ComponentInfo>();

            foreach (var item in eb.CompDict)
            {
                if (item.Value.GetType().IsSubclassOf(typeof(PlayerCommandBase)))
                {
                    CommandComponent cc   = (CommandComponent)item.Value;
                    ComponentInfo    info = new ComponentInfo();
                    cc.time         = 0;
                    cc.id           = eb.ID;
                    cc.frame        = m_world.FrameCount;
                    info.m_compName = item.Value.GetType().Name;
                    info.content    = Serializer.Serialize(item.Value);

                    einfo.infos.Add(info);
                }
                else if (IsFilter(item.Value.GetType().Name))
                {
                    ComponentInfo info = new ComponentInfo();
                    info.m_compName = item.Value.GetType().Name;
                    info.content    = Serializer.Serialize(item.Value);

                    einfo.infos.Add(info);
                }
            }

            if (einfo.infos.Count > 0)
            {
                msg.infos.Add(einfo);
            }
        }

        for (int i = 0; i < SingleCompFilter.Length; i++)
        {
            SingletonComponent sc   = m_world.GetSingletonComp(SingleCompFilter[i]);
            ComponentInfo      info = new ComponentInfo();

            info.m_compName = SingleCompFilter[i];
            info.content    = Serializer.Serialize(sc);

            msg.singleCompInfo.Add(info);
        }

        List <EntityBase> list = GetEntityList();

        for (int i = 0; i < list.Count; i++)
        {
            ConnectionComponent cc = list[i].GetComp <ConnectionComponent>();
            ProtocolAnalysisService.SendMsg(cc.m_session, msg);
        }
    }