Ejemplo n.º 1
0
        public void test(int mode)
        {
            LatencySimulator vnet = new LatencySimulator();

            vnet.init(20, 600, 600);
            TestOutPut output1 = new TestOutPut(vnet, 0);
            TestOutPut output2 = new TestOutPut(vnet, 1);


            Kcp kcp1 = new Kcp(0x11223344, output1);
            Kcp kcp2 = new Kcp(0x11223344, output2);

            //kcp1.setAckMaskSize(8);
            //kcp2.setAckMaskSize(8);

            current = long2Uint(kcp1.currentMs());
            long slap   = current + 20;
            int  index  = 0;
            int  next   = 0;
            long sumrtt = 0;
            int  count  = 0;
            int  maxrtt = 0;

            kcp1.RcvWnd = 512;
            kcp1.SndWnd = 512;
            kcp2.RcvWnd = 512;
            kcp2.SndWnd = 512;

            // 判断测试用例的模式
            if (mode == 0)
            {
                // 默认模式
                kcp1.initNodelay(false, 10, 0, false);
                kcp2.initNodelay(false, 10, 0, false);
            }
            else if (mode == 1)
            {
                // 普通模式,关闭流控等
                kcp1.initNodelay(false, 10, 0, true);
                kcp2.initNodelay(false, 10, 0, true);
            }
            else
            {
                // 启动快速模式
                // 第二个参数 nodelay-启用以后若干常规加速将启动
                // 第三个参数 interval为内部处理时钟,默认设置为 10ms
                // 第四个参数 resend为快速重传指标,设置为2
                // 第五个参数 为是否禁用常规流控,这里禁止
                kcp1.initNodelay(true, 10, 2, true);
                kcp2.initNodelay(true, 10, 2, true);
                kcp1.RxMinrto   = 10;
                kcp1.Fastresend = 1;
            }

            int  hr;
            long ts1 = kcp1.currentMs();

            //写数据 定时更新
            for (;;)
            {
                current = long2Uint(kcp1.currentMs());
                Thread.Sleep(1);
                long now = kcp1.currentMs();
                kcp1.update(now);
                kcp2.update(now);


                //每隔 20ms,kcp1发送数据
                for (; current >= slap; slap += 20)
                {
                    IByteBuffer buf = PooledByteBufferAllocator.Default.Buffer();
                    buf.WriteIntLE(index);
                    index++;
                    buf.WriteIntLE((int)current);
                    kcp1.send(buf);
                    buf.Release();
                }

                //处理虚拟网络:检测是否有udp包从p1->p2
                for (;;)
                {
                    IByteBuffer buffer = PooledByteBufferAllocator.Default.DirectBuffer(2000);
//                    Console.WriteLine("buffer:" +buffer.AddressOfPinnedMemory().ToString());
                    try
                    {
                        hr = vnet.recv(1, buffer);
                        if (hr < 0)
                        {
                            break;
                        }

                        kcp2.input(buffer, true, kcp1.currentMs());
                    }
                    finally
                    {
                        buffer.Release();
                    }
                }

                // 处理虚拟网络:检测是否有udp包从p2->p1
                for (;;)
                {
                    IByteBuffer buffer = PooledByteBufferAllocator.Default.Buffer(2000);
                    try
                    {
                        hr = vnet.recv(0, buffer);
                        if (hr < 0)
                        {
                            break;
                        }

                        // 如果 p1收到udp,则作为下层协议输入到kcp1
                        kcp1.input(buffer, true, kcp1.currentMs());
                    }
                    finally
                    {
                        buffer.Release();
                    }
                }

                // kcp2接收到任何包都返回回去
                List <IByteBuffer> bufList = new List <IByteBuffer>();
                kcp2.recv(bufList);
                foreach (var byteBuf in bufList)
                {
                    kcp2.send(byteBuf);
                    byteBuf.Release();
                }

                // kcp1收到kcp2的回射数据
                bufList = new List <IByteBuffer>();
                kcp1.recv(bufList);
                foreach (var byteBuf in bufList)
                {
                    long sn  = byteBuf.ReadIntLE();
                    long ts  = byteBuf.ReadUnsignedIntLE();
                    long rtt = 0;
                    rtt = current - ts;
                    Console.WriteLine("rtt :" + rtt);


                    if (sn != next)
                    {
                        // 如果收到的包不连续
                        //for i:=0;i<8 ;i++ {
                        //println("---", i, buffer[i])
                        //}
                        Console.WriteLine("ERROR sn " + count + "<->" + next + sn);
                        return;
                    }

                    next++;
                    sumrtt += rtt;
                    count++;
                    if (rtt > maxrtt)
                    {
                        maxrtt = (int)rtt;
                    }

                    byteBuf.Release();
                }
                if (next > 1000)
                {
                    break;
                }
            }

            ts1 = kcp1.currentMs() - ts1;
            String[] names = new String[] { "default", "normal", "fast" };
            Console.WriteLine(names[mode] + " mode result :" + ts1 + " \n");
            Console.WriteLine("avgrtt=" + (sumrtt / count) + " maxrtt=" + maxrtt + " \n");
            Console.WriteLine("lost percent: " + (Snmp.snmp.RetransSegs) + "\n");
            Console.WriteLine("snmp: " + (Snmp.snmp.ToString()));
        }
Ejemplo n.º 2
0
 public TestOutPut(LatencySimulator vnet, int id)
 {
     this.vnet = vnet;
     this.id   = id;
 }