package okhttp3.internal.ws;

import c.q.d.j;
import d.f;
import d.h;
import d.i;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import okhttp3.internal.Util;
import okhttp3.internal.http2.Settings;

/* JADX INFO: compiled from: WebSocketReader.kt */
/* JADX INFO: loaded from: classes.dex */
public final class WebSocketReader {
    private boolean closed;
    private final f controlFrameBuffer;
    private final FrameCallback frameCallback;
    private long frameLength;
    private final boolean isClient;
    private boolean isControlFrame;
    private boolean isFinalFrame;
    private final f.a maskCursor;
    private final byte[] maskKey;
    private final f messageFrameBuffer;
    private int opcode;
    private final h source;

    /* JADX INFO: compiled from: WebSocketReader.kt */
    public interface FrameCallback {
        void onReadClose(int i, String str);

        void onReadMessage(i iVar) throws IOException;

        void onReadMessage(String str) throws IOException;

        void onReadPing(i iVar);

        void onReadPong(i iVar);
    }

    public WebSocketReader(boolean z, h hVar, FrameCallback frameCallback) {
        j.c(hVar, "source");
        j.c(frameCallback, "frameCallback");
        this.isClient = z;
        this.source = hVar;
        this.frameCallback = frameCallback;
        this.controlFrameBuffer = new f();
        this.messageFrameBuffer = new f();
        this.maskKey = this.isClient ? null : new byte[4];
        this.maskCursor = this.isClient ? null : new f.a();
    }

    private final void readControlFrame() throws IOException {
        String strV;
        long j = this.frameLength;
        if (j > 0) {
            this.source.e(this.controlFrameBuffer, j);
            if (!this.isClient) {
                f fVar = this.controlFrameBuffer;
                f.a aVar = this.maskCursor;
                if (aVar == null) {
                    j.g();
                    throw null;
                }
                fVar.Q(aVar);
                this.maskCursor.F(0L);
                WebSocketProtocol webSocketProtocol = WebSocketProtocol.INSTANCE;
                f.a aVar2 = this.maskCursor;
                byte[] bArr = this.maskKey;
                if (bArr == null) {
                    j.g();
                    throw null;
                }
                webSocketProtocol.toggleMask(aVar2, bArr);
                this.maskCursor.close();
            }
        }
        switch (this.opcode) {
            case 8:
                short s = 1005;
                long jB0 = this.controlFrameBuffer.b0();
                if (jB0 == 1) {
                    throw new ProtocolException("Malformed close payload length of 1.");
                }
                if (jB0 != 0) {
                    s = this.controlFrameBuffer.readShort();
                    strV = this.controlFrameBuffer.V();
                    String strCloseCodeExceptionMessage = WebSocketProtocol.INSTANCE.closeCodeExceptionMessage(s);
                    if (strCloseCodeExceptionMessage != null) {
                        throw new ProtocolException(strCloseCodeExceptionMessage);
                    }
                } else {
                    strV = "";
                }
                this.frameCallback.onReadClose(s, strV);
                this.closed = true;
                return;
            case 9:
                this.frameCallback.onReadPing(this.controlFrameBuffer.R());
                return;
            case 10:
                this.frameCallback.onReadPong(this.controlFrameBuffer.R());
                return;
            default:
                throw new ProtocolException("Unknown control opcode: " + Util.toHexString(this.opcode));
        }
    }

    private final void readHeader() throws IOException {
        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);
            this.opcode = iAnd & 15;
            this.isFinalFrame = (iAnd & 128) != 0;
            boolean z = (iAnd & 8) != 0;
            this.isControlFrame = z;
            if (z && !this.isFinalFrame) {
                throw new ProtocolException("Control frames must be final.");
            }
            boolean z2 = (iAnd & 64) != 0;
            boolean z3 = (iAnd & 32) != 0;
            boolean z4 = (iAnd & 16) != 0;
            if (z2 || z3 || z4) {
                throw new ProtocolException("Reserved flags are unsupported.");
            }
            int iAnd2 = Util.and(this.source.readByte(), 255);
            boolean z5 = (iAnd2 & 128) != 0;
            if (z5 == this.isClient) {
                throw new 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 == WebSocketProtocol.PAYLOAD_SHORT) {
                this.frameLength = Util.and(this.source.readShort(), Settings.DEFAULT_INITIAL_WINDOW_SIZE);
            } else if (j == 127) {
                long j2 = this.source.readLong();
                this.frameLength = j2;
                if (j2 < 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 (z5) {
                h hVar = this.source;
                byte[] bArr = this.maskKey;
                if (bArr != null) {
                    hVar.readFully(bArr);
                } else {
                    j.g();
                    throw null;
                }
            }
        } catch (Throwable th) {
            this.source.timeout().timeout(jTimeoutNanos, TimeUnit.NANOSECONDS);
            throw th;
        }
    }

    private final void readMessage() throws IOException {
        while (!this.closed) {
            long j = this.frameLength;
            if (j > 0) {
                this.source.e(this.messageFrameBuffer, j);
                if (!this.isClient) {
                    f fVar = this.messageFrameBuffer;
                    f.a aVar = this.maskCursor;
                    if (aVar == null) {
                        j.g();
                        throw null;
                    }
                    fVar.Q(aVar);
                    this.maskCursor.F(this.messageFrameBuffer.b0() - this.frameLength);
                    WebSocketProtocol webSocketProtocol = WebSocketProtocol.INSTANCE;
                    f.a aVar2 = this.maskCursor;
                    byte[] bArr = this.maskKey;
                    if (bArr == null) {
                        j.g();
                        throw null;
                    }
                    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 i = this.opcode;
        if (i != 1 && i != 2) {
            throw new ProtocolException("Unknown opcode: " + Util.toHexString(i));
        }
        readMessage();
        if (i == 1) {
            this.frameCallback.onReadMessage(this.messageFrameBuffer.V());
        } else {
            this.frameCallback.onReadMessage(this.messageFrameBuffer.R());
        }
    }

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

    public final boolean getClosed() {
        return this.closed;
    }

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

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

    public final void setClosed(boolean z) {
        this.closed = z;
    }
}
