| 1 | package net.minecraft.network;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import io.netty.channel.ChannelDuplexHandler;
|
| 5 | import io.netty.channel.ChannelHandlerContext;
|
| 6 | import io.netty.channel.ChannelInboundHandler;
|
| 7 | import io.netty.channel.ChannelOutboundHandler;
|
| 8 | import io.netty.channel.ChannelOutboundHandlerAdapter;
|
| 9 | import io.netty.channel.ChannelPromise;
|
| 10 | import io.netty.handler.codec.DecoderException;
|
| 11 | import io.netty.handler.codec.EncoderException;
|
| 12 | import io.netty.util.ReferenceCountUtil;
|
| 13 | import net.minecraft.network.protocol.Packet;
|
| 14 |
|
| 15 | public class UnconfiguredPipelineHandler {
|
| 16 | public static <T extends PacketListener> UnconfiguredPipelineHandler.InboundConfigurationTask setupInboundProtocol(final ProtocolInfo<T> protocolInfo) {
|
| 17 | return setupInboundHandler(new PacketDecoder<>(protocolInfo));
|
| 18 | }
|
| 19 |
|
| 20 | private static UnconfiguredPipelineHandler.InboundConfigurationTask setupInboundHandler(final ChannelInboundHandler newHandler) {
|
| 21 | return ctx -> {
|
| 22 | ctx.pipeline().replace(ctx.name(), "decoder", newHandler);
|
| 23 | ctx.channel().config().setAutoRead(true);
|
| 24 | };
|
| 25 | }
|
| 26 |
|
| 27 | public static <T extends PacketListener> UnconfiguredPipelineHandler.OutboundConfigurationTask setupOutboundProtocol(final ProtocolInfo<T> codecData) {
|
| 28 | return setupOutboundHandler(new PacketEncoder<>(codecData));
|
| 29 | }
|
| 30 |
|
| 31 | private static UnconfiguredPipelineHandler.OutboundConfigurationTask setupOutboundHandler(final ChannelOutboundHandler newHandler) {
|
| 32 | return ctx -> ctx.pipeline().replace(ctx.name(), "encoder", newHandler);
|
| 33 | }
|
| 34 |
|
| 35 | public static class Inbound extends ChannelDuplexHandler {
|
| 36 | @Override
|
| 37 | public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
|
| 38 | if (!(msg instanceof ByteBuf) && !(msg instanceof Packet)) {
|
| 39 | ctx.fireChannelRead(msg);
|
| 40 | } else {
|
| 41 | ReferenceCountUtil.release(msg);
|
| 42 | throw new DecoderException("Pipeline has no inbound protocol configured, can't process packet " + msg);
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
|
| 48 | if (msg instanceof UnconfiguredPipelineHandler.InboundConfigurationTask configurationTask) {
|
| 49 | try {
|
| 50 | configurationTask.run(ctx);
|
| 51 | } finally {
|
| 52 | ReferenceCountUtil.release(msg);
|
| 53 | }
|
| 54 |
|
| 55 | promise.setSuccess();
|
| 56 | } else {
|
| 57 | ctx.write(msg, promise);
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | @FunctionalInterface
|
| 63 | public interface InboundConfigurationTask {
|
| 64 | void run(ChannelHandlerContext ctx);
|
| 65 |
|
| 66 | default UnconfiguredPipelineHandler.InboundConfigurationTask andThen(final UnconfiguredPipelineHandler.InboundConfigurationTask otherTask) {
|
| 67 | return ctx -> {
|
| 68 | this.run(ctx);
|
| 69 | otherTask.run(ctx);
|
| 70 | };
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | public static class Outbound extends ChannelOutboundHandlerAdapter {
|
| 75 | @Override
|
| 76 | public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
|
| 77 | if (msg instanceof Packet) {
|
| 78 | ReferenceCountUtil.release(msg);
|
| 79 | throw new EncoderException("Pipeline has no outbound protocol configured, can't process packet " + msg);
|
| 80 | }
|
| 81 |
|
| 82 | if (msg instanceof UnconfiguredPipelineHandler.OutboundConfigurationTask configurationTask) {
|
| 83 | try {
|
| 84 | configurationTask.run(ctx);
|
| 85 | } finally {
|
| 86 | ReferenceCountUtil.release(msg);
|
| 87 | }
|
| 88 |
|
| 89 | promise.setSuccess();
|
| 90 | } else {
|
| 91 | ctx.write(msg, promise);
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | @FunctionalInterface
|
| 97 | public interface OutboundConfigurationTask {
|
| 98 | void run(ChannelHandlerContext ctx);
|
| 99 |
|
| 100 | default UnconfiguredPipelineHandler.OutboundConfigurationTask andThen(final UnconfiguredPipelineHandler.OutboundConfigurationTask otherTask) {
|
| 101 | return ctx -> {
|
| 102 | this.run(ctx);
|
| 103 | otherTask.run(ctx);
|
| 104 | };
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 |
|