All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iteaj.iot.client.codec.LengthFieldBasedFrameClient Maven / Gradle / Ivy

package com.iteaj.iot.client.codec;

import com.iteaj.iot.SocketMessage;
import com.iteaj.iot.client.ClientComponent;
import com.iteaj.iot.client.ClientConnectProperties;
import com.iteaj.iot.client.TcpSocketClient;
import com.iteaj.iot.codec.adapter.LengthFieldBasedFrameMessageDecoderAdapter;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;

import java.nio.ByteOrder;

public class LengthFieldBasedFrameClient extends TcpSocketClient {

    private final ByteOrder byteOrder;
    private final int maxFrameLength;
    private final int lengthFieldOffset;
    private final int lengthFieldLength;
    private final int lengthAdjustment;
    private final int initialBytesToStrip;
    private final boolean failFast;

    public LengthFieldBasedFrameClient(ClientComponent clientComponent, ClientConnectProperties config
            , ByteOrder byteOrder, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength
            , int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
        super(clientComponent, config);
        this.failFast = failFast;
        this.byteOrder = byteOrder;

        this.maxFrameLength = maxFrameLength;
        this.lengthAdjustment = lengthAdjustment;
        this.lengthFieldOffset = lengthFieldOffset;
        this.lengthFieldLength = lengthFieldLength;

        this.initialBytesToStrip = initialBytesToStrip;
    }

    @Override
    protected ChannelInboundHandler createProtocolDecoder() {
        return new LengthFieldBasedFrameMessageDecoderAdapter(byteOrder, maxFrameLength
                , lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast) {

            @Override
            public Class getMessageClass() {
                return LengthFieldBasedFrameClient.this.getClientComponent().getMessageClass();
            }

            @Override
            public SocketMessage proxy(ChannelHandlerContext ctx, ByteBuf decode) throws Exception {
                return LengthFieldBasedFrameClient.this.getClientComponent().proxy(ctx, decode);
            }

            @Override
            public SocketMessage doDecode(ChannelHandlerContext ctx, ByteBuf decode) {
                return LengthFieldBasedFrameClient.this.getClientComponent().doDecode(ctx, decode);
            }

            @Override
            public SocketMessage createMessage(byte[] message) {
                return LengthFieldBasedFrameClient.this.getClientComponent().createMessage(message);
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy