Пример #1
0
        public override void execute()
        {
            try
            {
                //查看连接状态
                if (!kcp.isActive())
                {
                    return;
                }

                //从发送缓冲区到kcp缓冲区
                var         writeQueue = kcp.WriteQueue;
                IByteBuffer byteBuf    = null;
                while (kcp.canSend(false))
                {
                    if (!writeQueue.TryDequeue(out byteBuf))
                    {
                        break;
                    }
                    try
                    {
                        this.kcp.send(byteBuf);
                        byteBuf.Release();
                    }
                    catch (IOException e)
                    {
                        kcp.getKcpListener().handleException(kcp, e);
                        return;
                    }
                }

                //如果有发送 则检测时间
                if (kcp.canSend(false) && (!kcp.checkFlush() || !kcp.isFastFlush()))
                {
                    return;
                }

                long startTicks = DateTime.Now.Ticks;
                long now        = kcp.currentMs();
                long next       = kcp.flush(now);
                //Console.WriteLine(next);
                long currentTicks = DateTime.Now.Ticks;
                //Console.WriteLine("耗时"+(currentTicks - startTicks) / TimeSpan.TicksPerMillisecond);
                kcp.setTsUpdate(now + next);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                release();
            }
        }
Пример #2
0
        public override void execute()
        {
            try
            {
                //查看连接状态
                if (!kcp.isActive())
                {
                    return;
                }

                //从发送缓冲区到kcp缓冲区
                var         writeQueue = kcp.WriteQueue;
                IByteBuffer byteBuf    = null;
                while (kcp.canSend(false))
                {
                    if (!writeQueue.TryDequeue(out byteBuf))
                    {
                        break;
                    }
                    try
                    {
                        this.kcp.send(byteBuf);
                        byteBuf.Release();
                    }
                    catch (IOException e)
                    {
                        kcp.getKcpListener().handleException(e, kcp);
                        return;
                    }
                }

                //如果有发送 则检测时间
                if (kcp.canSend(false) && (!kcp.checkFlush() || !kcp.isFastFlush()))
                {
                    return;
                }

                long now  = kcp.currentMs();
                long next = kcp.flush(now);
                //System.out.println(next);
                //System.out.println("耗时"+(System.currentTimeMillis()-now));
                kcp.setTsUpdate(now + next);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                release();
            }
        }
Пример #3
0
        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);
            }
        }
Пример #4
0
        public void execute()
        {
            CodecOutputList <IByteBuffer> bufList = null;

            try {
                //Thread.sleep(1000);
                //查看连接状态
                if (!kcp.isActive())
                {
                    return;
                }
                bool        hasKcpMessage = false;
                long        current       = kcp.currentMs();
                var         readQueue     = kcp.ReadQueue;
                IByteBuffer byteBuf       = null;
                for (;;)
                {
                    if (!readQueue.TryDequeue(out byteBuf))
                    {
                        break;
                    }
                    hasKcpMessage = true;
                    kcp.input(byteBuf, current);
                    byteBuf.Release();
                }
                if (!hasKcpMessage)
                {
                    return;
                }
                if (kcp.isStream())
                {
                    while (kcp.canRecv())
                    {
                        if (bufList == null)
                        {
                            bufList = CodecOutputList <IByteBuffer> .NewInstance();
                        }
                        kcp.receive(bufList);
                    }
                    int size = bufList.Count;
                    for (int i = 0; i < size; i++)
                    {
                        byteBuf = bufList[i];
                        readBytebuf(byteBuf, current);
                    }
                }
                else
                {
                    while (kcp.canRecv())
                    {
                        IByteBuffer recvBuf = kcp.mergeReceive();
                        readBytebuf(recvBuf, current);
                    }
                }
                //判断写事件
                if (!kcp.WriteQueue.IsEmpty && kcp.canSend(false))
                {
                    kcp.notifyWriteEvent();
                }
            } catch (Exception e) {
                kcp.KcpListener.handleException(e, kcp);
                Console.WriteLine(e);
            } finally {
                release();
                bufList?.Return();
            }
        }