package io.netty.channel;

import g.a.a.a.a;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.internal.ThrowableUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.net.SocketAddress;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public class CombinedChannelDuplexHandler<I extends ChannelInboundHandler, O extends ChannelOutboundHandler> extends ChannelDuplexHandler {
    public static final /* synthetic */ boolean $assertionsDisabled = false;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) CombinedChannelDuplexHandler.class);
    private volatile boolean handlerAdded;
    private DelegatingChannelHandlerContext inboundCtx;
    private I inboundHandler;
    private DelegatingChannelHandlerContext outboundCtx;
    private O outboundHandler;

    public static class DelegatingChannelHandlerContext implements ChannelHandlerContext {
        private final ChannelHandlerContext ctx;
        private final ChannelHandler handler;
        public boolean removed;

        public DelegatingChannelHandlerContext(ChannelHandlerContext channelHandlerContext, ChannelHandler channelHandler) {
            this.ctx = channelHandlerContext;
            this.handler = channelHandler;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public void remove0() {
            if (this.removed) {
                return;
            }
            this.removed = true;
            try {
                this.handler.handlerRemoved(this);
            } catch (Throwable th) {
                fireExceptionCaught((Throwable) new ChannelPipelineException(this.handler.getClass().getName() + ".handlerRemoved() has thrown an exception.", th));
            }
        }

        @Override // io.netty.channel.ChannelHandlerContext
        public ByteBufAllocator alloc() {
            return this.ctx.alloc();
        }

        @Override // io.netty.channel.ChannelHandlerContext
        public <T> Attribute<T> attr(AttributeKey<T> attributeKey) {
            return this.ctx.channel().attr(attributeKey);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture bind(SocketAddress socketAddress) {
            return this.ctx.bind(socketAddress);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture bind(SocketAddress socketAddress, ChannelPromise channelPromise) {
            return this.ctx.bind(socketAddress, channelPromise);
        }

        @Override // io.netty.channel.ChannelHandlerContext
        public Channel channel() {
            return this.ctx.channel();
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture close() {
            return this.ctx.close();
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture close(ChannelPromise channelPromise) {
            return this.ctx.close(channelPromise);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture connect(SocketAddress socketAddress) {
            return this.ctx.connect(socketAddress);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture connect(SocketAddress socketAddress, ChannelPromise channelPromise) {
            return this.ctx.connect(socketAddress, channelPromise);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture connect(SocketAddress socketAddress, SocketAddress socketAddress2) {
            return this.ctx.connect(socketAddress, socketAddress2);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture connect(SocketAddress socketAddress, SocketAddress socketAddress2, ChannelPromise channelPromise) {
            return this.ctx.connect(socketAddress, socketAddress2, channelPromise);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture deregister() {
            return this.ctx.deregister();
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture deregister(ChannelPromise channelPromise) {
            return this.ctx.deregister(channelPromise);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture disconnect() {
            return this.ctx.disconnect();
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture disconnect(ChannelPromise channelPromise) {
            return this.ctx.disconnect(channelPromise);
        }

        @Override // io.netty.channel.ChannelHandlerContext
        public EventExecutor executor() {
            return this.ctx.executor();
        }

        @Override // io.netty.channel.ChannelInboundInvoker
        public ChannelHandlerContext fireChannelActive() {
            this.ctx.fireChannelActive();
            return this;
        }

        @Override // io.netty.channel.ChannelInboundInvoker
        public ChannelHandlerContext fireChannelInactive() {
            this.ctx.fireChannelInactive();
            return this;
        }

        @Override // io.netty.channel.ChannelInboundInvoker
        public ChannelHandlerContext fireChannelRead(Object obj) {
            this.ctx.fireChannelRead(obj);
            return this;
        }

        @Override // io.netty.channel.ChannelInboundInvoker
        public ChannelHandlerContext fireChannelReadComplete() {
            this.ctx.fireChannelReadComplete();
            return this;
        }

        @Override // io.netty.channel.ChannelInboundInvoker
        public ChannelHandlerContext fireChannelRegistered() {
            this.ctx.fireChannelRegistered();
            return this;
        }

        @Override // io.netty.channel.ChannelInboundInvoker
        public ChannelHandlerContext fireChannelUnregistered() {
            this.ctx.fireChannelUnregistered();
            return this;
        }

        @Override // io.netty.channel.ChannelInboundInvoker
        public ChannelHandlerContext fireChannelWritabilityChanged() {
            this.ctx.fireChannelWritabilityChanged();
            return this;
        }

        @Override // io.netty.channel.ChannelInboundInvoker
        public ChannelHandlerContext fireExceptionCaught(Throwable th) {
            this.ctx.fireExceptionCaught(th);
            return this;
        }

        @Override // io.netty.channel.ChannelInboundInvoker
        public ChannelHandlerContext fireUserEventTriggered(Object obj) {
            this.ctx.fireUserEventTriggered(obj);
            return this;
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelHandlerContext flush() {
            this.ctx.flush();
            return this;
        }

        @Override // io.netty.channel.ChannelHandlerContext
        public ChannelHandler handler() {
            return this.ctx.handler();
        }

        @Override // io.netty.channel.ChannelHandlerContext
        public <T> boolean hasAttr(AttributeKey<T> attributeKey) {
            return this.ctx.channel().hasAttr(attributeKey);
        }

        @Override // io.netty.channel.ChannelHandlerContext
        public boolean isRemoved() {
            return this.removed || this.ctx.isRemoved();
        }

        @Override // io.netty.channel.ChannelHandlerContext
        public String name() {
            return this.ctx.name();
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture newFailedFuture(Throwable th) {
            return this.ctx.newFailedFuture(th);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelProgressivePromise newProgressivePromise() {
            return this.ctx.newProgressivePromise();
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelPromise newPromise() {
            return this.ctx.newPromise();
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture newSucceededFuture() {
            return this.ctx.newSucceededFuture();
        }

        @Override // io.netty.channel.ChannelHandlerContext
        public ChannelPipeline pipeline() {
            return this.ctx.pipeline();
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelHandlerContext read() {
            this.ctx.read();
            return this;
        }

        public final void remove() {
            EventExecutor eventExecutorExecutor = executor();
            if (eventExecutorExecutor.inEventLoop()) {
                remove0();
            } else {
                eventExecutorExecutor.execute(new Runnable() { // from class: io.netty.channel.CombinedChannelDuplexHandler.DelegatingChannelHandlerContext.1
                    @Override // java.lang.Runnable
                    public void run() {
                        DelegatingChannelHandlerContext.this.remove0();
                    }
                });
            }
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelPromise voidPromise() {
            return this.ctx.voidPromise();
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture write(Object obj) {
            return this.ctx.write(obj);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture write(Object obj, ChannelPromise channelPromise) {
            return this.ctx.write(obj, channelPromise);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture writeAndFlush(Object obj) {
            return this.ctx.writeAndFlush(obj);
        }

        @Override // io.netty.channel.ChannelOutboundInvoker
        public ChannelFuture writeAndFlush(Object obj, ChannelPromise channelPromise) {
            return this.ctx.writeAndFlush(obj, channelPromise);
        }
    }

    public CombinedChannelDuplexHandler() {
        ensureNotSharable();
    }

    public CombinedChannelDuplexHandler(I i2, O o2) {
        ensureNotSharable();
        init(i2, o2);
    }

    private void checkAdded() {
        if (!this.handlerAdded) {
            throw new IllegalStateException("handler not added to pipeline yet");
        }
    }

    private void validate(I i2, O o2) {
        if (this.inboundHandler != null) {
            StringBuilder sbF = a.F("init() can not be invoked if ");
            sbF.append(CombinedChannelDuplexHandler.class.getSimpleName());
            sbF.append(" was constructed with non-default constructor.");
            throw new IllegalStateException(sbF.toString());
        }
        Objects.requireNonNull(i2, "inboundHandler");
        Objects.requireNonNull(o2, "outboundHandler");
        if (i2 instanceof ChannelOutboundHandler) {
            StringBuilder sbF2 = a.F("inboundHandler must not implement ");
            sbF2.append(ChannelOutboundHandler.class.getSimpleName());
            sbF2.append(" to get combined.");
            throw new IllegalArgumentException(sbF2.toString());
        }
        if (o2 instanceof ChannelInboundHandler) {
            StringBuilder sbF3 = a.F("outboundHandler must not implement ");
            sbF3.append(ChannelInboundHandler.class.getSimpleName());
            sbF3.append(" to get combined.");
            throw new IllegalArgumentException(sbF3.toString());
        }
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void bind(ChannelHandlerContext channelHandlerContext, SocketAddress socketAddress, ChannelPromise channelPromise) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.outboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.bind(socketAddress, channelPromise);
        } else {
            this.outboundHandler.bind(delegatingChannelHandlerContext, socketAddress, channelPromise);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelActive(ChannelHandlerContext channelHandlerContext) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.inboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.fireChannelActive();
        } else {
            this.inboundHandler.channelActive(delegatingChannelHandlerContext);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext channelHandlerContext) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.inboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.fireChannelInactive();
        } else {
            this.inboundHandler.channelInactive(delegatingChannelHandlerContext);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object obj) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.inboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.fireChannelRead(obj);
        } else {
            this.inboundHandler.channelRead(delegatingChannelHandlerContext, obj);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelReadComplete(ChannelHandlerContext channelHandlerContext) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.inboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.fireChannelReadComplete();
        } else {
            this.inboundHandler.channelReadComplete(delegatingChannelHandlerContext);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRegistered(ChannelHandlerContext channelHandlerContext) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.inboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.fireChannelRegistered();
        } else {
            this.inboundHandler.channelRegistered(delegatingChannelHandlerContext);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelUnregistered(ChannelHandlerContext channelHandlerContext) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.inboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.fireChannelUnregistered();
        } else {
            this.inboundHandler.channelUnregistered(delegatingChannelHandlerContext);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.inboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.fireChannelWritabilityChanged();
        } else {
            this.inboundHandler.channelWritabilityChanged(delegatingChannelHandlerContext);
        }
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void close(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.outboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.close(channelPromise);
        } else {
            this.outboundHandler.close(delegatingChannelHandlerContext, channelPromise);
        }
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void connect(ChannelHandlerContext channelHandlerContext, SocketAddress socketAddress, SocketAddress socketAddress2, ChannelPromise channelPromise) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.outboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.connect(socketAddress2, channelPromise);
        } else {
            this.outboundHandler.connect(delegatingChannelHandlerContext, socketAddress, socketAddress2, channelPromise);
        }
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void deregister(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.outboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.deregister(channelPromise);
        } else {
            this.outboundHandler.deregister(delegatingChannelHandlerContext, channelPromise);
        }
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void disconnect(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.outboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.disconnect(channelPromise);
        } else {
            this.outboundHandler.disconnect(delegatingChannelHandlerContext, channelPromise);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable th) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.inboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.fireExceptionCaught(th);
        } else {
            this.inboundHandler.exceptionCaught(delegatingChannelHandlerContext, th);
        }
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void flush(ChannelHandlerContext channelHandlerContext) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.outboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.flush();
        } else {
            this.outboundHandler.flush(delegatingChannelHandlerContext);
        }
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) {
        if (this.inboundHandler == null) {
            StringBuilder sbF = a.F("init() must be invoked before being added to a ");
            sbF.append(ChannelPipeline.class.getSimpleName());
            sbF.append(" if ");
            sbF.append(CombinedChannelDuplexHandler.class.getSimpleName());
            sbF.append(" was constructed with the default constructor.");
            throw new IllegalStateException(sbF.toString());
        }
        this.outboundCtx = new DelegatingChannelHandlerContext(channelHandlerContext, this.outboundHandler);
        this.inboundCtx = new DelegatingChannelHandlerContext(channelHandlerContext, this.inboundHandler) { // from class: io.netty.channel.CombinedChannelDuplexHandler.1
            @Override // io.netty.channel.CombinedChannelDuplexHandler.DelegatingChannelHandlerContext, io.netty.channel.ChannelInboundInvoker
            public ChannelHandlerContext fireExceptionCaught(Throwable th) {
                if (CombinedChannelDuplexHandler.this.outboundCtx.removed) {
                    super.fireExceptionCaught(th);
                } else {
                    try {
                        CombinedChannelDuplexHandler.this.outboundHandler.exceptionCaught(CombinedChannelDuplexHandler.this.outboundCtx, th);
                    } catch (Throwable th2) {
                        if (CombinedChannelDuplexHandler.logger.isDebugEnabled()) {
                            CombinedChannelDuplexHandler.logger.debug("An exception {}was thrown by a user handler's exceptionCaught() method while handling the following exception:", ThrowableUtil.stackTraceToString(th2), th);
                        } else if (CombinedChannelDuplexHandler.logger.isWarnEnabled()) {
                            CombinedChannelDuplexHandler.logger.warn("An exception '{}' [enable DEBUG level for full stacktrace] was thrown by a user handler's exceptionCaught() method while handling the following exception:", th2, th);
                        }
                    }
                }
                return this;
            }
        };
        this.handlerAdded = true;
        try {
            this.inboundHandler.handlerAdded(this.inboundCtx);
        } finally {
            this.outboundHandler.handlerAdded(this.outboundCtx);
        }
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) {
        try {
            this.inboundCtx.remove();
        } finally {
            this.outboundCtx.remove();
        }
    }

    public final I inboundHandler() {
        return this.inboundHandler;
    }

    public final void init(I i2, O o2) {
        validate(i2, o2);
        this.inboundHandler = i2;
        this.outboundHandler = o2;
    }

    public final O outboundHandler() {
        return this.outboundHandler;
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void read(ChannelHandlerContext channelHandlerContext) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.outboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.read();
        } else {
            this.outboundHandler.read(delegatingChannelHandlerContext);
        }
    }

    public final void removeInboundHandler() {
        checkAdded();
        this.inboundCtx.remove();
    }

    public final void removeOutboundHandler() {
        checkAdded();
        this.outboundCtx.remove();
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object obj) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.inboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.fireUserEventTriggered(obj);
        } else {
            this.inboundHandler.userEventTriggered(delegatingChannelHandlerContext, obj);
        }
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void write(ChannelHandlerContext channelHandlerContext, Object obj, ChannelPromise channelPromise) {
        DelegatingChannelHandlerContext delegatingChannelHandlerContext = this.outboundCtx;
        if (delegatingChannelHandlerContext.removed) {
            delegatingChannelHandlerContext.write(obj, channelPromise);
        } else {
            this.outboundHandler.write(delegatingChannelHandlerContext, obj, channelPromise);
        }
    }
}
