#3519: Queue configuration phase packets from API methods

This commit is contained in:
md_5 2023-09-23 10:29:09 +10:00
parent f486a251f3
commit 0509303fd3
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
3 changed files with 52 additions and 9 deletions

View File

@ -658,8 +658,8 @@ public enum Protocol
/*========================================================================*/ /*========================================================================*/
public static final int MAX_PACKET_ID = 0xFF; public static final int MAX_PACKET_ID = 0xFF;
/*========================================================================*/ /*========================================================================*/
final DirectionData TO_SERVER = new DirectionData( this, ProtocolConstants.Direction.TO_SERVER ); public 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_CLIENT = new DirectionData( this, ProtocolConstants.Direction.TO_CLIENT );
public static void main(String[] args) public static void main(String[] args)
{ {
@ -719,7 +719,7 @@ public enum Protocol
return new ProtocolMapping( protocol, id ); return new ProtocolMapping( protocol, id );
} }
static final class DirectionData public static final class DirectionData
{ {
private final TIntObjectMap<ProtocolData> protocols = new TIntObjectHashMap<>(); private final TIntObjectMap<ProtocolData> protocols = new TIntObjectHashMap<>();
@ -802,6 +802,17 @@ public enum Protocol
} }
} }
public boolean hasPacket(Class<? extends DefinedPacket> 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<? extends DefinedPacket> packet, int version) final int getId(Class<? extends DefinedPacket> packet, int version)
{ {

View File

@ -20,6 +20,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level; import java.util.logging.Level;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
@ -142,6 +143,7 @@ public final class UserConnection implements ProxiedPlayer
@Setter @Setter
private ForgeServerHandler forgeServerHandler; private ForgeServerHandler forgeServerHandler;
/*========================================================================*/ /*========================================================================*/
private final Queue<DefinedPacket> packetQueue = new ConcurrentLinkedQueue<>();
private final Unsafe unsafe = new Unsafe() private final Unsafe unsafe = new Unsafe()
{ {
@Override @Override
@ -177,6 +179,27 @@ public final class UserConnection implements ProxiedPlayer
ch.write( packet ); 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 @Deprecated
public boolean isActive() public boolean isActive()
{ {
@ -489,10 +512,10 @@ public final class UserConnection implements ProxiedPlayer
position = ChatMessageType.SYSTEM; position = ChatMessageType.SYSTEM;
} }
unsafe().sendPacket( new SystemChat( message, position.ordinal() ) ); sendPacketQueued( new SystemChat( message, position.ordinal() ) );
} else } 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(); 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.setAction( net.md_5.bungee.protocol.packet.Title.Action.ACTIONBAR );
title.setText( ComponentSerializer.toString( message ) ); title.setText( ComponentSerializer.toString( message ) );
unsafe.sendPacket( title ); sendPacketQueued( title );
} }
} else } else
{ {
@ -524,7 +547,7 @@ public final class UserConnection implements ProxiedPlayer
@Override @Override
public void sendData(String channel, byte[] data) public void sendData(String channel, byte[] data)
{ {
unsafe().sendPacket( new PluginMessage( channel, data, forgeClientHandler.isForgeUser() ) ); sendPacketQueued( new PluginMessage( channel, data, forgeClientHandler.isForgeUser() ) );
} }
@Override @Override
@ -700,7 +723,7 @@ public final class UserConnection implements ProxiedPlayer
header = ChatComponentTransformer.getInstance().transform( this, true, header )[0]; header = ChatComponentTransformer.getInstance().transform( this, true, header )[0];
footer = ChatComponentTransformer.getInstance().transform( this, true, footer )[0]; footer = ChatComponentTransformer.getInstance().transform( this, true, footer )[0];
unsafe().sendPacket( new PlayerListHeaderFooter( sendPacketQueued( new PlayerListHeaderFooter(
ComponentSerializer.toString( header ), ComponentSerializer.toString( header ),
ComponentSerializer.toString( footer ) ComponentSerializer.toString( footer )
) ); ) );
@ -712,7 +735,7 @@ public final class UserConnection implements ProxiedPlayer
header = ChatComponentTransformer.getInstance().transform( this, true, header ); header = ChatComponentTransformer.getInstance().transform( this, true, header );
footer = ChatComponentTransformer.getInstance().transform( this, true, footer ); footer = ChatComponentTransformer.getInstance().transform( this, true, footer );
unsafe().sendPacket( new PlayerListHeaderFooter( sendPacketQueued( new PlayerListHeaderFooter(
ComponentSerializer.toString( header ), ComponentSerializer.toString( header ),
ComponentSerializer.toString( footer ) ComponentSerializer.toString( footer )
) ); ) );

View File

@ -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.ClientChat;
import net.md_5.bungee.protocol.packet.ClientCommand; import net.md_5.bungee.protocol.packet.ClientCommand;
import net.md_5.bungee.protocol.packet.ClientSettings; 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.KeepAlive;
import net.md_5.bungee.protocol.packet.LoginAcknowledged; import net.md_5.bungee.protocol.packet.LoginAcknowledged;
import net.md_5.bungee.protocol.packet.PlayerListItem; 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 @Override
public String toString() public String toString()
{ {