package okhttp3.internal.ws;

import java.io.Closeable;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.l;
import okhttp3.HttpUrl;
import okhttp3.internal.Util;
import okio.C0688f;
import okio.h;
import okio.i;

/* JADX INFO: loaded from: classes2.dex */
public final class WebSocketReader implements Closeable {
    private boolean closed;
    private final C0688f controlFrameBuffer;
    private final FrameCallback frameCallback;
    private long frameLength;
    private final boolean isClient;
    private boolean isControlFrame;
    private boolean isFinalFrame;
    private final C0688f.a maskCursor;
    private final byte[] maskKey;
    private final C0688f messageFrameBuffer;
    private MessageInflater messageInflater;
    private final boolean noContextTakeover;
    private int opcode;
    private final boolean perMessageDeflate;
    private boolean readingCompressedMessage;
    private final h source;

    public interface FrameCallback {
        void onReadClose(int i2, String str);

        void onReadMessage(String str) throws IOException;

        void onReadMessage(i iVar) throws IOException;

        void onReadPing(i iVar);

        void onReadPong(i iVar);
    }

    public WebSocketReader(boolean z2, h source, FrameCallback frameCallback, boolean z3, boolean z4) {
        l.e(source, "source");
        l.e(frameCallback, "frameCallback");
        this.isClient = z2;
        this.source = source;
        this.frameCallback = frameCallback;
        this.perMessageDeflate = z3;
        this.noContextTakeover = z4;
        this.controlFrameBuffer = new C0688f();
        this.messageFrameBuffer = new C0688f();
        this.maskKey = z2 ? null : new byte[4];
        this.maskCursor = z2 ? null : new C0688f.a();
    }

    private final void readControlFrame() throws IOException {
        short s2;
        String strC;
        long j2 = this.frameLength;
        if (j2 > 0) {
            this.source.o(this.controlFrameBuffer, j2);
            if (!this.isClient) {
                C0688f c0688f = this.controlFrameBuffer;
                C0688f.a aVar = this.maskCursor;
                l.b(aVar);
                c0688f.u(aVar);
                this.maskCursor.d(0L);
                WebSocketProtocol webSocketProtocol = WebSocketProtocol.INSTANCE;
                C0688f.a aVar2 = this.maskCursor;
                byte[] bArr = this.maskKey;
                l.b(bArr);
                webSocketProtocol.toggleMask(aVar2, bArr);
                this.maskCursor.close();
            }
        }
        switch (this.opcode) {
            case 8:
                long jG = this.controlFrameBuffer.G();
                if (jG == 1) {
                    throw new ProtocolException("Malformed close payload length of 1.");
                }
                if (jG != 0) {
                    s2 = this.controlFrameBuffer.readShort();
                    strC = this.controlFrameBuffer.C();
                    String strCloseCodeExceptionMessage = WebSocketProtocol.INSTANCE.closeCodeExceptionMessage(s2);
                    if (strCloseCodeExceptionMessage != null) {
                        throw new ProtocolException(strCloseCodeExceptionMessage);
                    }
                } else {
                    s2 = 1005;
                    strC = HttpUrl.FRAGMENT_ENCODE_SET;
                }
                this.frameCallback.onReadClose(s2, strC);
                this.closed = true;
                return;
            case 9:
                this.frameCallback.onReadPing(this.controlFrameBuffer.B());
                return;
            case 10:
                this.frameCallback.onReadPong(this.controlFrameBuffer.B());
                return;
            default:
                throw new ProtocolException("Unknown control opcode: " + Util.toHexString(this.opcode));
        }
    }

    private final void readHeader() throws IOException {
        boolean z2;
        if (this.closed) {
            throw new IOException("closed");
        }
        long jTimeoutNanos = this.source.timeout().timeoutNanos();
        this.source.timeout().clearTimeout();
        try {
            int iAnd = Util.and(this.source.readByte(), 255);
            this.source.timeout().timeout(jTimeoutNanos, TimeUnit.NANOSECONDS);
            int i2 = iAnd & 15;
            this.opcode = i2;
            boolean z3 = (iAnd & 128) != 0;
            this.isFinalFrame = z3;
            boolean z4 = (iAnd & 8) != 0;
            this.isControlFrame = z4;
            if (z4 && !z3) {
                throw new ProtocolException("Control frames must be final.");
            }
            boolean z5 = (iAnd & 64) != 0;
            if (i2 == 1 || i2 == 2) {
                if (!z5) {
                    z2 = false;
                } else {
                    if (!this.perMessageDeflate) {
                        throw new ProtocolException("Unexpected rsv1 flag");
                    }
                    z2 = true;
                }
                this.readingCompressedMessage = z2;
            } else if (z5) {
                throw new ProtocolException("Unexpected rsv1 flag");
            }
            if ((iAnd & 32) != 0) {
                throw new ProtocolException("Unexpected rsv2 flag");
            }
            if ((iAnd & 16) != 0) {
                throw new ProtocolException("Unexpected rsv3 flag");
            }
            int iAnd2 = Util.and(this.source.readByte(), 255);
            boolean z6 = (iAnd2 & 128) != 0;
            if (z6 == this.isClient) {
                throw new ProtocolException(this.isClient ? "Server-sent frames must not be masked." : "Client-sent frames must be masked.");
            }
            long j2 = iAnd2 & 127;
            this.frameLength = j2;
            if (j2 == 126) {
                this.frameLength = Util.and(this.source.readShort(), 65535);
            } else if (j2 == 127) {
                long j3 = this.source.readLong();
                this.frameLength = j3;
                if (j3 < 0) {
                    throw new ProtocolException("Frame length 0x" + Util.toHexString(this.frameLength) + " > 0x7FFFFFFFFFFFFFFF");
                }
            }
            if (this.isControlFrame && this.frameLength > 125) {
                throw new ProtocolException("Control frame must be less than 125B.");
            }
            if (z6) {
                h hVar = this.source;
                byte[] bArr = this.maskKey;
                l.b(bArr);
                hVar.readFully(bArr);
            }
        } catch (Throwable th) {
            this.source.timeout().timeout(jTimeoutNanos, TimeUnit.NANOSECONDS);
            throw th;
        }
    }

    private final void readMessage() throws IOException {
        while (!this.closed) {
            long j2 = this.frameLength;
            if (j2 > 0) {
                this.source.o(this.messageFrameBuffer, j2);
                if (!this.isClient) {
                    C0688f c0688f = this.messageFrameBuffer;
                    C0688f.a aVar = this.maskCursor;
                    l.b(aVar);
                    c0688f.u(aVar);
                    this.maskCursor.d(this.messageFrameBuffer.G() - this.frameLength);
                    WebSocketProtocol webSocketProtocol = WebSocketProtocol.INSTANCE;
                    C0688f.a aVar2 = this.maskCursor;
                    byte[] bArr = this.maskKey;
                    l.b(bArr);
                    webSocketProtocol.toggleMask(aVar2, bArr);
                    this.maskCursor.close();
                }
            }
            if (this.isFinalFrame) {
                return;
            }
            readUntilNonControlFrame();
            if (this.opcode != 0) {
                throw new ProtocolException("Expected continuation opcode. Got: " + Util.toHexString(this.opcode));
            }
        }
        throw new IOException("closed");
    }

    private final void readMessageFrame() throws IOException {
        int i2 = this.opcode;
        if (i2 != 1 && i2 != 2) {
            throw new ProtocolException("Unknown opcode: " + Util.toHexString(i2));
        }
        readMessage();
        if (this.readingCompressedMessage) {
            MessageInflater messageInflater = this.messageInflater;
            if (messageInflater == null) {
                messageInflater = new MessageInflater(this.noContextTakeover);
                this.messageInflater = messageInflater;
            }
            messageInflater.inflate(this.messageFrameBuffer);
        }
        if (i2 == 1) {
            this.frameCallback.onReadMessage(this.messageFrameBuffer.C());
        } else {
            this.frameCallback.onReadMessage(this.messageFrameBuffer.B());
        }
    }

    private final void readUntilNonControlFrame() throws IOException {
        while (!this.closed) {
            readHeader();
            if (!this.isControlFrame) {
                return;
            } else {
                readControlFrame();
            }
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        MessageInflater messageInflater = this.messageInflater;
        if (messageInflater != null) {
            messageInflater.close();
        }
    }

    public final h getSource() {
        return this.source;
    }

    public final void processNextFrame() throws IOException {
        readHeader();
        if (this.isControlFrame) {
            readControlFrame();
        } else {
            readMessageFrame();
        }
    }
}
