package com.jsowell.netty.server.yunkuaichong;

import com.jsowell.netty.decoder.StartAndLengthFieldFrameDecoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;

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

@Component
public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {

	@Resource
	NettyServerHandler nettyServerHandler;

	@Override
	protected void initChannel(SocketChannel channel) throws Exception {
		ChannelPipeline pipeline = channel.pipeline();
		// pipeline.addLast("frameDecoder",new CustomDecoder());
		pipeline.addLast("frameDecoder", new StartAndLengthFieldFrameDecoder(0x68));
		pipeline.addLast("decoder", new ByteArrayDecoder());
		pipeline.addLast("encoder", new ByteArrayDecoder());
		//读超时时间设置为10s，0表示不监控
		pipeline.addLast(new IdleStateHandler(60, 0, 0, TimeUnit.SECONDS));
		pipeline.addLast("handler", nettyServerHandler);
	}

}
