Example usage for io.netty.buffer ByteBuf markReaderIndex

List of usage examples for io.netty.buffer ByteBuf markReaderIndex

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf markReaderIndex.

Prototype

public abstract ByteBuf markReaderIndex();

Source Link

Document

Marks the current readerIndex in this buffer.

Usage

From source file:com.tesora.dve.db.mysql.portal.protocol.MysqlClientAuthenticationHandler.java

License:Open Source License

protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out, boolean isLastBytes)
        throws Exception {
    ByteBuf leBuf = in.order(ByteOrder.LITTLE_ENDIAN);

    leBuf.markReaderIndex();
    boolean messageProcessed = false;

    try {/*from w  w w . ja  v  a  2 s.  c o  m*/
        if (leBuf.readableBytes() > MESSAGE_HEADER_LEN) {
            int payloadLen = leBuf.readMedium();
            leBuf.readByte(); // seq

            if (leBuf.readableBytes() >= payloadLen) {
                ByteBuf payload = leBuf.slice(leBuf.readerIndex(), payloadLen).order(ByteOrder.LITTLE_ENDIAN);
                Byte protocolVersion = leBuf.readByte();
                leBuf.skipBytes(payloadLen - 1);
                messageProcessed = true;

                if (state == AuthenticationState.AWAIT_GREETING)
                    processGreeting(ctx, payload, protocolVersion);
                else
                    processAcknowlegement(ctx, payload);
            }
        }

    } catch (Throwable t) {
        enterState(AuthenticationState.FAILURE);
        log.warn("Unexpected problem on outbound mysql connection.", t);
        throw t;
    } finally {
        if (!messageProcessed)
            leBuf.resetReaderIndex();

        if (isLastBytes && (state == AuthenticationState.AWAIT_ACKNOWLEGEMENT
                || state == AuthenticationState.AWAIT_GREETING)) {
            //we are waiting for handshake packets from mysql, but no more packets will ever arrive.  release blocked callers.
            Channel channel = ctx.channel();
            SocketAddress addr = (channel == null ? null : channel.remoteAddress());
            log.warn("Socket closed in middle of authentication handshake on socket " + addr);
            enterState(AuthenticationState.FAILURE);
        }
    }
}

From source file:com.tesora.dve.server.connectionmanager.loaddata.MSPLoadDataDecoder.java

License:Open Source License

private ByteBuf decodeNextFrame(ByteBuf in) {
    if (in.readableBytes() < 4) { //three bytes for the length, one for the sequence.
        return null;
    }//ww w  .  ja  v  a  2  s .  c  o  m

    in.markReaderIndex();

    ByteBuf buffer = in.order(ByteOrder.LITTLE_ENDIAN);
    int length = buffer.readUnsignedMedium();

    if (buffer.readableBytes() < length + 1) {
        in.resetReaderIndex();
        return null;
    }

    return buffer.readSlice(length + 1).order(ByteOrder.LITTLE_ENDIAN).retain();
}

From source file:com.torchmind.netty.msgpack.codec.MessageFrameCodec.java

License:Apache License

/**
 * {@inheritDoc}//  w  w w . ja  v  a  2s. co m
 */
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    while (in.readableBytes() >= 4) {
        // mark reader index
        in.markReaderIndex();

        // read length
        int length = in.readInt();

        // check whether enough data is available
        if (length > in.readableBytes()) {
            // reset index
            in.resetReaderIndex();

            // skip further execution due to missing data
            break;
        }

        // read buffer
        ByteBuf buffer = ctx.alloc().buffer(length);
        in.readBytes(buffer);

        // append to output
        out.add(buffer);
    }
}

From source file:com.vethrfolnir.game.network.mu.MuCyperDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    if (!in.isReadable()) {
        MuClient client = ctx.channel().attr(MuClient.ClientKey).get();
        _log.warn("Client[" + client + "] sent an empty packet!");

        return; //XXX: is it critical?
    }//from w ww  . j  a  va  2s . c  o m

    if (in.readableBytes() < 3) {
        return; // come back later
    }

    in.markReaderIndex();

    int opcode = in.readUnsignedByte();

    int lengthAt = 0;
    switch (opcode) {
    case 0xc1:
    case 0xc3:
        lengthAt = 1;
        break;
    case 0xc2:
    case 0xc4:
        lengthAt = 2;
        break;
    }

    //in.markReaderIndex();
    int rez = lengthAt > 1 ? in.readShort() : in.readUnsignedByte();
    in.resetReaderIndex();

    //System.out.println("1 Size[point="+(lengthAt > 1 ? "Short" : "Unsigned byte")+"]: "+rez+" opcode "+Integer.toHexString(opcode & 0xFF));
    if (in.readableBytes() < rez) {
        in.resetReaderIndex();
        return;
    }

    int header = in.getUnsignedByte(0);
    if (header == 0xC1 || header == 0xC2) {
        ByteBuf buff = ctx.alloc().heapBuffer(rez);
        in.readBytes(buff);
        out.add(DecodeXor32(buff));
    } else {
        out.add(DecodePacket(in));
    }
}

From source file:com.weibo.api.motan.transport.netty.NettyDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    if (in.readableBytes() <= MotanConstants.NETTY_HEADER) {
        return;//ww w .  j a v a  2  s . c om
    }

    in.markReaderIndex();

    short type = in.readShort();

    if (type != MotanConstants.NETTY_MAGIC_TYPE) {
        in.resetReaderIndex();
        throw new MotanFrameworkException("NettyDecoder transport header not support, type: " + type);
    }

    byte messageType = (byte) in.readShort();
    long requestId = in.readLong();

    int dataLength = in.readInt();

    // FIXME dataLength?
    if (in.readableBytes() < dataLength) {
        in.resetReaderIndex();
        return;
    }

    if (maxContentLength > 0 && dataLength > maxContentLength) {
        LoggerUtil.warn(
                "NettyDecoder transport data content length over of limit, size: {}  > {}. remote={} local={}",
                dataLength, maxContentLength, ctx.channel().remoteAddress(), ctx.channel().localAddress());
        Exception e = new MotanServiceException(
                "NettyDecoder transport data content length over of limit, size: " + dataLength + " > "
                        + maxContentLength);

        if (messageType == MotanConstants.FLAG_REQUEST) {
            Response response = buildExceptionResponse(requestId, e);
            ctx.channel().writeAndFlush(response);
            throw e;
        } else {
            throw e;
        }
    }

    byte[] data = new byte[dataLength];

    in.readBytes(data);

    try {
        String remoteIp = getRemoteIp(ctx.channel());
        out.add(codec.decode(client, remoteIp, data));
    } catch (Exception e) {
        //????
        if (messageType == MotanConstants.FLAG_REQUEST) {
            Response response = buildExceptionResponse(requestId, e);
            ctx.channel().writeAndFlush(response);
            return;
        } else {
            out.add(buildExceptionResponse(requestId, e));
            return;
        }
    }
}

From source file:com.xiovr.unibot.bot.network.impl.ConnectionDecoderImpl.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // System.out.println("Run decoder bytes="+ in.readableBytes());

    for (;;) {//from  w  w  w.  j  ava  2  s .  c o  m
        if (in.readableBytes() < 2)
            return;

        // System.out.println("Decoder size=" + in.readableBytes());
        in.markReaderIndex();

        // int dataLen = in.readUnsignedShort() - 2;
        byte b1 = in.readByte();
        byte b2 = in.readByte();
        int dataLen = (b1 & 0xFF) | ((b2 << 8) & 0xFF00);

        // System.out.println("packet len =" + dataLen);

        // System.out.println("readable bytes =" + in.readableBytes());
        if (in.readableBytes() < dataLen - 2) {
            in.resetReaderIndex();
            return;
        }
        Packet pck = PacketPool.obtain();
        pck.clear();

        byte[] pckArr = pck.getBuf().array();
        // byte[] inArr = in.array();
        in.readBytes(pckArr, 2, dataLen - 2);
        pck.putHeader(dataLen);
        pck.setPosition(dataLen);
        // int rIndex = in.readerIndex();
        // System.arraycopy(inArr, rIndex-2, pckArr, 0, dataLen+2);
        // in.readerIndex(rIndex+dataLen);
        out.add(pck);
        // System.out.println("DECODER END");
    }
}

From source file:com.yahoo.pulsar.broker.service.Consumer.java

License:Apache License

int getBatchSizeforEntry(ByteBuf metadataAndPayload) {
    try {//from   w w  w  .j av a 2s.  com
        // save the reader index and restore after parsing
        metadataAndPayload.markReaderIndex();
        PulsarApi.MessageMetadata metadata = Commands.parseMessageMetadata(metadataAndPayload);
        metadataAndPayload.resetReaderIndex();
        int batchSize = metadata.getNumMessagesInBatch();
        metadata.recycle();
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] num messages in batch are {} ", subscription, consumerId, batchSize);
        }
        return batchSize;
    } catch (Throwable t) {
        log.error("[{}] [{}] Failed to parse message metadata", subscription, consumerId, t);
    }
    return -1;
}

From source file:com.yahoo.pulsar.broker.service.ServerCnx.java

License:Apache License

private void printSendCommandDebug(CommandSend send, ByteBuf headersAndPayload) {
    headersAndPayload.markReaderIndex();
    MessageMetadata msgMetadata = Commands.parseMessageMetadata(headersAndPayload);
    headersAndPayload.resetReaderIndex();

    log.debug("[{}] Received send message request. producer: {}:{} {}:{} size: {}", remoteAddress,
            send.getProducerId(), send.getSequenceId(), msgMetadata.getProducerName(),
            msgMetadata.getSequenceId(), headersAndPayload.readableBytes());
    msgMetadata.recycle();//from   w w  w  .  j ava2s . c om
}

From source file:com.yahoo.pulsar.client.impl.ProducerImpl.java

License:Apache License

/**
 * Computes checksum again and verifies it against existing checksum. If checksum doesn't match it means that
 * message is corrupt.//from w  w w.j a  v a  2s .co  m
 * 
 * @param op
 * @return returns true only if message is not modified and computed-checksum is same as previous checksum else
 *         return false that means that message is corrupted. Returns true if checksum is not present.
 */
protected boolean verifyLocalBufferIsNotCorrupted(OpSendMsg op) {
    DoubleByteBuf msg = getDoubleByteBuf(op.cmd);

    if (msg != null) {
        ByteBuf headerFrame = msg.getFirst();
        msg.markReaderIndex();
        headerFrame.markReaderIndex();
        try {
            // skip bytes up to checksum index
            headerFrame.skipBytes(4); // skip [total-size]
            int cmdSize = (int) headerFrame.readUnsignedInt();
            headerFrame.skipBytes(cmdSize);
            // verify if checksum present
            if (hasChecksum(headerFrame)) {
                int checksum = readChecksum(headerFrame).intValue();
                // msg.readerIndex is already at header-payload index, Recompute checksum for headers-payload
                int metadataChecksum = computeChecksum(headerFrame);
                long computedChecksum = resumeChecksum(metadataChecksum, msg.getSecond());
                return checksum == computedChecksum;
            } else {
                log.warn("[{}] [{}] checksum is not present into message with id {}", topic, producerName,
                        op.sequenceId);
            }
        } finally {
            headerFrame.resetReaderIndex();
            msg.resetReaderIndex();
        }
        return true;
    } else {
        log.warn("[{}] Failed while casting {} into DoubleByteBuf", producerName, op.cmd.getClass().getName());
        return false;
    }
}

From source file:com.yahoo.pulsar.client.impl.ProducerImpl.java

License:Apache License

/**
 * Strips checksum from {@link OpSendMsg} command if present else ignore it.   
 * //w  w  w.  j  a v  a  2  s.  co m
 * @param op
 */
private void stripChecksum(OpSendMsg op) {
    op.cmd.markReaderIndex();
    int totalMsgBufSize = op.cmd.readableBytes();
    DoubleByteBuf msg = getDoubleByteBuf(op.cmd);
    if (msg != null) {
        ByteBuf headerFrame = msg.getFirst();
        msg.markReaderIndex();
        headerFrame.markReaderIndex();
        try {
            headerFrame.skipBytes(4); // skip [total-size]
            int cmdSize = (int) headerFrame.readUnsignedInt();

            // verify if checksum present
            headerFrame.skipBytes(cmdSize);

            if (!hasChecksum(headerFrame)) {
                headerFrame.resetReaderIndex();
                return;
            }

            int headerSize = 4 + 4 + cmdSize; // [total-size] [cmd-length] [cmd-size]
            int checksumSize = 4 + 2; // [magic-number] [checksum-size]
            int checksumMark = (headerSize + checksumSize); // [header-size] [checksum-size]
            int metaPayloadSize = (totalMsgBufSize - checksumMark); // metadataPayload = totalSize - checksumMark
            int newTotalFrameSizeLength = 4 + cmdSize + metaPayloadSize; // new total-size without checksum
            headerFrame.resetReaderIndex();
            int headerFrameSize = headerFrame.readableBytes();

            headerFrame.setInt(0, newTotalFrameSizeLength); // rewrite new [total-size]
            ByteBuf metadata = headerFrame.slice(checksumMark, headerFrameSize - checksumMark); // sliced only
                                                                                                // metadata
            headerFrame.writerIndex(headerSize); // set headerFrame write-index to overwrite metadata over checksum
            metadata.readBytes(headerFrame, metadata.readableBytes());
            headerFrame.capacity(headerFrameSize - checksumSize); // reduce capacity by removed checksum bytes
            headerFrame.resetReaderIndex();

        } finally {
            op.cmd.resetReaderIndex();
        }
    } else {
        log.warn("[{}] Failed while casting {} into DoubleByteBuf", producerName, op.cmd.getClass().getName());
    }
}