| 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.MessageToByteEncoder;
|
| 7 | import net.minecraft.network.protocol.Packet;
|
| 8 | import net.minecraft.network.protocol.PacketType;
|
| 9 | import net.minecraft.util.profiling.jfr.JvmProfiler;
|
| 10 | import org.slf4j.Logger;
|
| 11 |
|
| 12 | public class PacketEncoder<T extends PacketListener> extends MessageToByteEncoder<Packet<T>> {
|
| 13 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 14 | private final ProtocolInfo<T> protocolInfo;
|
| 15 |
|
| 16 | public PacketEncoder(final ProtocolInfo<T> protocolInfo) {
|
| 17 | this.protocolInfo = protocolInfo;
|
| 18 | }
|
| 19 |
|
| 20 | protected void encode(final ChannelHandlerContext ctx, final Packet<T> packet, final ByteBuf output) throws Exception {
|
| 21 | PacketType<? extends Packet<? super T>> packetId = packet.type();
|
| 22 |
|
| 23 | try {
|
| 24 | this.protocolInfo.codec().encode(output, packet);
|
| 25 | int writtenBytes = output.readableBytes();
|
| 26 | if (LOGGER.isDebugEnabled()) {
|
| 27 | LOGGER.debug(Connection.PACKET_SENT_MARKER, "OUT: [{}:{}] {} -> {} bytes", this.protocolInfo.id().id(), packetId, packet.getClass().getName(), writtenBytes);
|
| 28 | }
|
| 29 |
|
| 30 | JvmProfiler.INSTANCE.onPacketSent(this.protocolInfo.id(), packetId, ctx.channel().remoteAddress(), writtenBytes);
|
| 31 | } catch (Throwable t) {
|
| 32 | LOGGER.error("Error sending packet {}", packetId, t);
|
| 33 | if (packet.isSkippable()) {
|
| 34 | throw new SkipPacketEncoderException(t);
|
| 35 | }
|
| 36 |
|
| 37 | throw t;
|
| 38 | } finally {
|
| 39 | ProtocolSwapHandler.handleOutboundTerminalPacket(ctx, packet);
|
| 40 | }
|
| 41 | }
|
| 42 | }
|
| 43 |
|