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

xin.alum.aim.coder.TEXTDecoder Maven / Gradle / Ivy

There is a newer version: 1.9.6
Show newest version
package xin.alum.aim.coder;

import io.netty.util.ReferenceCountUtil;
import xin.alum.aim.config.DataAgreement;
import xin.alum.aim.constant.AIMConstant;
import xin.alum.aim.constant.ChannelAttr;
import xin.alum.aim.model.Sent;
import xin.alum.aim.util.JSONUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther Alum(alum @ live.cn)
 * @date 2021/8/20 18:44
 */
public class TEXTDecoder extends StringDecoder {

    public TEXTDecoder() {
        super(CharsetUtil.UTF_8);
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List list) {
        try {
            super.decode(ctx, in, list);
            DataAgreement dataAgreement = ctx.channel().attr(ChannelAttr.AGREEMENT).get();
            if (dataAgreement != DataAgreement.Json) {
                ctx.channel().attr(ChannelAttr.AGREEMENT).set(DataAgreement.Text);
            }
        } catch (Exception e) {
            System.err.println("自定义Binary解码器");
            ctx.channel().attr(ChannelAttr.AGREEMENT).set(DataAgreement.Binary);
            list.add(in);
        }
    }
}