From 657c4b402b6fdb35653063d7480e49a17cfd0ec2 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Sun, 30 Apr 2023 17:39:06 +0200 Subject: [PATCH] Fixed deadlocking issues with packet syncing --- .../client2proxy/Client2ProxyHandler.java | 2 +- .../proxy2server/Proxy2ServerHandler.java | 22 ++++++++++++++----- .../proxy/session/ProxyConnection.java | 4 ++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java index 672bc6e..d40d72b 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java @@ -262,7 +262,7 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler { this.proxyConnection.getChannel().writeAndFlush(haProxyMessage).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } - this.proxyConnection.getChannel().writeAndFlush(new C2SHandshakePacket(clientVersion.getOriginalVersion(), serverAddress.getAddress(), serverAddress.getPort(), packet.intendedState)).await().addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + this.proxyConnection.getChannel().writeAndFlush(new C2SHandshakePacket(clientVersion.getOriginalVersion(), serverAddress.getAddress(), serverAddress.getPort(), packet.intendedState)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE).await(); this.proxyConnection.setConnectionState(packet.intendedState); } diff --git a/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/Proxy2ServerHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/Proxy2ServerHandler.java index e0bb25a..9fd19fe 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/Proxy2ServerHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/Proxy2ServerHandler.java @@ -135,19 +135,31 @@ public class Proxy2ServerHandler extends SimpleChannelInboundHandler { } } - private void handleLoginSuccess(final S2CLoginSuccessPacket1_7 packet) throws Exception { + private void handleLoginSuccess(final S2CLoginSuccessPacket1_7 packet) { + if(true) throw new RuntimeException("test"); + if (this.proxyConnection.getClientVersion().isNewerThanOrEqualTo(VersionEnum.r1_8)) { if (Options.COMPRESSION_THRESHOLD > -1 && this.proxyConnection.getC2P().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).get() == -1) { - this.proxyConnection.getC2P().writeAndFlush(new S2CLoginCompressionPacket(Options.COMPRESSION_THRESHOLD)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE).await(); - this.proxyConnection.getC2P().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).set(Options.COMPRESSION_THRESHOLD); + this.proxyConnection.getC2P().writeAndFlush(new S2CLoginCompressionPacket(Options.COMPRESSION_THRESHOLD)).addListeners(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, (ChannelFutureListener) f -> { + if (f.isSuccess()) { + this.proxyConnection.getC2P().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).set(Options.COMPRESSION_THRESHOLD); + this.proxyConnection.getChannel().config().setAutoRead(true); + } + }); + this.proxyConnection.getChannel().config().setAutoRead(false); } } this.proxyConnection.setGameProfile(new GameProfile(packet.uuid, packet.name)); Logger.u_info("connect", this.proxyConnection.getC2P().remoteAddress(), this.proxyConnection.getGameProfile(), "Connected successfully! Switching to PLAY state"); - this.proxyConnection.getC2P().writeAndFlush(packet).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE).await(); - this.proxyConnection.setConnectionState(ConnectionState.PLAY); + this.proxyConnection.getC2P().writeAndFlush(packet).addListeners(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, (ChannelFutureListener) f -> { + if (f.isSuccess()) { + this.proxyConnection.setConnectionState(ConnectionState.PLAY); + this.proxyConnection.getChannel().config().setAutoRead(true); + } + }); + this.proxyConnection.getChannel().config().setAutoRead(false); } private void handleLoginCompression(final S2CLoginCompressionPacket packet) { diff --git a/src/main/java/net/raphimc/viaproxy/proxy/session/ProxyConnection.java b/src/main/java/net/raphimc/viaproxy/proxy/session/ProxyConnection.java index f7cce15..1572183 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/session/ProxyConnection.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/session/ProxyConnection.java @@ -237,7 +237,7 @@ public class ProxyConnection extends NetClient { return this.classicMpPass; } - public void kickClient(final String message) throws InterruptedException, CloseAndReturn { + public void kickClient(final String message) throws CloseAndReturn { Logger.u_err("kick", this.c2p.remoteAddress(), this.getGameProfile(), message.replaceAll("ยง.", "")); final ChannelFuture future; @@ -254,7 +254,7 @@ public class ProxyConnection extends NetClient { future = this.c2p.newSucceededFuture(); } - future.await().channel().close(); + future.channel().close(); throw CloseAndReturn.INSTANCE; }