mirror of
https://github.com/PaperMC/Velocity.git
synced 2024-11-21 03:11:38 +08:00
Remove PluginMessage slicing for now.
This was causing leak issues and needs to be reimplemented. If anyone's willing to undertake the work, I will gladly accept the PR!
This commit is contained in:
parent
d38c7467d9
commit
0c481d828d
@ -45,24 +45,16 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
|
||||
connection.getProxyPlayer().getConnection().write(packet);
|
||||
} else if (packet instanceof PluginMessage) {
|
||||
PluginMessage pm = (PluginMessage) packet;
|
||||
try {
|
||||
if (!canForwardPluginMessage(pm)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (PluginMessageUtil.isMCBrand(pm)) {
|
||||
connection.getProxyPlayer().getConnection().write(PluginMessageUtil.rewriteMCBrand(pm));
|
||||
return;
|
||||
}
|
||||
|
||||
// we'll decrement this twice: once when writing to the server, once just below this block,
|
||||
// and once in the MinecraftConnection (since this is a slice)
|
||||
pm.getData().retain();
|
||||
|
||||
connection.getProxyPlayer().getConnection().write(pm);
|
||||
} finally {
|
||||
pm.getData().release();
|
||||
if (!canForwardPluginMessage(pm)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (PluginMessageUtil.isMCBrand(pm)) {
|
||||
connection.getProxyPlayer().getConnection().write(PluginMessageUtil.rewriteMCBrand(pm));
|
||||
return;
|
||||
}
|
||||
|
||||
connection.getProxyPlayer().getConnection().write(pm);
|
||||
} else {
|
||||
// Just forward the packet on. We don't have anything to handle at this time.
|
||||
if (packet instanceof ScoreboardTeam ||
|
||||
|
@ -191,46 +191,40 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
|
||||
|
||||
public void handleClientPluginMessage(PluginMessage packet) {
|
||||
logger.info("Got client plugin message packet {}", packet);
|
||||
|
||||
try {
|
||||
if (packet.getChannel().equals("REGISTER") || packet.getChannel().equals("minecraft:register")) {
|
||||
List<String> actuallyRegistered = new ArrayList<>();
|
||||
List<String> channels = PluginMessageUtil.getChannels(packet);
|
||||
for (String channel : channels) {
|
||||
if (clientPluginMsgChannels.size() >= MAX_PLUGIN_CHANNELS &&
|
||||
!clientPluginMsgChannels.contains(channel)) {
|
||||
throw new IllegalStateException("Too many plugin message channels registered");
|
||||
}
|
||||
if (clientPluginMsgChannels.add(channel)) {
|
||||
actuallyRegistered.add(channel);
|
||||
}
|
||||
if (packet.getChannel().equals("REGISTER") || packet.getChannel().equals("minecraft:register")) {
|
||||
List<String> actuallyRegistered = new ArrayList<>();
|
||||
List<String> channels = PluginMessageUtil.getChannels(packet);
|
||||
for (String channel : channels) {
|
||||
if (clientPluginMsgChannels.size() >= MAX_PLUGIN_CHANNELS &&
|
||||
!clientPluginMsgChannels.contains(channel)) {
|
||||
throw new IllegalStateException("Too many plugin message channels registered");
|
||||
}
|
||||
|
||||
if (actuallyRegistered.size() > 0) {
|
||||
logger.info("Rewritten register packet: {}", actuallyRegistered);
|
||||
PluginMessage newRegisterPacket = PluginMessageUtil.constructChannelsPacket(packet.getChannel(), actuallyRegistered);
|
||||
player.getConnectedServer().getMinecraftConnection().write(newRegisterPacket);
|
||||
if (clientPluginMsgChannels.add(channel)) {
|
||||
actuallyRegistered.add(channel);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (packet.getChannel().equals("UNREGISTER") || packet.getChannel().equals("minecraft:unregister")) {
|
||||
List<String> channels = PluginMessageUtil.getChannels(packet);
|
||||
clientPluginMsgChannels.removeAll(channels);
|
||||
if (actuallyRegistered.size() > 0) {
|
||||
logger.info("Rewritten register packet: {}", actuallyRegistered);
|
||||
PluginMessage newRegisterPacket = PluginMessageUtil.constructChannelsPacket(packet.getChannel(), actuallyRegistered);
|
||||
player.getConnectedServer().getMinecraftConnection().write(newRegisterPacket);
|
||||
}
|
||||
|
||||
if (PluginMessageUtil.isMCBrand(packet)) {
|
||||
player.getConnectedServer().getMinecraftConnection().write(PluginMessageUtil.rewriteMCBrand(packet));
|
||||
return;
|
||||
}
|
||||
|
||||
// We're going to forward on the original packet.
|
||||
packet.getData().retain();
|
||||
player.getConnectedServer().getMinecraftConnection().write(packet);
|
||||
} finally {
|
||||
ReferenceCountUtil.release(packet.getData());
|
||||
return;
|
||||
}
|
||||
|
||||
if (packet.getChannel().equals("UNREGISTER") || packet.getChannel().equals("minecraft:unregister")) {
|
||||
List<String> channels = PluginMessageUtil.getChannels(packet);
|
||||
clientPluginMsgChannels.removeAll(channels);
|
||||
}
|
||||
|
||||
if (PluginMessageUtil.isMCBrand(packet)) {
|
||||
player.getConnectedServer().getMinecraftConnection().write(PluginMessageUtil.rewriteMCBrand(packet));
|
||||
return;
|
||||
}
|
||||
|
||||
// We're going to forward on the original packet.
|
||||
player.getConnectedServer().getMinecraftConnection().write(packet);
|
||||
}
|
||||
|
||||
public void handleServerScoreboardPacket(MinecraftPacket packet) {
|
||||
|
@ -8,7 +8,7 @@ import io.netty.buffer.ByteBufUtil;
|
||||
|
||||
public class PluginMessage implements MinecraftPacket {
|
||||
private String channel;
|
||||
private ByteBuf data;
|
||||
private byte[] data;
|
||||
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
@ -18,11 +18,11 @@ public class PluginMessage implements MinecraftPacket {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public ByteBuf getData() {
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(ByteBuf data) {
|
||||
public void setData(byte[] data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@ -37,7 +37,8 @@ public class PluginMessage implements MinecraftPacket {
|
||||
@Override
|
||||
public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) {
|
||||
this.channel = ProtocolUtils.readString(buf);
|
||||
this.data = buf.readRetainedSlice(buf.readableBytes());
|
||||
this.data = new byte[buf.readableBytes()];
|
||||
buf.readBytes(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -27,7 +27,7 @@ public enum PluginMessageUtil {
|
||||
message.getChannel().equals("minecraft:register") ||
|
||||
message.getChannel().equals("minecraft:unregister"),
|
||||
"Unknown channel type " + message.getChannel());
|
||||
String channels = message.getData().toString(StandardCharsets.UTF_8);
|
||||
String channels = new String(message.getData(), StandardCharsets.UTF_8);
|
||||
return ImmutableList.copyOf(channels.split("\0"));
|
||||
}
|
||||
|
||||
@ -37,15 +37,7 @@ public enum PluginMessageUtil {
|
||||
|
||||
PluginMessage message = new PluginMessage();
|
||||
message.setChannel(channel);
|
||||
|
||||
ByteBuf data = Unpooled.buffer();
|
||||
for (String s : channels) {
|
||||
ByteBufUtil.writeUtf8(data, s);
|
||||
data.writeByte(0);
|
||||
}
|
||||
data.writerIndex(data.writerIndex() - 1);
|
||||
|
||||
message.setData(data);
|
||||
message.setData(String.join("\0", channels).getBytes(StandardCharsets.UTF_8));
|
||||
return message;
|
||||
}
|
||||
|
||||
@ -53,13 +45,20 @@ public enum PluginMessageUtil {
|
||||
Preconditions.checkNotNull(message, "message");
|
||||
Preconditions.checkArgument(isMCBrand(message), "message is not a MC Brand plugin message");
|
||||
|
||||
byte[] rewrittenData;
|
||||
ByteBuf rewrittenBuf = Unpooled.buffer();
|
||||
String currentBrand = ProtocolUtils.readString(message.getData());
|
||||
ProtocolUtils.writeString(rewrittenBuf, currentBrand + " (Velocity)");
|
||||
try {
|
||||
String currentBrand = ProtocolUtils.readString(Unpooled.wrappedBuffer(message.getData()));
|
||||
ProtocolUtils.writeString(rewrittenBuf, currentBrand + " (Velocity)");
|
||||
rewrittenData = new byte[rewrittenBuf.readableBytes()];
|
||||
rewrittenBuf.readBytes(rewrittenData);
|
||||
} finally {
|
||||
rewrittenBuf.release();
|
||||
}
|
||||
|
||||
PluginMessage newMsg = new PluginMessage();
|
||||
newMsg.setChannel(message.getChannel());
|
||||
newMsg.setData(rewrittenBuf);
|
||||
newMsg.setData(rewrittenData);
|
||||
return newMsg;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user