use of com.github.dirtpowered.dirtmv.network.server.codec.PipelineFactory 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();
}
}
use of com.github.dirtpowered.dirtmv.network.server.codec.PipelineFactory in project DirtMultiversion by DirtPowered.
the class Client method createClient.
public void createClient(UUID key, Callback callback) {
EventLoopGroup loopGroup = serverSession.getMain().getLoopGroup();
Bootstrap clientBootstrap = new Bootstrap();
clientBootstrap.group(loopGroup);
clientBootstrap.channel(NioSocketChannel.class);
clientBootstrap.option(ChannelOption.SO_KEEPALIVE, true);
clientBootstrap.option(ChannelOption.TCP_NODELAY, true);
Configuration c = serverSession.getMain().getConfiguration();
clientBootstrap.remoteAddress(new InetSocketAddress(c.getRemoteServerAddress(), c.getRemoteServerPort()));
clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(ChannelConstants.DEFAULT_PIPELINE, new PipelineFactory(serverSession.getMain(), serverSession.getUserData(), PacketDirection.TO_CLIENT));
ch.pipeline().addLast(ChannelConstants.CLIENT_HANDLER, new ClientSession(serverSession.getMain(), key, serverSession, ch, callback));
}
});
clientBootstrap.connect().addListener((ChannelFutureListener) channelFuture -> {
if (!channelFuture.isSuccess()) {
serverSession.disconnect(ChatUtils.LEGACY_COLOR_CHAR + "cUnable to connect to remote server");
channelFuture.channel().close();
}
});
}
Aggregations