package com.yfd.platform.component.nettyclient;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;

/**
 * 通道初始化

 */
@ChannelHandler.Sharable
public class BootNettyChannelInitializer<SocketChannel> extends ChannelInitializer<Channel> {
    private static final int MAX_FRAME_LENGTH = 1024 * 1024;  //最大长度
    private static final int LENGTH_FIELD_LENGTH = 4;  //长度字段所占的字节数
    private static final int LENGTH_FIELD_OFFSET = 19;  //长度偏移
    private static final int LENGTH_ADJUSTMENT = 0;
    private static final int INITIAL_BYTES_TO_STRIP = 0;
    private static final byte[] DELIMITER = {(byte) 0xEB, (byte) 0x90};
    @Override
    protected void initChannel(Channel ch) throws Exception {

        ch.pipeline().addLast("encoder", new MyMessageEncoder());
        ch.pipeline().addLast(new MyDelimiterBasedFrameDecoder(MAX_FRAME_LENGTH,
                Unpooled.wrappedBuffer(DELIMITER)));
//        ch.pipeline().addLast("decoder", new MyMessageDecoder());
        /**
         * 自定义ChannelInboundHandlerAdapter
         */
        ch.pipeline().addLast(new BootNettyChannelInboundHandlerAdapter());

    }

}
