package io.netty.bootstrap;

import io.netty.channel.Channel;
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel> {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) ServerBootstrap.class);
    private final Map<AttributeKey<?>, Object> childAttrs;
    private volatile EventLoopGroup childGroup;
    private volatile ChannelHandler childHandler;
    private final Map<ChannelOption<?>, Object> childOptions;
    private final ServerBootstrapConfig config;

    private static class ServerBootstrapAcceptor extends ChannelInboundHandlerAdapter {
        private final Map.Entry<AttributeKey<?>, Object>[] childAttrs;
        private final EventLoopGroup childGroup;
        private final ChannelHandler childHandler;
        private final Map.Entry<ChannelOption<?>, Object>[] childOptions;

        ServerBootstrapAcceptor(EventLoopGroup eventLoopGroup, ChannelHandler channelHandler, Map.Entry<ChannelOption<?>, Object>[] entryArr, Map.Entry<AttributeKey<?>, Object>[] entryArr2) {
            this.childGroup = eventLoopGroup;
            this.childHandler = channelHandler;
            this.childOptions = entryArr;
            this.childAttrs = entryArr2;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static void forceClose(Channel channel, Throwable th) {
            channel.unsafe().closeForcibly();
            ServerBootstrap.logger.warn("Failed to register an accepted channel: " + channel, th);
        }

        @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
        public void channelRead(ChannelHandlerContext channelHandlerContext, Object obj) {
            final Channel channel = (Channel) obj;
            channel.pipeline().addLast(this.childHandler);
            for (Map.Entry<ChannelOption<?>, Object> entry : this.childOptions) {
                try {
                    if (!channel.config().setOption(entry.getKey(), entry.getValue())) {
                        ServerBootstrap.logger.warn("Unknown channel option: " + entry);
                    }
                } catch (Throwable th) {
                    ServerBootstrap.logger.warn("Failed to set a channel option: " + channel, th);
                }
            }
            for (Map.Entry<AttributeKey<?>, Object> entry2 : this.childAttrs) {
                channel.attr(entry2.getKey()).set(entry2.getValue());
            }
            try {
                this.childGroup.register(channel).addListener((GenericFutureListener<? extends Future<? super Void>>) new ChannelFutureListener() { // from class: io.netty.bootstrap.ServerBootstrap.ServerBootstrapAcceptor.1
                    @Override // io.netty.util.concurrent.GenericFutureListener
                    public void operationComplete(ChannelFuture channelFuture) throws Exception {
                        if (channelFuture.isSuccess()) {
                            return;
                        }
                        ServerBootstrapAcceptor.forceClose(channel, channelFuture.cause());
                    }
                });
            } catch (Throwable th2) {
                forceClose(channel, th2);
            }
        }

        @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
        public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable th) throws Exception {
            final ChannelConfig channelConfigConfig = channelHandlerContext.channel().config();
            if (channelConfigConfig.isAutoRead()) {
                channelConfigConfig.setAutoRead(false);
                channelHandlerContext.channel().eventLoop().schedule(new Runnable() { // from class: io.netty.bootstrap.ServerBootstrap.ServerBootstrapAcceptor.2
                    @Override // java.lang.Runnable
                    public void run() {
                        channelConfigConfig.setAutoRead(true);
                    }
                }, 1L, TimeUnit.SECONDS);
            }
            channelHandlerContext.fireExceptionCaught(th);
        }
    }

    public ServerBootstrap() {
        this.childOptions = new LinkedHashMap();
        this.childAttrs = new LinkedHashMap();
        this.config = new ServerBootstrapConfig(this);
    }

    private static Map.Entry<AttributeKey<?>, Object>[] newAttrArray(int i) {
        return new Map.Entry[i];
    }

    private static Map.Entry<ChannelOption<?>, Object>[] newOptionArray(int i) {
        return new Map.Entry[i];
    }

    public <T> ServerBootstrap childAttr(AttributeKey<T> attributeKey, T t) {
        Objects.requireNonNull(attributeKey, "childKey");
        if (t == null) {
            this.childAttrs.remove(attributeKey);
        } else {
            this.childAttrs.put(attributeKey, t);
        }
        return this;
    }

    final Map<AttributeKey<?>, Object> childAttrs() {
        return AbstractBootstrap.copiedMap(this.childAttrs);
    }

    @Deprecated
    public EventLoopGroup childGroup() {
        return this.childGroup;
    }

    public ServerBootstrap childHandler(ChannelHandler channelHandler) {
        Objects.requireNonNull(channelHandler, "childHandler");
        this.childHandler = channelHandler;
        return this;
    }

    public <T> ServerBootstrap childOption(ChannelOption<T> channelOption, T t) {
        Objects.requireNonNull(channelOption, "childOption");
        if (t == null) {
            synchronized (this.childOptions) {
                this.childOptions.remove(channelOption);
            }
        } else {
            synchronized (this.childOptions) {
                this.childOptions.put(channelOption, t);
            }
        }
        return this;
    }

    final Map<ChannelOption<?>, Object> childOptions() {
        return AbstractBootstrap.copiedMap(this.childOptions);
    }

    @Override // io.netty.bootstrap.AbstractBootstrap
    void init(Channel channel) throws Exception {
        final Map.Entry[] entryArr;
        final Map.Entry[] entryArr2;
        Map<ChannelOption<?>, ?> mapOptions0 = options0();
        synchronized (mapOptions0) {
            channel.config().setOptions(mapOptions0);
        }
        Map<AttributeKey<?>, Object> mapAttrs0 = attrs0();
        synchronized (mapAttrs0) {
            for (Map.Entry<AttributeKey<?>, Object> entry : mapAttrs0.entrySet()) {
                channel.attr(entry.getKey()).set(entry.getValue());
            }
        }
        ChannelPipeline channelPipelinePipeline = channel.pipeline();
        final EventLoopGroup eventLoopGroup = this.childGroup;
        final ChannelHandler channelHandler = this.childHandler;
        synchronized (this.childOptions) {
            entryArr = (Map.Entry[]) this.childOptions.entrySet().toArray(newOptionArray(this.childOptions.size()));
        }
        synchronized (this.childAttrs) {
            entryArr2 = (Map.Entry[]) this.childAttrs.entrySet().toArray(newAttrArray(this.childAttrs.size()));
        }
        channelPipelinePipeline.addLast(new ChannelInitializer<Channel>() { // from class: io.netty.bootstrap.ServerBootstrap.1
            @Override // io.netty.channel.ChannelInitializer
            public void initChannel(Channel channel2) throws Exception {
                ChannelPipeline channelPipelinePipeline2 = channel2.pipeline();
                ChannelHandler channelHandlerHandler = ServerBootstrap.this.config.handler();
                if (channelHandlerHandler != null) {
                    channelPipelinePipeline2.addLast(channelHandlerHandler);
                }
                channelPipelinePipeline2.addLast(new ServerBootstrapAcceptor(eventLoopGroup, channelHandler, entryArr, entryArr2));
            }
        });
    }

    @Override // io.netty.bootstrap.AbstractBootstrap
    public final ServerBootstrapConfig config() {
        return this.config;
    }

    @Override // io.netty.bootstrap.AbstractBootstrap
    public ServerBootstrap group(EventLoopGroup eventLoopGroup) {
        return group(eventLoopGroup, eventLoopGroup);
    }

    @Override // io.netty.bootstrap.AbstractBootstrap
    public ServerBootstrap validate() {
        super.validate();
        if (this.childHandler == null) {
            throw new IllegalStateException("childHandler not set");
        }
        if (this.childGroup == null) {
            logger.warn("childGroup is not set. Using parentGroup instead.");
            this.childGroup = this.config.group();
        }
        return this;
    }

    final ChannelHandler childHandler() {
        return this.childHandler;
    }

    @Override // io.netty.bootstrap.AbstractBootstrap
    /* JADX INFO: renamed from: clone */
    public ServerBootstrap mo4clone() {
        return new ServerBootstrap(this);
    }

    public ServerBootstrap group(EventLoopGroup eventLoopGroup, EventLoopGroup eventLoopGroup2) {
        super.group(eventLoopGroup);
        Objects.requireNonNull(eventLoopGroup2, "childGroup");
        if (this.childGroup == null) {
            this.childGroup = eventLoopGroup2;
            return this;
        }
        throw new IllegalStateException("childGroup set already");
    }

    private ServerBootstrap(ServerBootstrap serverBootstrap) {
        super(serverBootstrap);
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        this.childOptions = linkedHashMap;
        LinkedHashMap linkedHashMap2 = new LinkedHashMap();
        this.childAttrs = linkedHashMap2;
        this.config = new ServerBootstrapConfig(this);
        this.childGroup = serverBootstrap.childGroup;
        this.childHandler = serverBootstrap.childHandler;
        synchronized (serverBootstrap.childOptions) {
            linkedHashMap.putAll(serverBootstrap.childOptions);
        }
        synchronized (serverBootstrap.childAttrs) {
            linkedHashMap2.putAll(serverBootstrap.childAttrs);
        }
    }
}
