コード例 #1
0
ファイル: KcpClient.cs プロジェクト: dora-BYR/Fenix
        public Ukcp connect(IChannel localChannel, EndPoint remoteAddress, ChannelConfig channelConfig, KcpListener kcpListener)
        {
            KcpOutput   kcpOutput   = new KcpOutPutImp();
            ReedSolomon reedSolomon = null;

            if (channelConfig.FecDataShardCount != 0 && channelConfig.FecParityShardCount != 0)
            {
                reedSolomon = ReedSolomon.create(channelConfig.FecDataShardCount, channelConfig.FecParityShardCount);
            }

            var _messageExecutor = _executorPool.GetAutoMessageExecutor();

            var ukcp = new Ukcp(kcpOutput, kcpListener, _messageExecutor, reedSolomon, channelConfig);

            var user = new User(localChannel, remoteAddress, localChannel.LocalAddress);

            ukcp.user(user);

            _channelManager.New(localChannel.LocalAddress, ukcp, null);

            var scheduleTask = new ScheduleTask(_channelManager, ukcp);

            KcpUntils.scheduleHashedWheel(scheduleTask, TimeSpan.FromMilliseconds(ukcp.getInterval()));
            return(ukcp);
        }
コード例 #2
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            var  msg     = (DatagramPacket)message;
            var  channel = context.Channel;
            var  ukcp    = _channelManager.get(msg);
            var  content = msg.Content;
            User user;

            if (ukcp != null)
            {
                user = ukcp.user();
                //每次收到消息重绑定地址
                user.RemoteAddress = msg.Sender;
                ukcp.read(content);
                return;
            }


            //如果是新连接第一个包的sn必须为0
            var sn = getSn(content, _channelConfig);

            if (sn != 0)
            {
                msg.Release();
                return;
            }

            var       messageExecutor = _executorPool.GetAutoMessageExecutor();
            KcpOutput kcpOutput       = new KcpOutPutImp();

            ReedSolomon reedSolomon = null;

            if (_channelConfig.FecDataShardCount != 0 && _channelConfig.FecParityShardCount != 0)
            {
                reedSolomon = ReedSolomon.create(_channelConfig.FecDataShardCount, _channelConfig.FecParityShardCount);
            }

            ukcp = new Ukcp(kcpOutput, _kcpListener, messageExecutor, reedSolomon, _channelConfig);

            user = new User(channel, msg.Sender, msg.Recipient);
            ukcp.user(user);
            _channelManager.New(msg.Sender, ukcp, msg);

            messageExecutor.execute(new ConnectTask(ukcp, _kcpListener));

            ukcp.read(content);

            var scheduleTask = new ScheduleTask(_channelManager, ukcp);

            KcpUntils.scheduleHashedWheel(scheduleTask, TimeSpan.FromMilliseconds(ukcp.getInterval()));
        }
コード例 #3
0
        /**
         * count+timestamp+dataLen+data
         *
         * @param count
         * @return
         */
        public IByteBuffer rttMsg(int count)
        {
            IByteBuffer buf = Unpooled.DirectBuffer(10);

            buf.WriteShort(count);
            buf.WriteInt((int)(KcpUntils.currentMs() - startTime));

            //int dataLen = new Random().nextInt(200);
            //buf.writeBytes(new byte[dataLen]);

            int dataLen = data.ReadableBytes;

            buf.WriteShort(dataLen);
            buf.WriteBytes(data, data.ReaderIndex, dataLen);
            return(buf);
        }
コード例 #4
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            long last_ts = DateTime.Now.Ticks;
            var  msg     = (DatagramPacket)message;
            var  channel = context.Channel;
            var  ukcp    = _channelManager.get(msg);
            var  content = msg.Content;
            User user;

            if (ukcp != null)
            {
                user = ukcp.user();
                //每次收到消息重绑定地址
                user.RemoteAddress = msg.Sender;
                ukcp.read(content);
                //Console.WriteLine(string.Format("(LAGS1){0}", (DateTime.Now.Ticks - last_ts)/10000.0));
                return;
            }

            var       messageExecutor = _executorPool.GetAutoMessageExecutor();
            KcpOutput kcpOutput       = new KcpOutPutImp();

            ReedSolomon reedSolomon = null;

            if (_channelConfig.FecDataShardCount != 0 && _channelConfig.FecParityShardCount != 0)
            {
                reedSolomon = ReedSolomon.create(_channelConfig.FecDataShardCount, _channelConfig.FecParityShardCount);
            }

            ukcp = new Ukcp(kcpOutput, _kcpListener, messageExecutor, reedSolomon, _channelConfig);

            user = new User(channel, msg.Sender, msg.Recipient);
            ukcp.user(user);

            _channelManager.New(msg.Sender, ukcp, msg);

            ukcp.connect();

            ukcp.read(content);

            var scheduleTask = new ScheduleTask(_channelManager, ukcp);

            //Console.WriteLine(ukcp.getInterval());
            KcpUntils.scheduleHashedWheel(scheduleTask, TimeSpan.FromMilliseconds(ukcp.getInterval()));

            Console.WriteLine(string.Format("(LAGS2){0}", (DateTime.Now.Ticks - last_ts) / 10000.0));
        }
コード例 #5
0
ファイル: ScheduleTask.cs プロジェクト: dora-BYR/Fenix
        public override void execute()
        {
            try
            {
                long now = _ukcp.currentMs();
                //判断连接是否关闭
                if (_ukcp.ChannelConfig.TimeoutMillis != 0 && now - _ukcp.ChannelConfig.TimeoutMillis > _ukcp.LastRecieveTime)
                {
                    _ukcp.close();
                }
                if (!_ukcp.isActive())
                {
                    var user = _ukcp.user();
                    //抛回网络线程处理连接删除
                    user.Channel.EventLoop.Execute(() => { _channelManager.del(_ukcp); });
                    _ukcp.release();
                    return;
                }
                long timeLeft = _ukcp.getTsUpdate() - now;
                //判断执行时间是否到了
                if (timeLeft > 0)
                {
                    //System.err.println(timeLeft);
                    KcpUntils.scheduleHashedWheel(this, TimeSpan.FromMilliseconds(timeLeft));
                    return;
                }

                //long start = System.currentTimeMillis();
                long next = _ukcp.flush(now);
                //System.err.println(next);
                //System.out.println("耗时  "+(System.currentTimeMillis()-start));
                KcpUntils.scheduleHashedWheel(this, TimeSpan.FromMilliseconds(next));

                //检测写缓冲区 如果能写则触发写事件
                if (!_ukcp.WriteQueue.IsEmpty && _ukcp.canSend(false)
                    )
                {
                    _ukcp.notifyWriteEvent();
                }
            } catch (Exception e) {
                Console.WriteLine(e);
            }
        }
コード例 #6
0
        public KcpRttExampleClient()
        {
            data = Unpooled.DirectBuffer(200);
            for (int i = 0; i < data.Capacity; i++)
            {
                data.WriteByte((byte)i);
            }

            rtts = new int[300];
            for (int i = 0; i < rtts.Length; i++)
            {
                rtts[i] = -1;
            }

            timer20.Enabled  = true;
            timer20.Interval = 20;
            timer20.Start();
            startTime = KcpUntils.currentMs();
        }
コード例 #7
0
        public void handleReceive(IByteBuffer byteBuf, Ukcp ukcp)
        {
            int curCount = byteBuf.ReadShort();

            if (curCount == -1)
            {
                timer20.Elapsed += overHandler;
            }
            else
            {
                int  idx  = curCount - 1;
                long time = byteBuf.ReadInt();
                if (rtts[idx] != -1)
                {
                    Console.WriteLine("end");
                }
                //log.info("rcv count {} {}", curCount, System.currentTimeMillis());
                rtts[idx] = (int)(KcpUntils.currentMs() - startTime - time);
                Console.WriteLine("rtt : " + curCount + "  " + rtts[idx]);
            }
        }