package io.netty.handler.timeout;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public class IdleStateHandler extends ChannelDuplexHandler {
    private static final long MIN_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(1);
    private final long allIdleTimeNanos;
    volatile ScheduledFuture<?> allIdleTimeout;
    private boolean firstAllIdleEvent;
    private boolean firstReaderIdleEvent;
    private boolean firstWriterIdleEvent;
    volatile long lastReadTime;
    volatile long lastWriteTime;
    private final long readerIdleTimeNanos;
    volatile ScheduledFuture<?> readerIdleTimeout;
    private volatile boolean reading;
    private volatile int state;
    private final ChannelFutureListener writeListener;
    private final long writerIdleTimeNanos;
    volatile ScheduledFuture<?> writerIdleTimeout;

    /* JADX INFO: renamed from: io.netty.handler.timeout.IdleStateHandler$2, reason: invalid class name */
    static /* synthetic */ class AnonymousClass2 {
        static final /* synthetic */ int[] $SwitchMap$io$netty$handler$timeout$IdleState;

        static {
            int[] iArr = new int[IdleState.values().length];
            $SwitchMap$io$netty$handler$timeout$IdleState = iArr;
            try {
                iArr[IdleState.ALL_IDLE.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$timeout$IdleState[IdleState.READER_IDLE.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$timeout$IdleState[IdleState.WRITER_IDLE.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
        }
    }

    private final class AllIdleTimeoutTask implements Runnable {
        private final ChannelHandlerContext ctx;

        AllIdleTimeoutTask(ChannelHandlerContext channelHandlerContext) {
            this.ctx = channelHandlerContext;
        }

        @Override // java.lang.Runnable
        public void run() {
            if (this.ctx.channel().isOpen()) {
                long jNanoTime = IdleStateHandler.this.allIdleTimeNanos;
                if (!IdleStateHandler.this.reading) {
                    jNanoTime -= System.nanoTime() - Math.max(IdleStateHandler.this.lastReadTime, IdleStateHandler.this.lastWriteTime);
                }
                if (jNanoTime > 0) {
                    IdleStateHandler.this.allIdleTimeout = this.ctx.executor().schedule((Runnable) this, jNanoTime, TimeUnit.NANOSECONDS);
                    return;
                }
                IdleStateHandler.this.allIdleTimeout = this.ctx.executor().schedule((Runnable) this, IdleStateHandler.this.allIdleTimeNanos, TimeUnit.NANOSECONDS);
                try {
                    IdleStateEvent idleStateEventNewIdleStateEvent = IdleStateHandler.this.newIdleStateEvent(IdleState.ALL_IDLE, IdleStateHandler.this.firstAllIdleEvent);
                    if (IdleStateHandler.this.firstAllIdleEvent) {
                        IdleStateHandler.this.firstAllIdleEvent = false;
                    }
                    IdleStateHandler.this.channelIdle(this.ctx, idleStateEventNewIdleStateEvent);
                } catch (Throwable th) {
                    this.ctx.fireExceptionCaught(th);
                }
            }
        }
    }

    private final class ReaderIdleTimeoutTask implements Runnable {
        private final ChannelHandlerContext ctx;

        ReaderIdleTimeoutTask(ChannelHandlerContext channelHandlerContext) {
            this.ctx = channelHandlerContext;
        }

        @Override // java.lang.Runnable
        public void run() {
            if (this.ctx.channel().isOpen()) {
                long jNanoTime = IdleStateHandler.this.readerIdleTimeNanos;
                if (!IdleStateHandler.this.reading) {
                    jNanoTime -= System.nanoTime() - IdleStateHandler.this.lastReadTime;
                }
                if (jNanoTime > 0) {
                    IdleStateHandler.this.readerIdleTimeout = this.ctx.executor().schedule((Runnable) this, jNanoTime, TimeUnit.NANOSECONDS);
                    return;
                }
                IdleStateHandler.this.readerIdleTimeout = this.ctx.executor().schedule((Runnable) this, IdleStateHandler.this.readerIdleTimeNanos, TimeUnit.NANOSECONDS);
                try {
                    IdleStateEvent idleStateEventNewIdleStateEvent = IdleStateHandler.this.newIdleStateEvent(IdleState.READER_IDLE, IdleStateHandler.this.firstReaderIdleEvent);
                    if (IdleStateHandler.this.firstReaderIdleEvent) {
                        IdleStateHandler.this.firstReaderIdleEvent = false;
                    }
                    IdleStateHandler.this.channelIdle(this.ctx, idleStateEventNewIdleStateEvent);
                } catch (Throwable th) {
                    this.ctx.fireExceptionCaught(th);
                }
            }
        }
    }

    private final class WriterIdleTimeoutTask implements Runnable {
        private final ChannelHandlerContext ctx;

        WriterIdleTimeoutTask(ChannelHandlerContext channelHandlerContext) {
            this.ctx = channelHandlerContext;
        }

        @Override // java.lang.Runnable
        public void run() {
            if (this.ctx.channel().isOpen()) {
                long jNanoTime = IdleStateHandler.this.writerIdleTimeNanos - (System.nanoTime() - IdleStateHandler.this.lastWriteTime);
                if (jNanoTime > 0) {
                    IdleStateHandler.this.writerIdleTimeout = this.ctx.executor().schedule((Runnable) this, jNanoTime, TimeUnit.NANOSECONDS);
                    return;
                }
                IdleStateHandler.this.writerIdleTimeout = this.ctx.executor().schedule((Runnable) this, IdleStateHandler.this.writerIdleTimeNanos, TimeUnit.NANOSECONDS);
                try {
                    IdleStateEvent idleStateEventNewIdleStateEvent = IdleStateHandler.this.newIdleStateEvent(IdleState.WRITER_IDLE, IdleStateHandler.this.firstWriterIdleEvent);
                    if (IdleStateHandler.this.firstWriterIdleEvent) {
                        IdleStateHandler.this.firstWriterIdleEvent = false;
                    }
                    IdleStateHandler.this.channelIdle(this.ctx, idleStateEventNewIdleStateEvent);
                } catch (Throwable th) {
                    this.ctx.fireExceptionCaught(th);
                }
            }
        }
    }

    public IdleStateHandler(int i2, int i3, int i4) {
        this(i2, i3, i4, TimeUnit.SECONDS);
    }

    private void destroy() {
        this.state = 2;
        if (this.readerIdleTimeout != null) {
            this.readerIdleTimeout.cancel(false);
            this.readerIdleTimeout = null;
        }
        if (this.writerIdleTimeout != null) {
            this.writerIdleTimeout.cancel(false);
            this.writerIdleTimeout = null;
        }
        if (this.allIdleTimeout != null) {
            this.allIdleTimeout.cancel(false);
            this.allIdleTimeout = null;
        }
    }

    private void initialize(ChannelHandlerContext channelHandlerContext) {
        int i2 = this.state;
        if (i2 == 1 || i2 == 2) {
            return;
        }
        this.state = 1;
        EventExecutor eventExecutorExecutor = channelHandlerContext.executor();
        long jNanoTime = System.nanoTime();
        this.lastWriteTime = jNanoTime;
        this.lastReadTime = jNanoTime;
        if (this.readerIdleTimeNanos > 0) {
            this.readerIdleTimeout = eventExecutorExecutor.schedule((Runnable) new ReaderIdleTimeoutTask(channelHandlerContext), this.readerIdleTimeNanos, TimeUnit.NANOSECONDS);
        }
        if (this.writerIdleTimeNanos > 0) {
            this.writerIdleTimeout = eventExecutorExecutor.schedule((Runnable) new WriterIdleTimeoutTask(channelHandlerContext), this.writerIdleTimeNanos, TimeUnit.NANOSECONDS);
        }
        if (this.allIdleTimeNanos > 0) {
            this.allIdleTimeout = eventExecutorExecutor.schedule((Runnable) new AllIdleTimeoutTask(channelHandlerContext), this.allIdleTimeNanos, TimeUnit.NANOSECONDS);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {
        initialize(channelHandlerContext);
        super.channelActive(channelHandlerContext);
    }

    protected void channelIdle(ChannelHandlerContext channelHandlerContext, IdleStateEvent idleStateEvent) throws Exception {
        channelHandlerContext.fireUserEventTriggered((Object) idleStateEvent);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {
        destroy();
        super.channelInactive(channelHandlerContext);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object obj) throws Exception {
        if (this.readerIdleTimeNanos > 0 || this.allIdleTimeNanos > 0) {
            this.reading = true;
            this.firstAllIdleEvent = true;
            this.firstReaderIdleEvent = true;
        }
        channelHandlerContext.fireChannelRead(obj);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception {
        if (this.readerIdleTimeNanos > 0 || this.allIdleTimeNanos > 0) {
            this.lastReadTime = System.nanoTime();
            this.reading = false;
        }
        channelHandlerContext.fireChannelReadComplete();
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {
        if (channelHandlerContext.channel().isActive()) {
            initialize(channelHandlerContext);
        }
        super.channelRegistered(channelHandlerContext);
    }

    public long getAllIdleTimeInMillis() {
        return TimeUnit.NANOSECONDS.toMillis(this.allIdleTimeNanos);
    }

    public long getReaderIdleTimeInMillis() {
        return TimeUnit.NANOSECONDS.toMillis(this.readerIdleTimeNanos);
    }

    public long getWriterIdleTimeInMillis() {
        return TimeUnit.NANOSECONDS.toMillis(this.writerIdleTimeNanos);
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {
        if (channelHandlerContext.channel().isActive() && channelHandlerContext.channel().isRegistered()) {
            initialize(channelHandlerContext);
        }
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {
        destroy();
    }

    protected IdleStateEvent newIdleStateEvent(IdleState idleState, boolean z) {
        int i2 = AnonymousClass2.$SwitchMap$io$netty$handler$timeout$IdleState[idleState.ordinal()];
        if (i2 == 1) {
            return z ? IdleStateEvent.FIRST_ALL_IDLE_STATE_EVENT : IdleStateEvent.ALL_IDLE_STATE_EVENT;
        }
        if (i2 == 2) {
            return z ? IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT : IdleStateEvent.READER_IDLE_STATE_EVENT;
        }
        if (i2 == 3) {
            return z ? IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT : IdleStateEvent.WRITER_IDLE_STATE_EVENT;
        }
        throw new Error();
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void write(ChannelHandlerContext channelHandlerContext, Object obj, ChannelPromise channelPromise) throws Exception {
        if (this.writerIdleTimeNanos <= 0 && this.allIdleTimeNanos <= 0) {
            channelHandlerContext.write(obj, channelPromise);
            return;
        }
        ChannelPromise channelPromiseUnvoid = channelPromise.unvoid();
        channelPromiseUnvoid.addListener((GenericFutureListener<? extends Future<? super Void>>) this.writeListener);
        channelHandlerContext.write(obj, channelPromiseUnvoid);
    }

    public IdleStateHandler(long j2, long j3, long j4, TimeUnit timeUnit) {
        this.writeListener = new ChannelFutureListener() { // from class: io.netty.handler.timeout.IdleStateHandler.1
            @Override // io.netty.util.concurrent.GenericFutureListener
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                IdleStateHandler.this.lastWriteTime = System.nanoTime();
                IdleStateHandler idleStateHandler = IdleStateHandler.this;
                idleStateHandler.firstWriterIdleEvent = idleStateHandler.firstAllIdleEvent = true;
            }
        };
        this.firstReaderIdleEvent = true;
        this.firstWriterIdleEvent = true;
        this.firstAllIdleEvent = true;
        if (timeUnit == null) {
            throw new NullPointerException("unit");
        }
        if (j2 <= 0) {
            this.readerIdleTimeNanos = 0L;
        } else {
            this.readerIdleTimeNanos = Math.max(timeUnit.toNanos(j2), MIN_TIMEOUT_NANOS);
        }
        if (j3 <= 0) {
            this.writerIdleTimeNanos = 0L;
        } else {
            this.writerIdleTimeNanos = Math.max(timeUnit.toNanos(j3), MIN_TIMEOUT_NANOS);
        }
        if (j4 <= 0) {
            this.allIdleTimeNanos = 0L;
        } else {
            this.allIdleTimeNanos = Math.max(timeUnit.toNanos(j4), MIN_TIMEOUT_NANOS);
        }
    }
}
