perf: 重写部分代码
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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))
|
||||
|
Reference in New Issue
Block a user