Search in sources :

Example 26 with EncoderException

use of io.netty.handler.codec.EncoderException in project netty by netty.

the class MqttEncoder method encodeConnectMessage.

private static ByteBuf encodeConnectMessage(ChannelHandlerContext ctx, MqttConnectMessage message) {
    int payloadBufferSize = 0;
    MqttFixedHeader mqttFixedHeader = message.fixedHeader();
    MqttConnectVariableHeader variableHeader = message.variableHeader();
    MqttConnectPayload payload = message.payload();
    MqttVersion mqttVersion = MqttVersion.fromProtocolNameAndLevel(variableHeader.name(), (byte) variableHeader.version());
    setMqttVersion(ctx, mqttVersion);
    // as MQTT 3.1 & 3.1.1 spec, If the User Name Flag is set to 0, the Password Flag MUST be set to 0
    if (!variableHeader.hasUserName() && variableHeader.hasPassword()) {
        throw new EncoderException("Without a username, the password MUST be not set");
    }
    // Client id
    String clientIdentifier = payload.clientIdentifier();
    if (!isValidClientId(mqttVersion, DEFAULT_MAX_CLIENT_ID_LENGTH, clientIdentifier)) {
        throw new MqttIdentifierRejectedException("invalid clientIdentifier: " + clientIdentifier);
    }
    int clientIdentifierBytes = utf8Bytes(clientIdentifier);
    payloadBufferSize += 2 + clientIdentifierBytes;
    // Will topic and message
    String willTopic = payload.willTopic();
    int willTopicBytes = nullableUtf8Bytes(willTopic);
    byte[] willMessage = payload.willMessageInBytes();
    byte[] willMessageBytes = willMessage != null ? willMessage : EmptyArrays.EMPTY_BYTES;
    if (variableHeader.isWillFlag()) {
        payloadBufferSize += 2 + willTopicBytes;
        payloadBufferSize += 2 + willMessageBytes.length;
    }
    String userName = payload.userName();
    int userNameBytes = nullableUtf8Bytes(userName);
    if (variableHeader.hasUserName()) {
        payloadBufferSize += 2 + userNameBytes;
    }
    byte[] password = payload.passwordInBytes();
    byte[] passwordBytes = password != null ? password : EmptyArrays.EMPTY_BYTES;
    if (variableHeader.hasPassword()) {
        payloadBufferSize += 2 + passwordBytes.length;
    }
    // Fixed and variable header
    byte[] protocolNameBytes = mqttVersion.protocolNameBytes();
    ByteBuf propertiesBuf = encodePropertiesIfNeeded(mqttVersion, ctx.alloc(), message.variableHeader().properties());
    try {
        final ByteBuf willPropertiesBuf;
        if (variableHeader.isWillFlag()) {
            willPropertiesBuf = encodePropertiesIfNeeded(mqttVersion, ctx.alloc(), payload.willProperties());
            payloadBufferSize += willPropertiesBuf.readableBytes();
        } else {
            willPropertiesBuf = Unpooled.EMPTY_BUFFER;
        }
        try {
            int variableHeaderBufferSize = 2 + protocolNameBytes.length + 4 + propertiesBuf.readableBytes();
            int variablePartSize = variableHeaderBufferSize + payloadBufferSize;
            int fixedHeaderBufferSize = 1 + getVariableLengthInt(variablePartSize);
            ByteBuf buf = ctx.alloc().buffer(fixedHeaderBufferSize + variablePartSize);
            buf.writeByte(getFixedHeaderByte1(mqttFixedHeader));
            writeVariableLengthInt(buf, variablePartSize);
            buf.writeShort(protocolNameBytes.length);
            buf.writeBytes(protocolNameBytes);
            buf.writeByte(variableHeader.version());
            buf.writeByte(getConnVariableHeaderFlag(variableHeader));
            buf.writeShort(variableHeader.keepAliveTimeSeconds());
            buf.writeBytes(propertiesBuf);
            // Payload
            writeExactUTF8String(buf, clientIdentifier, clientIdentifierBytes);
            if (variableHeader.isWillFlag()) {
                buf.writeBytes(willPropertiesBuf);
                writeExactUTF8String(buf, willTopic, willTopicBytes);
                buf.writeShort(willMessageBytes.length);
                buf.writeBytes(willMessageBytes, 0, willMessageBytes.length);
            }
            if (variableHeader.hasUserName()) {
                writeExactUTF8String(buf, userName, userNameBytes);
            }
            if (variableHeader.hasPassword()) {
                buf.writeShort(passwordBytes.length);
                buf.writeBytes(passwordBytes, 0, passwordBytes.length);
            }
            return buf;
        } finally {
            willPropertiesBuf.release();
        }
    } finally {
        propertiesBuf.release();
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) MqttCodecUtil.setMqttVersion(io.netty.handler.codec.mqtt.MqttCodecUtil.setMqttVersion) MqttCodecUtil.getMqttVersion(io.netty.handler.codec.mqtt.MqttCodecUtil.getMqttVersion) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

EncoderException (io.netty.handler.codec.EncoderException)26 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)9 ByteBuf (io.netty.buffer.ByteBuf)6 IOException (java.io.IOException)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 Test (org.junit.jupiter.api.Test)3 Message (org.lanternpowered.server.network.message.Message)3 CodecException (io.netty.handler.codec.CodecException)2 Deflater (java.util.zip.Deflater)2 Inflater (java.util.zip.Inflater)2 MessageRegistration (org.lanternpowered.server.network.message.MessageRegistration)2 Codec (org.lanternpowered.server.network.message.codec.Codec)2 CodecRegistration (com.flowpowered.network.Codec.CodecRegistration)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 DecoderResult (io.netty.handler.codec.DecoderResult)1 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)1 MessageToMessageCodec (io.netty.handler.codec.MessageToMessageCodec)1 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)1