Skip to content

Expose sendPacketQueued to unsafe interface #3797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions api/src/main/java/net/md_5/bungee/api/connection/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,18 @@ interface Unsafe
* @param packet the packet to send
*/
void sendPacket(DefinedPacket packet);

/**
* Queue a packet to this connection.
* If the packet is not registered for the connections current encoder protocol, it will be queued until it is,
* otherwise it will be sent immediately.
*
* @param packet the packet to queued
* @throws UnsupportedOperationException if used for a PendingConnection
*/
default void sendPacketQueued(DefinedPacket packet)
{
throw new UnsupportedOperationException( "Not supported." );
}
}
}
15 changes: 15 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/ServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.packet.PluginMessage;

@RequiredArgsConstructor
Expand All @@ -40,6 +41,18 @@ public void sendPacket(DefinedPacket packet)
{
ch.write( packet );
}

@Override
public void sendPacketQueued(DefinedPacket packet)
{
if ( ch.getEncodeVersion() >= ProtocolConstants.MINECRAFT_1_20_2 )
{
ServerConnection.this.sendPacketQueued( packet );
} else
{
sendPacket( packet );
}
}
};

public void sendPacketQueued(DefinedPacket packet)
Expand All @@ -53,6 +66,8 @@ public void sendPacketQueued(DefinedPacket packet)
Protocol encodeProtocol = ch.getEncodeProtocol();
if ( !encodeProtocol.TO_SERVER.hasPacket( packet.getClass(), ch.getEncodeVersion() ) )
{
// we should limit this so bad api usage won't oom the server.
Preconditions.checkState( packetQueue.size() <= 4096, "too many queued packets" );
packetQueue.add( packet );
} else
{
Expand Down
14 changes: 14 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/UserConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ public void sendPacket(DefinedPacket packet)
{
ch.write( packet );
}

@Override
public void sendPacketQueued(DefinedPacket packet)
{
if ( pendingConnection.getVersion() >= ProtocolConstants.MINECRAFT_1_20_2 )
{
UserConnection.this.sendPacketQueued( packet );
} else
{
sendPacket( packet );
}
}
};

public boolean init()
Expand Down Expand Up @@ -191,6 +203,8 @@ public void sendPacketQueued(DefinedPacket packet)
Protocol encodeProtocol = ch.getEncodeProtocol();
if ( !encodeProtocol.TO_CLIENT.hasPacket( packet.getClass(), getPendingConnection().getVersion() ) )
{
// we should limit this so bad api usage won't oom the server.
Preconditions.checkState( packetQueue.size() <= 4096, "too many queued packets" );
packetQueue.add( packet );
} else
{
Expand Down