Example usage for io.netty.buffer ByteBuf resetReaderIndex

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

Introduction

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

Prototype

public abstract ByteBuf resetReaderIndex();

Source Link

Document

Repositions the current readerIndex to the marked readerIndex in this buffer.

Usage

From source file:org.jmqtt.core.codec.PingReqDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
    //Common decoding part
    in.resetReaderIndex();
    PingReqPacket message = new PingReqPacket();
    if (!decodeCommonHeader(message, 0x00, in)) {
        in.resetReaderIndex();/*from  w  w  w  .  j  a v a  2  s. c o m*/
        return;
    }
    out.add(message);
}

From source file:org.jmqtt.core.codec.PingRespDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
    //Common decoding part
    in.resetReaderIndex();
    PingRespPacket message = new PingRespPacket();
    if (!decodeCommonHeader(message, 0x00, in)) {
        in.resetReaderIndex();//from  ww w .  j  av  a2 s.c o m
        return;
    }
    out.add(message);
}

From source file:org.jmqtt.core.codec.PublishDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
    LOG.debug("decode invoked with buffer {}", in);
    in.resetReaderIndex();
    int startPos = in.readerIndex();

    //Common decoding part
    PublishPacket message = new PublishPacket();
    if (!decodeCommonHeader(message, in)) {
        LOG.debug("decode ask for more data after {}", in);
        in.resetReaderIndex();/* w w  w  . j a  va 2  s  .  c o  m*/
        return;
    }

    if (MqttUtils.isMQTT3_1_1(ctx)) {
        if (message.getQos() == QosType.MOST_ONE && message.isDupFlag()) {
            //bad protocol, if QoS=0 => DUP = 0
            throw new CorruptedFrameException("Received a PUBLISH with QoS=0 & DUP = 1, MQTT 3.1.1 violation");
        }

        if (message.getQos() == QosType.RESERVED) {
            throw new CorruptedFrameException(
                    "Received a PUBLISH with QoS flags setted 10 b11, MQTT 3.1.1 violation");
        }
    }

    int remainingLength = message.getRemainingLength();

    //Topic name
    String topic = MqttUtils.decodeString(in);
    if (topic == null) {
        in.resetReaderIndex();
        return;
    }
    //[MQTT-3.3.2-2] The Topic Name in the PUBLISH Packet MUST NOT contain wildcard characters.
    if (topic.contains("+") || topic.contains("#")) {
        throw new CorruptedFrameException(
                "Received a PUBLISH with topic containing wild card chars, topic: " + topic);
    }
    //check topic is at least one char [MQTT-4.7.3-1]
    if (topic.length() == 0) {
        throw new CorruptedFrameException("Received a PUBLISH with topic without any character");
    }

    message.setTopicName(topic);

    if (message.getQos() == QosType.LEAST_ONE || message.getQos() == QosType.EXACTLY_ONCE) {
        message.setPacketId(in.readUnsignedShort());
    }
    int stopPos = in.readerIndex();

    //read the payload
    int payloadSize = remainingLength - (stopPos - startPos - 2)
            + (MqttUtils.numBytesToEncode(remainingLength) - 1);
    if (in.readableBytes() < payloadSize) {
        in.resetReaderIndex();
        return;
    }
    ByteBuf bb = Unpooled.buffer(payloadSize);
    in.readBytes(bb);
    message.setPayload(bb.nioBuffer());

    out.add(message);
}

From source file:org.jmqtt.core.codec.PubRelDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException {
    in.resetReaderIndex();
    //Common decoding part
    PacketIdPacket message = new PubRelPacket();
    if (!decodeCommonHeader(message, 0x02, in)) {
        in.resetReaderIndex();/*  w  w w  .j av  a 2  s.c om*/
        return;
    }

    //read  messageIDs
    message.setPacketId(in.readUnsignedShort());
    out.add(message);
}

From source file:org.jmqtt.core.codec.SubAckDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
    //Common decoding part
    in.resetReaderIndex();
    SubAckPacket message = new SubAckPacket();
    if (!decodeCommonHeader(message, 0x00, in)) {
        in.resetReaderIndex();//from  w w w. j  a v a2  s.  co m
        return;
    }
    int remainingLength = message.getRemainingLength();

    //MessageID
    message.setPacketId(in.readUnsignedShort());
    remainingLength -= 2;

    //Qos array
    if (in.readableBytes() < remainingLength) {
        in.resetReaderIndex();
        return;
    }
    for (int i = 0; i < remainingLength; i++) {
        byte qos = in.readByte();
        message.addType(QosType.valueOf(qos));
    }

    out.add(message);
}

From source file:org.jmqtt.core.codec.SubscribeDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
    //Common decoding part
    SubscribePacket message = new SubscribePacket();
    in.resetReaderIndex();
    if (!decodeCommonHeader(message, 0x02, in)) {
        in.resetReaderIndex();//from   w  w w . j a  va 2s  . c  o  m
        return;
    }

    //check qos level
    if (message.getQos() != QosType.LEAST_ONE) {
        throw new CorruptedFrameException(
                "Received SUBSCRIBE message with QoS other than LEAST_ONE, was: " + message.getQos());
    }

    int start = in.readerIndex();
    //read  messageIDs
    message.setPacketId(in.readUnsignedShort());
    int read = in.readerIndex() - start;
    while (read < message.getRemainingLength()) {
        decodeSubscription(in, message);
        read = in.readerIndex() - start;
    }

    if (message.subscriptions().isEmpty()) {
        throw new CorruptedFrameException("subscribe MUST have got at least 1 couple topic/QoS");
    }

    out.add(message);
}

From source file:org.jmqtt.core.codec.UnsubscribeDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
    //Common decoding part
    in.resetReaderIndex();
    UnsubscribePacket message = new UnsubscribePacket();
    if (!decodeCommonHeader(message, 0x02, in)) {
        in.resetReaderIndex();//from  w  w  w  .  j  a  v  a  2 s.  com
        return;
    }

    //check qos level
    if (message.getQos() != QosType.LEAST_ONE) {
        throw new CorruptedFrameException(
                "Found an Unsubscribe message with qos other than LEAST_ONE, was: " + message.getQos());
    }

    int start = in.readerIndex();
    //read  messageIDs
    message.setPacketId(in.readUnsignedShort());
    int read = in.readerIndex() - start;
    while (read < message.getRemainingLength()) {
        String topicFilter = MqttUtils.decodeString(in);
        //check topic is at least one char [MQTT-4.7.3-1]
        if (topicFilter.length() == 0) {
            throw new CorruptedFrameException("Received an UNSUBSCRIBE with empty topic filter");
        }
        message.addTopicFilter(topicFilter);
        read = in.readerIndex() - start;
    }
    if (message.topicFilters().isEmpty()) {
        throw new CorruptedFrameException("unsubscribe MUST have got at least 1 topic");
    }
    out.add(message);
}

From source file:org.l2junity.loginserver.network.client.crypt.Crypt.java

License:Open Source License

@Override
public void encrypt(ByteBuf buf) {
    // Ensure that byte order is little endian because we set new packet size in first 2 bytes
    if (buf.order() != ByteOrder.LITTLE_ENDIAN) {
        buf = buf.order(ByteOrder.LITTLE_ENDIAN);
    }//  w ww. j a v a2s. c  om

    // Checksum & XOR Key or Checksum only
    buf.writeZero(_static ? 8 : 4);

    // Padding
    buf.writeZero(8 - (buf.readableBytes() % 8));

    if (_static) {
        _static = false;

        int key = Rnd.nextInt();
        buf.skipBytes(4); // The first 4 bytes are ignored
        while (buf.readerIndex() < (buf.writerIndex() - 8)) {
            int data = buf.readInt();
            key += data;
            data ^= key;
            buf.setInt(buf.readerIndex() - 4, data);
        }
        buf.setInt(buf.readerIndex(), key);

        buf.resetReaderIndex();

        final byte[] block = new byte[8];
        while (buf.isReadable(8)) {
            buf.readBytes(block);
            STATIC_BLOWFISH_ENGINE.encryptBlock(block, 0);
            buf.setBytes(buf.readerIndex() - block.length, block);
        }
    } else {
        int checksum = 0;
        while (buf.isReadable(8)) {
            checksum ^= buf.readInt();
        }
        buf.setInt(buf.readerIndex(), checksum);

        buf.resetReaderIndex();

        final byte[] block = new byte[8];
        while (buf.isReadable(8)) {
            buf.readBytes(block);
            _blowfishEngine.encryptBlock(block, 0);
            buf.setBytes(buf.readerIndex() - block.length, block);
        }
    }
}

From source file:org.lanternpowered.pingy.PingyFramingHandler.java

License:MIT License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> output) throws Exception {
    while (readableVarInt(buf)) {
        buf.markReaderIndex();/*from  www  . j a va  2 s  .c  om*/

        final int length = readVarInt(buf);
        if (buf.readableBytes() < length) {
            buf.resetReaderIndex();
            break;
        }

        final ByteBuf msg = ctx.alloc().buffer(length);
        buf.readBytes(msg, length);

        output.add(msg);
    }
}

From source file:org.lanternpowered.pingy.PingyLegacyHandler.java

License:MIT License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg0) throws Exception {
    final ByteBuf msg = (ByteBuf) msg0;

    boolean legacy = false;
    msg.markReaderIndex();/*  w ww.  ja va 2 s  .  com*/
    try {
        // Try first as a legacy ping message
        int messageId = msg.readUnsignedByte();
        // Make sure that old clients don't attempt to login
        if (messageId == 0x02) {
            legacy = this.tryHandleLegacyJoin(ctx, msg);
        } else if (messageId == 0xfe) {
            legacy = this.tryHandleLegacyPing(ctx, msg);
        }
    } catch (Exception e) {
    }
    if (!legacy) {
        msg.resetReaderIndex();
        ctx.pipeline().remove(this);
        ctx.fireChannelRead(msg);
    } else {
        msg.release();
    }
}