perf: 重写部分代码
This commit is contained in:
@@ -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'
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -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() {
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -5,10 +5,10 @@ port: 58888
|
||||
|
||||
# 线程池数量
|
||||
# 建议设置为全服最大玩家数 / 20
|
||||
# 不建议低于 5
|
||||
nio-thread: 10
|
||||
# 不建议低于 4
|
||||
event-loop-thread: 5
|
||||
|
||||
# 是否启用IP 白名单
|
||||
# 是否启用 IP 白名单
|
||||
enable-accept-list: true
|
||||
|
||||
# 允许连接至服务的 ip 名单
|
||||
|
Reference in New Issue
Block a user