package com.docus.server.common.netty.server;

import com.docus.server.common.JsonDecoder;
import com.docus.server.common.JsonEncoder;
import com.docus.server.common.netty.server.handler.NettyBusinessHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * netty服务端信道初始化
 */
@Component
public class NettyServerInitializer extends ChannelInitializer<Channel> {

    @Resource
    private NettyServerProperties serverProperties;
    @Resource
    private NettyBusinessHandler businessHandler;
    @Resource
    private CollectorChannelCacheMap channelRepository;

    @Override
    protected void initChannel(Channel channel) throws Exception {
        channel.pipeline()
            //空闲检测
            .addLast(new IdleStateHandler(serverProperties.getReaderIdleTimeSeconds(),
                serverProperties.getWriterIdleTimeSeconds(),
                serverProperties.getAllIdleTimeSeconds(),
                TimeUnit.SECONDS)
            )
            // 加载加码解码处理器，同时解决粘包拆包问题
//            .addLast(new ProtobufVarint32FrameDecoder())
//            .addLast(new ProtobufDecoder(Payload.CommMsg.getDefaultInstance()))
//            .addLast(new ProtobufVarint32LengthFieldPrepender())
//            .addLast(new ProtobufEncoder())

//            .addLast(new LineBasedFrameDecoder(2048))
//            .addLast(new LineBasedFrameDecoder(1024))
//            .addLast(new StringDecoder(CharsetUtil.UTF_8))
            .addLast(new LengthFieldBasedFrameDecoder(1024 * 1024, 0, 2, 0, 2))
            // LengthFieldPrepender是一个编码器，主要是在响应字节数据前面添加字节长度字段
            .addLast(new LengthFieldPrepender(2))
            // 对经过粘包和拆包处理之后的数据进行json反序列化，从而得到UserInfo对象
            .addLast(new JsonDecoder())
            // 对响应数据进行编码，主要是将UserInfo对象序列化为json
            .addLast(new JsonEncoder())
            // 加载业务处理器
//            .addLast(new NettyHeartbeatHandler(channelRepository))
            .addLast(businessHandler);
//            .addLast(new EchoServerHandler());
    }
}
