Пример #1
0
    private void RecvThread()
    {
        isRecv = true;
        IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(NetGlobal.Instance().serverIP), UdpManager.Instance.localPort);

        while (isRecv)
        {
            try {
                byte[] buf = sendClient.Receive(ref endpoint);

                byte   packMessageId = buf[PackageConstant.PackMessageIdOffset];                    //消息id (1个字节)
                Int16  packlength    = BitConverter.ToInt16(buf, PackageConstant.PacklengthOffset); //消息包长度 (2个字节)
                int    bodyDataLenth = packlength - PackageConstant.PacketHeadLength;
                byte[] bodyData      = new byte[bodyDataLenth];
                Array.Copy(buf, PackageConstant.PacketHeadLength, bodyData, 0, bodyDataLenth);

                delegate_analyze_message((SCID)packMessageId, bodyData);

                //是客户端,统计接收量
                BattleData.Instance.recvNum += buf.Length;
//				Debug.Log("发送量:" + buf.Length.ToString() + "," + GameData.Instance().recvNum.ToString());
            } catch (Exception ex) {
                Debug.Log("udpClient接收数据异常:" + ex.Message);
            }
        }
        Debug.Log("udp接收线程退出~~~~~");
    }
Пример #2
0
    public void OnCliclCancelMatch()
    {
        //取消匹配
        TcpCancelMatch _mes = new TcpCancelMatch();

        _mes.uid = NetGlobal.Instance().userUid;
        MarsTcp.Instance.SendMessage(CSData.GetSendMessage <TcpCancelMatch>(_mes, CSID.TCP_CANCEL_MATCH));
    }
Пример #3
0
    public void StartClientUdp()
    {
        mesNum = 0;
        _upd   = new MarsUdp();
        _upd.StartClientUdp(NetGlobal.Instance().serverIP);

        _upd.delegate_analyze_message = AnalyzeMessage;
    }
Пример #4
0
 public static NetGlobal Instance()
 {
     // 如果类的实例不存在则创建,否则直接返回
     if (singleInstance == null)
     {
         singleInstance = new NetGlobal();
     }
     return(singleInstance);
 }
Пример #5
0
    void Start()
    {
        Screen.sleepTimeout         = SleepTimeout.NeverSleep;
        Application.targetFrameRate = 30;

        NetGlobal.Instance();
        TcpPB.Instance().mes_login_result = Message_Login_Result;
        waitTip.SetActive(false);
    }
Пример #6
0
    public void OnClickMatch()
    {
        //开始匹配
//		SceneManager.LoadScene (GameConfig.battleScene);
        TcpRequestMatch _mes = new TcpRequestMatch();

        _mes.uid    = NetGlobal.Instance().userUid;
        _mes.roleID = 1;
        MarsTcp.Instance.SendMessage(CSData.GetSendMessage <TcpRequestMatch>(_mes, CSID.TCP_REQUEST_MATCH));
    }
Пример #7
0
    void CreatUpd()
    {
        _udpClient = new UdpClient();
        IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(NetGlobal.Instance().serverIP), NetGlobal.Instance().udpSendPort);

        _udpClient.Connect(endpoint);
        IPEndPoint _localEnd = (IPEndPoint)_udpClient.Client.LocalEndPoint;

        localPort = _localEnd.Port;
        Debug.Log("udp参数:" + _localEnd.Address + "," + _localEnd.Port);
    }
Пример #8
0
 void Message_Login_Result(TcpResponseLogin _mes)
 {
     if (_mes.result)
     {
         NetGlobal.Instance().userUid     = _mes.uid;
         NetGlobal.Instance().udpSendPort = _mes.udpPort;
         Debug.Log("登录成功~~~" + NetGlobal.Instance().userUid);
         ClearSceneData.LoadScene(GameConfig.mainScene);
     }
     else
     {
         Debug.Log("登录失败~~~");
         waitTip.SetActive(false);
     }
 }
Пример #9
0
    public void UpdateBattleInfo(int _randseed, List <BattleUserInfo> _userInfo)
    {
        randSeed        = _randseed;
        list_battleUser = new List <BattleUserInfo> (_userInfo);

        foreach (var item in list_battleUser)
        {
            if (item.uid == NetGlobal.Instance().userUid)
            {
                battleID = item.battleID;
                selfOperation.battleID = battleID;
                Debug.Log("自己的战斗id:" + battleID);
            }

            dic_rightOperationID [item.battleID] = 0;
        }
    }
Пример #10
0
    public void AnalyzeMessage(SCID messageId, byte[] bodyData)
    {
        switch (messageId)
        {
        case SCID.TCP_RESPONSE_LOGIN:
        {
            TcpResponseLogin pb_ReceiveMes = CSData.DeserializeData <TcpResponseLogin>(bodyData);
            NetGlobal.Instance().AddAction(() => {
                    mes_login_result(pb_ReceiveMes);
                });
        }
        break;

        case SCID.TCP_RESPONSE_REQUEST_MATCH:
        {
            TcpResponseRequestMatch pb_ReceiveMes = CSData.DeserializeData <TcpResponseRequestMatch>(bodyData);
            NetGlobal.Instance().AddAction(() => {
                    mes_request_match_result(pb_ReceiveMes);
                });
        }
        break;

        case SCID.TCP_RESPONSE_CANCEL_MATCH:
        {
            TcpResponseCancelMatch pb_ReceiveMes = CSData.DeserializeData <TcpResponseCancelMatch>(bodyData);
            NetGlobal.Instance().AddAction(() => {
                    mes_cancel_match_result(pb_ReceiveMes);
                });
        }
        break;

        case SCID.TCP_ENTER_BATTLE:
        {
            TcpEnterBattle pb_ReceiveMes = CSData.DeserializeData <TcpEnterBattle>(bodyData);
            NetGlobal.Instance().AddAction(() => {
                    mes_enter_battle(pb_ReceiveMes);
                });
        }
        break;

        default:
            break;
        }
    }
Пример #11
0
    public void AnalyzeMessage(SCID messageId, byte[] bodyData)
    {
        switch (messageId)
        {
        case SCID.UDP_BATTLE_START:
        {
            UdpBattleStart pb_ReceiveMes = CSData.DeserializeData <UdpBattleStart>(bodyData);
            NetGlobal.Instance().AddAction(() => {
                    mes_battle_start(pb_ReceiveMes);
                });
        }
        break;

        case SCID.UDP_DOWN_FRAME_OPERATIONS:
        {
            UdpDownFrameOperations pb_ReceiveMes = CSData.DeserializeData <UdpDownFrameOperations>(bodyData);
            NetGlobal.Instance().AddAction(() => {
                    mes_frame_operation(pb_ReceiveMes);
                });
        }
        break;

        case SCID.UDP_DOWN_DELTA_FRAMES:
        {
            UdpDownDeltaFrames pb_ReceiveMes = CSData.DeserializeData <UdpDownDeltaFrames>(bodyData);
            NetGlobal.Instance().AddAction(() => {
                    mes_delta_frame_data(pb_ReceiveMes);
                });
        }
        break;

        case SCID.UDP_DOWN_GAME_OVER:
        {
            UdpDownGameOver pb_ReceiveMes = CSData.DeserializeData <UdpDownGameOver>(bodyData);
            NetGlobal.Instance().AddAction(() => {
                    mes_down_game_over(pb_ReceiveMes);
                });
        }
        break;

        default:
            break;
        }
    }
Пример #12
0
    private void requestConnectCallBack(IAsyncResult iar)
    {
        try
        {
            //还原原始的TcpClient对象
            Socket client = (Socket)iar.AsyncState;
            //
            client.EndConnect(iar);

            Debug.Log("连接服务器成功:" + client.RemoteEndPoint.ToString());
            isRun = true;

            NetGlobal.Instance().AddAction(() => {
                if (ac_connect != null)
                {
                    ac_connect(true);
                }
            });


            Thread myThread = new Thread(ReceiveMessage);
            myThread.Start();
        }
        catch (Exception e)
        {
            NetGlobal.Instance().AddAction(() => {
                if (ac_connect != null)
                {
                    ac_connect(false);
                }
            });

            Debug.Log("tcp连接异常:" + e.Message);
        }
        finally
        {
        }
    }
Пример #13
0
    public void OnClickLogin()
    {
        Debug.Log(inputField.text);

        waitTip.SetActive(true);

        string _ip = inputField.text;

        MarsTcp.Instance.ConnectServer(_ip, (_result) => {
            if (_result)
            {
                Debug.Log("连接成功~~");
                NetGlobal.Instance().serverIP = _ip;
                TcpLogin _loginInfo           = new TcpLogin();
                _loginInfo.token = SystemInfo.deviceUniqueIdentifier;
                MarsTcp.Instance.SendMessage(CSData.GetSendMessage <TcpLogin>(_loginInfo, CSID.TCP_LOGIN));
            }
            else
            {
                Debug.Log("连接失败~~");
                waitTip.SetActive(false);
            }
        });
    }
Пример #14
0
 void Send_BattleReady()
 {
     UdpPB.Instance().SendBattleReady(NetGlobal.Instance().userUid, BattleData.Instance.battleID);
 }
Пример #15
0
 void Update()
 {
     NetGlobal.Instance().DoForAction();
 }
Пример #16
0
 public void Destory()
 {
     singleInstance = null;
 }