perf: 重写部分代码
This commit is contained in:
@@ -5,6 +5,7 @@ evaluationDependsOn(':hamster-ball-common')
|
|||||||
dependencies {
|
dependencies {
|
||||||
apiShade(project(":hamster-ball-common")) { transitive = false }
|
apiShade(project(":hamster-ball-common")) { transitive = false }
|
||||||
|
|
||||||
|
//noinspection VulnerableLibrariesLocal
|
||||||
compileOnly 'net.md-5:bungeecord-api:1.19-R0.1-SNAPSHOT'
|
compileOnly 'net.md-5:bungeecord-api:1.19-R0.1-SNAPSHOT'
|
||||||
|
|
||||||
compileOnly "cn.hamster3.mc.plugin.core:bungeecord:${hamster_core_version}"
|
compileOnly "cn.hamster3.mc.plugin.core:bungeecord:${hamster_core_version}"
|
||||||
|
@@ -51,14 +51,15 @@ public abstract class BallAPI {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final BallConfig config;
|
private final BallConfig config;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final List<BallListener> listeners;
|
|
||||||
|
|
||||||
private final Bootstrap bootstrap;
|
private final Bootstrap bootstrap;
|
||||||
|
@NotNull
|
||||||
private final EventLoopGroup executors;
|
private final EventLoopGroup executors;
|
||||||
|
|
||||||
protected boolean enabled;
|
protected boolean enabled;
|
||||||
protected Channel channel;
|
protected Channel channel;
|
||||||
|
@NotNull
|
||||||
|
private List<BallListener> listeners;
|
||||||
|
|
||||||
protected BallAPI(@NotNull BallConfig config) {
|
protected BallAPI(@NotNull BallConfig config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
@@ -286,6 +287,11 @@ public abstract class BallAPI {
|
|||||||
if (channel != null) {
|
if (channel != null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(3000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
reconnect(ttl - 1);
|
reconnect(ttl - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -683,12 +689,16 @@ public abstract class BallAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addListener(@NotNull BallListener listener) {
|
public void addListener(@NotNull BallListener listener) {
|
||||||
listeners.add(listener);
|
ArrayList<BallListener> newListeners = new ArrayList<>(listeners);
|
||||||
listeners.sort(Comparator.comparing(BallListener::getPriority));
|
newListeners.add(listener);
|
||||||
|
newListeners.sort(Comparator.comparing(BallListener::getPriority));
|
||||||
|
listeners = newListeners;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeListener(@NotNull BallListener listener) {
|
public void removeListener(@NotNull BallListener listener) {
|
||||||
listeners.remove(listener);
|
ArrayList<BallListener> newListeners = new ArrayList<>(listeners);
|
||||||
|
newListeners.remove(listener);
|
||||||
|
listeners = newListeners;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -27,7 +27,11 @@ public class BallChannelHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
}
|
}
|
||||||
if ("connection refused".equals(message)) {
|
if ("connection refused".equals(message)) {
|
||||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||||
listener.onConnectRefused();
|
try {
|
||||||
|
listener.onConnectRefused();
|
||||||
|
} catch (Exception | Error e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package cn.hamster3.mc.plugin.ball.common.connector;
|
package cn.hamster3.mc.plugin.ball.common.connector;
|
||||||
|
|
||||||
import io.netty.channel.ChannelInitializer;
|
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.LengthFieldBasedFrameDecoder;
|
||||||
import io.netty.handler.codec.LengthFieldPrepender;
|
import io.netty.handler.codec.LengthFieldPrepender;
|
||||||
import io.netty.handler.codec.string.StringDecoder;
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
@@ -12,14 +12,14 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class BallChannelInitializer extends ChannelInitializer<NioSocketChannel> {
|
public class BallChannelInitializer extends ChannelInitializer<SocketChannel> {
|
||||||
public BallChannelInitializer() {
|
public BallChannelInitializer() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(@NotNull NioSocketChannel channel) {
|
protected void initChannel(@NotNull SocketChannel channel) {
|
||||||
channel.pipeline()
|
channel.pipeline()
|
||||||
.addLast(new IdleStateHandler(0, 7, 0, TimeUnit.SECONDS))
|
.addLast(new IdleStateHandler(0, 5, 0, TimeUnit.SECONDS))
|
||||||
.addLast(new BallKeepAliveHandler())
|
.addLast(new BallKeepAliveHandler())
|
||||||
.addLast(new LengthFieldPrepender(8))
|
.addLast(new LengthFieldPrepender(8))
|
||||||
.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 8, 0, 8))
|
.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 8, 0, 8))
|
||||||
|
@@ -31,8 +31,8 @@ public final class ServerConfig {
|
|||||||
map = new Yaml().load(stream);
|
map = new Yaml().load(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
host = (String) map.get("host");
|
host = map.getOrDefault("host", "localhost").toString();
|
||||||
port = (int) map.get("port");
|
port = (int) map.getOrDefault("port", 58888);
|
||||||
eventLoopThread = (int) map.getOrDefault("event-loop-thread", 5);
|
eventLoopThread = (int) map.getOrDefault("event-loop-thread", 5);
|
||||||
enableAcceptList = (boolean) map.get("enable-accept-list");
|
enableAcceptList = (boolean) map.get("enable-accept-list");
|
||||||
acceptList = (List<String>) map.get("accept-list");
|
acceptList = (List<String>) map.get("accept-list");
|
||||||
|
@@ -20,7 +20,7 @@ public class BallServerChannelHandler extends SimpleChannelInboundHandler<String
|
|||||||
@Override
|
@Override
|
||||||
protected void channelRead0(ChannelHandlerContext context, String message) {
|
protected void channelRead0(ChannelHandlerContext context, String message) {
|
||||||
if ("ping".equals(message)) {
|
if ("ping".equals(message)) {
|
||||||
context.writeAndFlush("pong");
|
context.channel().writeAndFlush("pong");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@@ -4,7 +4,7 @@ import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
|
|||||||
import cn.hamster3.mc.plugin.ball.server.config.ServerConfig;
|
import cn.hamster3.mc.plugin.ball.server.config.ServerConfig;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelInitializer;
|
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.LengthFieldBasedFrameDecoder;
|
||||||
import io.netty.handler.codec.LengthFieldPrepender;
|
import io.netty.handler.codec.LengthFieldPrepender;
|
||||||
import io.netty.handler.codec.string.StringDecoder;
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
@@ -14,12 +14,13 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
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<>();
|
public static final List<Channel> CHANNELS = new ArrayList<>();
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger("BallServerChannelInitializer");
|
private static final Logger LOGGER = LoggerFactory.getLogger("BallServerChannelInitializer");
|
||||||
|
|
||||||
@@ -36,13 +37,26 @@ public class BallServerChannelInitializer extends ChannelInitializer<NioSocketCh
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(@NotNull NioSocketChannel channel) {
|
protected void initChannel(@NotNull SocketChannel channel) {
|
||||||
LOGGER.info("远程地址 {} 请求建立连接.", channel.remoteAddress().toString());
|
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)) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user