package okhttp3.internal.ws;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlin.Metadata(d1 = {"\u0000P\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0012\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0002\b\u0004\n\u0002\u0010\u0002\n\u0002\b\b\u0018\u00002\u00020\u0001:\u0001&B-\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007\u0012\u0006\u0010\b\u001a\u00020\u0003\u0012\u0006\u0010\t\u001a\u00020\u0003¢\u0006\u0002\u0010\nJ\b\u0010\u001e\u001a\u00020\u001fH\u0016J\u0006\u0010 \u001a\u00020\u001fJ\b\u0010!\u001a\u00020\u001fH\u0002J\b\u0010\"\u001a\u00020\u001fH\u0002J\b\u0010#\u001a\u00020\u001fH\u0002J\b\u0010$\u001a\u00020\u001fH\u0002J\b\u0010%\u001a\u00020\u001fH\u0002R\u000e\u0010\u000b\u001a\u00020\u0003X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\f\u001a\u00020\rX\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u000e\u001a\u00020\u000fX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0010\u001a\u00020\u0003X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0011\u001a\u00020\u0003X\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0012\u001a\u0004\u0018\u00010\u0013X\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u0014\u001a\u0004\u0018\u00010\u0015X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0016\u001a\u00020\rX\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u0017\u001a\u0004\u0018\u00010\u0018X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0019\u001a\u00020\u001aX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u001b\u001a\u00020\u0003X\u0082\u000e¢\u0006\u0002\n\u0000R\u0011\u0010\u0004\u001a\u00020\u0005¢\u0006\b\n\u0000\u001a\u0004\b\u001c\u0010\u001d¨\u0006'"}, d2 = {"Lokhttp3/internal/ws/WebSocketReader;", "Ljava/io/Closeable;", "isClient", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "source", "Lokio/BufferedSource;", "frameCallback", "Lokhttp3/internal/ws/WebSocketReader$FrameCallback;", "perMessageDeflate", "noContextTakeover", "(ZLokio/BufferedSource;Lokhttp3/internal/ws/WebSocketReader$FrameCallback;ZZ)V", "closed", "controlFrameBuffer", "Lokio/Buffer;", "frameLength", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "isControlFrame", "isFinalFrame", "maskCursor", "Lokio/Buffer$UnsafeCursor;", "maskKey", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "messageFrameBuffer", "messageInflater", "Lokhttp3/internal/ws/MessageInflater;", "opcode", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "readingCompressedMessage", "getSource", "()Lokio/BufferedSource;", "close", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "processNextFrame", "readControlFrame", "readHeader", "readMessage", "readMessageFrame", "readUntilNonControlFrame", "FrameCallback", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public final class WebSocketReader implements java.io.Closeable {
    private boolean closed;

    @org.jetbrains.annotations.NotNull
    private final okio.Buffer controlFrameBuffer;

    @org.jetbrains.annotations.NotNull
    private final okhttp3.internal.ws.WebSocketReader.FrameCallback frameCallback;
    private long frameLength;
    private final boolean isClient;
    private boolean isControlFrame;
    private boolean isFinalFrame;

    @org.jetbrains.annotations.Nullable
    private final okio.Buffer.UnsafeCursor maskCursor;

    @org.jetbrains.annotations.Nullable
    private final byte[] maskKey;

    @org.jetbrains.annotations.NotNull
    private final okio.Buffer messageFrameBuffer;

    @org.jetbrains.annotations.Nullable
    private okhttp3.internal.ws.MessageInflater messageInflater;
    private final boolean noContextTakeover;
    private int opcode;
    private final boolean perMessageDeflate;
    private boolean readingCompressedMessage;

    @org.jetbrains.annotations.NotNull
    private final okio.BufferedSource source;

    /* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
    @kotlin.Metadata(d1 = {"\u0000&\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0004\bf\u0018\u00002\u00020\u0001J\u0018\u0010\u0002\u001a\u00020\u00032\u0006\u0010\u0004\u001a\u00020\u00052\u0006\u0010\u0006\u001a\u00020\u0007H&J\u0010\u0010\b\u001a\u00020\u00032\u0006\u0010\t\u001a\u00020\u0007H&J\u0010\u0010\b\u001a\u00020\u00032\u0006\u0010\n\u001a\u00020\u000bH&J\u0010\u0010\f\u001a\u00020\u00032\u0006\u0010\r\u001a\u00020\u000bH&J\u0010\u0010\u000e\u001a\u00020\u00032\u0006\u0010\r\u001a\u00020\u000bH&¨\u0006\u000f"}, d2 = {"Lokhttp3/internal/ws/WebSocketReader$FrameCallback;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "onReadClose", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "code", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "reason", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "onReadMessage", "text", "bytes", "Lokio/ByteString;", "onReadPing", "payload", "onReadPong", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = 48)
    public interface FrameCallback {
        void onReadClose(int code, @org.jetbrains.annotations.NotNull java.lang.String reason);

        void onReadMessage(@org.jetbrains.annotations.NotNull java.lang.String text) throws java.io.IOException;

        void onReadMessage(@org.jetbrains.annotations.NotNull okio.ByteString bytes) throws java.io.IOException;

        void onReadPing(@org.jetbrains.annotations.NotNull okio.ByteString payload);

        void onReadPong(@org.jetbrains.annotations.NotNull okio.ByteString payload);
    }

    public WebSocketReader(boolean z, @org.jetbrains.annotations.NotNull okio.BufferedSource bufferedSource, @org.jetbrains.annotations.NotNull okhttp3.internal.ws.WebSocketReader.FrameCallback frameCallback, boolean z4, boolean z5) {
        kotlin.jvm.internal.Intrinsics.l(bufferedSource, "source");
        kotlin.jvm.internal.Intrinsics.l(frameCallback, "frameCallback");
        this.isClient = z;
        this.source = bufferedSource;
        this.frameCallback = frameCallback;
        this.perMessageDeflate = z4;
        this.noContextTakeover = z5;
        this.controlFrameBuffer = new okio.Buffer();
        this.messageFrameBuffer = new okio.Buffer();
        this.maskKey = z ? null : new byte[4];
        this.maskCursor = z ? null : new okio.Buffer.UnsafeCursor();
    }

    private final void readControlFrame() throws java.io.IOException {
        short s;
        java.lang.String strC0;
        long j = this.frameLength;
        if (j > 0) {
            this.source.s1(this.controlFrameBuffer, j);
            if (!this.isClient) {
                okio.Buffer buffer = this.controlFrameBuffer;
                okio.Buffer.UnsafeCursor unsafeCursor = this.maskCursor;
                kotlin.jvm.internal.Intrinsics.i(unsafeCursor);
                buffer.X(unsafeCursor);
                this.maskCursor.i(0L);
                okhttp3.internal.ws.WebSocketProtocol webSocketProtocol = okhttp3.internal.ws.WebSocketProtocol.INSTANCE;
                okio.Buffer.UnsafeCursor unsafeCursor2 = this.maskCursor;
                byte[] bArr = this.maskKey;
                kotlin.jvm.internal.Intrinsics.i(bArr);
                webSocketProtocol.toggleMask(unsafeCursor2, bArr);
                this.maskCursor.close();
            }
        }
        switch (this.opcode) {
            case 8:
                long size = this.controlFrameBuffer.getSize();
                if (size == 1) {
                    throw new java.net.ProtocolException("Malformed close payload length of 1.");
                }
                if (size != 0) {
                    s = this.controlFrameBuffer.readShort();
                    strC0 = this.controlFrameBuffer.c0();
                    java.lang.String strCloseCodeExceptionMessage = okhttp3.internal.ws.WebSocketProtocol.INSTANCE.closeCodeExceptionMessage(s);
                    if (strCloseCodeExceptionMessage != null) {
                        throw new java.net.ProtocolException(strCloseCodeExceptionMessage);
                    }
                } else {
                    s = 1005;
                    strC0 = okhttp3.HttpUrl.FRAGMENT_ENCODE_SET;
                }
                this.frameCallback.onReadClose(s, strC0);
                this.closed = true;
                return;
            case 9:
                this.frameCallback.onReadPing(this.controlFrameBuffer.N0());
                return;
            case 10:
                this.frameCallback.onReadPong(this.controlFrameBuffer.N0());
                return;
            default:
                throw new java.net.ProtocolException("Unknown control opcode: " + okhttp3.internal.Util.toHexString(this.opcode));
        }
    }

    private final void readHeader() throws java.io.IOException {
        boolean z;
        if (this.closed) {
            throw new java.io.IOException("closed");
        }
        long timeoutNanos = this.source.getTimeout().getTimeoutNanos();
        this.source.getTimeout().clearTimeout();
        try {
            int iAnd = okhttp3.internal.Util.and(this.source.readByte(), 255);
            this.source.getTimeout().timeout(timeoutNanos, java.util.concurrent.TimeUnit.NANOSECONDS);
            int i = iAnd & 15;
            this.opcode = i;
            boolean z4 = (iAnd & 128) != 0;
            this.isFinalFrame = z4;
            boolean z5 = (iAnd & 8) != 0;
            this.isControlFrame = z5;
            if (z5 && !z4) {
                throw new java.net.ProtocolException("Control frames must be final.");
            }
            boolean z6 = (iAnd & 64) != 0;
            if (i == 1 || i == 2) {
                if (!z6) {
                    z = false;
                } else {
                    if (!this.perMessageDeflate) {
                        throw new java.net.ProtocolException("Unexpected rsv1 flag");
                    }
                    z = true;
                }
                this.readingCompressedMessage = z;
            } else if (z6) {
                throw new java.net.ProtocolException("Unexpected rsv1 flag");
            }
            if ((iAnd & 32) != 0) {
                throw new java.net.ProtocolException("Unexpected rsv2 flag");
            }
            if ((iAnd & 16) != 0) {
                throw new java.net.ProtocolException("Unexpected rsv3 flag");
            }
            int iAnd2 = okhttp3.internal.Util.and(this.source.readByte(), 255);
            boolean z7 = (iAnd2 & 128) != 0;
            if (z7 == this.isClient) {
                throw new java.net.ProtocolException(this.isClient ? "Server-sent frames must not be masked." : "Client-sent frames must be masked.");
            }
            long j = iAnd2 & 127;
            this.frameLength = j;
            if (j == 126) {
                this.frameLength = okhttp3.internal.Util.and(this.source.readShort(), okhttp3.internal.http2.Settings.DEFAULT_INITIAL_WINDOW_SIZE);
            } else if (j == 127) {
                long j2 = this.source.readLong();
                this.frameLength = j2;
                if (j2 < 0) {
                    throw new java.net.ProtocolException("Frame length 0x" + okhttp3.internal.Util.toHexString(this.frameLength) + " > 0x7FFFFFFFFFFFFFFF");
                }
            }
            if (this.isControlFrame && this.frameLength > 125) {
                throw new java.net.ProtocolException("Control frame must be less than 125B.");
            }
            if (z7) {
                okio.BufferedSource bufferedSource = this.source;
                byte[] bArr = this.maskKey;
                kotlin.jvm.internal.Intrinsics.i(bArr);
                bufferedSource.readFully(bArr);
            }
        } catch (java.lang.Throwable th) {
            this.source.getTimeout().timeout(timeoutNanos, java.util.concurrent.TimeUnit.NANOSECONDS);
            throw th;
        }
    }

    private final void readMessage() throws java.io.IOException {
        while (!this.closed) {
            long j = this.frameLength;
            if (j > 0) {
                this.source.s1(this.messageFrameBuffer, j);
                if (!this.isClient) {
                    okio.Buffer buffer = this.messageFrameBuffer;
                    okio.Buffer.UnsafeCursor unsafeCursor = this.maskCursor;
                    kotlin.jvm.internal.Intrinsics.i(unsafeCursor);
                    buffer.X(unsafeCursor);
                    this.maskCursor.i(this.messageFrameBuffer.getSize() - this.frameLength);
                    okhttp3.internal.ws.WebSocketProtocol webSocketProtocol = okhttp3.internal.ws.WebSocketProtocol.INSTANCE;
                    okio.Buffer.UnsafeCursor unsafeCursor2 = this.maskCursor;
                    byte[] bArr = this.maskKey;
                    kotlin.jvm.internal.Intrinsics.i(bArr);
                    webSocketProtocol.toggleMask(unsafeCursor2, bArr);
                    this.maskCursor.close();
                }
            }
            if (this.isFinalFrame) {
                return;
            }
            readUntilNonControlFrame();
            if (this.opcode != 0) {
                throw new java.net.ProtocolException("Expected continuation opcode. Got: " + okhttp3.internal.Util.toHexString(this.opcode));
            }
        }
        throw new java.io.IOException("closed");
    }

    private final void readMessageFrame() throws java.io.IOException {
        int i = this.opcode;
        if (i != 1 && i != 2) {
            throw new java.net.ProtocolException("Unknown opcode: " + okhttp3.internal.Util.toHexString(i));
        }
        readMessage();
        if (this.readingCompressedMessage) {
            okhttp3.internal.ws.MessageInflater messageInflater = this.messageInflater;
            if (messageInflater == null) {
                messageInflater = new okhttp3.internal.ws.MessageInflater(this.noContextTakeover);
                this.messageInflater = messageInflater;
            }
            messageInflater.inflate(this.messageFrameBuffer);
        }
        if (i == 1) {
            this.frameCallback.onReadMessage(this.messageFrameBuffer.c0());
        } else {
            this.frameCallback.onReadMessage(this.messageFrameBuffer.N0());
        }
    }

    private final void readUntilNonControlFrame() throws java.io.IOException {
        while (!this.closed) {
            readHeader();
            if (!this.isControlFrame) {
                return;
            } else {
                readControlFrame();
            }
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws java.io.IOException {
        okhttp3.internal.ws.MessageInflater messageInflater = this.messageInflater;
        if (messageInflater != null) {
            messageInflater.close();
        }
    }

    @org.jetbrains.annotations.NotNull
    public final okio.BufferedSource getSource() {
        return this.source;
    }

    public final void processNextFrame() throws java.io.IOException {
        readHeader();
        if (this.isControlFrame) {
            readControlFrame();
        } else {
            readMessageFrame();
        }
    }
}
