Skip to content

Commit 3396e0c

Browse files
committed
try fix
1 parent 0a6acc8 commit 3396e0c

4 files changed

Lines changed: 26 additions & 35 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mod_name=Channel
3030
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
3131
mod_license=GNU GPL 3.0
3232
# The mod version. See https://semver.org/
33-
mod_version=26.1.2-9+alpha
33+
mod_version=26.1.2-10+alpha
3434
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
3535
# This should match the base package used for the mod sources.
3636
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html

src/main/java/cn/ussshenzhou/channel/subspace/client/SubspaceConnection.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
public class SubspaceConnection {
2727
private static Protocol protocol;
2828
private static SecurityLevel securityLevel;
29-
private static EventLoopGroup group;
29+
private static final EventLoopGroup EVENT_LOOP_GROUP = new MultiThreadIoEventLoopGroup(1, new DefaultThreadFactory("Channel-Client-Subspace", true), NioIoHandler.newFactory());
30+
;
3031
private static volatile Channel channel;
3132
private static volatile boolean activelyDisconnect;
3233
private static ScheduledFuture<?> reconnectFuture;
@@ -179,12 +180,8 @@ public static void connect(SubspaceInitPacket packet) {
179180
}
180181

181182
private static void connectTcp(SubspaceInitPacket packet) {
182-
if (group != null) {
183-
group.shutdownGracefully();
184-
}
185-
group = new MultiThreadIoEventLoopGroup(1, new DefaultThreadFactory("Channel-Client-Subspace", true), NioIoHandler.newFactory());
186183
new Bootstrap()
187-
.group(group)
184+
.group(EVENT_LOOP_GROUP)
188185
.channel(NioSocketChannel.class)
189186
.handler(new ChannelInitializer<SocketChannel>() {
190187
@Override
@@ -210,17 +207,19 @@ protected void initChannel(SocketChannel ch) {
210207
if (future.isSuccess()) {
211208
channel = future.channel();
212209
channel.closeFuture().addListener((ChannelFutureListener) f -> {
213-
if (!activelyDisconnect) {
210+
if (activelyDisconnect) {
211+
activelyDisconnect = false;
212+
} else {
214213
LogUtils.getLogger().warn("Disconnected from subspace. Reconnecting in 10s...");
215-
reconnectFuture = group.schedule(() -> connect(packet), 10, TimeUnit.SECONDS);
214+
reconnectFuture = EVENT_LOOP_GROUP.schedule(() -> connect(packet), 10, TimeUnit.SECONDS);
216215
channel = null;
217216
}
218217
});
219218
send(new HandshakePacket());
220219
MicrophoneHud.setStatus(MicrophoneHud.Status.STANDBY);
221220
} else {
222221
LogUtils.getLogger().error("Failed to connect to subspace server. Try again in 10s...");
223-
reconnectFuture = group.schedule(() -> connect(packet), 10, TimeUnit.SECONDS);
222+
reconnectFuture = EVENT_LOOP_GROUP.schedule(() -> connect(packet), 10, TimeUnit.SECONDS);
224223
channel = null;
225224
}
226225
});
@@ -240,14 +239,11 @@ public static void terminate() {
240239
channel.close();
241240
channel = null;
242241
}
243-
if (group != null) {
244-
group.shutdownGracefully();
245-
}
242+
EVENT_LOOP_GROUP.shutdownGracefully();
246243
protocol = null;
247244
securityLevel = null;
248245
reconnectFuture = null;
249-
activelyDisconnect = false;
250-
MicrophoneHud.setStatus(MicrophoneHud.Status.STANDBY);
246+
MicrophoneHud.resumeStatus();
251247
}
252248

253249
public static Protocol getProtocol() {
@@ -262,7 +258,7 @@ public static Channel getChannel() {
262258
return channel;
263259
}
264260

265-
public static boolean using(){
261+
public static boolean using() {
266262
return channel != null && channel.isActive();
267263
}
268264
}

src/main/java/cn/ussshenzhou/channel/subspace/server/ServerEventListener.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public static void leaveSubspace(ServerStoppingEvent event) {
3434

3535
@SubscribeEvent
3636
public static void playerLogIn(PlayerEvent.PlayerLoggedInEvent event) {
37-
if (SubspaceConnection.using()) {
38-
SubspaceConnection.newPlayer(event.getEntity());
39-
}
40-
37+
SubspaceConnection.newPlayer(event.getEntity());
4138
}
4239

4340
@SubscribeEvent

src/main/java/cn/ussshenzhou/channel/subspace/server/SubspaceConnection.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@
1818
import net.minecraft.network.Varint21FrameDecoder;
1919
import net.minecraft.network.Varint21LengthFieldPrepender;
2020
import net.minecraft.server.level.ServerPlayer;
21-
import net.minecraft.server.players.PlayerList;
2221
import net.minecraft.world.entity.player.Player;
2322
import net.neoforged.neoforge.server.ServerLifecycleHooks;
2423

2524
import java.security.SecureRandom;
26-
import java.util.concurrent.CompletableFuture;
2725
import java.util.concurrent.TimeUnit;
2826
import java.util.concurrent.locks.LockSupport;
2927

3028
/**
3129
* @author USS_Shenzhou
3230
*/
3331
public class SubspaceConnection {
34-
private static EventLoopGroup group;
32+
private static final EventLoopGroup EVENT_LOOP_GROUP = new MultiThreadIoEventLoopGroup(1, new DefaultThreadFactory("Channel-Server-Subspace", true), NioIoHandler.newFactory());
3533
private static volatile Channel channel;
3634
private static volatile boolean activelyDisconnect;
3735

@@ -52,9 +50,8 @@ public class SubspaceConnection {
5250
*/
5351
public static void connect() {
5452
var cfg = ChannelServerConfig.get();
55-
group = new MultiThreadIoEventLoopGroup(1, new DefaultThreadFactory("Channel-Server-Subspace", true), NioIoHandler.newFactory());
5653
new Bootstrap()
57-
.group(group)
54+
.group(EVENT_LOOP_GROUP)
5855
.channel(NioSocketChannel.class)
5956
.handler(new ChannelInitializer<SocketChannel>() {
6057
@Override
@@ -72,23 +69,25 @@ protected void initChannel(SocketChannel ch) {
7269
if (future.isSuccess()) {
7370
channel = future.channel();
7471
channel.closeFuture().addListener((ChannelFutureListener) f -> {
75-
if (!activelyDisconnect) {
72+
if (activelyDisconnect) {
73+
activelyDisconnect = false;
74+
} else {
7675
LogUtils.getLogger().warn("Disconnected from subspace. Reconnecting in 10s...");
77-
group.schedule(SubspaceConnection::connect, 10, TimeUnit.SECONDS);
76+
EVENT_LOOP_GROUP.schedule(SubspaceConnection::connect, 10, TimeUnit.SECONDS);
7877
channel = null;
7978
}
8079
});
8180
send(new InitPacket());
8281
LogUtils.getLogger().info("Subspace server connected.");
83-
group.schedule(() -> {
82+
EVENT_LOOP_GROUP.schedule(() -> {
8483
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(3000));
8584
if (channel.isActive() && ServerLifecycleHooks.getCurrentServer() != null) {
8685
ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayers().forEach(SubspaceConnection::newPlayer);
8786
}
8887
}, 1, TimeUnit.SECONDS);
8988
} else {
9089
LogUtils.getLogger().error("Failed to connect to subspace server. Try again in 10s...");
91-
group.schedule(SubspaceConnection::connect, 10, TimeUnit.SECONDS);
90+
EVENT_LOOP_GROUP.schedule(SubspaceConnection::connect, 10, TimeUnit.SECONDS);
9291
channel = null;
9392
}
9493
});
@@ -108,7 +107,6 @@ public static void changeSubspace() {
108107
if (channel != null) {
109108
channel.close();
110109
}
111-
activelyDisconnect = false;
112110
connect();
113111
}
114112

@@ -117,20 +115,20 @@ public static void shutdown() {
117115
if (channel != null) {
118116
channel.close();
119117
}
120-
if (group != null) {
121-
group.shutdownGracefully();
122-
}
123-
activelyDisconnect = false;
118+
EVENT_LOOP_GROUP.shutdownGracefully();
124119
}
125120

126121
public static void newPlayer(Player player) {
122+
if (!using()) {
123+
return;
124+
}
127125
byte[] token = new SecureRandom().generateSeed(32);
128126
SubspaceConnection.send(new PlayerLoginPacket(token, player.getUUID(), player.getId()));
129127
var cfg = ChannelServerConfig.get();
130128
NetworkHelper.sendToPlayer((ServerPlayer) player, new SubspaceInitPacket(token, cfg.subspaceProtocol, cfg.subspaceAddress, cfg.subspaceClientPort, cfg.subspaceSecurityLevel));
131129
}
132130

133-
public static boolean using(){
131+
public static boolean using() {
134132
return channel != null && channel.isActive();
135133
}
136134
}

0 commit comments

Comments
 (0)