Search in sources :

Example 1 with PipelineFactory

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();
    }
}
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)

Example 2 with PipelineFactory

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();
        }
    });
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelOption(io.netty.channel.ChannelOption) Configuration(com.github.dirtpowered.dirtmv.api.Configuration) ChatUtils(com.github.dirtpowered.dirtmv.data.utils.ChatUtils) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) PacketDirection(com.github.dirtpowered.dirtmv.data.translator.PacketDirection) PipelineFactory(com.github.dirtpowered.dirtmv.network.server.codec.PipelineFactory) Bootstrap(io.netty.bootstrap.Bootstrap) Callback(com.github.dirtpowered.dirtmv.data.interfaces.Callback) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) ChannelConstants(com.github.dirtpowered.dirtmv.network.server.codec.ChannelConstants) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) Configuration(com.github.dirtpowered.dirtmv.api.Configuration) InetSocketAddress(java.net.InetSocketAddress) PipelineFactory(com.github.dirtpowered.dirtmv.network.server.codec.PipelineFactory) Bootstrap(io.netty.bootstrap.Bootstrap)

Aggregations

Configuration (com.github.dirtpowered.dirtmv.api.Configuration)2 PacketDirection (com.github.dirtpowered.dirtmv.data.translator.PacketDirection)2 ChannelConstants (com.github.dirtpowered.dirtmv.network.server.codec.ChannelConstants)2 PipelineFactory (com.github.dirtpowered.dirtmv.network.server.codec.PipelineFactory)2 ChannelInitializer (io.netty.channel.ChannelInitializer)2 ChannelOption (io.netty.channel.ChannelOption)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 DirtMultiVersion (com.github.dirtpowered.dirtmv.DirtMultiVersion)1 DirtServer (com.github.dirtpowered.dirtmv.api.DirtServer)1 Callback (com.github.dirtpowered.dirtmv.data.interfaces.Callback)1 UserData (com.github.dirtpowered.dirtmv.data.user.UserData)1 ChatUtils (com.github.dirtpowered.dirtmv.data.utils.ChatUtils)1 ServerSession (com.github.dirtpowered.dirtmv.network.server.ServerSession)1 ConnectionLimiterHandler (com.github.dirtpowered.dirtmv.network.server.codec.ConnectionLimiterHandler)1 MultiSession (com.github.dirtpowered.dirtmv.session.MultiSession)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1