Example #1
0
 void OnVerifyResult(ProtocolVerifyRsp rsp)
 {
     if (verifyTimeOut != null)
     {
         verifyTimeOut.Stop();
         verifyTimeOut = null;
     }
     WaitDialogState.State.WaitExit(0.5f);
     VerifySuccess = rsp.result == 1;
     if (rsp.result == 1)
     {
         U3D.InsertSystemMsg("验证成功,拉取房间列表");
         if (MainLobbyDialogState.Exist)
         {
             MainLobbyDialogState.Instance.OnSelectService();
         }
         NetWorkBattle.Ins.PlayerId = (int)rsp.player;
         TcpClientProxy.Ins.UpdateGameServer();//取得房间列表
     }
     else
     {
         NetWorkBattle.Ins.PlayerId = -1;
         U3D.PopupTip(rsp.message);
         TcpClientProxy.Ins.Exit();
     }
 }
 static void OnVerifyResult(ProtocolVerifyRsp rsp)
 {
     if (rsp.result == 1)
     {
         TcpClientProxy.UpdateGameServer();//取得房间列表
     }
     else
     {
         U3D.PopupTip(string.Format("客户端版本过低-无法连接到服务器"));
         TcpClientProxy.Exit();
     }
 }
    //套接口消息.
    public static void Update()
    {
        //处理网络消息
        for (int i = 0; i < packets.Count; i++)
        {
            Dictionary <int, byte[]> pack = packets[i];
            lock (pack)
            {
                MemoryStream ms = null;
                try
                {
                    foreach (var each in pack)
                    {
                        //UnityEngine.Debug.LogError(string.Format("收到:{0}", each.Key));
                        switch (each.Key)
                        {
                        case (int)MeteorMsg.MsgType.ProtocolVerifyRsp:
                            ms = new MemoryStream(each.Value);
                            ProtocolVerifyRsp rspVer = ProtoBuf.Serializer.Deserialize <ProtocolVerifyRsp>(ms);
                            OnVerifyResult(rspVer);
                            break;

                        case (int)MeteorMsg.MsgType.GetRoomRsp:
                            ms = new MemoryStream(each.Value);
                            GetRoomRsp rspG = ProtoBuf.Serializer.Deserialize <GetRoomRsp>(ms);
                            OnGetRoomRsp(rspG);
                            break;

                        case (int)MeteorMsg.MsgType.JoinRoomRsp:
                            ms = new MemoryStream(each.Value);
                            JoinRoomRsp rspJ = ProtoBuf.Serializer.Deserialize <JoinRoomRsp>(ms);
                            ClientJoinRoomRsp(rspJ);
                            break;

                        //case (int)MeteorMsg.MsgType.OnJoinRoomRsp:
                        //    ms = new MemoryStream(each.Value);
                        //    OnEnterRoomRsp rspE = ProtoBuf.Serializer.Deserialize<OnEnterRoomRsp>(ms);
                        //    OnEnterRoomRsp_(rspE);
                        //    break;
                        case (int)MeteorMsg.MsgType.CreateRoomRsp:
                            ms = new MemoryStream(each.Value);
                            CreateRoomRsp rspC = ProtoBuf.Serializer.Deserialize <CreateRoomRsp>(ms);
                            OnCreateRoomRsp(rspC);
                            break;

                        //case (int)MeteorMsg.MsgType.EnterLevelRsp:
                        //    ms = new MemoryStream(each.Value);
                        //    EnterLevelRsp rspER = ProtoBuf.Serializer.Deserialize<EnterLevelRsp>(ms);
                        //    EnterLevelRsp_(rspER);
                        //    break;
                        //case (int)MeteorMsg.MsgType.OnEnterLevelRsp:
                        //    ms = new MemoryStream(each.Value);
                        //    OnEnterLevelRsp rspOE = ProtoBuf.Serializer.Deserialize<OnEnterLevelRsp>(ms);
                        //    OnEnterLevelRsp_(rspOE);
                        //    break;
                        //case (int)MeteorMsg.MsgType.UserRebornSB2C:
                        //    ms = new MemoryStream(each.Value);
                        //    OnEnterLevelRsp rspReborn = ProtoBuf.Serializer.Deserialize<OnEnterLevelRsp>(ms);
                        //    OnUserRebornRsp_(rspReborn);
                        //    break;
                        //case (int)MeteorMsg.MsgType.OnLeaveRoomRsp:
                        //    ms = new MemoryStream(each.Value);
                        //    OnLeaveRoomRsp rspL = ProtoBuf.Serializer.Deserialize<OnLeaveRoomRsp>(ms);
                        //    OnLeaveRoomRsp_(rspL);
                        //    break;
                        case (int)MeteorMsg.MsgType.ChatInRoomRsp:
                            ms = new MemoryStream(each.Value);
                            ChatMsg chatRsp = ProtoBuf.Serializer.Deserialize <ChatMsg>(ms);
                            OnReceiveChatMsg(chatRsp);
                            break;

                        case (int)MeteorMsg.MsgType.AudioChat:
                            ms = new MemoryStream(each.Value);
                            AudioChatMsg audioRsp = ProtoBuf.Serializer.Deserialize <AudioChatMsg>(ms);
                            OnReceiveAudioMsg(audioRsp);
                            break;
                        //case (int)MeteorMsg.MsgType.UserDeadSB2C:
                        //    //Debug.LogError("OnUserDead");
                        //    ms = new MemoryStream(each.Value);
                        //    UserId userDeadRsp = ProtoBuf.Serializer.Deserialize<UserId>(ms);
                        //    OnUserDead(userDeadRsp);
                        //    break;

                        //case (int)MeteorMsg.MsgType.ExitQueueRsp:
                        //    OnExitQueue();
                        //    break;
                        case (int)MeteorMsg.MsgType.EnterQueueRsp:
                            OnEnterQueue();
                            break;

                        //帧同步信息-UDP
                        //收到服务器的帧同步信息.
                        case (int)MeteorMsg.MsgType.SyncCommand:
                            ms = new MemoryStream(each.Value);
                            GameFrames t = ProtoBuf.Serializer.Deserialize <GameFrames>(ms);
                            Main.Ins.FrameSync.OnReceiveCommand(t);
                            break;
                        }
                    }
                }
                catch (Exception exp)
                {
                    UnityEngine.Debug.LogError(exp.Message + exp.StackTrace);
                }
                finally
                {
                    pack.Clear();
                }
            }
        }

        lock (messageQueue)
        {
            int length = messageQueue.Count;
            for (int i = 0; i < length; i++)
            {
                switch (messageQueue[i].Message)
                {
                case (short)LocalMsgType.Connect: OnConnect(messageQueue[i].Result, messageQueue[i].message); break;

                case (short)LocalMsgType.DisConnect: OnDisconnect(); break;

                case (short)LocalMsgType.SendFTPLogComplete: OnSendComplete(messageQueue[i].Result, messageQueue[i].Param); break;

                case (short)LocalMsgType.GameStart: OnGameStart(); break;

                case (short)LocalMsgType.SaveRecord: OnSaveRecord(messageQueue[i]); break;

                case (short)LocalMsgType.PathCalcFinished: OnPathCalcFinished(messageQueue[i]); break;
                }
            }
            messageQueue.Clear();
        }
    }
Example #4
0
    public override void Update()
    {
        for (int i = 0; i < packets.Count; i++)
        {
            SortedDictionary <int, byte[]> pack = packets[i];
            lock (pack) {
                try {
                    foreach (var each in pack)
                    {
                        ms.SetLength(0);
                        ms.Write(each.Value, 0, each.Value.Length);
                        ms.Seek(0, SeekOrigin.Begin);
                        TcpClientProxy.Ins.endPing = Time.time;
                        TcpClientProxy.Ins.ping    = Mathf.FloorToInt(1000 * (TcpClientProxy.Ins.endPing - TcpClientProxy.Ins.startPing));
                        Main.Ins.EventBus.Fire(EventId.PingChanged);
                        switch (each.Key)
                        {
                        case (int)MeteorMsg.MsgType.ProtocolVerify:
                            //ms = new MemoryStream(each.Value);
                            ProtocolVerifyRsp rspVer = Serializer.Deserialize <ProtocolVerifyRsp>(ms);
                            OnVerifyResult(rspVer);
                            break;

                        case (int)MeteorMsg.MsgType.AliveUpdate:
                            TcpClientProxy.Ins.HeartBeat();
                            break;

                        case (int)MeteorMsg.MsgType.GetRoomRsp:
                            //ms = new MemoryStream(each.Value);
                            GetRoomRsp rspG = Serializer.Deserialize <GetRoomRsp>(ms);
                            OnGetRoomRsp(rspG);
                            break;

                        case (int)MeteorMsg.MsgType.JoinRoomRsp:
                            //ms = new MemoryStream(each.Value);
                            JoinRoomRsp rspJ = Serializer.Deserialize <JoinRoomRsp>(ms);
                            ClientJoinRoomRsp(rspJ);
                            break;

                        case (int)MeteorMsg.MsgType.CreateRoomRsp:
                            //ms = new MemoryStream(each.Value);
                            CreateRoomRsp rspC = Serializer.Deserialize <CreateRoomRsp>(ms);
                            OnCreateRoomRsp(rspC);
                            break;

                        case (int)MeteorMsg.MsgType.OnPlayerJoinRoom:
                            //ms = new MemoryStream(each.Value);
                            OnPlayerJoinRoom rspOE = Serializer.Deserialize <OnPlayerJoinRoom>(ms);
                            OnPlayerEnterRoomRsp(rspOE);
                            break;

                        //case (int)MeteorMsg.MsgType.UserRebornSB2C:
                        //    ms = new MemoryStream(each.Value);
                        //    OnEnterLevelRsp rspReborn = ProtoBuf.Serializer.Deserialize<OnEnterLevelRsp>(ms);
                        //    OnUserRebornRsp_(rspReborn);
                        //    break;
                        //case (int)MeteorMsg.MsgType.OnLeaveRoomRsp:
                        //    ms = new MemoryStream(each.Value);
                        //    OnLeaveRoomRsp rspL = ProtoBuf.Serializer.Deserialize<OnLeaveRoomRsp>(ms);
                        //    OnLeaveRoomRsp_(rspL);
                        //    break;
                        case (int)MeteorMsg.MsgType.OnPlayerEnterLevel:    //其他玩家进入关卡
                            //ms = new MemoryStream(each.Value);
                            PlayerEvent rspEnterLevel = Serializer.Deserialize <PlayerEvent>(ms);
                            OnPlayerEnterLevel(rspEnterLevel);
                            break;

                        case (int)MeteorMsg.MsgType.EnterLevelRsp:    //自己进入关卡,拉取到场景里所有已知角色
                            //ms = new MemoryStream(each.Value);
                            OnEnterLevelRsp EnterLevel = Serializer.Deserialize <OnEnterLevelRsp>(ms);
                            OnEnterLevel(EnterLevel);
                            break;

                        case (int)MeteorMsg.MsgType.ChatInRoomRsp:
                            //ms = new MemoryStream(each.Value);
                            ChatMsg chatRsp = Serializer.Deserialize <ChatMsg>(ms);
                            OnReceiveChatMsg(chatRsp);
                            break;

                        //case (int)MeteorMsg.MsgType.AudioChat:
                        //    //ms = new MemoryStream(each.Value);
                        //    AudioChatMsg audioRsp = Serializer.Deserialize<AudioChatMsg>(ms);
                        //    OnReceiveAudioMsg(audioRsp);
                        //    break;
                        case (int)MeteorMsg.MsgType.OnPlayerLeaveLevel:    //其他玩家离开关卡
                            //ms = new MemoryStream(each.Value);
                            PlayerEvent rspLeaveLevel = Serializer.Deserialize <PlayerEvent>(ms);
                            OnPlayerLeaveLevel(rspLeaveLevel);
                            break;

                        case (int)MeteorMsg.MsgType.SyncRate:    //KCP识别出错,关闭旧的KCP,创建新的
                            SyncMsg syncRate = Serializer.Deserialize <SyncMsg>(ms);
                            FrameSyncServer.Ins.ChangeSyncRate(syncRate.syncrate);
                            break;
                        }
                    }
                } catch (Exception exp) {
                    UnityEngine.Debug.LogError(exp.Message + exp.StackTrace);
                } finally {
                    pack.Clear();
                }
            }
        }

        lock (messageQueue) {
            int length = messageQueue.Count;
            for (int i = 0; i < length; i++)
            {
                switch (messageQueue[i].Message)
                {
                case (short)LocalMsgType.TimeOut: OnTimeOut(messageQueue[i].Result, messageQueue[i].message); break;

                case (short)LocalMsgType.Connect: OnConnect(messageQueue[i].Result, messageQueue[i].message); break;

                case (short)LocalMsgType.DisConnect: OnDisconnect(); break;

                case (short)LocalMsgType.SendFTPLogComplete: OnSendComplete(messageQueue[i].Result, messageQueue[i].Param); break;

                case (short)LocalMsgType.SaveRecord: OnSaveRecord(messageQueue[i]); break;
                }
            }
            messageQueue.Clear();
        }
    }