Exemplo n.º 1
0
        private void OnOptimisticFrame(Session client, GM_Frame recvData)
        {
            {
                int roleId = recvData.roleId;

                long frame = recvData.frame;

                Debug.Log(string.Format("Receive roleid={0} serverframe:{1} clientframe:{2} command:{3}", roleId, mCurrentFrame, frame, recvData.command.Count), ConsoleColor.DarkYellow);

                if (mFrameDic.ContainsKey(mCurrentFrame) == false)
                {
                    mFrameDic[mCurrentFrame] = new Dictionary <int, List <Command> >();
                }
                for (int i = 0; i < recvData.command.Count; ++i)
                {
                    //乐观模式以服务器收到的时间为准
                    Command frameData = new Command(recvData.command[i].frame, recvData.command[i].type, recvData.command[i].data, mFrameTime);
                    frameData.eventType = recvData.command[i].eventType;
                    if (mFrameDic[mCurrentFrame].ContainsKey(roleId) == false)
                    {
                        mFrameDic[mCurrentFrame].Add(roleId, new List <Command>());
                    }
                    mFrameDic[mCurrentFrame][roleId].Add(frameData);
                }
            }
        }
Exemplo n.º 2
0
    /// <summary>
    /// 发送关键帧
    /// </summary>
    private void SendFrame()
    {
        if (mSentFrame >= mCurrentFrame)
        {
            return;
        }

        GM_Frame sendData = SharedValue <GM_Frame> .sData;

        sendData.command.Clear();

        sendData.roleId    = PlayerManager.GetSingleton().mRoleId;
        sendData.frame     = mCurrentFrame;
        sendData.frametime = mFrameTime;
        lock (mCommandQueue)
        {
            while (mCommandQueue.Count > 0)
            {
                Command   frame = mCommandQueue.Dequeue();
                GMCommand cmd   = ProtoTransfer.Get(frame);

                sendData.command.Add(cmd);
            }
            mCommandQueue.Clear();
        }

        ClientService.GetSingleton().SendUdp(ClientID.Frame, MessageID.GM_FRAME_CS, sendData);


        mSentFrame++;
    }
Exemplo n.º 3
0
        private void OnMessage(Session client, MessageBuffer msg)
        {
            MessageID messageId = (MessageID)msg.id();

            switch (messageId)
            {
            case MessageID.GM_ACCEPT_CS:
            {
                GM_Accept recvData = JsonSerializerUtil.FromJsonByte <GM_Accept>(msg.body());
                //ProtoTransfer.DeserializeProtoBuf<GM_Accept>(msg);
                if (recvData.conv == client.id)
                {
                    OnConnect(client, recvData.roleId);
                }
            }
            break;

            case MessageID.GM_READY_CS:
            {
                GM_Ready recvData = JsonSerializerUtil.FromJsonByte <GM_Ready>(msg.body());
                //ProtoTransfer.DeserializeProtoBuf<GM_Ready>(msg);
                OnReceiveReady(client, recvData);
            }
            break;

            case MessageID.GM_FRAME_CS:
            {
                GM_Frame recvData = JsonSerializerUtil.FromJsonByte <GM_Frame>(msg.body());
                //ProtoTransfer.DeserializeProtoBuf<GM_Frame>(msg);
                if (mMode == Mode.LockStep)
                {
                    OnLockStepFrame(client, recvData);
                }
                else
                {
                    OnOptimisticFrame(client, recvData);
                }
            }
            break;

            case MessageID.GM_PING_CS:
            {
                GM_Request recvData = JsonSerializerUtil.FromJsonByte <GM_Request>(msg.body());
                //ProtoTransfer.DeserializeProtoBuf<GM_Request>(msg);
                User u = GetUser(recvData.id);
                if (u != null)
                {
                    GM_Return sendData = new GM_Return();
                    sendData.id = recvData.id;
                    u.SendTcp(MessageID.GM_PING_SC, sendData);
                }
            }
            break;
            }
        }
Exemplo n.º 4
0
    void OnAddCommand(Command cmd)
    {
        if (cmd == null)
        {
            return;
        }

        if (mBegin == false)
        {
            Debug.Log("Frame not Begin ");

            return;
        }

        Debug.Log("AddFrame " + (CommandID)cmd.type);

        cmd.SetFrame(mCurrentFrame, mFrameTime);

        if (GameApplication.GetSingleton().mode == Mode.LockStep)
        {
            lock (mCommandQueue)
            {
                mCommandQueue.Enqueue(cmd);
            }
        }
        else
        {
            //乐观模式马上发送命令

            GM_Frame sendData = SharedValue <GM_Frame> .sData;
            sendData.command.Clear();

            sendData.roleId    = PlayerManager.GetSingleton().mRoleId;
            sendData.frame     = mCurrentFrame;
            sendData.frametime = mFrameTime;

            GMCommand data = ProtoTransfer.Get(cmd);

            sendData.command.Add(data);


            //string s1 = JsonSerializerUtil.ToJson<GM_Frame>(sendData);
            //GM_Frame sendData2 = JsonSerializerUtil.FromJson<GM_Frame>(s1);

            //byte[] bytes = JsonSerializerUtil.ToJsonByte<GM_Frame>(sendData);
            //GM_Frame sendData1 = JsonSerializerUtil.FromJsonByte<GM_Frame>(bytes);

            if (Debuger.ENABLELOG)
            {
                Debug.Log("当前帧:" + mCurrentFrame + "发送关键帧 = " + cmd.id.ToString() + ",frame = " + cmd.frame + ",time = " + cmd.time);
            }

            ClientService.GetSingleton().SendTcp(ClientID.Frame, MessageID.GM_FRAME_CS, sendData);
        }
    }
Exemplo n.º 5
0
    void OnAddCommand(Command cmd)
    {
        if (cmd == null)
        {
            return;
        }

        if (mBegin == false)
        {
            Debug.Log("Frame not Begin ");

            return;
        }

        Debug.Log("AddFrame " + (CommandID)cmd.type);

        cmd.SetFrame(mCurrentFrame, mFrameTime);

        if (GameApplication.GetSingleton().mode == Mode.LockStep)
        {
            lock (mCommandQueue)
            {
                mCommandQueue.Enqueue(cmd);
            }
        }
        else
        {
            //乐观模式马上发送命令

            GM_Frame sendData = SharedValue <GM_Frame> .sData;
            sendData.command.Clear();

            sendData.roleId    = PlayerManager.GetSingleton().mRoleId;
            sendData.frame     = mCurrentFrame;
            sendData.frametime = mFrameTime;

            GMCommand data = ProtoTransfer.Get(cmd);

            sendData.command.Add(data);
            ClientService.GetSingleton().SendUdp(ClientID.Frame, MessageID.GM_FRAME_CS, sendData);
        }
    }
Exemplo n.º 6
0
        private void OnLockStepFrame(Session client, GM_Frame recvData)
        {
            long frame  = recvData.frame;
            int  roleId = recvData.roleId;

            if (recvData.command.Count > 0 || frame % 30 == 0)
            {
                Debug.Log(string.Format("Receive {0} serverframe:{1} clientframe:{2} command:{3}", roleId, mCurrentFrame, frame, recvData.command.Count), ConsoleColor.DarkGray);
            }
            if (mFrameDic.ContainsKey(frame) == false)
            {
                mFrameDic.Add(frame, new Dictionary <int, List <Command> >());
            }

            var frames = mFrameDic[frame];

            //当前帧的服务器命令
            if (frames.ContainsKey(SERVER_ROLEID) == false)
            {
                frames.Add(SERVER_ROLEID, new List <Command>());
            }

            //该玩家是否发送了当前帧
            if (frames.ContainsKey(roleId) == false)
            {
                frames.Add(roleId, new List <Command>());
            }

            for (int i = 0; i < recvData.command.Count; ++i)
            {
                Command cmd = new Command(recvData.command[i].frame, recvData.command[i].type, recvData.command[i].data, recvData.command[i].frametime);

                frames[roleId].Add(cmd);
            }

            //当所有玩家都发送了该帧,就可以广播了
            //减去1是因为服务器命令也在当前帧中
            if (frames.Count - 1 >= mUserList.Count)
            {
                GM_Frame_BC sendData = new GM_Frame_BC();
                sendData.frame     = frame;
                sendData.frametime = mFrameTime;
                var it = frames.GetEnumerator();
                while (it.MoveNext())
                {
                    for (int i = 0, count = it.Current.Value.Count; i < count; ++i)
                    {
                        GMCommand cmd = ProtoTransfer.Get(it.Current.Value[i]);

                        sendData.command.Add(cmd);
                    }
                }


                BroadCast(MessageID.GM_FRAME_BC, sendData, true);

                mCurrentFrame = frame + 1;
            }
            else
            {
                Debug.Log(string.Format("Waiting {0} frame:{1} count:{2} current:{3} ", roleId, frame, mFrameDic[frame].Count, mUserList.Count), ConsoleColor.Red);
            }
        }