}
final EventLoopGroup serverGroup = new NioEventLoopGroup();
final EventLoopGroup workerGroup = new NioEventLoopGroup(protocol.getThreads());
final ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(serverGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
if (sslContext != null) {
pipeline.addLast(sslContext.newHandler(channel.alloc()));
}
pipeline.addLast(
new ObjectEncoder(),
new ObjectDecoder(ClassResolvers.softCachingConcurrentResolver(getClass().getClassLoader())),
new TcpProtocolServerHandler(NettyTcpProtocolServer.this)
);
}
})
.option(ChannelOption.SO_BACKLOG, 128);
if (protocol.getSendBufferSize() > -1) {
bootstrap.option(ChannelOption.SO_SNDBUF, protocol.getSendBufferSize());
}
if (protocol.getReceiveBufferSize() > -1) {
bootstrap.option(ChannelOption.SO_RCVBUF, protocol.getReceiveBufferSize());
}
bootstrap.option(ChannelOption.TCP_NODELAY, true);
bootstrap.option(ChannelOption.SO_REUSEADDR, true);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.option(ChannelOption.SO_BACKLOG, protocol.getAcceptBacklog());
if (protocol.getTrafficClass() > -1) {
bootstrap.option(ChannelOption.IP_TOS, protocol.getTrafficClass());
}
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
bootstrap.bind(member.host(), member.port()).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
channelFuture.channel().closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {