From 0509303fd37c2b236c8a3b5f5cea85cada937ad2 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 23 Sep 2023 10:29:09 +1000 Subject: [PATCH] #3519: Queue configuration phase packets from API methods --- .../net/md_5/bungee/protocol/Protocol.java | 17 +++++++-- .../java/net/md_5/bungee/UserConnection.java | 35 +++++++++++++++---- .../bungee/connection/UpstreamBridge.java | 9 +++++ 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java b/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java index 285e591e..b6985fee 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java @@ -658,8 +658,8 @@ public enum Protocol /*========================================================================*/ public static final int MAX_PACKET_ID = 0xFF; /*========================================================================*/ - final DirectionData TO_SERVER = new DirectionData( this, ProtocolConstants.Direction.TO_SERVER ); - final DirectionData TO_CLIENT = new DirectionData( this, ProtocolConstants.Direction.TO_CLIENT ); + public final DirectionData TO_SERVER = new DirectionData( this, ProtocolConstants.Direction.TO_SERVER ); + public final DirectionData TO_CLIENT = new DirectionData( this, ProtocolConstants.Direction.TO_CLIENT ); public static void main(String[] args) { @@ -719,7 +719,7 @@ public enum Protocol return new ProtocolMapping( protocol, id ); } - static final class DirectionData + public static final class DirectionData { private final TIntObjectMap protocols = new TIntObjectHashMap<>(); @@ -802,6 +802,17 @@ public enum Protocol } } + public boolean hasPacket(Class packet, int version) + { + ProtocolData protocolData = getProtocolData( version ); + if ( protocolData == null ) + { + throw new BadPacketException( "Unsupported protocol version" ); + } + + return protocolData.packetMap.containsKey( packet ); + } + final int getId(Class packet, int version) { diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 9d2df335..c1c73aba 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -20,6 +20,7 @@ import java.util.Map; import java.util.Objects; import java.util.Queue; import java.util.UUID; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.logging.Level; import lombok.Getter; import lombok.NonNull; @@ -142,6 +143,7 @@ public final class UserConnection implements ProxiedPlayer @Setter private ForgeServerHandler forgeServerHandler; /*========================================================================*/ + private final Queue packetQueue = new ConcurrentLinkedQueue<>(); private final Unsafe unsafe = new Unsafe() { @Override @@ -177,6 +179,27 @@ public final class UserConnection implements ProxiedPlayer ch.write( packet ); } + public void sendPacketQueued(DefinedPacket packet) + { + Protocol encodeProtocol = ch.getEncodeProtocol(); + if ( encodeProtocol != Protocol.GAME && !encodeProtocol.TO_CLIENT.hasPacket( packet.getClass(), getPendingConnection().getVersion() ) ) + { + packetQueue.add( packet ); + } else + { + unsafe().sendPacket( packet ); + } + } + + public void sendQueuedPackets() + { + DefinedPacket packet; + while ( ( packet = packetQueue.poll() ) != null ) + { + unsafe().sendPacket( packet ); + } + } + @Deprecated public boolean isActive() { @@ -489,10 +512,10 @@ public final class UserConnection implements ProxiedPlayer position = ChatMessageType.SYSTEM; } - unsafe().sendPacket( new SystemChat( message, position.ordinal() ) ); + sendPacketQueued( new SystemChat( message, position.ordinal() ) ); } else { - unsafe().sendPacket( new Chat( message, (byte) position.ordinal(), sender ) ); + sendPacketQueued( new Chat( message, (byte) position.ordinal(), sender ) ); } } @@ -513,7 +536,7 @@ public final class UserConnection implements ProxiedPlayer net.md_5.bungee.protocol.packet.Title title = new net.md_5.bungee.protocol.packet.Title(); title.setAction( net.md_5.bungee.protocol.packet.Title.Action.ACTIONBAR ); title.setText( ComponentSerializer.toString( message ) ); - unsafe.sendPacket( title ); + sendPacketQueued( title ); } } else { @@ -524,7 +547,7 @@ public final class UserConnection implements ProxiedPlayer @Override public void sendData(String channel, byte[] data) { - unsafe().sendPacket( new PluginMessage( channel, data, forgeClientHandler.isForgeUser() ) ); + sendPacketQueued( new PluginMessage( channel, data, forgeClientHandler.isForgeUser() ) ); } @Override @@ -700,7 +723,7 @@ public final class UserConnection implements ProxiedPlayer header = ChatComponentTransformer.getInstance().transform( this, true, header )[0]; footer = ChatComponentTransformer.getInstance().transform( this, true, footer )[0]; - unsafe().sendPacket( new PlayerListHeaderFooter( + sendPacketQueued( new PlayerListHeaderFooter( ComponentSerializer.toString( header ), ComponentSerializer.toString( footer ) ) ); @@ -712,7 +735,7 @@ public final class UserConnection implements ProxiedPlayer header = ChatComponentTransformer.getInstance().transform( this, true, header ); footer = ChatComponentTransformer.getInstance().transform( this, true, footer ); - unsafe().sendPacket( new PlayerListHeaderFooter( + sendPacketQueued( new PlayerListHeaderFooter( ComponentSerializer.toString( header ), ComponentSerializer.toString( footer ) ) ); diff --git a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java index d288b6f2..48a523c6 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java @@ -31,6 +31,7 @@ import net.md_5.bungee.protocol.packet.Chat; import net.md_5.bungee.protocol.packet.ClientChat; import net.md_5.bungee.protocol.packet.ClientCommand; import net.md_5.bungee.protocol.packet.ClientSettings; +import net.md_5.bungee.protocol.packet.FinishConfiguration; import net.md_5.bungee.protocol.packet.KeepAlive; import net.md_5.bungee.protocol.packet.LoginAcknowledged; import net.md_5.bungee.protocol.packet.PlayerListItem; @@ -334,6 +335,14 @@ public class UpstreamBridge extends PacketHandler } } + @Override + public void handle(FinishConfiguration finishConfiguration) throws Exception + { + con.sendQueuedPackets(); + + super.handle( finishConfiguration ); + } + @Override public String toString() {