Exemple #1
0
        public static CodecOutputList <T> NewInstance(int minCapacity)
        {
            CodecOutputList <T> ret = Pool.Take();

            if (ret.Capacity < minCapacity)
            {
                ret.Capacity = minCapacity;
            }
            return(ret);
        }
Exemple #2
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();
            }
        }