@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex(); if (!Utils.checkHeaderAvailability(in)) { in.resetReaderIndex(); return; } in.resetReaderIndex(); byte messageType = Utils.readMessageType(in); DemuxDecoder decoder = m_decoderMap.get(messageType); if (decoder == null) { throw new CorruptedFrameException( "Can't find any suitable decoder for message type: " + messageType); } decoder.decode(ctx, in, out); }
private void initHeaderQos(ByteBuf buff, int messageID, AbstractMessage.QOSType... qoss) throws IllegalAccessException { buff.clear() .writeByte(AbstractMessage.SUBACK << 4) .writeBytes(Utils.encodeRemainingLength(2 + qoss.length)); buff.writeShort(messageID); for (AbstractMessage.QOSType qos : qoss) { buff.writeByte(qos.ordinal()); } }
static boolean checkHeaderAvailability(ByteBuf in) { if (in.readableBytes() < 1) { return false; } // byte h1 = in.get(); // byte messageType = (byte) ((h1 & 0x00F0) >> 4); in.skipBytes(1); // skip the messageType byte int remainingLength = Utils.decodeRemainingLenght(in); if (remainingLength == -1) { return false; } // check remaining length if (in.readableBytes() < remainingLength) { return false; } // return messageType == type ? MessageDecoderResult.OK : MessageDecoderResult.NOT_OK; return true; }