package okhttp3.internal.ws;

import d.b.a.a.a;
import g.h0.c.l;
import j.d;
import j.g;
import j.h;
import java.io.Closeable;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import jxl.SheetSettings;
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 implements Closeable {
    private boolean closed;
    private final d controlFrameBuffer;
    private final FrameCallback frameCallback;
    private long frameLength;
    private final boolean isClient;
    private boolean isControlFrame;
    private boolean isFinalFrame;
    private final d.a maskCursor;
    private final byte[] maskKey;
    private final d messageFrameBuffer;
    private MessageInflater messageInflater;
    private final boolean noContextTakeover;
    private int opcode;
    private final boolean perMessageDeflate;
    private boolean readingCompressedMessage;
    private final g source;

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

        void onReadMessage(h hVar) throws IOException;

        void onReadMessage(String str) throws IOException;

        void onReadPing(h hVar);

        void onReadPong(h hVar);
    }

    public WebSocketReader(boolean z, g gVar, FrameCallback frameCallback, boolean z2, boolean z3) {
        l.f(gVar, "source");
        l.f(frameCallback, "frameCallback");
        this.isClient = z;
        this.source = gVar;
        this.frameCallback = frameCallback;
        this.perMessageDeflate = z2;
        this.noContextTakeover = z3;
        this.controlFrameBuffer = new d();
        this.messageFrameBuffer = new d();
        this.maskKey = z ? null : new byte[4];
        this.maskCursor = z ? null : new d.a();
    }

    private final void readControlFrame() throws IOException {
        String strT;
        long j2 = this.frameLength;
        if (j2 > 0) {
            this.source.r(this.controlFrameBuffer, j2);
            if (!this.isClient) {
                d dVar = this.controlFrameBuffer;
                d.a aVar = this.maskCursor;
                l.c(aVar);
                dVar.m(aVar);
                this.maskCursor.b(0L);
                WebSocketProtocol webSocketProtocol = WebSocketProtocol.INSTANCE;
                d.a aVar2 = this.maskCursor;
                byte[] bArr = this.maskKey;
                l.c(bArr);
                webSocketProtocol.toggleMask(aVar2, bArr);
                this.maskCursor.close();
            }
        }
        switch (this.opcode) {
            case 8:
                short sZ = 1005;
                d dVar2 = this.controlFrameBuffer;
                long j3 = dVar2.f4601e;
                if (j3 == 1) {
                    throw new ProtocolException("Malformed close payload length of 1.");
                }
                if (j3 != 0) {
                    sZ = dVar2.Z();
                    strT = this.controlFrameBuffer.t();
                    String strCloseCodeExceptionMessage = WebSocketProtocol.INSTANCE.closeCodeExceptionMessage(sZ);
                    if (strCloseCodeExceptionMessage != null) {
                        throw new ProtocolException(strCloseCodeExceptionMessage);
                    }
                } else {
                    strT = "";
                }
                this.frameCallback.onReadClose(sZ, strT);
                this.closed = true;
                return;
            case 9:
                this.frameCallback.onReadPing(this.controlFrameBuffer.I());
                return;
            case 10:
                this.frameCallback.onReadPong(this.controlFrameBuffer.I());
                return;
            default:
                StringBuilder sbB = a.B("Unknown control opcode: ");
                sbB.append(Util.toHexString(this.opcode));
                throw new ProtocolException(sbB.toString());
        }
    }

    private final void readHeader() throws IOException {
        boolean z;
        if (this.closed) {
            throw new IOException("closed");
        }
        long jTimeoutNanos = this.source.timeout().timeoutNanos();
        this.source.timeout().clearTimeout();
        try {
            int iAnd = Util.and(this.source.F(), SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT);
            this.source.timeout().timeout(jTimeoutNanos, TimeUnit.NANOSECONDS);
            int i2 = iAnd & 15;
            this.opcode = i2;
            boolean z2 = (iAnd & 128) != 0;
            this.isFinalFrame = z2;
            boolean z3 = (iAnd & 8) != 0;
            this.isControlFrame = z3;
            if (z3 && !z2) {
                throw new ProtocolException("Control frames must be final.");
            }
            boolean z4 = (iAnd & 64) != 0;
            if (i2 == 1 || i2 == 2) {
                if (!z4) {
                    z = false;
                } else {
                    if (!this.perMessageDeflate) {
                        throw new ProtocolException("Unexpected rsv1 flag");
                    }
                    z = true;
                }
                this.readingCompressedMessage = z;
            } else if (z4) {
                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.F(), SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT);
            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 j2 = iAnd2 & 127;
            this.frameLength = j2;
            if (j2 == 126) {
                this.frameLength = Util.and(this.source.Z(), Settings.DEFAULT_INITIAL_WINDOW_SIZE);
            } else if (j2 == 127) {
                long j3 = this.source.j();
                this.frameLength = j3;
                if (j3 < 0) {
                    StringBuilder sbB = a.B("Frame length 0x");
                    sbB.append(Util.toHexString(this.frameLength));
                    sbB.append(" > 0x7FFFFFFFFFFFFFFF");
                    throw new ProtocolException(sbB.toString());
                }
            }
            if (this.isControlFrame && this.frameLength > 125) {
                throw new ProtocolException("Control frame must be less than 125B.");
            }
            if (z5) {
                g gVar = this.source;
                byte[] bArr = this.maskKey;
                l.c(bArr);
                gVar.H(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.r(this.messageFrameBuffer, j2);
                if (!this.isClient) {
                    d dVar = this.messageFrameBuffer;
                    d.a aVar = this.maskCursor;
                    l.c(aVar);
                    dVar.m(aVar);
                    this.maskCursor.b(this.messageFrameBuffer.f4601e - this.frameLength);
                    WebSocketProtocol webSocketProtocol = WebSocketProtocol.INSTANCE;
                    d.a aVar2 = this.maskCursor;
                    byte[] bArr = this.maskKey;
                    l.c(bArr);
                    webSocketProtocol.toggleMask(aVar2, bArr);
                    this.maskCursor.close();
                }
            }
            if (this.isFinalFrame) {
                return;
            }
            readUntilNonControlFrame();
            if (this.opcode != 0) {
                StringBuilder sbB = a.B("Expected continuation opcode. Got: ");
                sbB.append(Util.toHexString(this.opcode));
                throw new ProtocolException(sbB.toString());
            }
        }
        throw new IOException("closed");
    }

    private final void readMessageFrame() throws IOException {
        int i2 = this.opcode;
        if (i2 != 1 && i2 != 2) {
            StringBuilder sbB = a.B("Unknown opcode: ");
            sbB.append(Util.toHexString(i2));
            throw new ProtocolException(sbB.toString());
        }
        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.t());
        } else {
            this.frameCallback.onReadMessage(this.messageFrameBuffer.I());
        }
    }

    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 g getSource() {
        return this.source;
    }

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