Search in sources :

Example 1 with ConnectionLimiterHandler

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();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOption(io.netty.channel.ChannelOption) Getter(lombok.Getter) Logger(org.pmw.tinylog.Logger) Configuration(com.github.dirtpowered.dirtmv.api.Configuration) UserData(com.github.dirtpowered.dirtmv.data.user.UserData) Base64(org.apache.commons.codec.binary.Base64) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) PacketDirection(com.github.dirtpowered.dirtmv.data.translator.PacketDirection) PipelineFactory(com.github.dirtpowered.dirtmv.network.server.codec.PipelineFactory) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) ImageIO(javax.imageio.ImageIO) MultiSession(com.github.dirtpowered.dirtmv.session.MultiSession) SocketChannel(io.netty.channel.socket.SocketChannel) ConnectionLimiterHandler(com.github.dirtpowered.dirtmv.network.server.codec.ConnectionLimiterHandler) EventLoopGroup(io.netty.channel.EventLoopGroup) DirtServer(com.github.dirtpowered.dirtmv.api.DirtServer) ChannelInitializer(io.netty.channel.ChannelInitializer) BufferedImage(java.awt.image.BufferedImage) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) DirtMultiVersion(com.github.dirtpowered.dirtmv.DirtMultiVersion) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ServerChannel(io.netty.channel.ServerChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ChannelFuture(io.netty.channel.ChannelFuture) Epoll(io.netty.channel.epoll.Epoll) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelConstants(com.github.dirtpowered.dirtmv.network.server.codec.ChannelConstants) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Configuration(com.github.dirtpowered.dirtmv.api.Configuration) PipelineFactory(com.github.dirtpowered.dirtmv.network.server.codec.PipelineFactory) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) ConnectionLimiterHandler(com.github.dirtpowered.dirtmv.network.server.codec.ConnectionLimiterHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

DirtMultiVersion (com.github.dirtpowered.dirtmv.DirtMultiVersion)1 Configuration (com.github.dirtpowered.dirtmv.api.Configuration)1 DirtServer (com.github.dirtpowered.dirtmv.api.DirtServer)1 PacketDirection (com.github.dirtpowered.dirtmv.data.translator.PacketDirection)1 UserData (com.github.dirtpowered.dirtmv.data.user.UserData)1 ChannelConstants (com.github.dirtpowered.dirtmv.network.server.codec.ChannelConstants)1 ConnectionLimiterHandler (com.github.dirtpowered.dirtmv.network.server.codec.ConnectionLimiterHandler)1 PipelineFactory (com.github.dirtpowered.dirtmv.network.server.codec.PipelineFactory)1 MultiSession (com.github.dirtpowered.dirtmv.session.MultiSession)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelOption (io.netty.channel.ChannelOption)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 ServerChannel (io.netty.channel.ServerChannel)1 Epoll (io.netty.channel.epoll.Epoll)1 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)1 EpollServerSocketChannel (io.netty.channel.epoll.EpollServerSocketChannel)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1