Search in sources :

Example 1 with HostingFailedException

use of org.terasology.engine.network.exceptions.HostingFailedException 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)

Example 2 with HostingFailedException

use of org.terasology.engine.network.exceptions.HostingFailedException in project Terasology by MovingBlocks.

the class StartServer method step.

@Override
public boolean step() {
    try {
        Config config = context.get(Config.class);
        int port = config.getNetwork().getServerPort();
        context.get(NetworkSystem.class).host(port, dedicated);
    } catch (HostingFailedException e) {
        NUIManager nui = context.get(NUIManager.class);
        if (nui != null) {
            nui.pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Failed to Host", e.getMessage() + " - Reverting to single player");
        } else {
            logger.error("Failed to Host. NUI was also unavailable for warning popup (headless?)", e);
            throw new RuntimeException("Cannot host game successfully from likely headless start, terminating");
        }
    }
    return true;
}
Also used : Config(org.terasology.engine.config.Config) HostingFailedException(org.terasology.engine.network.exceptions.HostingFailedException) NetworkSystem(org.terasology.engine.network.NetworkSystem) NUIManager(org.terasology.engine.rendering.nui.NUIManager)

Aggregations

HostingFailedException (org.terasology.engine.network.exceptions.HostingFailedException)2 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 Config (org.terasology.engine.config.Config)1 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)1 NetworkSystem (org.terasology.engine.network.NetworkSystem)1 TerasologyServerPipelineFactory (org.terasology.engine.network.internal.pipelineFactory.TerasologyServerPipelineFactory)1 NUIManager (org.terasology.engine.rendering.nui.NUIManager)1