优化代码格式,移除多余空行

This commit is contained in:
Xujiayao 2026-01-20 22:28:02 +08:00
parent 8a5c003918
commit aaefabc7d9
6 changed files with 25 additions and 27 deletions

View file

@ -102,9 +102,9 @@ public class DMCC {
// Generate ephemeral credentials for internal loopback connection
String internalServerName = "Internal";
String internalSharedSecret = CryptUtils.generateRandomString(32);
// Server instance gets the secret to verify the client
serverInstance = new ServerDMCC("127.0.0.1", 0, internalSharedSecret);
serverInstance = new ServerDMCC("127.0.0.1", 0, internalSharedSecret);
int port = serverInstance.start();
if (port == -1) {
@ -122,7 +122,7 @@ public class DMCC {
int port = ConfigManager.getInt("multi_server.connection.port");
String name = ConfigManager.getString("multi_server.server_name");
String secret = ConfigManager.getString("multi_server.connection.shared_secret");
clientInstance = new ClientDMCC(host, port, name, secret);
if (!clientInstance.start()) {
return false;
@ -132,7 +132,7 @@ public class DMCC {
String host = ConfigManager.getString("multi_server.connection.host");
int port = ConfigManager.getInt("multi_server.connection.port");
String secret = ConfigManager.getString("multi_server.connection.shared_secret");
serverInstance = new ServerDMCC(host, port, secret);
if (serverInstance.start() == -1) {
return false;

View file

@ -38,11 +38,11 @@ public class ClientHandler extends SimpleChannelInboundHandler<Packet> {
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(new HandshakePacket(client.getServerName(), Constants.VERSION));
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
LOGGER.info(I18nManager.getDmccTranslation("network.client.disconnected_generic"));
// Trigger reconnection if this was not an intentional stop
if (client.isRunning()) {
LOGGER.info(I18nManager.getDmccTranslation("network.client.reconnecting"));
@ -59,7 +59,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<Packet> {
} else if (packet instanceof LoginSuccessPacket p) {
I18nManager.load(p.language);
LOGGER.info(I18nManager.getDmccTranslation("network.client.connected"));
if (!initialLoginFuture.isDone()) {
initialLoginFuture.complete(true);
}
@ -67,7 +67,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<Packet> {
} else if (packet instanceof DisconnectPacket p) {
String reason = I18nManager.getDmccTranslation(p.key, p.args);
LOGGER.error(I18nManager.getDmccTranslation("network.client.disconnected_reason", reason));
if (!initialLoginFuture.isDone()) {
initialLoginFuture.complete(false);
}

View file

@ -31,17 +31,15 @@ import static com.xujiayao.discord_mc_chat.Constants.LOGGER;
*/
public class NettyClient {
private static final int MAX_RECONNECT_DELAY = 512;
private final String host;
private final int port;
private final String serverName;
private final String sharedSecret;
private EventLoopGroup workerGroup;
private CompletableFuture<Boolean> initialLoginFuture;
// Reconnection settings
private final AtomicBoolean isRunning = new AtomicBoolean(false);
private final AtomicInteger reconnectDelay = new AtomicInteger(2); // Initial delay seconds
private static final int MAX_RECONNECT_DELAY = 512;
private EventLoopGroup workerGroup;
private CompletableFuture<Boolean> initialLoginFuture;
public NettyClient(String host, int port, String serverName, String sharedSecret) {
this.host = host;
@ -49,11 +47,11 @@ public class NettyClient {
this.serverName = serverName;
this.sharedSecret = sharedSecret;
}
public String getServerName() {
return serverName;
}
public String getSharedSecret() {
return sharedSecret;
}
@ -61,9 +59,9 @@ public class NettyClient {
public boolean start() {
isRunning.set(true);
initialLoginFuture = new CompletableFuture<>();
// Use MultiThreadIoEventLoopGroup with NioIoHandler
workerGroup = new MultiThreadIoEventLoopGroup(0,
workerGroup = new MultiThreadIoEventLoopGroup(0,
ExecutorServiceUtils.newThreadFactory("DMCC-NettyClient"),
NioIoHandler.newFactory());
@ -135,7 +133,7 @@ public class NettyClient {
workerGroup.shutdownGracefully();
}
}
public boolean isRunning() {
return isRunning.get();
}

View file

@ -15,7 +15,7 @@ import java.io.Serializable;
* @author Xujiayao
*/
public class JavaSerializerEncoder extends MessageToByteEncoder<Serializable> {
@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
// Use ObjectOutputStream to serialize the object into the ByteBuf

View file

@ -39,17 +39,17 @@ public class NettyServer {
this.port = port;
this.sharedSecret = sharedSecret;
}
public String getSharedSecret() {
return sharedSecret;
}
public int start() {
bossGroup = new MultiThreadIoEventLoopGroup(1,
bossGroup = new MultiThreadIoEventLoopGroup(1,
ExecutorServiceUtils.newThreadFactory("DMCC-NettyServer-Boss"),
NioIoHandler.newFactory());
workerGroup = new MultiThreadIoEventLoopGroup(0,
workerGroup = new MultiThreadIoEventLoopGroup(0,
ExecutorServiceUtils.newThreadFactory("DMCC-NettyServer-Worker"),
NioIoHandler.newFactory());
@ -75,7 +75,7 @@ public class NettyServer {
ChannelFuture channelFuture = b.bind(host, port).sync();
int boundPort = ((InetSocketAddress) channelFuture.channel().localAddress()).getPort();
LOGGER.info(I18nManager.getDmccTranslation("network.server.listening", boundPort));
return boundPort;

View file

@ -9,8 +9,8 @@ import com.xujiayao.discord_mc_chat.network.packets.HandshakePacket;
import com.xujiayao.discord_mc_chat.network.packets.KeepAlivePacket;
import com.xujiayao.discord_mc_chat.network.packets.LoginSuccessPacket;
import com.xujiayao.discord_mc_chat.network.packets.Packet;
import com.xujiayao.discord_mc_chat.utils.config.ConfigManager;
import com.xujiayao.discord_mc_chat.utils.CryptUtils;
import com.xujiayao.discord_mc_chat.utils.config.ConfigManager;
import com.xujiayao.discord_mc_chat.utils.config.ModeManager;
import com.xujiayao.discord_mc_chat.utils.i18n.I18nManager;
import io.netty.channel.ChannelHandlerContext;
@ -31,7 +31,7 @@ public class ServerHandler extends SimpleChannelInboundHandler<Packet> {
private String expectedNonce;
private boolean authenticated = false;
private String clientName;
public ServerHandler(NettyServer server) {
this.server = server;
}
@ -40,7 +40,7 @@ public class ServerHandler extends SimpleChannelInboundHandler<Packet> {
public void channelActive(ChannelHandlerContext ctx) {
// Wait for handshake
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
if (clientName != null) {