From 5851272688ff204c098bb439fd7825a2e4fbd631 Mon Sep 17 00:00:00 2001 From: MiniDay <372403923@qq.com> Date: Wed, 31 May 2023 00:58:08 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E9=87=8D=E5=86=99=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/ball/bukkit/api/BallBukkitAPI.java | 2 +- .../src/main/resources/config.yml | 2 +- .../core/bungee/api/BallBungeeCordAPI.java | 15 +-- .../src/main/resources/config.yml | 2 +- .../mc/plugin/ball/common/api/BallAPI.java | 98 ++++++++++--------- .../plugin/ball/common/config/BallConfig.java | 2 +- .../common/connector/BallChannelHandler.java | 11 ++- .../connector/BallChannelInitializer.java | 9 +- .../connector/BallKeepAliveHandler.java | 6 -- .../ball/common/data/BallMessageInfo.java | 4 +- .../ball/common/listener/BallListener.java | 3 + .../mc/plugin/ball/common/utils/OS.java | 84 ++++++++++++++++ hamster-ball-server/build.gradle | 12 +-- .../mc/plugin/ball/server/Bootstrap.java | 36 +++---- .../ball/server/command/CommandHandler.java | 8 +- .../ball/server/config/ServerConfig.java | 10 +- .../connector/BallServerChannelHandler.java | 3 +- .../BallServerChannelInitializer.java | 8 +- .../connector/BallServerKeepAliveHandler.java | 4 +- .../src/main/resources/config.yml | 6 +- 20 files changed, 205 insertions(+), 120 deletions(-) create mode 100644 hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/utils/OS.java diff --git a/hamster-ball-bukkit/src/main/java/cn/hamster3/mc/plugin/ball/bukkit/api/BallBukkitAPI.java b/hamster-ball-bukkit/src/main/java/cn/hamster3/mc/plugin/ball/bukkit/api/BallBukkitAPI.java index 3ce0d6f..607ddf0 100644 --- a/hamster-ball-bukkit/src/main/java/cn/hamster3/mc/plugin/ball/bukkit/api/BallBukkitAPI.java +++ b/hamster-ball-bukkit/src/main/java/cn/hamster3/mc/plugin/ball/bukkit/api/BallBukkitAPI.java @@ -42,7 +42,7 @@ public class BallBukkitAPI extends BallAPI { ), pluginConfig.getString("ball-server.host", "ball.hamster3.cn"), pluginConfig.getInt("ball-server.port", 58888), - pluginConfig.getInt("ball-server.nio-thread", 10) + pluginConfig.getInt("ball-server.event-loop-thread", 2) ); instance = new BallBukkitAPI(config); diff --git a/hamster-ball-bukkit/src/main/resources/config.yml b/hamster-ball-bukkit/src/main/resources/config.yml index 6b536d6..12da589 100644 --- a/hamster-ball-bukkit/src/main/resources/config.yml +++ b/hamster-ball-bukkit/src/main/resources/config.yml @@ -4,7 +4,7 @@ debug: false ball-server: host: "ball.hamster3.cn" port: 58888 - nio-thread: 2 + event-loop-thread: 2 server-info: # 服务器唯一识别码,最长 32 字符 diff --git a/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/BallBungeeCordAPI.java b/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/BallBungeeCordAPI.java index 8f88a28..711321f 100644 --- a/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/BallBungeeCordAPI.java +++ b/hamster-ball-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/BallBungeeCordAPI.java @@ -29,17 +29,18 @@ public class BallBungeeCordAPI extends BallAPI { } HamsterBallPlugin plugin = HamsterBallPlugin.getInstance(); Configuration pluginConfig = CoreBungeeCordUtils.getPluginConfig(plugin); + BallConfig config = new BallConfig( new BallServerInfo( - pluginConfig.getString("server-info.id"), - pluginConfig.getString("server-info.name"), + pluginConfig.getString("server-info.id", "Proxy"), + pluginConfig.getString("server-info.name", "Proxy"), BallServerType.PROXY, - pluginConfig.getString("server-info.host", ""), - pluginConfig.getInt("server-info.port", 25577) + pluginConfig.getString("server-info.host"), + pluginConfig.getInt("server-info.port") ), - pluginConfig.getString("ball-server.host"), - pluginConfig.getInt("ball-server.port"), - pluginConfig.getInt("ball-server.nio-thread") + pluginConfig.getString("ball-server.host", "ball.hamster3.cn"), + pluginConfig.getInt("ball-server.port", 58888), + pluginConfig.getInt("ball-server.event-loop-thread", 5) ); instance = new BallBungeeCordAPI(config); diff --git a/hamster-ball-bungeecord/src/main/resources/config.yml b/hamster-ball-bungeecord/src/main/resources/config.yml index 93dc002..a017905 100644 --- a/hamster-ball-bungeecord/src/main/resources/config.yml +++ b/hamster-ball-bungeecord/src/main/resources/config.yml @@ -4,7 +4,7 @@ debug: false ball-server: host: "ball.hamster3.cn" port: 58888 - nio-thread: 10 + event-loop-thread: 10 server-info: # 服务器唯一识别码,最长 32 字符 diff --git a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java index 09a398c..79927dd 100644 --- a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java +++ b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java @@ -14,6 +14,7 @@ import cn.hamster3.mc.plugin.ball.common.event.server.ServerOfflineEvent; import cn.hamster3.mc.plugin.ball.common.event.server.ServerOnlineEvent; import cn.hamster3.mc.plugin.ball.common.listener.BallListener; import cn.hamster3.mc.plugin.ball.common.listener.ListenerPriority; +import cn.hamster3.mc.plugin.ball.common.utils.OS; import cn.hamster3.mc.plugin.core.common.api.CoreAPI; import cn.hamster3.mc.plugin.core.common.data.DisplayMessage; import cn.hamster3.mc.plugin.core.common.util.CoreUtils; @@ -23,8 +24,7 @@ import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.channel.EventLoopGroup; import net.kyori.adventure.text.Component; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -55,7 +55,7 @@ public abstract class BallAPI { private final List listeners; private final Bootstrap bootstrap; - private final NioEventLoopGroup executors; + private final EventLoopGroup executors; protected boolean enabled; protected Channel channel; @@ -63,17 +63,23 @@ public abstract class BallAPI { protected BallAPI(@NotNull BallConfig config) { this.config = config; this.enabled = false; - executors = new NioEventLoopGroup(config.getNioThread()); serverInfo = new ConcurrentHashMap<>(); playerInfo = new ConcurrentHashMap<>(); listeners = new ArrayList<>(); - bootstrap = new Bootstrap(); - bootstrap.group(executors) - .channel(NioSocketChannel.class) + OS currentOS = OS.getCurrentOS(); + getLogger().info(String.format( + "当前操作系统为: %s. 选择 IO 模式为: %s", + currentOS.name(), currentOS.getIOModeName() + )); + + executors = currentOS.getEventLoopGroup(config.getEventLoopThread()); + bootstrap = new Bootstrap() + .group(executors) + .channel(currentOS.getSocketChannel()) .option(ChannelOption.TCP_NODELAY, true) - .handler(BallChannelInitializer.INSTANCE); + .handler(new BallChannelInitializer()); addListener(new BallListener() { @Override @@ -143,6 +149,13 @@ public abstract class BallAPI { } } } + + @Override + public void onConnectRefused() { + enabled = false; + executors.shutdownGracefully(); + getLogger().info("连接至服务中心的请求被拒绝,已关闭仓鼠球。"); + } }); } @@ -165,9 +178,7 @@ public abstract class BallAPI { connect(); try (Connection connection = CoreAPI.getInstance().getConnection()) { - - { - Statement statement = connection.createStatement(); + try (Statement statement = connection.createStatement()) { statement.execute("CREATE TABLE IF NOT EXISTS `hamster_ball_player_info`(" + "`uuid` CHAR(36) PRIMARY KEY," + "`name` VARCHAR(16) NOT NULL," + @@ -186,53 +197,48 @@ public abstract class BallAPI { "`uuid` CHAR(36) NOT NULL," + "`message` TEXT NOT NULL" + ") CHARSET utf8mb4;"); - statement.close(); } - - { - PreparedStatement statement = connection.prepareStatement("REPLACE INTO `hamster_ball_server_info` VALUES(?, ?, ?, ?, ?);"); + try (PreparedStatement statement = connection.prepareStatement( + "REPLACE INTO `hamster_ball_server_info` VALUES(?, ?, ?, ?, ?);" + )) { statement.setString(1, localInfo.getId()); statement.setString(2, localInfo.getName()); statement.setString(3, localInfo.getType().name()); statement.setString(4, localInfo.getHost()); statement.setInt(5, localInfo.getPort()); statement.executeUpdate(); - statement.close(); } - - { - PreparedStatement statement = connection.prepareStatement("SELECT * FROM `hamster_ball_server_info`;"); - ResultSet set = statement.executeQuery(); - while (set.next()) { - String serverID = set.getString("id"); - serverInfo.put(serverID, new BallServerInfo( - serverID, - set.getString("name"), - BallServerType.valueOf(set.getString("type")), - set.getString("host"), - set.getInt("port") - )); + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM `hamster_ball_server_info`;" + )) { + try (ResultSet set = statement.executeQuery()) { + while (set.next()) { + String serverID = set.getString("id"); + serverInfo.put(serverID, new BallServerInfo( + serverID, + set.getString("name"), + BallServerType.valueOf(set.getString("type")), + set.getString("host"), + set.getInt("port") + )); + } } - set.close(); - statement.close(); } - - { - PreparedStatement statement = connection.prepareStatement("SELECT * FROM `hamster_ball_player_info`;"); - ResultSet set = statement.executeQuery(); - while (set.next()) { - UUID uuid = UUID.fromString(set.getString("uuid")); - playerInfo.put(uuid, new BallPlayerInfo(uuid, - set.getString("name"), - set.getString("game_server"), - set.getString("proxy_server"), - set.getBoolean("online") - )); + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM `hamster_ball_player_info`;" + )) { + try (ResultSet set = statement.executeQuery()) { + while (set.next()) { + UUID uuid = UUID.fromString(set.getString("uuid")); + playerInfo.put(uuid, new BallPlayerInfo(uuid, + set.getString("name"), + set.getString("game_server"), + set.getString("proxy_server"), + set.getBoolean("online") + )); + } } - set.close(); - statement.close(); } - } } diff --git a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/config/BallConfig.java b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/config/BallConfig.java index d8fec80..32ea9ce 100644 --- a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/config/BallConfig.java +++ b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/config/BallConfig.java @@ -15,5 +15,5 @@ public class BallConfig { private String host; private int port; - private int nioThread; + private int eventLoopThread; } diff --git a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallChannelHandler.java b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallChannelHandler.java index 73a179a..507ac72 100644 --- a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallChannelHandler.java +++ b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallChannelHandler.java @@ -16,9 +16,7 @@ import java.util.logging.Level; @ChannelHandler.Sharable public class BallChannelHandler extends SimpleChannelInboundHandler { - public static final BallChannelHandler INSTANCE = new BallChannelHandler(); - - private BallChannelHandler() { + public BallChannelHandler() { super(true); } @@ -27,6 +25,12 @@ public class BallChannelHandler extends SimpleChannelInboundHandler { if ("pong".equals(message)) { return; } + if ("connection refused".equals(message)) { + for (BallListener listener : BallAPI.getInstance().getListeners()) { + listener.onConnectRefused(); + } + return; + } BallMessageInfo info = CoreUtils.GSON.fromJson(message, BallMessageInfo.class); for (BallListener listener : BallAPI.getInstance().getListeners()) { try { @@ -186,5 +190,4 @@ public class BallChannelHandler extends SimpleChannelInboundHandler { } } } - } diff --git a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallChannelInitializer.java b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallChannelInitializer.java index d8390d4..321ee74 100644 --- a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallChannelInitializer.java +++ b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallChannelInitializer.java @@ -13,22 +13,19 @@ import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; public class BallChannelInitializer extends ChannelInitializer { - public static final BallChannelInitializer INSTANCE = new BallChannelInitializer(); - - private BallChannelInitializer() { + public BallChannelInitializer() { } @Override protected void initChannel(@NotNull NioSocketChannel channel) { channel.pipeline() .addLast(new IdleStateHandler(0, 7, 0, TimeUnit.SECONDS)) - .addLast(BallKeepAliveHandler.INSTANCE) + .addLast(new BallKeepAliveHandler()) .addLast(new LengthFieldPrepender(8)) .addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 8, 0, 8)) .addLast(new StringDecoder(StandardCharsets.UTF_8)) .addLast(new StringEncoder(StandardCharsets.UTF_8)) - .addLast(BallChannelHandler.INSTANCE); + .addLast(new BallChannelHandler()); } - } diff --git a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallKeepAliveHandler.java b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallKeepAliveHandler.java index 3e868a0..e914800 100644 --- a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallKeepAliveHandler.java +++ b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/connector/BallKeepAliveHandler.java @@ -7,12 +7,6 @@ import io.netty.handler.timeout.IdleStateEvent; @ChannelHandler.Sharable public class BallKeepAliveHandler extends ChannelInboundHandlerAdapter { - public static final BallKeepAliveHandler INSTANCE = new BallKeepAliveHandler(); - - private BallKeepAliveHandler() { - super(); - } - @Override public void userEventTriggered(ChannelHandlerContext context, Object event) throws Exception { if (event instanceof IdleStateEvent) { diff --git a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/data/BallMessageInfo.java b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/data/BallMessageInfo.java index da85dc2..73a2e4f 100644 --- a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/data/BallMessageInfo.java +++ b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/data/BallMessageInfo.java @@ -106,7 +106,7 @@ public class BallMessageInfo { * @return json对象 */ @NotNull - public JsonObject saveToJson() { + public JsonObject toJson() { JsonObject object = new JsonObject(); object.addProperty("channel", channel); object.addProperty("senderID", senderID); @@ -191,6 +191,6 @@ public class BallMessageInfo { @Override public String toString() { - return saveToJson().toString(); + return toJson().toString(); } } diff --git a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/listener/BallListener.java b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/listener/BallListener.java index b0b48ac..c702f9a 100644 --- a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/listener/BallListener.java +++ b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/listener/BallListener.java @@ -28,6 +28,9 @@ public interface BallListener { default void onConnectException(Throwable throwable) { } + default void onConnectRefused() { + } + default void onServiceDead() { } diff --git a/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/utils/OS.java b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/utils/OS.java new file mode 100644 index 0000000..ef5b1de --- /dev/null +++ b/hamster-ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/utils/OS.java @@ -0,0 +1,84 @@ +package cn.hamster3.mc.plugin.ball.common.utils; + +import io.netty.channel.Channel; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.ServerChannel; +import io.netty.channel.epoll.EpollEventLoopGroup; +import io.netty.channel.epoll.EpollServerSocketChannel; +import io.netty.channel.epoll.EpollSocketChannel; +import io.netty.channel.kqueue.KQueueEventLoopGroup; +import io.netty.channel.kqueue.KQueueServerSocketChannel; +import io.netty.channel.kqueue.KQueueSocketChannel; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import org.jetbrains.annotations.NotNull; + +public enum OS { + WINDOWS, + LINUX, + MACOS, + OTHER; + + @NotNull + public static OS getCurrentOS() { + String s = System.getProperties().get("os.name").toString().toLowerCase(); + if (s.contains("windows")) { + return WINDOWS; + } + if (s.contains("linux")) { + return LINUX; + } + if (s.contains("mac")) { + return MACOS; + } + return OTHER; + } + + public String getIOModeName() { + switch (this) { + case LINUX: + return "epoll"; + case MACOS: + return "kqueue"; + default: + return "nio"; + } + } + + @NotNull + public EventLoopGroup getEventLoopGroup(int nThread) { + switch (this) { + case LINUX: + return new EpollEventLoopGroup(nThread); + case MACOS: + return new KQueueEventLoopGroup(nThread); + default: + return new NioEventLoopGroup(nThread); + } + } + + @NotNull + public Class getSocketChannel() { + switch (this) { + case LINUX: + return EpollSocketChannel.class; + case MACOS: + return KQueueSocketChannel.class; + default: + return NioSocketChannel.class; + } + } + + @NotNull + public Class getServerSocketChannel() { + switch (this) { + case LINUX: + return EpollServerSocketChannel.class; + case MACOS: + return KQueueServerSocketChannel.class; + default: + return NioServerSocketChannel.class; + } + } +} diff --git a/hamster-ball-server/build.gradle b/hamster-ball-server/build.gradle index 46f4bd5..d0431b3 100644 --- a/hamster-ball-server/build.gradle +++ b/hamster-ball-server/build.gradle @@ -5,19 +5,19 @@ evaluationDependsOn(':hamster-ball-common') dependencies { apiShade(project(":hamster-ball-common")) { transitive = false } - // https://mvnrepository.com/artifact/org.slf4j/slf4j-api - implementation 'org.slf4j:slf4j-api:2.0.3' +// // https://mvnrepository.com/artifact/org.slf4j/slf4j-api +// implementation 'org.slf4j:slf4j-api:2.0.3' // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core - shade 'org.apache.logging.log4j:log4j-core:2.19.0' + implementationShade 'org.apache.logging.log4j:log4j-core:2.19.0' // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl - shade 'org.apache.logging.log4j:log4j-slf4j-impl:2.19.0' + implementationShade 'org.apache.logging.log4j:log4j-slf4j-impl:2.19.0' // https://mvnrepository.com/artifact/io.netty/netty-all //noinspection GradlePackageUpdate - implementationShade 'io.netty:netty-all:4.1.82.Final' + implementationShade 'io.netty:netty-all:4.1.86.Final' // https://mvnrepository.com/artifact/org.yaml/snakeyaml - implementationShade 'org.yaml:snakeyaml:1.33' + implementationShade 'org.yaml:snakeyaml:2.0' // https://mvnrepository.com/artifact/com.google.code.gson/gson //noinspection GradlePackageUpdate implementationShade 'com.google.code.gson:gson:2.8.9' diff --git a/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/Bootstrap.java b/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/Bootstrap.java index e1b8118..abec97c 100644 --- a/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/Bootstrap.java +++ b/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/Bootstrap.java @@ -1,49 +1,51 @@ package cn.hamster3.mc.plugin.ball.server; +import cn.hamster3.mc.plugin.ball.common.utils.OS; import cn.hamster3.mc.plugin.ball.server.command.CommandHandler; import cn.hamster3.mc.plugin.ball.server.config.ServerConfig; import cn.hamster3.mc.plugin.ball.server.connector.BallServerChannelInitializer; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.channel.EventLoopGroup; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.lang.management.ManagementFactory; import java.nio.file.Files; import java.nio.file.StandardCopyOption; public class Bootstrap { private static final Logger LOGGER = LoggerFactory.getLogger("Bootstrap"); - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, InterruptedException { if (initDefaultFile()) { LOGGER.info("请重新启动该程序."); return; } ServerConfig.init(); LOGGER.info("配置文件加载完成."); - - NioEventLoopGroup loopGroup = new NioEventLoopGroup(ServerConfig.getNioThread()); + OS currentOS = OS.getCurrentOS(); + LOGGER.info("当前操作系统为: {}. 选择 IO 模式为: {}", currentOS.name(), currentOS.getIOModeName()); + EventLoopGroup loopGroup = currentOS.getEventLoopGroup(ServerConfig.getEventLoopThread()); ServerBootstrap bootstrap = new ServerBootstrap() .group(loopGroup) - .channel(NioServerSocketChannel.class) + .channel(currentOS.getServerSocketChannel()) .childOption(ChannelOption.TCP_NODELAY, true) - .childHandler(BallServerChannelInitializer.INSTANCE); - ChannelFuture channelFuture = bootstrap.bind(ServerConfig.getHost(), ServerConfig.getPort()); - channelFuture.addListener(future -> { - if (future.isSuccess()) { - LOGGER.info("服务器已启动. 输入 stop 来关闭该程序."); - } else { - LOGGER.error("仓鼠球服务器启动失败!", future.cause()); - loopGroup.shutdownGracefully(); - } - }); - + .childHandler(new BallServerChannelInitializer()); + ChannelFuture future = bootstrap.bind(ServerConfig.getHost(), ServerConfig.getPort()); + future.await(); + if (future.isSuccess()) { + LOGGER.info("进程信息: {}", ManagementFactory.getRuntimeMXBean().getName()); + LOGGER.info("服务器已启动. 输入 stop 来关闭该程序."); + } else { + LOGGER.error("仓鼠球服务器启动失败!", future.cause()); + loopGroup.shutdownGracefully(); + return; + } CommandHandler.INSTANCE.start(loopGroup); } diff --git a/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/command/CommandHandler.java b/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/command/CommandHandler.java index 1e9aa6a..72811b7 100644 --- a/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/command/CommandHandler.java +++ b/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/command/CommandHandler.java @@ -1,6 +1,6 @@ package cn.hamster3.mc.plugin.ball.server.command; -import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.EventLoopGroup; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,12 +9,12 @@ import java.util.Scanner; public class CommandHandler { public static final CommandHandler INSTANCE = new CommandHandler(); - private static final Logger LOGGER = LoggerFactory.getLogger("command"); - private NioEventLoopGroup loopGroup; + private static final Logger LOGGER = LoggerFactory.getLogger("CommandHandler"); + private EventLoopGroup loopGroup; private boolean started; - public void start(NioEventLoopGroup loopGroup) { + public void start(EventLoopGroup loopGroup) { this.loopGroup = loopGroup; started = true; diff --git a/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/config/ServerConfig.java b/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/config/ServerConfig.java index da56c0a..8e1e439 100644 --- a/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/config/ServerConfig.java +++ b/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/config/ServerConfig.java @@ -16,7 +16,7 @@ public final class ServerConfig { private static String host; private static int port; - private static int nioThread; + private static int eventLoopThread; private static boolean enableAcceptList; private static List acceptList; @@ -33,13 +33,13 @@ public final class ServerConfig { host = (String) map.get("host"); port = (int) map.get("port"); - nioThread = (int) map.get("nio-thread"); + eventLoopThread = (int) map.getOrDefault("event-loop-thread", 5); enableAcceptList = (boolean) map.get("enable-accept-list"); acceptList = (List) map.get("accept-list"); LOGGER.info("host: {}", host); LOGGER.info("port: {}", port); - LOGGER.info("nioThread: {}", nioThread); + LOGGER.info("eventLoopThread: {}", eventLoopThread); LOGGER.info("enableAcceptList: {}", enableAcceptList); LOGGER.info("acceptList: {}", acceptList); } @@ -52,8 +52,8 @@ public final class ServerConfig { return port; } - public static int getNioThread() { - return nioThread; + public static int getEventLoopThread() { + return eventLoopThread; } public static boolean isEnableAcceptList() { diff --git a/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/connector/BallServerChannelHandler.java b/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/connector/BallServerChannelHandler.java index c0c1001..b0327eb 100644 --- a/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/connector/BallServerChannelHandler.java +++ b/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/connector/BallServerChannelHandler.java @@ -11,10 +11,9 @@ import org.slf4j.LoggerFactory; @ChannelHandler.Sharable public class BallServerChannelHandler extends SimpleChannelInboundHandler { - public static final BallServerChannelHandler INSTANCE = new BallServerChannelHandler(); private static final Logger LOGGER = LoggerFactory.getLogger("BallServerChannelHandler"); - private BallServerChannelHandler() { + public BallServerChannelHandler() { super(true); } diff --git a/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/connector/BallServerChannelInitializer.java b/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/connector/BallServerChannelInitializer.java index f9157e2..5821ecd 100644 --- a/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/connector/BallServerChannelInitializer.java +++ b/hamster-ball-server/src/main/java/cn/hamster3/mc/plugin/ball/server/connector/BallServerChannelInitializer.java @@ -20,12 +20,10 @@ import java.util.List; import java.util.concurrent.TimeUnit; public class BallServerChannelInitializer extends ChannelInitializer { - public static final BallServerChannelInitializer INSTANCE = new BallServerChannelInitializer(); public static final List CHANNELS = new ArrayList<>(); - private static final Logger LOGGER = LoggerFactory.getLogger("BallServerChannelInitializer"); - private BallServerChannelInitializer() { + public BallServerChannelInitializer() { } public static void broadcastMessage(BallMessageInfo messageInfo) { @@ -50,12 +48,12 @@ public class BallServerChannelInitializer extends ChannelInitializer { - public static final BallServerKeepAliveHandler INSTANCE = new BallServerKeepAliveHandler(); - private static final Logger LOGGER = LoggerFactory.getLogger("BallServerKeepAliveHandler"); - private BallServerKeepAliveHandler() { + public BallServerKeepAliveHandler() { super(true); } diff --git a/hamster-ball-server/src/main/resources/config.yml b/hamster-ball-server/src/main/resources/config.yml index 6bf69c3..195701a 100644 --- a/hamster-ball-server/src/main/resources/config.yml +++ b/hamster-ball-server/src/main/resources/config.yml @@ -5,10 +5,10 @@ port: 58888 # 线程池数量 # 建议设置为全服最大玩家数 / 20 -# 不建议低于 5 -nio-thread: 10 +# 不建议低于 4 +event-loop-thread: 5 -# 是否启用IP 白名单 +# 是否启用 IP 白名单 enable-accept-list: true # 允许连接至服务的 ip 名单