Example usage for io.netty.buffer ByteBuf readUnsignedInt

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

Introduction

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

Prototype

public abstract long readUnsignedInt();

Source Link

Document

Gets an unsigned 32-bit integer at the current readerIndex and increases the readerIndex by 4 in this buffer.

Usage

From source file:com.yahoo.pulsar.common.api.Commands.java

License:Apache License

public static Long readChecksum(ByteBuf buffer) {
    if (hasChecksum(buffer)) {
        buffer.skipBytes(2); //skip magic bytes
        return buffer.readUnsignedInt();
    } else {//from w ww  .  j  a  v a 2s  . co m
        return null;
    }
}

From source file:com.yahoo.pulsar.common.api.Commands.java

License:Apache License

public static MessageMetadata parseMessageMetadata(ByteBuf buffer) {
    try {//from ww w .  j  a va  2  s  .  c  o m
        // initially reader-index may point to start_of_checksum : increment reader-index to start_of_metadata to parse
        // metadata
        readChecksum(buffer);
        int metadataSize = (int) buffer.readUnsignedInt();

        int writerIndex = buffer.writerIndex();
        buffer.writerIndex(buffer.readerIndex() + metadataSize);
        ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(buffer);
        MessageMetadata.Builder messageMetadataBuilder = MessageMetadata.newBuilder();
        MessageMetadata res = messageMetadataBuilder.mergeFrom(stream, null).build();
        buffer.writerIndex(writerIndex);
        messageMetadataBuilder.recycle();
        stream.recycle();
        return res;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.yahoo.pulsar.common.api.Commands.java

License:Apache License

public static ByteBuf deSerializeSingleMessageInBatch(ByteBuf uncompressedPayload,
        PulsarApi.SingleMessageMetadata.Builder singleMessageMetadataBuilder, int index, int batchSize)
        throws IOException {
    int singleMetaSize = (int) uncompressedPayload.readUnsignedInt();
    int writerIndex = uncompressedPayload.writerIndex();
    int beginIndex = uncompressedPayload.readerIndex() + singleMetaSize;
    uncompressedPayload.writerIndex(beginIndex);
    ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(uncompressedPayload);
    PulsarApi.SingleMessageMetadata singleMessageMetadata = singleMessageMetadataBuilder.mergeFrom(stream, null)
            .build();//ww  w  . j  a va 2s. c o m
    int singleMessagePayloadSize = singleMessageMetadata.getPayloadSize();

    uncompressedPayload.markReaderIndex();
    ByteBuf singleMessagePayload = uncompressedPayload.slice(uncompressedPayload.readerIndex(),
            singleMessagePayloadSize);
    singleMessagePayload.retain();
    uncompressedPayload.writerIndex(writerIndex);
    uncompressedPayload.resetReaderIndex();
    // reader now points to beginning of payload read; so move it past message payload just read
    if (index < batchSize) {
        uncompressedPayload.readerIndex(uncompressedPayload.readerIndex() + singleMessagePayloadSize);
    }
    return singleMessagePayload;
}

From source file:com.yahoo.pulsar.common.api.PulsarDecoder.java

License:Apache License

@Override
final public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // Get a buffer that contains the full frame
    ByteBuf buffer = (ByteBuf) msg;
    BaseCommand cmd = null;// w  ww . j ava 2s .co  m
    BaseCommand.Builder cmdBuilder = null;

    try {
        // De-serialize the command
        int cmdSize = (int) buffer.readUnsignedInt();
        int writerIndex = buffer.writerIndex();
        buffer.writerIndex(buffer.readerIndex() + cmdSize);
        ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(buffer);
        cmdBuilder = BaseCommand.newBuilder();
        cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build();
        buffer.writerIndex(writerIndex);

        cmdInputStream.recycle();

        if (log.isDebugEnabled()) {
            log.debug("[{}] Received cmd {}", ctx.channel().remoteAddress(), cmd.getType());
        }

        messageReceived();

        switch (cmd.getType()) {
        case PARTITIONED_METADATA:
            checkArgument(cmd.hasPartitionMetadata());
            handlePartitionMetadataRequest(cmd.getPartitionMetadata());
            cmd.getPartitionMetadata().recycle();
            break;

        case PARTITIONED_METADATA_RESPONSE:
            checkArgument(cmd.hasPartitionMetadataResponse());
            handlePartitionResponse(cmd.getPartitionMetadataResponse());
            cmd.getPartitionMetadataResponse().recycle();
            break;

        case LOOKUP:
            checkArgument(cmd.hasLookupTopic());
            handleLookup(cmd.getLookupTopic());
            cmd.getLookupTopic().recycle();
            break;

        case LOOKUP_RESPONSE:
            checkArgument(cmd.hasLookupTopicResponse());
            handleLookupResponse(cmd.getLookupTopicResponse());
            cmd.getLookupTopicResponse().recycle();
            break;

        case ACK:
            checkArgument(cmd.hasAck());
            handleAck(cmd.getAck());
            cmd.getAck().getMessageId().recycle();
            cmd.getAck().recycle();
            break;

        case CLOSE_CONSUMER:
            checkArgument(cmd.hasCloseConsumer());
            handleCloseConsumer(cmd.getCloseConsumer());
            cmd.getCloseConsumer().recycle();
            break;

        case CLOSE_PRODUCER:
            checkArgument(cmd.hasCloseProducer());
            handleCloseProducer(cmd.getCloseProducer());
            cmd.getCloseProducer().recycle();
            break;

        case CONNECT:
            checkArgument(cmd.hasConnect());
            handleConnect(cmd.getConnect());
            cmd.getConnect().recycle();
            break;
        case CONNECTED:
            checkArgument(cmd.hasConnected());
            handleConnected(cmd.getConnected());
            cmd.getConnected().recycle();
            break;

        case ERROR:
            checkArgument(cmd.hasError());
            handleError(cmd.getError());
            cmd.getError().recycle();
            break;

        case FLOW:
            checkArgument(cmd.hasFlow());
            handleFlow(cmd.getFlow());
            cmd.getFlow().recycle();
            break;

        case MESSAGE: {
            checkArgument(cmd.hasMessage());
            handleMessage(cmd.getMessage(), buffer);
            cmd.getMessage().recycle();
            break;
        }
        case PRODUCER:
            checkArgument(cmd.hasProducer());
            handleProducer(cmd.getProducer());
            cmd.getProducer().recycle();
            break;

        case SEND: {
            checkArgument(cmd.hasSend());

            // Store a buffer marking the content + headers
            ByteBuf headersAndPayload = buffer.markReaderIndex();
            handleSend(cmd.getSend(), headersAndPayload);
            cmd.getSend().recycle();
            break;
        }
        case SEND_ERROR:
            checkArgument(cmd.hasSendError());
            handleSendError(cmd.getSendError());
            cmd.getSendError().recycle();
            break;

        case SEND_RECEIPT:
            checkArgument(cmd.hasSendReceipt());
            handleSendReceipt(cmd.getSendReceipt());
            cmd.getSendReceipt().recycle();
            break;

        case SUBSCRIBE:
            checkArgument(cmd.hasSubscribe());
            handleSubscribe(cmd.getSubscribe());
            cmd.getSubscribe().recycle();
            break;

        case SUCCESS:
            checkArgument(cmd.hasSuccess());
            handleSuccess(cmd.getSuccess());
            cmd.getSuccess().recycle();
            break;

        case PRODUCER_SUCCESS:
            checkArgument(cmd.hasProducerSuccess());
            handleProducerSuccess(cmd.getProducerSuccess());
            cmd.getProducerSuccess().recycle();
            break;

        case UNSUBSCRIBE:
            checkArgument(cmd.hasUnsubscribe());
            handleUnsubscribe(cmd.getUnsubscribe());
            cmd.getUnsubscribe().recycle();
            break;

        case PING:
            checkArgument(cmd.hasPing());
            handlePing(cmd.getPing());
            cmd.getPing().recycle();
            break;

        case PONG:
            checkArgument(cmd.hasPong());
            handlePong(cmd.getPong());
            cmd.getPong().recycle();
            break;

        case REDELIVER_UNACKNOWLEDGED_MESSAGES:
            checkArgument(cmd.hasRedeliverUnacknowledgedMessages());
            handleRedeliverUnacknowledged(cmd.getRedeliverUnacknowledgedMessages());
            cmd.getRedeliverUnacknowledgedMessages().recycle();
            break;
        }

    } finally {
        if (cmdBuilder != null) {
            cmdBuilder.recycle();
        }

        if (cmd != null) {
            cmd.recycle();
        }

        buffer.release();
    }
}

From source file:com.yahoo.pulsar.common.compression.CommandsTest.java

License:Apache License

@Test
public void testChecksumSendCommand() throws Exception {

    // test checksum in send command
    String producerName = "prod-name";
    int sequenceId = 0;
    ByteBuf data = Unpooled.buffer(1024);
    MessageMetadata messageMetadata = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis())
            .setProducerName(producerName).setSequenceId(sequenceId).build();
    int expectedChecksum = computeChecksum(messageMetadata, data);
    ByteBuf clientCommand = Commands.newSend(1, 0, 1, ChecksumType.Crc32c, messageMetadata, data);
    clientCommand.retain();//from w  w  w  . ja va  2 s.  c  o m
    ByteBuffer inputBytes = clientCommand.nioBuffer();
    ByteBuf receivedBuf = Unpooled.wrappedBuffer(inputBytes);
    receivedBuf.skipBytes(4); //skip [total-size]
    int cmdSize = (int) receivedBuf.readUnsignedInt();
    receivedBuf.readerIndex(8 + cmdSize);
    int startMessagePos = receivedBuf.readerIndex();

    /*** 1. verify checksum and metadataParsing ***/
    boolean hasChecksum = Commands.hasChecksum(receivedBuf);
    int checksum = Commands.readChecksum(receivedBuf).intValue();

    // verify checksum is present
    assertTrue(hasChecksum);
    // verify checksum value
    assertEquals(expectedChecksum, checksum);
    MessageMetadata metadata = Commands.parseMessageMetadata(receivedBuf);
    // verify metadata parsing
    assertEquals(metadata.getProducerName(), producerName);

    /** 2. parseMessageMetadata should skip checksum if present **/
    receivedBuf.readerIndex(startMessagePos);
    metadata = Commands.parseMessageMetadata(receivedBuf);
    // verify metadata parsing
    assertEquals(metadata.getProducerName(), producerName);

}

From source file:eu.xworlds.util.raknet.protocol.BaseMessage.java

License:Open Source License

protected long readIPv4Address(ByteBuf buf) {
    return buf.readUnsignedInt();
}

From source file:eu.xworlds.util.raknet.protocol.SndReceiptAcked.java

License:Open Source License

@Override
protected void parseMessage(ByteBuf buf) {
    this.serial = buf.readUnsignedInt();
}

From source file:io.advantageous.conekt.dns.impl.netty.decoder.StartOfAuthorityDecoder.java

License:Open Source License

/**
 * Returns a decoded SOA (start of authority) resource record, stored as an
 * instance of {@link StartOfAuthorityRecord}.
 *
 * @param response the DNS response that contains the resource record being
 *                 decoded/*from ww  w.jav a 2  s . c  o m*/
 * @param resource the resource record being decoded
 */
@Override
public StartOfAuthorityRecord decode(DnsResponse response, DnsResource resource) {
    ByteBuf packet = response.content().readerIndex(resource.contentIndex());
    String mName = DnsResponseDecoder.readName(packet);
    String rName = DnsResponseDecoder.readName(packet);
    long serial = packet.readUnsignedInt();
    int refresh = packet.readInt();
    int retry = packet.readInt();
    int expire = packet.readInt();
    long minimum = packet.readUnsignedInt();
    return new StartOfAuthorityRecord(mName, rName, serial, refresh, retry, expire, minimum);
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java

License:Open Source License

/**
 * Decodes a resource record, given a DNS packet in a byte buffer.
 *
 * @param buf the byte buffer containing the DNS packet
 * @return a {@link DnsResource} record containing response data
 *//*www  .  ja va 2s .  co  m*/
private static DnsResource decodeResource(ByteBuf buf, ByteBufAllocator allocator) {
    String name = readName(buf);
    int type = buf.readUnsignedShort();
    int aClass = buf.readUnsignedShort();
    long ttl = buf.readUnsignedInt();
    int len = buf.readUnsignedShort();
    ByteBuf resourceData = allocator.buffer(len);
    int contentIndex = buf.readerIndex();
    resourceData.writeBytes(buf, len);
    return new DnsResource(name, type, aClass, ttl, contentIndex, resourceData);
}

From source file:io.airlift.drift.transport.netty.codec.ThriftFramedDecoder.java

License:Apache License

private Optional<ByteBuf> decode(ByteBuf buffer) {
    if (bytesToDiscard > 0) {
        discardTooLongFrame(buffer);//w  w w  .jav  a 2  s  .  c om
        return Optional.empty();
    }

    int initialReaderIndex = buffer.readerIndex();

    if (buffer.readableBytes() < Integer.BYTES) {
        return Optional.empty();
    }
    long frameSizeInBytes = buffer.readUnsignedInt();

    if (frameSizeInBytes > maxFrameSizeInBytes) {
        // this invocation doesn't move the readerIndex
        Optional<FrameInfo> frameInfo = frameInfoDecoder.tryDecodeFrameInfo(buffer);
        if (frameInfo.isPresent()) {
            tooLongFrameInfo = frameInfo;
            tooLongFrameSizeInBytes = frameSizeInBytes;
            bytesToDiscard = frameSizeInBytes;
            discardTooLongFrame(buffer);
            return Optional.empty();
        }
        // Basic frame info cannot be decoded and the max frame size is already exceeded.
        // Instead of waiting forever, fail without providing the sequence ID.
        if (buffer.readableBytes() >= maxFrameSizeInBytes) {
            tooLongFrameInfo = Optional.empty();
            tooLongFrameSizeInBytes = frameSizeInBytes;
            bytesToDiscard = frameSizeInBytes;
            discardTooLongFrame(buffer);
            return Optional.empty();
        }
        buffer.readerIndex(initialReaderIndex);
        return Optional.empty();
    }

    if (buffer.readableBytes() >= frameSizeInBytes) {
        // toIntExact must be safe, as frameSizeInBytes <= maxFrameSize
        ByteBuf frame = buffer.retainedSlice(buffer.readerIndex(), toIntExact(frameSizeInBytes));
        buffer.readerIndex(buffer.readerIndex() + toIntExact(frameSizeInBytes));
        return Optional.of(frame);
    }

    buffer.readerIndex(initialReaderIndex);
    return Optional.empty();
}