List of usage examples for io.netty.buffer ByteBuf readUnsignedShort
public abstract int readUnsignedShort();
From source file:org.jfxvnc.net.rfb.codec.handshaker.RfbClient38Decoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { switch (state()) { case SEC_TYPES: int numberOfSecurtiyTypes = in.readUnsignedByte(); if (numberOfSecurtiyTypes == 0) { logger.error("no security types available"); decodeErrorMessage(ctx, in); return; }/* www . j a v a 2s. c o m*/ SecurityType[] serverSecTypes = new SecurityType[numberOfSecurtiyTypes]; for (int i = 0; i < numberOfSecurtiyTypes; i++) { int sec = in.readUnsignedByte(); serverSecTypes[i] = SecurityType.valueOf(sec); if (serverSecTypes[i] == SecurityType.UNKNOWN) { logger.error("un supported security types: {}", sec); } } logger.debug("supported security types: {}", Arrays.toString(serverSecTypes)); checkpoint(State.SEC_RESULT); out.add(new SecurityTypesEvent(true, serverSecTypes)); break; case SEC_RESULT: int secResult = in.readInt(); logger.debug("server login {}", secResult == 0 ? "succeed" : "failed"); if (secResult == 1) { int length = in.readInt(); if (length == 0) { out.add(new SecurityResultEvent(false, new ProtocolException("decode error message failed"))); return; } byte[] text = new byte[length]; in.readBytes(text); out.add(new SecurityResultEvent(false, new SecurityException(new String(text, ASCII)))); return; } out.add(new SecurityResultEvent(true)); checkpoint(State.SERVER_INIT); break; case SERVER_INIT: ServerInitEvent initMsg = new ServerInitEvent(); initMsg.setFrameBufferWidth(in.readUnsignedShort()); initMsg.setFrameBufferHeight(in.readUnsignedShort()); initMsg.setPixelFormat(parsePixelFormat(in)); byte[] name = new byte[in.readInt()]; in.readBytes(name); initMsg.setServerName(new String(name, ASCII)); out.add(initMsg); break; default: break; } }
From source file:org.jfxvnc.net.rfb.codec.handshaker.RfbClient38Decoder.java
License:Apache License
private PixelFormat parsePixelFormat(ByteBuf m) { PixelFormat pf = new PixelFormat(); pf.setBitPerPixel(m.readUnsignedByte()); pf.setDepth(m.readUnsignedByte());/*from w w w. j ava 2 s .c o m*/ pf.setBigEndian(m.readUnsignedByte() == 1); pf.setTrueColor(m.readUnsignedByte() == 1); pf.setRedMax(m.readUnsignedShort()); pf.setGreenMax(m.readUnsignedShort()); pf.setBlueMax(m.readUnsignedShort()); pf.setRedShift(m.readUnsignedByte()); pf.setGreenShift(m.readUnsignedByte()); pf.setBlueShift(m.readUnsignedByte()); m.skipBytes(3); return pf; }
From source file:org.jmqtt.core.codec.ConnectDecoder.java
License:Open Source License
@Override void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException { in.resetReaderIndex();/* w ww . j a va2 s. com*/ //Common decoding part ConnectPacket message = new ConnectPacket(); if (!decodeCommonHeader(message, 0x00, in)) { in.resetReaderIndex(); return; } int remainingLength = message.getRemainingLength(); int start = in.readerIndex(); int protocolNameLen = in.readUnsignedShort(); byte[] encProtoName; String protoName; Attribute<Integer> versionAttr = ctx.attr(MQTTDecoder.PROTOCOL_VERSION); switch (protocolNameLen) { case 6: //MQTT version 3.1 "MQIsdp" //ProtocolName 8 bytes or 6 bytes if (in.readableBytes() < 10) { in.resetReaderIndex(); return; } encProtoName = new byte[6]; in.readBytes(encProtoName); protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); versionAttr.set((int) MqttUtils.VERSION_3_1); break; case 4: //MQTT version 3.1.1 "MQTT" //ProtocolName 6 bytes if (in.readableBytes() < 8) { in.resetReaderIndex(); return; } encProtoName = new byte[4]; in.readBytes(encProtoName); protoName = new String(encProtoName, "UTF-8"); if (!"MQTT".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); versionAttr.set((int) MqttUtils.VERSION_3_1_1); break; default: //protocol broken throw new CorruptedFrameException("Invalid protoName size: " + protocolNameLen); } //ProtocolVersion 1 byte (value 0x03 for 3.1, 0x04 for 3.1.1) message.setProtocolVersion(in.readByte()); if (message.getProtocolVersion() == MqttUtils.VERSION_3_1_1) { //if 3.1.1, check the flags (dup, retain and qos == 0) if (message.isDupFlag() || message.isRetainFlag() || message.getQos() != QosType.MOST_ONE) { throw new CorruptedFrameException("Received a CONNECT with fixed header flags != 0"); } //check if this is another connect from the same client on the same session Attribute<Boolean> connectAttr = ctx.attr(ConnectDecoder.CONNECT_STATUS); Boolean alreadyConnected = connectAttr.get(); if (alreadyConnected == null) { //never set connectAttr.set(true); } else if (alreadyConnected) { throw new CorruptedFrameException("Received a second CONNECT on the same network connection"); } } //Connection flag byte connFlags = in.readByte(); if (message.getProtocolVersion() == MqttUtils.VERSION_3_1_1) { if ((connFlags & 0x01) != 0) { //bit(0) of connection flags is != 0 throw new CorruptedFrameException("Received a CONNECT with connectionFlags[0(bit)] != 0"); } } boolean cleanSession = ((connFlags & 0x02) >> 1) == 1; boolean willFlag = ((connFlags & 0x04) >> 2) == 1; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected will QoS in range 0..2 but found: " + willQos); } boolean willRetain = ((connFlags & 0x20) >> 5) == 1; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1; boolean userFlag = ((connFlags & 0x80) >> 7) == 1; //a password is true iff user is true. if (!userFlag && passwordFlag) { in.resetReaderIndex(); throw new CorruptedFrameException( "Expected password flag to true if the user flag is true but was: " + passwordFlag); } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); //Keep Alive timer 2 bytes //int keepAlive = MqttUtils.readWord(in); int keepAlive = in.readUnsignedShort(); message.setKeepAlive(keepAlive); if ((remainingLength == 12 && message.getProtocolVersion() == MqttUtils.VERSION_3_1) || (remainingLength == 10 && message.getProtocolVersion() == MqttUtils.VERSION_3_1_1)) { out.add(message); return; } //Decode the ClientID String clientID = MqttUtils.decodeString(in); if (clientID == null) { in.resetReaderIndex(); return; } message.setClientID(clientID); //Decode willTopic if (willFlag) { String willTopic = MqttUtils.decodeString(in); if (willTopic == null) { in.resetReaderIndex(); return; } message.setWillTopic(willTopic); } //Decode willMessage if (willFlag) { byte[] willMessage = MqttUtils.readFixedLengthContent(in); if (willMessage == null) { in.resetReaderIndex(); return; } message.setWillMessage(willMessage); } //Compatibility check with v3.0, remaining length has precedence over //the user and password flags int readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } //Decode username if (userFlag) { String userName = MqttUtils.decodeString(in); if (userName == null) { in.resetReaderIndex(); return; } message.setUsername(userName); } readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } //Decode password if (passwordFlag) { byte[] password = MqttUtils.readFixedLengthContent(in); if (password == null) { in.resetReaderIndex(); return; } message.setPassword(password); } out.add(message); }
From source file:org.jmqtt.core.codec.MessageIDDecoder.java
License:Open Source License
@Override void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception { in.resetReaderIndex();//from www .ja v a2 s . co m //Common decoding part PacketIdPacket message = createMessage(); if (!decodeCommonHeader(message, 0x00, in)) { in.resetReaderIndex(); return; } //read messageIDs message.setPacketId(in.readUnsignedShort()); 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();// w ww .j a v a 2 s . c om 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(); 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();/*from w w w .j a va2s. com*/ //Common decoding part PacketIdPacket message = new PubRelPacket(); if (!decodeCommonHeader(message, 0x02, in)) { in.resetReaderIndex(); 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();//www.j a v a2 s .c o m SubAckPacket message = new SubAckPacket(); if (!decodeCommonHeader(message, 0x00, in)) { in.resetReaderIndex(); 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();//w w w .ja v a 2 s. c o m if (!decodeCommonHeader(message, 0x02, in)) { in.resetReaderIndex(); 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();//from w w w. j av a2 s . com UnsubscribePacket message = new UnsubscribePacket(); if (!decodeCommonHeader(message, 0x02, in)) { in.resetReaderIndex(); 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.jmqtt.core.util.MqttUtils.java
License:Open Source License
/** * Read a byte array from the buffer, use two bytes as length information followed by length bytes. *///from ww w .j a v a 2s. c om public 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; }