Skip to content

Commit

Permalink
chore: PacketUtils > VarInt
Browse files Browse the repository at this point in the history
  • Loading branch information
RealBauHD committed May 31, 2024
1 parent f53d5c1 commit f884679
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.github.sculkpowered.server.container.item.data.DataComponents;
import io.github.sculkpowered.server.container.item.data.SculkDataComponentType;
import io.github.sculkpowered.server.entity.player.GameProfile.Property;
import io.github.sculkpowered.server.protocol.packet.PacketUtils;
import io.github.sculkpowered.server.protocol.packet.VarInt;
import io.github.sculkpowered.server.registry.Registries;
import io.github.sculkpowered.server.scoreboard.NumberFormat;
import io.github.sculkpowered.server.util.Utf8;
Expand Down Expand Up @@ -103,11 +103,11 @@ public int readInt() {
}

public int readVarInt() {
return PacketUtils.readVarInt(this.buf);
return VarInt.read(this.buf);
}

public @NotNull Buffer writeVarInt(final int value) {
PacketUtils.writeVarInt(this.buf, value);
VarInt.write(this.buf, value);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.util.MoreByteBufUtils;
import io.github.sculkpowered.server.protocol.packet.PacketUtils;
import io.github.sculkpowered.server.protocol.packet.VarInt;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
Expand All @@ -18,7 +18,7 @@ public CompressorDecoder(final VelocityCompressor compressor) {

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
final int size = PacketUtils.readVarInt(buf);
final int size = VarInt.read(buf);
if (size == 0) {
out.add(buf.retain());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.util.MoreByteBufUtils;
import io.github.sculkpowered.server.MinecraftConfig;
import io.github.sculkpowered.server.protocol.packet.PacketUtils;
import io.github.sculkpowered.server.protocol.packet.VarInt;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
Expand All @@ -24,12 +24,12 @@ public CompressorEncoder(final VelocityCompressor compressor, final MinecraftCon
protected void encode(ChannelHandlerContext ctx, ByteBuf buf, ByteBuf out) throws Exception {
final var size = buf.readableBytes();
if (size < this.threshold) { // under threshold - so nothing to do
PacketUtils.writeVarInt(out, size + 1);
PacketUtils.writeVarInt(out, 0);
VarInt.write(out, size + 1);
VarInt.write(out, 0);
out.writeBytes(buf);
} else {
out.writeMedium(8421376);
PacketUtils.writeVarInt(out, size);
VarInt.write(out, size);
final var compatible = MoreByteBufUtils.ensureCompatible(ctx.alloc(), this.compressor, buf);
try {
this.compressor.deflate(compatible, out);
Expand All @@ -49,11 +49,11 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf buf, ByteBuf out) throw
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf buf, boolean preferDirect) {
var size = buf.readableBytes();
if (size < this.threshold) {
size += 1 + PacketUtils.varIntLength(size + 1);
size += 1 + VarInt.length(size + 1);
return IS_JAVA_CIPHER ? ctx.alloc().heapBuffer(size)
: ctx.alloc().directBuffer(size);
} else {
size += 2 + PacketUtils.varIntLength(size);
size += 2 + VarInt.length(size);
return MoreByteBufUtils.preferredBuffer(ctx.alloc(), this.compressor, size);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.github.sculkpowered.server.protocol.Buffer;
import io.github.sculkpowered.server.protocol.State;
import io.github.sculkpowered.server.protocol.packet.PacketUtils;
import io.github.sculkpowered.server.protocol.packet.VarInt;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
Expand All @@ -26,7 +26,7 @@ public void channelRead(@NotNull ChannelHandlerContext ctx, @NotNull Object mess
}

final var offset = buf.readerIndex();
final var id = PacketUtils.readVarInt(buf);
final var id = VarInt.read(buf);
final var packet = this.registry.createPacket(id);
if (packet == null) {
buf.readerIndex(offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.github.sculkpowered.server.protocol.Buffer;
import io.github.sculkpowered.server.protocol.State;
import io.github.sculkpowered.server.protocol.packet.Packet;
import io.github.sculkpowered.server.protocol.packet.PacketUtils;
import io.github.sculkpowered.server.protocol.packet.VarInt;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
Expand All @@ -14,14 +14,14 @@ public final class MinecraftEncoder extends MessageToByteEncoder<Packet> {

@Override
protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf buf) {
PacketUtils.writeVarInt(buf, this.registry.packetId(packet));
VarInt.write(buf, this.registry.packetId(packet));
packet.encode(new Buffer(buf));
}

@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, Packet packet, boolean preferDirect) {
return ctx.alloc().directBuffer(
PacketUtils.varIntLength(this.registry.packetId(packet)) + packet.minLength());
VarInt.length(this.registry.packetId(packet)) + packet.minLength());
}

public void setState(final State state) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.sculkpowered.server.protocol.netty.codec;

import io.github.sculkpowered.server.protocol.packet.PacketUtils;
import io.github.sculkpowered.server.protocol.packet.VarInt;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
Expand All @@ -16,7 +16,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out)
}

final var readerIndex = buf.readerIndex();
final var length = PacketUtils.readVarInt(buf);
final var length = VarInt.read(buf);

if (readerIndex == buf.readerIndex()) {
buf.readerIndex(readerIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static io.github.sculkpowered.server.util.Constants.IS_JAVA_CIPHER;

import io.github.sculkpowered.server.protocol.packet.PacketUtils;
import io.github.sculkpowered.server.protocol.packet.VarInt;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
Expand All @@ -13,13 +13,13 @@ public final class VarIntLengthEncoder extends MessageToByteEncoder<ByteBuf> {

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) {
PacketUtils.writeVarInt(out, msg.readableBytes());
VarInt.write(out, msg.readableBytes());
out.writeBytes(msg);
}

@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf buf, boolean preferDirect) {
final var size = PacketUtils.varIntLength(buf.readableBytes()) + buf.readableBytes();
final var size = VarInt.length(buf.readableBytes()) + buf.readableBytes();
return IS_JAVA_CIPHER ? ctx.alloc().heapBuffer(size)
: ctx.alloc().directBuffer(size);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import io.netty.buffer.ByteBuf;

public final class PacketUtils {
public final class VarInt {

private static final byte[] VARINT_EXACT_BYTE_LENGTHS =
{5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1,
1, 1};

public static int varIntLength(final int value) {
public static int length(final int value) {
return VARINT_EXACT_BYTE_LENGTHS[Integer.numberOfLeadingZeros(value)];
}

// https://steinborn.me/posts/performance/how-fast-can-you-write-a-varint/
public static void writeVarInt(final ByteBuf buf, int value) {
public static void write(final ByteBuf buf, int value) {
if ((value & (0xFFFFFFFF << 7)) == 0) {
buf.writeByte((byte) value);
} else if ((value & (0xFFFFFFFF << 14)) == 0) {
Expand All @@ -31,7 +31,7 @@ public static void writeVarInt(final ByteBuf buf, int value) {
}
}

public static int readVarInt(final ByteBuf buf) {
public static int read(final ByteBuf buf) {
var value = 0;
var position = 0;

Expand Down

0 comments on commit f884679

Please sign in to comment.