From 77b5a34a33aded89d869508410e73659ca2091cb Mon Sep 17 00:00:00 2001 From: Sen Date: Sun, 25 May 2025 17:53:55 +0200 Subject: [PATCH] downgrade netty to old version to fix annoying network bugs WHYYYYYYYYYYYYYYYYYYYYY?!?!?!?! --- client/src/client/Client.java | 39 ++-- client/src/client/gui/GuiInfo.java | 2 +- .../client/network/ClientLoginHandler.java | 1 + .../src/client/util}/Message.java | 2 +- common/src/common/init/Registry.java | 3 - common/src/common/log/NettyLogger.java | 170 ------------------ common/src/common/network/ILoginHandler.java | 2 - common/src/common/network/NetConnection.java | 15 +- common/src/common/network/PacketDecoder.java | 1 - common/src/common/network/PacketEncoder.java | 1 - common/src/common/network/PacketRegistry.java | 2 - common/src/common/packet/LPacketLogin.java | 23 --- server/src/server/Server.java | 1 + .../src/server/network/HandshakeHandler.java | 4 +- server/src/server/network/LoginHandler.java | 5 +- 15 files changed, 51 insertions(+), 220 deletions(-) rename {common/src/common/log => client/src/client/util}/Message.java (89%) delete mode 100644 common/src/common/log/NettyLogger.java delete mode 100644 common/src/common/packet/LPacketLogin.java diff --git a/client/src/client/Client.java b/client/src/client/Client.java index 1e6c919..f6c01b8 100755 --- a/client/src/client/Client.java +++ b/client/src/client/Client.java @@ -71,6 +71,7 @@ import client.renderer.texture.EntityTexManager; import client.renderer.texture.TextureManager; import client.renderer.texture.TextureMap; import client.util.FileUtils; +import client.util.Message; import client.util.PerfSection; import client.util.PlayerController; import client.vars.BoolVar; @@ -129,19 +130,18 @@ import common.item.ItemControl; import common.item.ItemStack; import common.log.Log; import common.log.LogLevel; -import common.log.Message; import common.network.IThreadListener; import common.network.NetConnection; import common.network.PacketDecoder; import common.network.PacketEncoder; import common.network.PacketPrepender; +import common.network.PacketRegistry; import common.network.PacketSplitter; import common.network.NetHandler.ThreadQuickExitException; import common.packet.CPacketAction; import common.packet.CPacketCheat; import common.packet.CPacketMessage; import common.packet.HPacketHandshake; -import common.packet.LPacketLogin; import common.packet.CPacketAction.Action; import common.potion.Potion; import common.potion.PotionEffect; @@ -171,6 +171,8 @@ import io.netty.channel.ChannelOption; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.timeout.ReadTimeoutHandler; +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.GenericFutureListener; /* Een net ganz funktionierndes Programm ... @@ -513,13 +515,16 @@ public class Client implements IThreadListener { public void connect(String address, int port, String user, String pass, String access) { this.displayGuiScreen(GuiLoading.makeWaitTask("Verbinde zu " + (address == null ? "localhost" : address) + ":" + port + " ...")); Log.JNI.info("Verbinde zu " + (address == null ? "localhost" : address) + ":" + port); - NetConnection connection = null; + final NetConnection connection; try { connection = createNetworkManagerAndConnect(address == null ? InetAddress.getLoopbackAddress() : InetAddress.getByName(IDN.toASCII(address)), port); connection.setNetHandler(new ClientLoginHandler(connection, this, user, access, pass)); - connection.sendPacket(new HPacketHandshake(Config.PROTOCOL)); - connection.sendPacket(new LPacketLogin()); + connection.sendPacket(new HPacketHandshake(Config.PROTOCOL), new GenericFutureListener>() { + public void operationComplete(Future u) throws Exception { + connection.setConnectionState(PacketRegistry.LOGIN); + } + }); } catch (UnknownHostException u) { @@ -793,8 +798,10 @@ public class Client implements IThreadListener { } else if (this.connection != null) { - this.connection.processReceivedPackets(); - this.connection.checkDisconnected(); + if(this.connection.isChannelOpen()) + this.connection.processReceivedPackets(); + else + this.connection.checkDisconnected(); } } @@ -2649,9 +2656,21 @@ public class Client implements IThreadListener { }); this.registerDebug(Keysym.AE, "Programm sofort beenden und server beenden", new DebugRunner() { public void execute(Keysym key) { - if(Client.this.player != null) - Client.this.player.client.addToSendQueue(new CPacketMessage(CPacketMessage.Type.COMMAND, "shutdown")); - Client.this.interrupted = true; + if(Client.this.getNetHandler() != null) { + Client.this.getNetHandler().getNetworkManager().sendPacket(new CPacketMessage(CPacketMessage.Type.COMMAND, "shutdown"), new GenericFutureListener>() { + public void operationComplete(Future u) throws Exception { + try { + Thread.sleep(1000L); + } + catch(InterruptedException e) { + } + Client.this.interrupted = true; + } + }); + } + else { + Client.this.interrupted = true; + } } }); } diff --git a/client/src/client/gui/GuiInfo.java b/client/src/client/gui/GuiInfo.java index 5653efb..8c52e6c 100644 --- a/client/src/client/gui/GuiInfo.java +++ b/client/src/client/gui/GuiInfo.java @@ -18,7 +18,7 @@ public class GuiInfo extends Gui { private static final String[] LIBRARIES = { "LWJGL 3.3.6+1 (GLFW + OpenGL)", - "Netty 4.1.119-Final" + "Netty 4.0.23-Final" }; private static final String[] CODE = { "Albert Pham - WorldEdit (Snippets)", diff --git a/client/src/client/network/ClientLoginHandler.java b/client/src/client/network/ClientLoginHandler.java index cfda5c6..c4d9a18 100755 --- a/client/src/client/network/ClientLoginHandler.java +++ b/client/src/client/network/ClientLoginHandler.java @@ -45,6 +45,7 @@ public class ClientLoginHandler extends NetHandler implements IClientLoginHandle } public void handleEncrypt(RPacketRequestEncrypt packet) { + this.networkManager.setConnectionState(PacketRegistry.LOGIN); final SecretKey secret = EncryptUtil.createNewSharedKey(); PublicKey pubkey = packet.getKey(); this.networkManager.sendPacket(new LPacketStartEncrypt(secret, pubkey, packet.getToken()), new GenericFutureListener < Future > () { diff --git a/common/src/common/log/Message.java b/client/src/client/util/Message.java similarity index 89% rename from common/src/common/log/Message.java rename to client/src/client/util/Message.java index 939962d..441a7b0 100644 --- a/common/src/common/log/Message.java +++ b/client/src/client/util/Message.java @@ -1,4 +1,4 @@ -package common.log; +package client.util; public class Message { public final String message; diff --git a/common/src/common/init/Registry.java b/common/src/common/init/Registry.java index 4880af1..c70b9ce 100755 --- a/common/src/common/init/Registry.java +++ b/common/src/common/init/Registry.java @@ -14,8 +14,6 @@ import java.util.Locale; import javax.imageio.ImageIO; -import common.log.NettyLogger; - public abstract class Registry { private static boolean crashed; @@ -116,7 +114,6 @@ public abstract class Registry { timer.setDaemon(true); timer.start(); System.setProperty("java.net.preferIPv4Stack", "true"); - NettyLogger.init(); register(); } diff --git a/common/src/common/log/NettyLogger.java b/common/src/common/log/NettyLogger.java deleted file mode 100644 index 8371653..0000000 --- a/common/src/common/log/NettyLogger.java +++ /dev/null @@ -1,170 +0,0 @@ -package common.log; - -import java.io.PrintWriter; -import java.io.StringWriter; - -import io.netty.util.internal.logging.AbstractInternalLogger; -import io.netty.util.internal.logging.FormattingTuple; -import io.netty.util.internal.logging.InternalLogger; -import io.netty.util.internal.logging.InternalLoggerFactory; -import io.netty.util.internal.logging.MessageFormatter; - -public class NettyLogger extends AbstractInternalLogger { - public static void init() { - InternalLoggerFactory.setDefaultFactory(new InternalLoggerFactory() { - protected InternalLogger newInstance(String name) { - return new NettyLogger(name); - } - }); - } - - private NettyLogger(String name) { - super(name); - } - - public boolean isTraceEnabled() { - return Log.getLevel().ordinal() >= LogLevel.TRACE.ordinal(); - } - - public void trace(String msg) { - this.log(LogLevel.TRACE, msg, null); - } - - public void trace(String format, Object arg) { - FormattingTuple ft = MessageFormatter.format(format, arg); - this.log(LogLevel.TRACE, ft.getMessage(), ft.getThrowable()); - } - - public void trace(String format, Object argA, Object argB) { - FormattingTuple ft = MessageFormatter.format(format, argA, argB); - this.log(LogLevel.TRACE, ft.getMessage(), ft.getThrowable()); - } - - public void trace(String format, Object... argArray) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); - this.log(LogLevel.TRACE, ft.getMessage(), ft.getThrowable()); - } - - public void trace(String msg, Throwable t) { - this.log(LogLevel.TRACE, msg, t); - } - - public boolean isDebugEnabled() { - return Log.getLevel().ordinal() >= LogLevel.DEBUG.ordinal(); - } - - public void debug(String msg) { - this.log(LogLevel.DEBUG, msg, null); - } - - public void debug(String format, Object arg) { - FormattingTuple ft = MessageFormatter.format(format, arg); - this.log(LogLevel.DEBUG, ft.getMessage(), ft.getThrowable()); - } - - public void debug(String format, Object argA, Object argB) { - FormattingTuple ft = MessageFormatter.format(format, argA, argB); - this.log(LogLevel.DEBUG, ft.getMessage(), ft.getThrowable()); - } - - public void debug(String format, Object... argArray) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); - this.log(LogLevel.DEBUG, ft.getMessage(), ft.getThrowable()); - } - - public void debug(String msg, Throwable t) { - this.log(LogLevel.DEBUG, msg, t); - } - - public boolean isInfoEnabled() { - return Log.getLevel().ordinal() >= LogLevel.INFO.ordinal(); - } - - public void info(String msg) { - this.log(LogLevel.INFO, msg, null); - } - - public void info(String format, Object arg) { - FormattingTuple ft = MessageFormatter.format(format, arg); - this.log(LogLevel.INFO, ft.getMessage(), ft.getThrowable()); - } - - public void info(String format, Object argA, Object argB) { - FormattingTuple ft = MessageFormatter.format(format, argA, argB); - this.log(LogLevel.INFO, ft.getMessage(), ft.getThrowable()); - } - - public void info(String format, Object... argArray) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); - this.log(LogLevel.INFO, ft.getMessage(), ft.getThrowable()); - } - - public void info(String msg, Throwable t) { - this.log(LogLevel.INFO, msg, t); - } - - public boolean isWarnEnabled() { - return Log.getLevel().ordinal() >= LogLevel.WARN.ordinal(); - } - - public void warn(String msg) { - this.log(LogLevel.WARN, msg, null); - } - - public void warn(String format, Object arg) { - FormattingTuple ft = MessageFormatter.format(format, arg); - this.log(LogLevel.WARN, ft.getMessage(), ft.getThrowable()); - } - - public void warn(String format, Object argA, Object argB) { - FormattingTuple ft = MessageFormatter.format(format, argA, argB); - this.log(LogLevel.WARN, ft.getMessage(), ft.getThrowable()); - } - - public void warn(String format, Object... argArray) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); - this.log(LogLevel.WARN, ft.getMessage(), ft.getThrowable()); - } - - public void warn(String msg, Throwable t) { - this.log(LogLevel.WARN, msg, t); - } - - public boolean isErrorEnabled() { - return Log.getLevel().ordinal() >= LogLevel.ERROR.ordinal(); - } - - public void error(String msg) { - this.log(LogLevel.ERROR, msg, null); - } - - public void error(String format, Object arg) { - FormattingTuple ft = MessageFormatter.format(format, arg); - this.log(LogLevel.ERROR, ft.getMessage(), ft.getThrowable()); - } - - public void error(String format, Object argA, Object argB) { - FormattingTuple ft = MessageFormatter.format(format, argA, argB); - this.log(LogLevel.ERROR, ft.getMessage(), ft.getThrowable()); - } - - public void error(String format, Object... arguments) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments); - this.log(LogLevel.ERROR, ft.getMessage(), ft.getThrowable()); - } - - public void error(String msg, Throwable t) { - this.log(LogLevel.ERROR, msg, t); - } - - private void log(LogLevel level, String msg, Throwable t) { - Log.NETTY.log(level, msg); - if(t != null) { - StringWriter sw = new StringWriter(); - PrintWriter st = new PrintWriter(sw); - t.printStackTrace(st); - sw.flush(); - Log.NETTY.log(level, sw.toString().trim().replace('\t', ' ')); - } - } -} diff --git a/common/src/common/network/ILoginHandler.java b/common/src/common/network/ILoginHandler.java index b33bca6..7cc7ffb 100644 --- a/common/src/common/network/ILoginHandler.java +++ b/common/src/common/network/ILoginHandler.java @@ -1,11 +1,9 @@ package common.network; -import common.packet.LPacketLogin; import common.packet.LPacketPasswordResponse; import common.packet.LPacketStartEncrypt; public interface ILoginHandler { void processPasswordResponse(LPacketPasswordResponse packet); void processEncryption(LPacketStartEncrypt packet); - void processLogin(LPacketLogin packet); } diff --git a/common/src/common/network/NetConnection.java b/common/src/common/network/NetConnection.java index c430fdb..a18827f 100755 --- a/common/src/common/network/NetConnection.java +++ b/common/src/common/network/NetConnection.java @@ -24,6 +24,7 @@ import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; public class NetConnection extends SimpleChannelInboundHandler { + private static final boolean DEBUG = System.getProperty("network.debug") != null; private static final Pattern IP_REPLACER = Pattern.compile("([0-9]*)\\.([0-9]*)\\.[0-9]*\\.[0-9]*"); public static final AttributeKey ATTR_STATE = AttributeKey.valueOf("protocol"); @@ -52,7 +53,8 @@ public class NetConnection extends SimpleChannelInboundHandler { public void setConnectionState(PacketRegistry newState) { this.channel.attr(ATTR_STATE).set(newState); this.channel.config().setAutoRead(true); -// Log.debug("Automatisches Lesen eingeschaltet"); + if(DEBUG) + Log.SYSTEM.info("Automatisches Lesen eingeschaltet"); } public void channelInactive(ChannelHandlerContext context) throws Exception { @@ -75,6 +77,8 @@ public class NetConnection extends SimpleChannelInboundHandler { } protected void channelRead0(ChannelHandlerContext context, Packet packet) throws Exception { + if(DEBUG) + Log.SYSTEM.info("EIN: [" + context.channel().attr(NetConnection.ATTR_STATE).get() + "] " + packet.getClass().getName()); if(this.channel.isOpen()) { try { packet.processPacket(this.handler); @@ -132,7 +136,8 @@ public class NetConnection extends SimpleChannelInboundHandler { final PacketRegistry current = this.channel.attr(ATTR_STATE).get(); if(current != state) { -// Log.debug("Automatisches Lesen ausgeschaltet"); + if(DEBUG) + Log.SYSTEM.info("Automatisches Lesen ausgeschaltet"); this.channel.config().setAutoRead(false); } @@ -141,6 +146,8 @@ public class NetConnection extends SimpleChannelInboundHandler { this.setConnectionState(state); } + if(DEBUG) + Log.SYSTEM.info("AUS: [" + this.channel.attr(NetConnection.ATTR_STATE).get() + "] " + packet.getClass().getName()); ChannelFuture future = this.channel.writeAndFlush(packet); if(listeners != null) { @@ -156,6 +163,8 @@ public class NetConnection extends SimpleChannelInboundHandler { NetConnection.this.setConnectionState(state); } + if(DEBUG) + Log.SYSTEM.info("AUS: [" + NetConnection.this.channel.attr(NetConnection.ATTR_STATE).get() + "] " + packet.getClass().getName()); ChannelFuture future = NetConnection.this.channel.writeAndFlush(packet); if(listeners != null) { @@ -190,6 +199,8 @@ public class NetConnection extends SimpleChannelInboundHandler { this.handler.update(); if(this.channel != null) this.channel.flush(); + if(DEBUG) + Log.SYSTEM.info("Pakete synchronisiert"); } public String getCutAddress() { diff --git a/common/src/common/network/PacketDecoder.java b/common/src/common/network/PacketDecoder.java index 9d5f1c3..e7e25bd 100755 --- a/common/src/common/network/PacketDecoder.java +++ b/common/src/common/network/PacketDecoder.java @@ -27,6 +27,5 @@ public class PacketDecoder extends ByteToMessageDecoder { throw new IOException("Paket " + ((PacketRegistry)context.channel().attr(NetConnection.ATTR_STATE).get()).ordinal() + "/" + id + " (" + packet.getClass() + ") war größer als erwartet, " + pbuf.readableBytes() + " weitere Bytes wurden beim Lesen von Paket " + id + " gefunden"); output.add(packet); -// common.log.Log.SYSTEM.info("EIN: [" + context.channel().attr(NetConnection.ATTR_STATE).get() + ":" + id + "] " + packet.getClass().getName()); } } diff --git a/common/src/common/network/PacketEncoder.java b/common/src/common/network/PacketEncoder.java index 26f1190..5a0a628 100755 --- a/common/src/common/network/PacketEncoder.java +++ b/common/src/common/network/PacketEncoder.java @@ -17,7 +17,6 @@ public class PacketEncoder extends MessageToByteEncoder { Integer id = context.channel().attr(NetConnection.ATTR_STATE).get().getId(this.client, packet); if(id == null) throw new IOException("Kann nicht registriertes Paket nicht serialisieren"); -// common.log.Log.SYSTEM.debug("AUS: [" + context.channel().attr(NetConnection.ATTR_STATE).get() + ":" + id + "] " + packet.getClass().getName()); PacketBuffer pbuf = new PacketBuffer(output); pbuf.writeVarInt(id); try { diff --git a/common/src/common/network/PacketRegistry.java b/common/src/common/network/PacketRegistry.java index e896dbb..6278291 100755 --- a/common/src/common/network/PacketRegistry.java +++ b/common/src/common/network/PacketRegistry.java @@ -22,7 +22,6 @@ import common.packet.CPacketPlayer; import common.packet.CPacketSign; import common.packet.CPacketSkin; import common.packet.HPacketHandshake; -import common.packet.LPacketLogin; import common.packet.LPacketPasswordResponse; import common.packet.LPacketStartEncrypt; import common.packet.RPacketDisconnect; @@ -105,7 +104,6 @@ public enum PacketRegistry { this.server(RPacketLoginSuccess.class); this.server(RPacketEnableCompression.class); - this.client(LPacketLogin.class); this.client(LPacketStartEncrypt.class); this.client(LPacketPasswordResponse.class); }}, diff --git a/common/src/common/packet/LPacketLogin.java b/common/src/common/packet/LPacketLogin.java deleted file mode 100644 index 50c647d..0000000 --- a/common/src/common/packet/LPacketLogin.java +++ /dev/null @@ -1,23 +0,0 @@ -package common.packet; - -import java.io.IOException; -import common.network.ILoginHandler; -import common.network.Packet; -import common.network.PacketBuffer; - -public class LPacketLogin implements Packet { - public LPacketLogin() { - } - - public void readPacketData(PacketBuffer buf) throws IOException { - buf.readString(16); - } - - public void writePacketData(PacketBuffer buf) throws IOException { - buf.writeString("TESTtestPlceHldr"); - } - - public void processPacket(ILoginHandler handler) { - handler.processLogin(this); - } -} diff --git a/server/src/server/Server.java b/server/src/server/Server.java index 96774ac..2928f08 100755 --- a/server/src/server/Server.java +++ b/server/src/server/Server.java @@ -663,6 +663,7 @@ public final class Server implements IThreadListener { this.saveAllWorlds(false); this.saveTimer = 0; } + Log.flushLog(); this.tickTimes[this.perfTimer++] = System.currentTimeMillis() - now; if(this.perfTimer == 100) { this.perfTimer = 0; diff --git a/server/src/server/network/HandshakeHandler.java b/server/src/server/network/HandshakeHandler.java index a3fc59e..718e66f 100755 --- a/server/src/server/network/HandshakeHandler.java +++ b/server/src/server/network/HandshakeHandler.java @@ -43,7 +43,9 @@ public class HandshakeHandler extends NetHandler implements IHandshakeHandler } else { - this.networkManager.setNetHandler(new LoginHandler(this.server, this.networkManager)); + LoginHandler handler = new LoginHandler(this.server, this.networkManager); + this.networkManager.setNetHandler(handler); + handler.sendLoginPacket(); } } } diff --git a/server/src/server/network/LoginHandler.java b/server/src/server/network/LoginHandler.java index d71d102..dad02f7 100755 --- a/server/src/server/network/LoginHandler.java +++ b/server/src/server/network/LoginHandler.java @@ -13,7 +13,6 @@ import common.network.ILoginHandler; import common.network.IPlayer; import common.network.NetConnection; import common.network.NetHandler; -import common.packet.LPacketLogin; import common.packet.LPacketPasswordResponse; import common.packet.LPacketStartEncrypt; import common.packet.RPacketDisconnect; @@ -105,9 +104,9 @@ public class LoginHandler extends NetHandler implements ILoginHandler this.state = LoginState.ACCEPTED; } - public void processLogin(LPacketLogin packet) { + public void sendLoginPacket() { if(this.state != LoginState.INIT) - throw new IllegalStateException("Unerwartetes Login-Paket"); + throw new IllegalStateException("Unerwartetes Handshake-Paket"); this.state = LoginState.ENCRYPT; this.loginToken = new byte[4]; TOKEN_RNG.nextBytes(this.loginToken);