Example usage for io.netty.buffer CompositeByteBuf readerIndex

List of usage examples for io.netty.buffer CompositeByteBuf readerIndex

Introduction

In this page you can find the example usage for io.netty.buffer CompositeByteBuf readerIndex.

Prototype

@Override
    public CompositeByteBuf readerIndex(int readerIndex) 

Source Link

Usage

From source file:com.talent.nio.communicate.receive.DecodeRunnable.java

License:Open Source License

@Override
public void run() {
    while (getMsgQueue().size() > 0) {
        ByteBuf queuedatas = null;/*  w  w w.ja v  a  2 s.  c  o  m*/
        CompositeByteBuf datas = Unpooled.compositeBuffer();

        if (lastDatas != null) {
            channelContext.getStatVo().setCurrentOgnzTimestamp(SystemTimer.currentTimeMillis());
            lastDatas.readerIndex(0);
            datas.addComponents(lastDatas);
            lastDatas = null;
        }

        int count = 0;

        label_2: while ((queuedatas = getMsgQueue().poll()) != null) {
            queuedatas = queuedatas.order(channelContext.getByteOrder());

            if (DebugUtils.isNeedDebug(channelContext)) {
                // long xx = 999999999999999999L;
                log.error("queuedatas:" + ArrayUtils.toString(queuedatas));
            }
            datas.addComponents(queuedatas);
            channelContext.getStatVo().setCurrentOgnzTimestamp(SystemTimer.currentTimeMillis());
            count++;

            if (needLength != -1) // ????
            {
                if (datas.capacity() < needLength) // ??
                {
                    //                        log.error("??----capacity:{}, needLength:{}", datas.capacity(), needLength);
                    continue;
                } else {
                    //                        log.error("?----capacity:{}, needLength:{}", datas.capacity(), needLength);
                    break label_2;
                }

            } else
            // ???
            {
                if (count == 50) {
                    log.warn(
                            "???{}???{}",
                            count, getMsgQueue().size());
                    break label_2;
                }
            }
        }
        channelContext.getStatVo().setCurrentOgnzTimestamp(SystemTimer.currentTimeMillis());

        PacketWithMeta packetWithMeta = null;
        try {
            // ByteBuffer buffer = ByteBuffer.wrap(datas);
            datas.writerIndex(datas.capacity());
            datas.readerIndex(0);
            packetWithMeta = channelContext.getDecoder().decode(datas, channelContext);
            needLength = -1;
            if (packetWithMeta == null) { // ???
                lastDatas = datas;
                lastDatas.readerIndex(0);

                if (DebugUtils.isNeedDebug(channelContext)) {
                    log.error("???:{}", lastDatas);
                }
            } else if (packetWithMeta.getPackets() == null || packetWithMeta.getPackets().size() == 0) {
                // ???
                lastDatas = datas;
                lastDatas.readerIndex(0);
                needLength = packetWithMeta.getNeedLength();
                if (DebugUtils.isNeedDebug(channelContext)) {
                    log.error("????:{}", needLength);
                }
            } else {
                int len = packetWithMeta.getPacketLenght();
                // lastDatas = new byte[datas.capacity() - len];
                // System.arraycopy(datas, len, lastDatas, 0,
                // lastDatas.length);

                if (datas.capacity() - len > 0) {

                    lastDatas = datas.copy(len, datas.capacity() - len);
                    if (DebugUtils.isNeedDebug(channelContext)) {
                        log.error("??:{}, {}", datas.capacity() - len, lastDatas);
                    }
                } else {
                    lastDatas = null;
                    if (DebugUtils.isNeedDebug(channelContext)) {
                        log.error("??:{}", lastDatas);
                    }
                }
                processMsgAndStat(packetWithMeta.getPackets(), len, false);
            }

        } catch (DecodeException e) {
            log.error(e.getMessage(), e);
            channelContext.getErrorPackageHandler().handle(channelContext.getSocketChannel(), channelContext,
                    e.getMessage());
        }
    }

}