diff --git a/game/src/main/org/apollo/Server.java b/game/src/main/org/apollo/Server.java index 41673ecd..f1df758b 100644 --- a/game/src/main/org/apollo/Server.java +++ b/game/src/main/org/apollo/Server.java @@ -1,12 +1,6 @@ package org.apollo; -import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioServerSocketChannel; - +import java.net.BindException; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.file.Paths; @@ -28,6 +22,13 @@ import org.apollo.net.release.Release; import com.google.common.base.Stopwatch; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; + /** * The core class of the Apollo server. * @@ -59,6 +60,7 @@ public final class Server { server.bind(service, http, jaggrab); } catch (Throwable t) { logger.log(Level.SEVERE, "Error whilst starting server.", t); + System.exit(0); } logger.fine("Starting apollo took " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms."); @@ -97,25 +99,42 @@ public final class Server { * @param service The service address to bind to. * @param http The HTTP address to bind to. * @param jaggrab The JAGGRAB address to bind to. + * @throws BindException If the ServerBootstrap fails to bind to the SocketAddress for any + * reason. */ - public void bind(SocketAddress service, SocketAddress http, SocketAddress jaggrab) { + public void bind(SocketAddress service, SocketAddress http, SocketAddress jaggrab) throws BindException { + logger.fine("Binding service listener to address: " + service + "..."); + bind(serviceBootstrap, service); + try { - logger.fine("Binding service listener to address: " + service + "..."); - serviceBootstrap.bind(service).sync(); - - logger.fine("Binding HTTP listener to address: " + http + "..."); - httpBootstrap.bind(http).sync(); - - logger.fine("Binding JAGGRAB listener to address: " + jaggrab + "..."); - jaggrabBootstrap.bind(jaggrab).sync(); - } catch (InterruptedException exception) { - logger.log(Level.SEVERE, "Binding to a port failed: ensure apollo isn't already running.", exception); - System.exit(1); + logger.fine("Binding HTTP listener to address: " + http + "..."); + bind(httpBootstrap, http); + } catch (Exception cause) { + logger.warning("Unable to bind to HTTP, JAGGRAB will be used as a fallback however this is not recommended."); } + logger.fine("Binding JAGGRAB listener to address: " + jaggrab + "..."); + bind(jaggrabBootstrap, jaggrab); + logger.info("Ready for connections."); } + /** + * Attempts to bind the specified ServerBootstrap to the specified SocketAddress. + * + * @param bootstrap The ServerBootstrap. + * @param address The SocketAddress. + * @throws BindException If the ServerBootstrap fails to bind to the SocketAddress for any + * reason. + */ + private void bind(ServerBootstrap bootstrap, SocketAddress address) throws BindException { + try { + bootstrap.bind(address).sync(); + } catch (Exception cause) { + throw new BindException("Failed to bind to: " + address); + } + } + /** * Initialises the server. * diff --git a/pom.xml b/pom.xml index 28626d87..29a33b97 100644 --- a/pom.xml +++ b/pom.xml @@ -47,13 +47,13 @@ org.apache.commons commons-compress - 1.9 + 1.10 org.jruby jruby-complete - 1.7.19 + 9.0.0.0 @@ -65,7 +65,7 @@ io.netty netty-all - 4.0.27.Final + 4.0.30.Final compile @@ -78,7 +78,7 @@ com.mchange c3p0 - 0.9.5 + 0.9.5.1