@Override
public void run() {
SERVER.set(AsyncServer.this);
try {
// ServerBootstrap is a helper class that sets up a server
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, backlogLength)
.childOption(ChannelOption.MAX_MESSAGES_PER_READ, NIO_BUFFER_LIMIT)
.childOption(ChannelOption.TCP_NODELAY, tcpNoDelay)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_RCVBUF, 30 * 1024 * 1024)
.childOption(ChannelOption.RCVBUF_ALLOCATOR,
new FixedRecvByteBufAllocator(100 * 1024))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
// Register accumulation processing handler
p.addLast(new NioFrameDecoder(100 * 1024 * 1024, 0, 4, 0, 0));
// Register message processing handler
p.addLast(new NioServerInboundHandler());
}
});
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(port).sync();
LOG.info("AsyncServer startup");
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();