perf: 重写部分代码

This commit is contained in:
2023-05-31 04:33:29 +08:00
parent 5851272688
commit e557202ea3
7 changed files with 50 additions and 21 deletions

View File

@@ -5,6 +5,7 @@ evaluationDependsOn(':hamster-ball-common')
dependencies {
apiShade(project(":hamster-ball-common")) { transitive = false }
//noinspection VulnerableLibrariesLocal
compileOnly 'net.md-5:bungeecord-api:1.19-R0.1-SNAPSHOT'
compileOnly "cn.hamster3.mc.plugin.core:bungeecord:${hamster_core_version}"

View File

@@ -51,14 +51,15 @@ public abstract class BallAPI {
@NotNull
private final BallConfig config;
@NotNull
private final List<BallListener> listeners;
private final Bootstrap bootstrap;
@NotNull
private final EventLoopGroup executors;
protected boolean enabled;
protected Channel channel;
@NotNull
private List<BallListener> listeners;
protected BallAPI(@NotNull BallConfig config) {
this.config = config;
@@ -286,6 +287,11 @@ public abstract class BallAPI {
if (channel != null) {
return;
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
reconnect(ttl - 1);
}
@@ -683,12 +689,16 @@ public abstract class BallAPI {
}
public void addListener(@NotNull BallListener listener) {
listeners.add(listener);
listeners.sort(Comparator.comparing(BallListener::getPriority));
ArrayList<BallListener> newListeners = new ArrayList<>(listeners);
newListeners.add(listener);
newListeners.sort(Comparator.comparing(BallListener::getPriority));
listeners = newListeners;
}
public void removeListener(@NotNull BallListener listener) {
listeners.remove(listener);
ArrayList<BallListener> newListeners = new ArrayList<>(listeners);
newListeners.remove(listener);
listeners = newListeners;
}
/**

View File

@@ -27,7 +27,11 @@ public class BallChannelHandler extends SimpleChannelInboundHandler<String> {
}
if ("connection refused".equals(message)) {
for (BallListener listener : BallAPI.getInstance().getListeners()) {
listener.onConnectRefused();
try {
listener.onConnectRefused();
} catch (Exception | Error e) {
e.printStackTrace();
}
}
return;
}

View File

@@ -1,7 +1,7 @@
package cn.hamster3.mc.plugin.ball.common.connector;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
@@ -12,14 +12,14 @@ import org.jetbrains.annotations.NotNull;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
public class BallChannelInitializer extends ChannelInitializer<NioSocketChannel> {
public class BallChannelInitializer extends ChannelInitializer<SocketChannel> {
public BallChannelInitializer() {
}
@Override
protected void initChannel(@NotNull NioSocketChannel channel) {
protected void initChannel(@NotNull SocketChannel channel) {
channel.pipeline()
.addLast(new IdleStateHandler(0, 7, 0, TimeUnit.SECONDS))
.addLast(new IdleStateHandler(0, 5, 0, TimeUnit.SECONDS))
.addLast(new BallKeepAliveHandler())
.addLast(new LengthFieldPrepender(8))
.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 8, 0, 8))

View File

@@ -31,8 +31,8 @@ public final class ServerConfig {
map = new Yaml().load(stream);
}
host = (String) map.get("host");
port = (int) map.get("port");
host = map.getOrDefault("host", "localhost").toString();
port = (int) map.getOrDefault("port", 58888);
eventLoopThread = (int) map.getOrDefault("event-loop-thread", 5);
enableAcceptList = (boolean) map.get("enable-accept-list");
acceptList = (List<String>) map.get("accept-list");

View File

@@ -20,7 +20,7 @@ public class BallServerChannelHandler extends SimpleChannelInboundHandler<String
@Override
protected void channelRead0(ChannelHandlerContext context, String message) {
if ("ping".equals(message)) {
context.writeAndFlush("pong");
context.channel().writeAndFlush("pong");
return;
}
try {

View File

@@ -4,7 +4,7 @@ import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
import cn.hamster3.mc.plugin.ball.server.config.ServerConfig;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
@@ -14,12 +14,13 @@ import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class BallServerChannelInitializer extends ChannelInitializer<NioSocketChannel> {
public class BallServerChannelInitializer extends ChannelInitializer<SocketChannel> {
public static final List<Channel> CHANNELS = new ArrayList<>();
private static final Logger LOGGER = LoggerFactory.getLogger("BallServerChannelInitializer");
@@ -36,13 +37,26 @@ public class BallServerChannelInitializer extends ChannelInitializer<NioSocketCh
}
@Override
protected void initChannel(@NotNull NioSocketChannel channel) {
LOGGER.info("远程地址 {} 请求建立连接.", channel.remoteAddress().toString());
protected void initChannel(@NotNull SocketChannel channel) {
InetSocketAddress remoteAddress = channel.remoteAddress();
LOGGER.info("远程地址 {} 请求建立连接.", remoteAddress.toString());
String hostAddress = channel.remoteAddress().getAddress().getHostAddress();
String hostAddress = remoteAddress.getAddress().getHostAddress();
if (ServerConfig.isEnableAcceptList() && !ServerConfig.getAcceptList().contains(hostAddress)) {
channel.disconnect();
LOGGER.warn("{} 不在白名单列表中, 已断开连接!", hostAddress);
LOGGER.warn("{} 不在白名单列表中, 断开连接!", hostAddress);
channel.pipeline()
.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));
try {
channel.writeAndFlush("connection refused").await();
channel.disconnect().await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return;
}