perf: 重写部分代码

This commit is contained in:
2023-05-31 00:58:08 +08:00
parent 8e19103334
commit 5851272688
20 changed files with 205 additions and 120 deletions

View File

@@ -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);

View File

@@ -4,7 +4,7 @@ debug: false
ball-server:
host: "ball.hamster3.cn"
port: 58888
nio-thread: 2
event-loop-thread: 2
server-info:
# 服务器唯一识别码,最长 32 字符

View File

@@ -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);

View File

@@ -4,7 +4,7 @@ debug: false
ball-server:
host: "ball.hamster3.cn"
port: 58888
nio-thread: 10
event-loop-thread: 10
server-info:
# 服务器唯一识别码,最长 32 字符

View File

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

View File

@@ -15,5 +15,5 @@ public class BallConfig {
private String host;
private int port;
private int nioThread;
private int eventLoopThread;
}

View File

@@ -16,9 +16,7 @@ import java.util.logging.Level;
@ChannelHandler.Sharable
public class BallChannelHandler extends SimpleChannelInboundHandler<String> {
public static final BallChannelHandler INSTANCE = new BallChannelHandler();
private BallChannelHandler() {
public BallChannelHandler() {
super(true);
}
@@ -27,6 +25,12 @@ public class BallChannelHandler extends SimpleChannelInboundHandler<String> {
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<String> {
}
}
}
}

View File

@@ -13,22 +13,19 @@ import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
public class BallChannelInitializer extends ChannelInitializer<NioSocketChannel> {
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());
}
}

View File

@@ -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) {

View File

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

View File

@@ -28,6 +28,9 @@ public interface BallListener {
default void onConnectException(Throwable throwable) {
}
default void onConnectRefused() {
}
default void onServiceDead() {
}

View File

@@ -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<? extends Channel> getSocketChannel() {
switch (this) {
case LINUX:
return EpollSocketChannel.class;
case MACOS:
return KQueueSocketChannel.class;
default:
return NioSocketChannel.class;
}
}
@NotNull
public Class<? extends ServerChannel> getServerSocketChannel() {
switch (this) {
case LINUX:
return EpollServerSocketChannel.class;
case MACOS:
return KQueueServerSocketChannel.class;
default:
return NioServerSocketChannel.class;
}
}
}

View File

@@ -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'

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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<String> 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<String>) 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() {

View File

@@ -11,10 +11,9 @@ import org.slf4j.LoggerFactory;
@ChannelHandler.Sharable
public class BallServerChannelHandler extends SimpleChannelInboundHandler<String> {
public static final BallServerChannelHandler INSTANCE = new BallServerChannelHandler();
private static final Logger LOGGER = LoggerFactory.getLogger("BallServerChannelHandler");
private BallServerChannelHandler() {
public BallServerChannelHandler() {
super(true);
}

View File

@@ -20,12 +20,10 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
public class BallServerChannelInitializer extends ChannelInitializer<NioSocketChannel> {
public static final BallServerChannelInitializer INSTANCE = new BallServerChannelInitializer();
public static final List<Channel> 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<NioSocketCh
channel.pipeline()
.addLast(new IdleStateHandler(10, 0, 0, TimeUnit.SECONDS))
.addLast(BallServerKeepAliveHandler.INSTANCE)
.addLast(new BallServerKeepAliveHandler())
.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(BallServerChannelHandler.INSTANCE);
.addLast(new BallServerChannelHandler());
synchronized (CHANNELS) {
CHANNELS.add(channel);

View File

@@ -9,11 +9,9 @@ import org.slf4j.LoggerFactory;
@ChannelHandler.Sharable
public class BallServerKeepAliveHandler extends SimpleUserEventChannelHandler<IdleStateEvent> {
public static final BallServerKeepAliveHandler INSTANCE = new BallServerKeepAliveHandler();
private static final Logger LOGGER = LoggerFactory.getLogger("BallServerKeepAliveHandler");
private BallServerKeepAliveHandler() {
public BallServerKeepAliveHandler() {
super(true);
}

View File

@@ -5,10 +5,10 @@ port: 58888
# 线程池数量
# 建议设置为全服最大玩家数 / 20
# 不建议低于 5
nio-thread: 10
# 不建议低于 4
event-loop-thread: 5
# 是否启用IP 白名单
# 是否启用 IP 白名单
enable-accept-list: true
# 允许连接至服务的 ip 名单