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

com.envisioniot.sub.client.internal.netty.ClientDecoder Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
package com.envisioniot.sub.client.internal.netty;

import com.envisioniot.sub.common.generated.Common;
import com.envisioniot.sub.common.netty.PackageAnalyser;
import com.envisioniot.sub.common.netty.TransferVer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * created by jie.jin on 2018/12/24.
 */
public class ClientDecoder extends ByteToMessageDecoder {
    // TODO maxFrameLength + safe skip + fail-fast option (just like LengthFieldBasedFrameDecoder)

    private static final Logger LOG = Logger.getLogger(ClientDecoder.class);

    @Override
    protected void decode(final ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception {
        Common.TransferPkg pkg = PackageAnalyser.parse(in);
        if (pkg == null) {
            return;
        }
        TransferVer.setChannelVer(ctx, pkg);
        int seqId = pkg.getSeqId();

        // ACK_CMD is not only ack, but also heartbeat.
        if (pkg.getCmdId() == PackageAnalyser.ACK_CMD) {
            LOG.info("ack: " + seqId + ": " + ctx.channel().remoteAddress());
            return;
        }

        PackageAnalyser.parseData(out, pkg);
    }
}