Varint21FrameDecoder.java

net.minecraft.network.Varint21FrameDecoder

信息

  • 全限定名:net.minecraft.network.Varint21FrameDecoder
  • 类型:public class
  • 包:net.minecraft.network
  • 源码路径:src/main/java/net/minecraft/network/Varint21FrameDecoder.java
  • 起始行号:L11
  • 继承:ByteToMessageDecoder
  • 职责:

    TODO

字段/常量

  • MAX_VARINT21_BYTES

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L12
    • 说明:

      TODO

  • helperBuf

    • 类型: ByteBuf
    • 修饰符: private final
    • 源码定位: L13
    • 说明:

      TODO

  • monitor

    • 类型: BandwidthDebugMonitor
    • 修饰符: private final
    • 源码定位: L14
    • 说明:

      TODO

内部类/嵌套类型

构造器

public Varint21FrameDecoder(BandwidthDebugMonitor monitor) @ L16

  • 构造器名:Varint21FrameDecoder
  • 源码定位:L16
  • 修饰符:public

参数:

  • monitor: BandwidthDebugMonitor

说明:

TODO

方法

下面的方法块按源码顺序生成。

protected void handlerRemoved0(ChannelHandlerContext ctx) @ L20

  • 方法名:handlerRemoved0
  • 源码定位:L20
  • 返回类型:void
  • 修饰符:protected

参数:

  • ctx: ChannelHandlerContext

说明:

TODO

private static boolean copyVarint(ByteBuf in, ByteBuf out) @ L25

  • 方法名:copyVarint
  • 源码定位:L25
  • 返回类型:boolean
  • 修饰符:private static

参数:

  • in: ByteBuf
  • out: ByteBuf

说明:

TODO

protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) @ L41

  • 方法名:decode
  • 源码定位:L41
  • 返回类型:void
  • 修饰符:protected

参数:

  • ctx: ChannelHandlerContext
  • in: ByteBuf
  • out: List

    说明:

    TODO

    代码

    public class Varint21FrameDecoder extends ByteToMessageDecoder {
        private static final int MAX_VARINT21_BYTES = 3;
        private final ByteBuf helperBuf = Unpooled.directBuffer(3);
        private final @Nullable BandwidthDebugMonitor monitor;
     
        public Varint21FrameDecoder(@Nullable BandwidthDebugMonitor monitor) {
            this.monitor = monitor;
        }
     
        @Override
        protected void handlerRemoved0(ChannelHandlerContext ctx) {
            this.helperBuf.release();
        }
     
        private static boolean copyVarint(ByteBuf in, ByteBuf out) {
            for (int i = 0; i < 3; i++) {
                if (!in.isReadable()) {
                    return false;
                }
     
                byte b = in.readByte();
                out.writeByte(b);
                if (!VarInt.hasContinuationBit(b)) {
                    return true;
                }
            }
     
            throw new CorruptedFrameException("length wider than 21-bit");
        }
     
        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
            in.markReaderIndex();
            this.helperBuf.clear();
            if (!copyVarint(in, this.helperBuf)) {
                in.resetReaderIndex();
            } else {
                int length = VarInt.read(this.helperBuf);
                if (length == 0) {
                    throw new CorruptedFrameException("Frame length cannot be zero");
                } else if (in.readableBytes() < length) {
                    in.resetReaderIndex();
                } else {
                    if (this.monitor != null) {
                        this.monitor.onReceive(length + VarInt.getByteSize(length));
                    }
     
                    out.add(in.readBytes(length));
                }
            }
        }
    }

    引用的其他类

    • BandwidthDebugMonitor

      • 引用位置: 参数/字段
    • VarInt

      • 引用位置: 方法调用
      • 关联成员: VarInt.getByteSize(), VarInt.hasContinuationBit(), VarInt.read()