use of com.github.dirtpowered.dirtmv.network.server.codec.ConnectionLimiterHandler in project DirtMultiversion by DirtPowered.
the class Server method bind.
public void bind() {
Class<? extends ServerChannel> socketChannel;
if (Epoll.isAvailable()) {
Logger.info("Epoll is available. Using it");
socketChannel = EpollServerSocketChannel.class;
bossGroup = new EpollEventLoopGroup();
} else {
Logger.warn("Epoll not available, using NIO. Reason: " + Epoll.unavailabilityCause().getMessage());
socketChannel = NioServerSocketChannel.class;
bossGroup = new NioEventLoopGroup();
}
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup).channel(socketChannel).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel) {
ServerSession serverSession = new ServerSession(channel, main, instance);
channel.pipeline().addFirst("timer", new ReadTimeoutHandler(30));
channel.pipeline().addLast(ChannelConstants.CONNECTION_THROTTLE, new ConnectionLimiterHandler(main.getConfiguration()));
channel.pipeline().addLast(ChannelConstants.DEFAULT_PIPELINE, new PipelineFactory(main, serverSession.getUserData(), PacketDirection.TO_SERVER));
channel.pipeline().addLast(ChannelConstants.SERVER_HANDLER, serverSession);
}
}).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture future;
try {
Configuration c = main.getConfiguration();
future = b.bind(c.getBindAddress(), c.getBindPort()).sync().addListener(callback -> {
Logger.info("ready for connections!");
});
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
Logger.error("address already in use: {}", e.getLocalizedMessage());
stop();
}
}
Aggregations