List of usage examples for io.netty.buffer ByteBuf readUnsignedShort
public abstract int readUnsignedShort();
From source file:com.dempe.chat.common.mqtt.codec.MessageIDDecoder.java
License:Open Source License
@Override void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception { in.resetReaderIndex();/*w w w .j a v a2 s. c om*/ //Common decoding part MessageIDMessage message = createMessage(); if (!decodeCommonHeader(message, 0x00, in)) { in.resetReaderIndex(); return; } //read messageIDs message.setMessageID(in.readUnsignedShort()); out.add(message); }
From source file:com.dempe.chat.common.mqtt.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();/*from w w w . j av a 2s. c om*/ int startPos = in.readerIndex(); //Common decoding part PublishMessage message = new PublishMessage(); if (!decodeCommonHeader(message, in)) { LOG.debug("decode ask for more data after {}", in); in.resetReaderIndex(); return; } if (Utils.isMQTT3_1_1(ctx)) { if (message.getQos() == AbstractMessage.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() == AbstractMessage.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 = Utils.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() == AbstractMessage.QOSType.LEAST_ONE || message.getQos() == AbstractMessage.QOSType.EXACTLY_ONCE) { message.setMessageID(in.readUnsignedShort()); } int stopPos = in.readerIndex(); //read the payload int payloadSize = remainingLength - (stopPos - startPos - 2) + (Utils.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:com.dempe.chat.common.mqtt.codec.PubRelDecoder.java
License:Open Source License
@Override void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException { in.resetReaderIndex();// w w w . j a v a 2s . c o m //Common decoding part MessageIDMessage message = new PubRelMessage(); if (!decodeCommonHeader(message, 0x02, in)) { in.resetReaderIndex(); return; } //read messageIDs message.setMessageID(in.readUnsignedShort()); out.add(message); }
From source file:com.dempe.chat.common.mqtt.codec.SubAckDecoder.java
License:Open Source License
@Override void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception { //Common decoding part in.resetReaderIndex();/*w w w .j av a 2s .c o m*/ SubAckMessage message = new SubAckMessage(); if (!decodeCommonHeader(message, 0x00, in)) { in.resetReaderIndex(); return; } int remainingLength = message.getRemainingLength(); //MessageID message.setMessageID(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(AbstractMessage.QOSType.valueOf(qos)); } out.add(message); }
From source file:com.dempe.chat.common.mqtt.codec.SubscribeDecoder.java
License:Open Source License
@Override void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception { //Common decoding part SubscribeMessage message = new SubscribeMessage(); in.resetReaderIndex();/*from ww w . j a va 2 s. c om*/ if (!decodeCommonHeader(message, 0x02, in)) { in.resetReaderIndex(); return; } //check qos level if (message.getQos() != AbstractMessage.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.setMessageID(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:com.dempe.chat.common.mqtt.codec.UnsubscribeDecoder.java
License:Open Source License
@Override void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception { //Common decoding part in.resetReaderIndex();/*from w w w . j a v a2 s . c o m*/ UnsubscribeMessage message = new UnsubscribeMessage(); if (!decodeCommonHeader(message, 0x02, in)) { in.resetReaderIndex(); return; } //check qos level if (message.getQos() != AbstractMessage.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.setMessageID(in.readUnsignedShort()); int read = in.readerIndex() - start; while (read < message.getRemainingLength()) { String topicFilter = Utils.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:com.dempe.chat.common.mqtt.codec.Utils.java
License:Open Source License
/** * Read a byte array from the buffer, use two bytes as length information followed by length bytes. *///w ww . j a va2s . c om static byte[] readFixedLengthContent(ByteBuf in) throws UnsupportedEncodingException { if (in.readableBytes() < 2) { return null; } int strLen = in.readUnsignedShort(); if (in.readableBytes() < strLen) { return null; } byte[] strRaw = new byte[strLen]; in.readBytes(strRaw); return strRaw; }
From source file:com.digitalpetri.modbus.codec.MbapHeader.java
License:Apache License
public static MbapHeader decode(ByteBuf buffer) { return new MbapHeader(buffer.readShort(), buffer.readUnsignedShort(), buffer.readUnsignedShort(), buffer.readUnsignedByte());/*from ww w. j a v a 2 s . c o m*/ }
From source file:com.digitalpetri.modbus.codec.ModbusRequestDecoder.java
License:Apache License
private ReadCoilsRequest decodeReadCoils(ByteBuf buffer) { int address = buffer.readUnsignedShort(); int quantity = buffer.readUnsignedShort(); return new ReadCoilsRequest(address, quantity); }
From source file:com.digitalpetri.modbus.codec.ModbusRequestDecoder.java
License:Apache License
private ReadDiscreteInputsRequest decodeReadDiscreteInputs(ByteBuf buffer) { int address = buffer.readUnsignedShort(); int quantity = buffer.readUnsignedShort(); return new ReadDiscreteInputsRequest(address, quantity); }