package io.netty.channel.epoll;

import g.a.a.a.a;
import io.netty.channel.epoll.NativeDatagramPacketArray;
import io.netty.channel.unix.Errors;
import io.netty.channel.unix.FileDescriptor;
import io.netty.channel.unix.Socket;
import io.netty.util.internal.NativeLibraryLoader;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.ThrowableUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.nio.channels.ClosedChannelException;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
public final class Native {
    public static final int EPOLLERR;
    public static final int EPOLLET;
    public static final int EPOLLIN;
    public static final int EPOLLOUT;
    public static final int EPOLLRDHUP;
    public static final boolean IS_SUPPORTING_SENDMMSG;
    public static final boolean IS_SUPPORTING_TCP_FASTOPEN;
    public static final String KERNEL_VERSION;
    private static final ClosedChannelException SENDMMSG_CLOSED_CHANNEL_EXCEPTION;
    private static final Errors.NativeIoException SENDMMSG_CONNECTION_RESET_EXCEPTION;
    private static final ClosedChannelException SPLICE_CLOSED_CHANNEL_EXCEPTION;
    private static final Errors.NativeIoException SPLICE_CONNECTION_RESET_EXCEPTION;
    public static final int TCP_MD5SIG_MAXKEYLEN;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) Native.class);

    static {
        try {
            offsetofEpollData();
        } catch (UnsatisfiedLinkError unused) {
            loadNativeLibrary();
        }
        Socket.initialize();
        EPOLLIN = NativeStaticallyReferencedJniMethods.epollin();
        EPOLLOUT = NativeStaticallyReferencedJniMethods.epollout();
        EPOLLRDHUP = NativeStaticallyReferencedJniMethods.epollrdhup();
        EPOLLET = NativeStaticallyReferencedJniMethods.epollet();
        EPOLLERR = NativeStaticallyReferencedJniMethods.epollerr();
        IS_SUPPORTING_SENDMMSG = NativeStaticallyReferencedJniMethods.isSupportingSendmmsg();
        IS_SUPPORTING_TCP_FASTOPEN = NativeStaticallyReferencedJniMethods.isSupportingTcpFastopen();
        TCP_MD5SIG_MAXKEYLEN = NativeStaticallyReferencedJniMethods.tcpMd5SigMaxKeyLen();
        KERNEL_VERSION = NativeStaticallyReferencedJniMethods.kernelVersion();
        SENDMMSG_CLOSED_CHANNEL_EXCEPTION = (ClosedChannelException) a.N(Native.class, "sendmmsg(...)");
        SPLICE_CLOSED_CHANNEL_EXCEPTION = (ClosedChannelException) a.N(Native.class, "splice(...)");
        int i2 = Errors.ERRNO_EPIPE_NEGATIVE;
        SENDMMSG_CONNECTION_RESET_EXCEPTION = Errors.newConnectionResetException("syscall:sendmmsg(...)", i2);
        SPLICE_CONNECTION_RESET_EXCEPTION = Errors.newConnectionResetException("syscall:splice(...)", i2);
    }

    private Native() {
    }

    private static native int epollCreate();

    public static void epollCtlAdd(int i2, int i3, int i4) throws Errors.NativeIoException {
        int iEpollCtlAdd0 = epollCtlAdd0(i2, i3, i4);
        if (iEpollCtlAdd0 < 0) {
            throw Errors.newIOException("epoll_ctl", iEpollCtlAdd0);
        }
    }

    private static native int epollCtlAdd0(int i2, int i3, int i4);

    public static void epollCtlDel(int i2, int i3) throws Errors.NativeIoException {
        int iEpollCtlDel0 = epollCtlDel0(i2, i3);
        if (iEpollCtlDel0 < 0) {
            throw Errors.newIOException("epoll_ctl", iEpollCtlDel0);
        }
    }

    private static native int epollCtlDel0(int i2, int i3);

    public static void epollCtlMod(int i2, int i3, int i4) throws Errors.NativeIoException {
        int iEpollCtlMod0 = epollCtlMod0(i2, i3, i4);
        if (iEpollCtlMod0 < 0) {
            throw Errors.newIOException("epoll_ctl", iEpollCtlMod0);
        }
    }

    private static native int epollCtlMod0(int i2, int i3, int i4);

    public static int epollWait(FileDescriptor fileDescriptor, EpollEventArray epollEventArray, FileDescriptor fileDescriptor2, int i2, int i3) throws Errors.NativeIoException {
        int iEpollWait0 = epollWait0(fileDescriptor.intValue(), epollEventArray.memoryAddress(), epollEventArray.length(), fileDescriptor2.intValue(), i2, i3);
        if (iEpollWait0 >= 0) {
            return iEpollWait0;
        }
        throw Errors.newIOException("epoll_wait", iEpollWait0);
    }

    private static native int epollWait0(int i2, long j2, int i3, int i4, int i5, int i6);

    private static native int eventFd();

    public static native void eventFdRead(int i2);

    public static native void eventFdWrite(int i2, long j2);

    private static void loadNativeLibrary() throws Throwable {
        if (!SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim().startsWith("linux")) {
            throw new IllegalStateException("Only supported on Linux");
        }
        String str = "netty_transport_native_epoll_" + PlatformDependent.normalizedArch();
        ClassLoader classLoader = PlatformDependent.getClassLoader(Native.class);
        try {
            NativeLibraryLoader.load(str, classLoader);
        } catch (UnsatisfiedLinkError e2) {
            try {
                NativeLibraryLoader.load("netty_transport_native_epoll", classLoader);
                logger.debug("Failed to load {}", str, e2);
            } catch (UnsatisfiedLinkError e3) {
                ThrowableUtil.addSuppressed(e2, e3);
                throw e2;
            }
        }
    }

    public static FileDescriptor newEpollCreate() {
        return new FileDescriptor(epollCreate());
    }

    public static FileDescriptor newEventFd() {
        return new FileDescriptor(eventFd());
    }

    public static FileDescriptor newTimerFd() {
        return new FileDescriptor(timerFd());
    }

    public static native int offsetofEpollData();

    public static int sendmmsg(int i2, NativeDatagramPacketArray.NativeDatagramPacket[] nativeDatagramPacketArr, int i3, int i4) {
        int iSendmmsg0 = sendmmsg0(i2, nativeDatagramPacketArr, i3, i4);
        return iSendmmsg0 >= 0 ? iSendmmsg0 : Errors.ioResult("sendmmsg", iSendmmsg0, SENDMMSG_CONNECTION_RESET_EXCEPTION, SENDMMSG_CLOSED_CHANNEL_EXCEPTION);
    }

    private static native int sendmmsg0(int i2, NativeDatagramPacketArray.NativeDatagramPacket[] nativeDatagramPacketArr, int i3, int i4);

    public static native int sizeofEpollEvent();

    public static int splice(int i2, long j2, int i3, long j3, long j4) {
        int iSplice0 = splice0(i2, j2, i3, j3, j4);
        return iSplice0 >= 0 ? iSplice0 : Errors.ioResult("splice", iSplice0, SPLICE_CONNECTION_RESET_EXCEPTION, SPLICE_CLOSED_CHANNEL_EXCEPTION);
    }

    private static native int splice0(int i2, long j2, int i3, long j3, long j4);

    private static native int timerFd();

    public static native void timerFdRead(int i2);
}
