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