Beispiel #1
0
    /// <summary>
    /// 包含了一些初始化
    /// </summary>
    private void Awake()
    {
        //单例赋值
        _instance = this;
        GameObject.DontDestroyOnLoad(this);

        //随便设置一下
        InitPrint();//初始化日志系统,为了尽早的能够输出日志

#if UNITY_EDITOR
        Screen.SetResolution(854, 480, false);
        DNET.Config.DefaultConfigOnWindows();
        DNET.Config.IsAutoHeartbeat = false;
        //不输出日志了以查看GC(日志产生GC)
        Config.IsLogFile    = true;
        Config.IsLogConsole = true;
        DNET.DxDebug.isLog  = true;
        DNClient.GetInstance().isDebugLog = false;
#elif UNITY_ANDROID
        //Config.CreatLogFile("");
        Config.IsLogFile       = false;
        Config.IsLogConsole    = false;
        Config.IsAutoHeartbeat = true;
        //Config.SetCacheDir("");
#elif UNITY_IPHONE
        //Config.CreatLogFile("");
        Config.IsLogFile       = false;
        Config.IsLogConsole    = false;
        Config.IsAutoHeartbeat = true;
        //Config.SetCacheDir("");
#else
        Screen.SetResolution(854, 480, false);
        DNET.Config.DefaultConfigOnWindows();
#endif
    }
Beispiel #2
0
    /// <summary>
    /// 消息轮询处理,在update中调用
    /// </summary>
    private void MsgUpdate()
    {
        try
        {
            int msgCount = DNClient.GetInstance().GetReceiveData(dataBuff, 0, dataBuff.Length);
            if (msgCount != 0)
            {
                for (int i = 0; i < msgCount; i++)
                {
                    //递增一个标记,接收到了数据
                    receCount++;

                    ByteBuffer data = dataBuff[i];
                    if (data != null)
                    {
                        //DxDebug.LogConsole("接收到了" + data.validLength + "的数据");

                        //进行消息处理

                        data.Recycle();
                    }
                    else
                    {
                        DxDebug.LogConsole("NetPoll.MsgUpdate():接收到data为null!");
                    }
                }
            }
        }
        catch (Exception e)
        {
            DxDebug.LogConsole("NetPoll.MsgUpdate():执行MsgUpdate()异常");
        }
    }
Beispiel #3
0
 private void Start()
 {
     //挂上ShowFPS脚本
     if (gameObject.GetComponent <ShowFPS>() == null)
     {
         gameObject.AddComponent <ShowFPS>();
     }
     DNClient.GetInstance().Connect(IP, prot); //ywz:10.1.32.81  127.0.0.1
 }
Beispiel #4
0
    public void OnGUI()
    {
        GUILayout.Label("");
        GUILayout.Label("");//空出来显示fps
        GUILayout.Label("连接状态:" + DNClient.GetInstance().IsConnected);

        //GUILayout.Label("延迟:" + RequestStatus.GetInstance().Delay + "ms");
        GUILayout.Label("SaveGC:" + ByteBufferPool.countSaveGC);
        GUILayout.Label("BadGC:" + ByteBufferPool.countBadGC);
        GUILayout.Label("New:" + ByteBufferPool.countNew);
        if (GUILayout.Button("Exit Application"))
        {
            Application.Quit();
        }
    }
Beispiel #5
0
    /// <summary>
    /// 自动重连处理,在update中调用
    /// </summary>
    private void AutoReConnect()
    {
        try
        {
            _sumDeltaTime_ARC += Time.deltaTime;
            if (_sumDeltaTime_ARC >= 0.2f) //0.2秒的定时,每一秒都会进入
            {
                _sumDeltaTime_ARC = 0;
                //检测到了断线
                if (isAutoReConnect == true && DNClient.GetInstance().IsConnected == false && DNClient.GetInstance().IsConnecting == false)
                {
                    DxDebug.LogWarning("NetPoll.AutoReConnect():当前已经断线!开始自动重连...");
                    _isConnectionBreak = true;//标记当前已经断线
                    if (EventConnectionBreak != null)
                    {
                        EventConnectionBreak();//执行事件当前已经断线
                    }
                    //连一下服务器算了
                    DNClient.GetInstance().Connect(IP, prot); //ywz:10.1.32.81 "127.0.0.1"
                }

                //检测到了自动重连成功
                if (_isConnectionBreak == true && DNClient.GetInstance().IsConnected == true)//此时已经连接成功
                {
                    DxDebug.LogWarning("NetPoll.AutoReConnect():当前已经自动重连成功!");
                    _isConnectionBreak = false;//标记当前不再断线
                    if (EventReconnectsucceed != null)
                    {
                        EventReconnectsucceed();//执行事件当前已经重连成功
                    }
                }
            }
        }
        catch (Exception e)
        {
            DxDebug.LogWarning("NetPoll.AutoReConnect():执行AutoReConnect()异常!" + e.Message);
        }
    }
Beispiel #6
0
        public void TestMethod_SendRecePressure()
        {
            Config.DefaultConfigOnWindows();
            Config.IsAutoHeartbeat = false;
            DNClient.GetInstance().isDebugLog    = false;
            LogFile.GetInst().isImmediatelyFlush = false;
            DxDebug.isLog = false;

            DNServer.GetInstance().EventTokenReceData += (token) =>
            {
                byte[][] datas = token.GetReceiveData();
                if (datas == null)
                {
                    return;
                }

                for (int i = 0; i < datas.Length; i++)
                {
                    //收到的每一条消息.
                    byte[] data = datas[i];

                    DxDebug.LogConsole("服务端接收到:msgNum=" + BitConverter.ToInt32(data, 0));
                    //直接原样回发
                    DNServer.GetInstance().Send(token, data);

                    //得到消息类型然后处理
                    //int pType = BitConverter.ToInt32(data, 0);
                    //TypeRegister.GetInstance().Dispatch(token, pType, data, sizeof(int), data.Length - sizeof(int));
                }
            };

            DNServer.GetInstance().Start(21024);//启动服务器
            while (true)
            {
                if (DNServer.GetInstance().IsStarted)
                {
                    DxDebug.LogConsole("TestMethod_Send():服务器启动成功");
                    break;
                }
            }
            Random rand           = new Random();
            int    sendDataLength = rand.Next(128, 256);

            byte[] sendData = new byte[sendDataLength];
            for (int i = 0; i < sendData.Length; i++)
            {
                sendData[i] = (byte)rand.Next(128, 256);
            }
            DNClient.GetInstance().Connect("127.0.0.1", 21024);

            while (true)
            {
                if (DNClient.GetInstance().IsConnected)
                {
                    DxDebug.LogConsole("TestMethod_Send():连接成功");
                    break;
                }
            }

            int receCount = 0;//接收的消息总条数
            int sendCount = 0;

            //发送n次
            for (int count = 0; count < 500; count++)
            {
                //如果已经队列太满,那就等一下再发
                while (DNClient.GetInstance().isSendQueueIsFull)
                {
                    Thread.Sleep(20);
                }
                //一次连发n条
                for (int i = 0; i < 1500; i++)
                {
                    //发送sendDataLength字节的sendData
                    DxDebug.LogConsole("客户端发送:msgNum=" + sendCount);
                    Buffer.BlockCopy(BitConverter.GetBytes(sendCount), 0, sendData, 0, 4);
                    DNClient.GetInstance().Send(sendData);
                    sendCount++;
                }
                Thread.Sleep(1);
                //边发边收
                byte[][] datas = DNClient.GetInstance().GetReceiveData();
                if (datas != null)
                {
                    for (int i = 0; i < datas.Length; i++)
                    {
                        byte[] msg = datas[i];
                        //判断接收长度是否一致
                        Assert.IsTrue(msg.Length == sendDataLength);
                        //判断消息序号
                        int msgNum = BitConverter.ToInt32(msg, 0);
                        DxDebug.LogConsole("客户端接收到回发:msgNum=" + msgNum);
                        Assert.IsTrue(msgNum == receCount);

                        for (int j = 4; j < msg.Length; j++)
                        {
                            //判断每个字节是否一致
                            Assert.IsTrue(msg[j] == sendData[j]);
                        }

                        receCount++;
                    }
                }
            }

            int tryCount = 0;

            while (receCount != sendCount)
            {
                Thread.Sleep(20);
                byte[][] datas = DNClient.GetInstance().GetReceiveData();
                if (datas != null)
                {
                    for (int i = 0; i < datas.Length; i++)
                    {
                        byte[] msg = datas[i];
                        //判断接收长度是否一致
                        Assert.IsTrue(msg.Length == sendDataLength);
                        //判断消息序号
                        int msgNum = BitConverter.ToInt32(msg, 0);
                        DxDebug.LogConsole("客户端接收到回发:msgNum=" + msgNum);
                        Assert.IsTrue(msgNum == receCount);

                        for (int j = 4; j < msg.Length; j++)
                        {
                            //判断每个字节是否一致
                            Assert.IsTrue(msg[j] == sendData[j]);
                        }

                        receCount++;
                    }
                }
                else
                {
                    if (tryCount >= 20)
                    {
                        DxDebug.LogConsole("重试超过次数!失败退出!");
                        break;
                    }
                    tryCount++;
                    Thread.Sleep(100);
                }
            }

            Assert.IsTrue(receCount == sendCount);

            DNClient.GetInst().CloseImmediate();
            DNServer.GetInst().Close();
            LogFile.GetInst().Close();
        }
Beispiel #7
0
    /// <summary>
    /// 程序退出的时候必须要关闭
    /// </summary>
    public void OnApplicationQuit()
    {
        Debug.Log("NetPoll.OnApplicationQuit():关闭了应用程序.");

        DNClient.GetInstance().CloseImmediate();
    }