package okhttp3.internal.ws;

import com.cvte.vman.instance.InvokeCmd;
import com.seewo.adbcommons.model.base.ICmdMsg;
import h.c;
import h.e;
import h.f;
import java.io.EOFException;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
final class WebSocketReader {
    boolean closed;
    long frameBytesRead;
    final FrameCallback frameCallback;
    long frameLength;
    final boolean isClient;
    boolean isControlFrame;
    boolean isFinalFrame;
    boolean isMasked;
    int opcode;
    final e source;
    final byte[] maskKey = new byte[4];
    final byte[] maskBuffer = new byte[InvokeCmd.CMD_TOUCH_CTRL_START];

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

        void onReadMessage(f fVar) throws IOException;

        void onReadMessage(String str) throws IOException;

        void onReadPing(f fVar);

        void onReadPong(f fVar);
    }

    WebSocketReader(boolean z, e eVar, FrameCallback frameCallback) {
        if (eVar == null) {
            throw new NullPointerException("source == null");
        }
        if (frameCallback == null) {
            throw new NullPointerException("frameCallback == null");
        }
        this.isClient = z;
        this.source = eVar;
        this.frameCallback = frameCallback;
    }

    private void readControlFrame() throws IOException {
        String strR;
        c cVar = new c();
        long j2 = this.frameBytesRead;
        long j3 = this.frameLength;
        if (j2 < j3) {
            if (!this.isClient) {
                while (true) {
                    long j4 = this.frameBytesRead;
                    long j5 = this.frameLength;
                    if (j4 >= j5) {
                        break;
                    }
                    int i2 = this.source.read(this.maskBuffer, 0, (int) Math.min(j5 - j4, this.maskBuffer.length));
                    if (i2 == -1) {
                        throw new EOFException();
                    }
                    long j6 = i2;
                    WebSocketProtocol.toggleMask(this.maskBuffer, j6, this.maskKey, this.frameBytesRead);
                    cVar.M(this.maskBuffer, 0, i2);
                    this.frameBytesRead += j6;
                }
            } else {
                this.source.G(cVar, j3);
            }
        }
        switch (this.opcode) {
            case 8:
                short s = 1005;
                long jB = cVar.B();
                if (jB == 1) {
                    throw new ProtocolException("Malformed close payload length of 1.");
                }
                if (jB != 0) {
                    s = cVar.readShort();
                    strR = cVar.r();
                    String strCloseCodeExceptionMessage = WebSocketProtocol.closeCodeExceptionMessage(s);
                    if (strCloseCodeExceptionMessage != null) {
                        throw new ProtocolException(strCloseCodeExceptionMessage);
                    }
                } else {
                    strR = "";
                }
                this.frameCallback.onReadClose(s, strR);
                this.closed = true;
                return;
            case 9:
                this.frameCallback.onReadPing(cVar.p());
                return;
            case 10:
                this.frameCallback.onReadPong(cVar.p());
                return;
            default:
                throw new ProtocolException("Unknown control opcode: " + Integer.toHexString(this.opcode));
        }
    }

    /* JADX WARN: Finally extract failed */
    private void readHeader() throws IOException {
        if (this.closed) {
            throw new IOException("closed");
        }
        long jTimeoutNanos = this.source.timeout().timeoutNanos();
        this.source.timeout().clearTimeout();
        try {
            int i2 = this.source.readByte() & 255;
            this.source.timeout().timeout(jTimeoutNanos, TimeUnit.NANOSECONDS);
            this.opcode = i2 & 15;
            this.isFinalFrame = (i2 & ICmdMsg.ACTION_START_ACTIVITY) != 0;
            boolean z = (i2 & 8) != 0;
            this.isControlFrame = z;
            if (z && !this.isFinalFrame) {
                throw new ProtocolException("Control frames must be final.");
            }
            boolean z2 = (i2 & 64) != 0;
            boolean z3 = (i2 & 32) != 0;
            boolean z4 = (i2 & 16) != 0;
            if (z2 || z3 || z4) {
                throw new ProtocolException("Reserved flags are unsupported.");
            }
            int i3 = this.source.readByte() & 255;
            boolean z5 = (i3 & ICmdMsg.ACTION_START_ACTIVITY) != 0;
            this.isMasked = z5;
            if (z5 == this.isClient) {
                throw new ProtocolException(this.isClient ? "Server-sent frames must not be masked." : "Client-sent frames must be masked.");
            }
            long j2 = i3 & ICmdMsg.ACTION_SOURCE_EVENT;
            this.frameLength = j2;
            if (j2 == 126) {
                this.frameLength = ((long) 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" + Long.toHexString(this.frameLength) + " > 0x7FFFFFFFFFFFFFFF");
                }
            }
            this.frameBytesRead = 0L;
            if (this.isControlFrame && this.frameLength > 125) {
                throw new ProtocolException("Control frame must be less than 125B.");
            }
            if (this.isMasked) {
                this.source.readFully(this.maskKey);
            }
        } catch (Throwable th) {
            this.source.timeout().timeout(jTimeoutNanos, TimeUnit.NANOSECONDS);
            throw th;
        }
    }

    private void readMessage(c cVar) throws IOException {
        long j2;
        while (!this.closed) {
            if (this.frameBytesRead == this.frameLength) {
                if (this.isFinalFrame) {
                    return;
                }
                readUntilNonControlFrame();
                if (this.opcode != 0) {
                    throw new ProtocolException("Expected continuation opcode. Got: " + Integer.toHexString(this.opcode));
                }
                if (this.isFinalFrame && this.frameLength == 0) {
                    return;
                }
            }
            long j3 = this.frameLength - this.frameBytesRead;
            if (this.isMasked) {
                j2 = this.source.read(this.maskBuffer, 0, (int) Math.min(j3, this.maskBuffer.length));
                if (j2 == -1) {
                    throw new EOFException();
                }
                WebSocketProtocol.toggleMask(this.maskBuffer, j2, this.maskKey, this.frameBytesRead);
                cVar.M(this.maskBuffer, 0, (int) j2);
            } else {
                j2 = this.source.read(cVar, j3);
                if (j2 == -1) {
                    throw new EOFException();
                }
            }
            this.frameBytesRead += j2;
        }
        throw new IOException("closed");
    }

    private void readMessageFrame() throws IOException {
        int i2 = this.opcode;
        if (i2 != 1 && i2 != 2) {
            throw new ProtocolException("Unknown opcode: " + Integer.toHexString(i2));
        }
        c cVar = new c();
        readMessage(cVar);
        if (i2 == 1) {
            this.frameCallback.onReadMessage(cVar.r());
        } else {
            this.frameCallback.onReadMessage(cVar.p());
        }
    }

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

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