Skip to content

Commit

Permalink
Add (hopefully temporary) queue for plugin messages to server
Browse files Browse the repository at this point in the history
  • Loading branch information
md-5 committed Sep 23, 2023
1 parent 7b27dfa commit 497c687
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class MinecraftEncoder extends MessageToByteEncoder<DefinedPacket>
@Setter
private Protocol protocol;
private boolean server;
@Getter
@Setter
private int protocolVersion;

Expand Down
26 changes: 25 additions & 1 deletion proxy/src/main/java/net/md_5/bungee/ServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.net.SocketAddress;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,6 +14,7 @@
import net.md_5.bungee.api.connection.Server;
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.packet.PluginMessage;

@RequiredArgsConstructor
Expand All @@ -30,6 +32,7 @@ public class ServerConnection implements Server
private final boolean forgeServer = false;
@Getter
private final Queue<KeepAliveData> keepAlives = new ArrayDeque<>();
private final Queue<DefinedPacket> packetQueue = new ConcurrentLinkedQueue<>();

private final Unsafe unsafe = new Unsafe()
{
Expand All @@ -40,10 +43,31 @@ public void sendPacket(DefinedPacket packet)
}
};

public void sendPacketQueued(DefinedPacket packet)
{
Protocol encodeProtocol = ch.getEncodeProtocol();
if ( !encodeProtocol.TO_SERVER.hasPacket( packet.getClass(), ch.getEncodeVersion() ) )
{
packetQueue.add( packet );
} else
{
unsafe().sendPacket( packet );
}
}

public void sendQueuedPackets()
{
DefinedPacket packet;
while ( ( packet = packetQueue.poll() ) != null )
{
unsafe().sendPacket( packet );
}
}

@Override
public void sendData(String channel, byte[] data)
{
unsafe().sendPacket( new PluginMessage( channel, data, forgeServer ) );
sendPacketQueued( new PluginMessage( channel, data, forgeServer ) );
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion proxy/src/main/java/net/md_5/bungee/UserConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void sendPacket(PacketWrapper packet)
public void sendPacketQueued(DefinedPacket packet)
{
Protocol encodeProtocol = ch.getEncodeProtocol();
if ( encodeProtocol != Protocol.GAME && !encodeProtocol.TO_CLIENT.hasPacket( packet.getClass(), getPendingConnection().getVersion() ) )
if ( !encodeProtocol.TO_CLIENT.hasPacket( packet.getClass(), getPendingConnection().getVersion() ) )
{
packetQueue.add( packet );
} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ public void handle(StartConfiguration startConfiguration) throws Exception
ch.setDecodeProtocol( Protocol.CONFIGURATION );
ch.write( new LoginAcknowledged() );
ch.setEncodeProtocol( Protocol.CONFIGURATION );

con.getServer().sendQueuedPackets();

throw CancelSendSignal.INSTANCE;
}
}
Expand Down
5 changes: 5 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public void setVersion(int protocol)
ch.pipeline().get( MinecraftEncoder.class ).setProtocolVersion( protocol );
}

public int getEncodeVersion()
{
return ch.pipeline().get( MinecraftEncoder.class ).getProtocolVersion();
}

public void write(Object packet)
{
if ( !closed )
Expand Down

0 comments on commit 497c687

Please sign in to comment.