3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-25 15:50:19 +01:00

More efficient message handling

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-07-30 15:05:24 -04:00
Ursprung 8a73e3f12b
Commit dde6560a5a
5 geänderte Dateien mit 74 neuen und 59 gelöschten Zeilen

Datei anzeigen

@ -6,6 +6,7 @@ import com.velocitypowered.proxy.protocol.packets.*;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import io.netty.buffer.ByteBuf;
import io.netty.util.ReferenceCountUtil;
public class BackendPlaySessionHandler implements MinecraftSessionHandler {
private final ServerConnection connection;
@ -43,15 +44,20 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
connection.getProxyPlayer().getConnection().write(packet);
} else if (packet instanceof PluginMessage) {
PluginMessage pm = (PluginMessage) packet;
if (!canForwardMessage(pm)) {
try {
PluginMessage newPacket = pm;
if (!canForwardMessage(newPacket)) {
return;
}
if (pm.getChannel().equals("MC|Brand")) {
pm = PluginMessageUtil.rewriteMCBrand(pm);
if (newPacket.getChannel().equals("MC|Brand")) {
newPacket = PluginMessageUtil.rewriteMCBrand(pm);
}
connection.getProxyPlayer().getConnection().write(pm);
connection.getProxyPlayer().getConnection().write(newPacket);
} finally {
ReferenceCountUtil.release(pm.getData());
}
} else {
// Just forward the packet on. We don't have anything to handle at this time.
connection.getProxyPlayer().getConnection().write(packet);

Datei anzeigen

@ -10,6 +10,7 @@ import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import com.velocitypowered.proxy.util.ThrowableUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.EventLoop;
import io.netty.util.ReferenceCountUtil;
import net.kyori.text.TextComponent;
import net.kyori.text.format.TextColor;
import org.apache.logging.log4j.LogManager;
@ -173,6 +174,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
public void handleClientPluginMessage(PluginMessage packet) {
logger.info("Got client plugin message packet {}", packet);
PluginMessage original = packet;
try {
if (packet.getChannel().equals("REGISTER")) {
List<String> actuallyRegistered = new ArrayList<>();
List<String> channels = PluginMessageUtil.getChannels(packet);
@ -208,6 +211,9 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
// No other special handling?
player.getConnectedServer().getChannel().write(packet);
} finally {
ReferenceCountUtil.release(original.getData());
}
}
public Set<String> getClientPluginMsgChannels() {

Datei anzeigen

@ -2,6 +2,7 @@ package com.velocitypowered.proxy.protocol;
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
@ -40,15 +41,15 @@ public enum ProtocolUtils { ;
public static String readString(ByteBuf buf, int cap) {
int length = readVarInt(buf);
Preconditions.checkArgument(length <= cap, "Bad string size (got %s, maximum is %s)", length, cap);
byte[] str = new byte[length];
buf.readBytes(str);
return new String(str, StandardCharsets.UTF_8);
String str = buf.toString(buf.readerIndex(), length, StandardCharsets.UTF_8);
buf.skipBytes(length);
return str;
}
public static void writeString(ByteBuf buf, String str) {
byte[] asUtf8 = str.getBytes(StandardCharsets.UTF_8);
writeVarInt(buf, asUtf8.length);
buf.writeBytes(asUtf8);
int size = ByteBufUtil.utf8Bytes(str);
writeVarInt(buf, size);
ByteBufUtil.writeUtf8(buf, str);
}
public static byte[] readByteArray(ByteBuf buf) {

Datei anzeigen

@ -4,12 +4,13 @@ import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.ProtocolConstants;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import java.util.Arrays;
public class PluginMessage implements MinecraftPacket {
private String channel;
private byte[] data;
private ByteBuf data;
public String getChannel() {
return channel;
@ -19,11 +20,11 @@ public class PluginMessage implements MinecraftPacket {
this.channel = channel;
}
public byte[] getData() {
public ByteBuf getData() {
return data;
}
public void setData(byte[] data) {
public void setData(ByteBuf data) {
this.data = data;
}
@ -31,15 +32,14 @@ public class PluginMessage implements MinecraftPacket {
public String toString() {
return "PluginMessage{" +
"channel='" + channel + '\'' +
", data=" + Arrays.toString(data) +
", data=" + ByteBufUtil.hexDump(data) +
'}';
}
@Override
public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) {
this.channel = ProtocolUtils.readString(buf, 20);
this.data = new byte[buf.readableBytes()];
buf.readBytes(this.data);
this.data = buf.readRetainedSlice(buf.readableBytes());
}
@Override

Datei anzeigen

@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableList;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.packets.PluginMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import java.nio.charset.StandardCharsets;
@ -18,32 +19,33 @@ public enum PluginMessageUtil {
public static List<String> getChannels(PluginMessage message) {
Preconditions.checkArgument(message.getChannel().equals("REGISTER") ||
message.getChannel().equals("UNREGISTER"), "Unknown channel type " + message.getChannel());
return ImmutableList.copyOf(new String(message.getData()).split("\0"));
String channels = message.getData().toString(StandardCharsets.UTF_8);
return ImmutableList.copyOf(channels.split("\0"));
}
public static PluginMessage constructChannelsPacket(String channel, Collection<String> channels) {
PluginMessage message = new PluginMessage();
message.setChannel(channel);
message.setData(Joiner.on("\0").join(channels).getBytes(StandardCharsets.UTF_8));
ByteBuf data = Unpooled.buffer();
for (String s : channels) {
ByteBufUtil.writeUtf8(data, s);
data.writeByte(0);
}
data.writerIndex(data.writerIndex() - 1);
message.setData(data);
return message;
}
public static PluginMessage rewriteMCBrand(PluginMessage message) {
ByteBuf currentBrandBuf = Unpooled.wrappedBuffer(message.getData());
ByteBuf rewrittenBuf = Unpooled.buffer();
byte[] rewrittenBrand;
try {
String currentBrand = ProtocolUtils.readString(currentBrandBuf);
String currentBrand = ProtocolUtils.readString(message.getData());
ProtocolUtils.writeString(rewrittenBuf, currentBrand + " (Velocity)");
rewrittenBrand = new byte[rewrittenBuf.readableBytes()];
rewrittenBuf.readBytes(rewrittenBrand);
} finally {
rewrittenBuf.release();
}
PluginMessage newMsg = new PluginMessage();
newMsg.setChannel("MC|Brand");
newMsg.setData(rewrittenBrand);
newMsg.setData(rewrittenBuf);
return newMsg;
}
}