예제 #1
0
파일: NetPoll.cs 프로젝트: KOLLSOFT/DNET
    /// <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()异常");
        }
    }
예제 #2
0
파일: NetPoll.cs 프로젝트: KOLLSOFT/DNET
 /// <summary>
 /// 获得实例
 /// </summary>
 /// <returns></returns>
 public static NetPoll GetInstance()
 {
     if (_instance == null)
     {
         DxDebug.LogError("NetPoll.GetInstance():还未被实例化!");
     }
     return(_instance);
 }
예제 #3
0
파일: NetPoll.cs 프로젝트: KOLLSOFT/DNET
    /// <summary>
    /// 程序退出的时候必须要关闭(这个函数现在是重复了)
    /// </summary>
    //public void OnDestroy()
    //{
    //    Debug.Log("NetPoll.OnDestroy():关闭了程序。");
    //    DNClient.GetInstance().Close();
    //}
#endif

    #region Print

    /// <summary>
    /// 关联打印到Untiy的控制台系统,可以多次调用,只会生效一次。目的是今早的使日志打印生效
    /// </summary>
    public void InitPrint()
    {
        if (isSetPrintEvent == false)
        {
            isSetPrintEvent     = true;
            DxDebug.EventPrint += OnPrint;
        }
        DxDebug.AllMemLogOutput();
        DxDebug.ClearMemLog();
    }
예제 #4
0
        public void TestMethod_Log()
        {
            Config.DefaultConfigOnWindows();
            Config.IsAutoHeartbeat = false;
            DNClient.GetInst().isDebugLog        = true;
            LogFile.GetInst().isImmediatelyFlush = true;
            DxDebug.LogWarning("123");
            DxDebug.LogError("123");

            DNClient.GetInst().CloseImmediate();
            DNServer.GetInst().Close();
            LogFile.GetInst().Close();
        }
예제 #5
0
파일: NetPoll.cs 프로젝트: KOLLSOFT/DNET
    /// <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);
        }
    }
예제 #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();
        }