Search in sources :

Example 1 with TerasologyServerPipelineFactory

use of org.terasology.engine.network.internal.pipelineFactory.TerasologyServerPipelineFactory in project Terasology by MovingBlocks.

the class NetworkSystemImpl method host.

@Override
public void host(int port, boolean dedicatedServer) throws HostingFailedException {
    if (mode == NetworkMode.NONE) {
        try {
            if (hibernationSettings.isPresent()) {
                hibernationSettings.get().setHibernationAllowed(false);
            }
            mode = dedicatedServer ? NetworkMode.DEDICATED_SERVER : NetworkMode.LISTEN_SERVER;
            for (EntityRef entity : entityManager.getEntitiesWith(NetworkComponent.class)) {
                registerNetworkEntity(entity);
            }
            generateSerializationTables();
            // Configure the server.
            bossGroup = new NioEventLoopGroup();
            workerGroup = new NioEventLoopGroup();
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).localAddress(port).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new TerasologyServerPipelineFactory(this));
            // Start the server.
            serverChannelFuture = b.bind();
            logger.info("Started server on port {}", port);
            if (config.getServerMOTD() != null) {
                logger.info("Server MOTD is \"{}\"", config.getServerMOTD());
            } else {
                logger.info("No server MOTD is defined");
            }
            if (serverChannelFuture.isSuccess()) {
                logger.info("Server started");
            }
            serverChannelFuture.sync();
            nextNetworkTick = time.getRealTimeInMs();
        } catch (ChannelException e) {
            if (e.getCause() instanceof BindException) {
                throw new HostingFailedException("Port already in use (are you already hosting a game?)", e.getCause());
            } else {
                throw new HostingFailedException("Failed to host game", e.getCause());
            }
        } catch (InterruptedException e) {
            shutdown();
            throw new HostingFailedException("Server has been interrupted", e.getCause());
        }
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) HostingFailedException(org.terasology.engine.network.exceptions.HostingFailedException) TerasologyServerPipelineFactory(org.terasology.engine.network.internal.pipelineFactory.TerasologyServerPipelineFactory) BindException(java.net.BindException) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelException(io.netty.channel.ChannelException)

Aggregations

ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelException (io.netty.channel.ChannelException)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 BindException (java.net.BindException)1 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)1 HostingFailedException (org.terasology.engine.network.exceptions.HostingFailedException)1 TerasologyServerPipelineFactory (org.terasology.engine.network.internal.pipelineFactory.TerasologyServerPipelineFactory)1