From c7ef8d4481137fca888da800dcb507ca53d4b2a5 Mon Sep 17 00:00:00 2001 From: HSGamer Date: Sun, 5 Jun 2022 10:30:28 +0700 Subject: [PATCH] multi port binding --- .../kotlin/com/viaversion/aas/AspirinServer.kt | 17 ++++++++++------- .../com/viaversion/aas/config/VIAaaSConfig.kt | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/com/viaversion/aas/AspirinServer.kt b/src/main/kotlin/com/viaversion/aas/AspirinServer.kt index 5120c3e..70538f4 100644 --- a/src/main/kotlin/com/viaversion/aas/AspirinServer.kt +++ b/src/main/kotlin/com/viaversion/aas/AspirinServer.kt @@ -70,7 +70,7 @@ object AspirinServer { val parentLoop = eventLoopGroup() val childLoop = eventLoopGroup() - var chFuture: ChannelFuture? = null + var chFutures = mutableListOf() val dnsResolver = DnsNameResolverBuilder(childLoop.next()) .socketChannelFactory(channelSocketFactory(childLoop)) .channelFactory(channelDatagramFactory(childLoop)) @@ -95,11 +95,10 @@ object AspirinServer { mainFinishSignal() ktorServer?.stop(1000, 1000) httpClient.close() - listOf?>( - chFuture?.channel()?.close(), + (chFutures.map { it.channel().close() } + listOf?>( parentLoop.shutdownGracefully(), childLoop.shutdownGracefully() - ) + )) .forEach { it?.sync() } } } @@ -115,7 +114,7 @@ object AspirinServer { fun mainStartSignal() = initFuture.complete(Unit) fun listenPorts(args: Array) { - chFuture = ServerBootstrap() + val serverBootstrap = ServerBootstrap() .group(parentLoop, childLoop) .channelFactory(channelServerSocketFactory(parentLoop)) .childHandler(FrontEndInit) @@ -123,12 +122,16 @@ object AspirinServer { .childOption(ChannelOption.IP_TOS, 0x18) .childOption(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.TCP_FASTOPEN, 32) - .bind(InetAddress.getByName(VIAaaSConfig.bindAddress), VIAaaSConfig.port) + VIAaaSConfig.ports.forEach { + chFutures.add(serverBootstrap.bind(InetAddress.getByName(VIAaaSConfig.bindAddress), it)) + } ktorServer = embeddedServer(Netty, commandLineEnvironment(args)) {}.start(false) viaaasLogger.info("Using compression: ${Natives.compress.loadedVariant}, crypto: ${Natives.cipher.loadedVariant}") - viaaasLogger.info("Binded minecraft into " + chFuture!!.sync().channel().localAddress()) + chFutures.forEach { + viaaasLogger.info("Binded minecraft into into " + it.sync().channel().localAddress()) + } viaaasLogger.info( "Application started in " + ManagementFactory.getRuntimeMXBean().uptime .milliseconds.toDouble(DurationUnit.SECONDS) + "s" diff --git a/src/main/kotlin/com/viaversion/aas/config/VIAaaSConfig.kt b/src/main/kotlin/com/viaversion/aas/config/VIAaaSConfig.kt index bc505e0..35a3627 100644 --- a/src/main/kotlin/com/viaversion/aas/config/VIAaaSConfig.kt +++ b/src/main/kotlin/com/viaversion/aas/config/VIAaaSConfig.kt @@ -63,7 +63,7 @@ object VIAaaSConfig : Config(File("config/viaaas.yml")) { } } - val port: Int get() = this.getInt("port", 25565) + val ports: List get() = Objects.toString(this.get("port", Any::class.java, "25565"))!!.split(',').map { it.trim().toInt() }.distinct() val bindAddress: String get() = this.getString("bind-address", "localhost")!! val hostName: List get() = this.get("host-name", List::class.java, listOf("viaaas.localhost"))!!.map { it.toString() }