| 1 | package net.minecraft.network;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import io.netty.channel.ChannelHandlerContext;
|
| 6 | import io.netty.handler.codec.ByteToMessageDecoder;
|
| 7 | import java.io.IOException;
|
| 8 | import java.util.List;
|
| 9 | import net.minecraft.network.protocol.Packet;
|
| 10 | import net.minecraft.network.protocol.PacketType;
|
| 11 | import net.minecraft.util.profiling.jfr.JvmProfiler;
|
| 12 | import org.slf4j.Logger;
|
| 13 |
|
| 14 | public class PacketDecoder<T extends PacketListener> extends ByteToMessageDecoder implements ProtocolSwapHandler {
|
| 15 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 16 | private final ProtocolInfo<T> protocolInfo;
|
| 17 |
|
| 18 | public PacketDecoder(final ProtocolInfo<T> protocolInfo) {
|
| 19 | this.protocolInfo = protocolInfo;
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | protected void decode(final ChannelHandlerContext ctx, final ByteBuf input, final List<Object> out) throws Exception {
|
| 24 | int readableBytes = input.readableBytes();
|
| 25 |
|
| 26 | Packet<? super T> packet;
|
| 27 | try {
|
| 28 | packet = this.protocolInfo.codec().decode(input);
|
| 29 | } catch (Exception e) {
|
| 30 | if (e instanceof SkipPacketException) {
|
| 31 | input.skipBytes(input.readableBytes());
|
| 32 | }
|
| 33 |
|
| 34 | throw e;
|
| 35 | }
|
| 36 |
|
| 37 | PacketType<? extends Packet<? super T>> packetId = packet.type();
|
| 38 | JvmProfiler.INSTANCE.onPacketReceived(this.protocolInfo.id(), packetId, ctx.channel().remoteAddress(), readableBytes);
|
| 39 | if (input.readableBytes() > 0) {
|
| 40 | throw new IOException(
|
| 41 | "Packet "
|
| 42 | + this.protocolInfo.id().id()
|
| 43 | + "/"
|
| 44 | + packetId
|
| 45 | + " ("
|
| 46 | + packet.getClass().getSimpleName()
|
| 47 | + ") was larger than I expected, found "
|
| 48 | + input.readableBytes()
|
| 49 | + " bytes extra whilst reading packet "
|
| 50 | + packetId
|
| 51 | );
|
| 52 | }
|
| 53 |
|
| 54 | out.add(packet);
|
| 55 | if (LOGGER.isDebugEnabled()) {
|
| 56 | LOGGER.debug(
|
| 57 | Connection.PACKET_RECEIVED_MARKER, " IN: [{}:{}] {} -> {} bytes", this.protocolInfo.id().id(), packetId, packet.getClass().getName(), readableBytes
|
| 58 | );
|
| 59 | }
|
| 60 |
|
| 61 | ProtocolSwapHandler.handleInboundTerminalPacket(ctx, packet);
|
| 62 | }
|
| 63 | }
|
| 64 |
|