3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Clarity improvements.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-12-28 12:29:24 -05:00
Ursprung 5b4992fd9c
Commit 566a306d18
4 geänderte Dateien mit 79 neuen und 12 gelöschten Zeilen

Datei anzeigen

@ -264,8 +264,6 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
return;
}
writeString(buf, "plugins");
StringBuilder pluginsString = new StringBuilder();
pluginsString.append(serverVersion).append(':').append(' ');
Iterator<QueryResponse.PluginInformation> iterator = plugins.iterator();
@ -279,7 +277,7 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
}
}
writeString(buf, pluginsString.toString());
write("plugins", pluginsString.toString());
}
}
}

Datei anzeigen

@ -24,6 +24,6 @@ public class LegacyPingEncoder extends MessageToByteEncoder<LegacyDisconnect> {
private static void writeLegacyString(ByteBuf out, String string) {
out.writeShort(string.length());
out.writeBytes(string.getBytes(StandardCharsets.UTF_16BE));
out.writeCharSequence(string, StandardCharsets.UTF_16BE);
}
}

Datei anzeigen

@ -1,6 +1,7 @@
package com.velocitypowered.proxy.protocol.netty;
import com.google.common.base.Preconditions;
import static com.velocitypowered.proxy.protocol.util.NettyPreconditions.checkFrame;
import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
@ -22,23 +23,24 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder<ByteBuf> {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
int uncompressedSize = ProtocolUtils.readVarInt(msg);
if (uncompressedSize == 0) {
int expectedUncompressedSize = ProtocolUtils.readVarInt(msg);
if (expectedUncompressedSize == 0) {
// Strip the now-useless uncompressed size, this message is already uncompressed.
out.add(msg.retainedSlice());
msg.skipBytes(msg.readableBytes());
return;
}
Preconditions.checkState(uncompressedSize >= threshold,
checkFrame(expectedUncompressedSize >= threshold,
"Uncompressed size %s is greater than threshold %s",
uncompressedSize, threshold);
expectedUncompressedSize, threshold);
ByteBuf uncompressed = ctx.alloc()
.buffer(Math.min(uncompressedSize, MAXIMUM_INITIAL_BUFFER_SIZE));
.buffer(Math.min(expectedUncompressedSize, MAXIMUM_INITIAL_BUFFER_SIZE));
try {
compressor.inflate(msg, uncompressed);
Preconditions.checkState(uncompressedSize == uncompressed.readableBytes(),
"Mismatched compression sizes");
checkFrame(expectedUncompressedSize == uncompressed.readableBytes(),
"Mismatched compression sizes (got %s, expected %s)",
uncompressed.readableBytes(), expectedUncompressedSize);
out.add(uncompressed);
} catch (Exception e) {
uncompressed.release();

Datei anzeigen

@ -0,0 +1,67 @@
package com.velocitypowered.proxy.protocol.util;
import com.google.common.base.Strings;
import io.netty.handler.codec.CorruptedFrameException;
/**
* Extends {@link com.google.common.base.Preconditions} for Netty's {@link CorruptedFrameException}.
*/
public class NettyPreconditions {
private NettyPreconditions() {
throw new AssertionError();
}
/**
* Throws {@link CorruptedFrameException} if {@code b} is false.
* @param b the expression to check
* @param message the message to include in the thrown {@link CorruptedFrameException}
*/
public static void checkFrame(boolean b, String message) {
if (!b) {
throw new CorruptedFrameException(message);
}
}
/**
* Throws {@link CorruptedFrameException} if {@code b} is false.
* @param b the expression to check
* @param message the message to include in the thrown {@link CorruptedFrameException}, formatted
* like {@link com.google.common.base.Preconditions#checkArgument(boolean)} and
* friends
* @param arg1 the first argument to format the message with
*/
public static void checkFrame(boolean b, String message, Object arg1) {
if (!b) {
throw new CorruptedFrameException(Strings.lenientFormat(message, arg1));
}
}
/**
* Throws {@link CorruptedFrameException} if {@code b} is false.
* @param b the expression to check
* @param message the message to include in the thrown {@link CorruptedFrameException}, formatted
* like {@link com.google.common.base.Preconditions#checkArgument(boolean)} and
* friends
* @param arg1 the first argument to format the message with
* @param arg2 the second argument to format the message with
*/
public static void checkFrame(boolean b, String message, Object arg1, Object arg2) {
if (!b) {
throw new CorruptedFrameException(Strings.lenientFormat(message, arg1, arg2));
}
}
/**
* Throws {@link CorruptedFrameException} if {@code b} is false.
* @param b the expression to check
* @param message the message to include in the thrown {@link CorruptedFrameException}, formatted
* like {@link com.google.common.base.Preconditions#checkArgument(boolean)} and
* friends
* @param args the arguments to format the message with-
*/
public static void checkFrame(boolean b, String message, Object... args) {
if (!b) {
throw new CorruptedFrameException(Strings.lenientFormat(message, args));
}
}
}