Package io.netty.handler.codec.mqtt
Class MqttDecoder
- java.lang.Object
-
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
public final class MqttDecoder extends ReplayingDecoder<MqttDecoder.DecoderState>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
MqttDecoder.DecoderState
States of the decoder.private static class
MqttDecoder.Result<T>
-
Nested classes/interfaces inherited from class io.netty.handler.codec.ByteToMessageDecoder
ByteToMessageDecoder.Cumulator
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private int
bytesRemainingInVariablePart
private int
maxBytesInMessage
private int
maxClientIdLength
private MqttFixedHeader
mqttFixedHeader
private java.lang.Object
variableHeader
-
Fields inherited from class io.netty.handler.codec.ByteToMessageDecoder
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
-
-
Constructor Summary
Constructors Constructor Description MqttDecoder()
MqttDecoder(int maxBytesInMessage)
MqttDecoder(int maxBytesInMessage, int maxClientIdLength)
-
Method Summary
-
Methods inherited from class io.netty.handler.codec.ReplayingDecoder
callDecode, checkpoint, checkpoint, state, state
-
Methods inherited from class io.netty.handler.codec.ByteToMessageDecoder
actualReadableBytes, channelInactive, channelRead, channelReadComplete, decodeLast, discardSomeReadBytes, handlerRemoved, handlerRemoved0, internalBuffer, isSingleDecode, setCumulator, setDiscardAfterReads, setSingleDecode, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, isSharable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded
-
-
-
-
Field Detail
-
mqttFixedHeader
private MqttFixedHeader mqttFixedHeader
-
variableHeader
private java.lang.Object variableHeader
-
bytesRemainingInVariablePart
private int bytesRemainingInVariablePart
-
maxBytesInMessage
private final int maxBytesInMessage
-
maxClientIdLength
private final int maxClientIdLength
-
-
Method Detail
-
decode
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, java.util.List<java.lang.Object> out) throws java.lang.Exception
Description copied from class:ByteToMessageDecoder
Decode the from oneByteBuf
to an other. This method will be called till either the inputByteBuf
has nothing to read when return from this method or till nothing was read from the inputByteBuf
.- Specified by:
decode
in classByteToMessageDecoder
- Parameters:
ctx
- theChannelHandlerContext
which thisByteToMessageDecoder
belongs tobuffer
- theByteBuf
from which to read dataout
- theList
to which decoded messages should be added- Throws:
java.lang.Exception
- is thrown if an error occurs
-
invalidMessage
private MqttMessage invalidMessage(java.lang.Throwable cause)
-
decodeFixedHeader
private static MqttFixedHeader decodeFixedHeader(ChannelHandlerContext ctx, ByteBuf buffer)
Decodes the fixed header. It's one byte for the flags and then variable bytes for the remaining length.- Parameters:
buffer
- the buffer to decode from- Returns:
- the fixed header
-
decodeVariableHeader
private MqttDecoder.Result<?> decodeVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, MqttFixedHeader mqttFixedHeader)
Decodes the variable header (if any)- Parameters:
buffer
- the buffer to decode frommqttFixedHeader
- MqttFixedHeader of the same message- Returns:
- the variable header
-
decodeConnectionVariableHeader
private static MqttDecoder.Result<MqttConnectVariableHeader> decodeConnectionVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer)
-
decodeConnAckVariableHeader
private static MqttDecoder.Result<MqttConnAckVariableHeader> decodeConnAckVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer)
-
decodeMessageIdAndPropertiesVariableHeader
private static MqttDecoder.Result<MqttMessageIdAndPropertiesVariableHeader> decodeMessageIdAndPropertiesVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer)
-
decodePubReplyMessage
private MqttDecoder.Result<MqttPubReplyMessageVariableHeader> decodePubReplyMessage(ByteBuf buffer)
-
decodeReasonCodeAndPropertiesVariableHeader
private MqttDecoder.Result<MqttReasonCodeAndPropertiesVariableHeader> decodeReasonCodeAndPropertiesVariableHeader(ByteBuf buffer)
-
decodePublishVariableHeader
private MqttDecoder.Result<MqttPublishVariableHeader> decodePublishVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, MqttFixedHeader mqttFixedHeader)
-
decodeMessageId
private static int decodeMessageId(ByteBuf buffer)
- Returns:
- messageId with numberOfBytesConsumed is 2
-
decodePayload
private static MqttDecoder.Result<?> decodePayload(ChannelHandlerContext ctx, ByteBuf buffer, MqttMessageType messageType, int bytesRemainingInVariablePart, int maxClientIdLength, java.lang.Object variableHeader)
Decodes the payload.- Parameters:
buffer
- the buffer to decode frommessageType
- type of the message being decodedbytesRemainingInVariablePart
- bytes remainingvariableHeader
- variable header of the same message- Returns:
- the payload
-
decodeConnectionPayload
private static MqttDecoder.Result<MqttConnectPayload> decodeConnectionPayload(ByteBuf buffer, int maxClientIdLength, MqttConnectVariableHeader mqttConnectVariableHeader)
-
decodeSubscribePayload
private static MqttDecoder.Result<MqttSubscribePayload> decodeSubscribePayload(ByteBuf buffer, int bytesRemainingInVariablePart)
-
decodeSubackPayload
private static MqttDecoder.Result<MqttSubAckPayload> decodeSubackPayload(ByteBuf buffer, int bytesRemainingInVariablePart)
-
decodeUnsubAckPayload
private static MqttDecoder.Result<MqttUnsubAckPayload> decodeUnsubAckPayload(ChannelHandlerContext ctx, ByteBuf buffer, int bytesRemainingInVariablePart)
-
decodeUnsubscribePayload
private static MqttDecoder.Result<MqttUnsubscribePayload> decodeUnsubscribePayload(ByteBuf buffer, int bytesRemainingInVariablePart)
-
decodePublishPayload
private static MqttDecoder.Result<ByteBuf> decodePublishPayload(ByteBuf buffer, int bytesRemainingInVariablePart)
-
decodeString
private static MqttDecoder.Result<java.lang.String> decodeString(ByteBuf buffer)
-
decodeString
private static MqttDecoder.Result<java.lang.String> decodeString(ByteBuf buffer, int minBytes, int maxBytes)
-
decodeByteArray
private static byte[] decodeByteArray(ByteBuf buffer)
- Returns:
- the decoded byte[], numberOfBytesConsumed = byte[].length + 2
-
packInts
private static long packInts(int a, int b)
-
unpackA
private static int unpackA(long ints)
-
unpackB
private static int unpackB(long ints)
-
decodeMsbLsb
private static int decodeMsbLsb(ByteBuf buffer)
numberOfBytesConsumed = 2. return decoded result.
-
decodeVariableByteInteger
private static long decodeVariableByteInteger(ByteBuf buffer)
See 1.5.5 Variable Byte Integer section of MQTT 5.0 specification for encoding/decoding rules- Parameters:
buffer
- the buffer to decode from- Returns:
- result pack with a = decoded integer, b = numberOfBytesConsumed. Need to unpack to read them.
- Throws:
DecoderException
- if bad MQTT protocol limits Remaining Length
-
decodeProperties
private static MqttDecoder.Result<MqttProperties> decodeProperties(ByteBuf buffer)
-
-