package okhttp3.internal.ws;

import com.cvte.vman.instance.InvokeCmd;
import j.n;
import j.p0.d.r;
import java.io.IOException;
import java.util.Random;
import l.b0;
import l.f;
import l.g;
import l.i;
import l.y;
import okhttp3.HttpUrl;

/* JADX INFO: compiled from: WebSocketWriter.kt */
/* JADX INFO: loaded from: classes.dex */
@n(bv = {1, 0, 3}, d1 = {"\u0000^\n\u0002\u0018\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0010\t\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0006\n\u0002\u0010\u000b\n\u0002\b\r\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0012\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\n\b\u0000\u0018\u0000:\u0001=B\u001f\u0012\u0006\u0010(\u001a\u00020\u0012\u0012\u0006\u00105\u001a\u000204\u0012\u0006\u00100\u001a\u00020/¢\u0006\u0004\b;\u0010<J\u001d\u0010\u0006\u001a\u00020\u00052\u0006\u0010\u0002\u001a\u00020\u00012\u0006\u0010\u0004\u001a\u00020\u0003¢\u0006\u0004\b\u0006\u0010\u0007J\u001f\u0010\f\u001a\u00020\u000b2\u0006\u0010\b\u001a\u00020\u00012\b\u0010\n\u001a\u0004\u0018\u00010\t¢\u0006\u0004\b\f\u0010\rJ\u001f\u0010\u0010\u001a\u00020\u000b2\u0006\u0010\u000e\u001a\u00020\u00012\u0006\u0010\u000f\u001a\u00020\tH\u0002¢\u0006\u0004\b\u0010\u0010\rJ-\u0010\u0015\u001a\u00020\u000b2\u0006\u0010\u0002\u001a\u00020\u00012\u0006\u0010\u0011\u001a\u00020\u00032\u0006\u0010\u0013\u001a\u00020\u00122\u0006\u0010\u0014\u001a\u00020\u0012¢\u0006\u0004\b\u0015\u0010\u0016J\u0015\u0010\u0017\u001a\u00020\u000b2\u0006\u0010\u000f\u001a\u00020\t¢\u0006\u0004\b\u0017\u0010\u0018J\u0015\u0010\u0019\u001a\u00020\u000b2\u0006\u0010\u000f\u001a\u00020\t¢\u0006\u0004\b\u0019\u0010\u0018R\"\u0010\u001a\u001a\u00020\u00128\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b\u001a\u0010\u001b\u001a\u0004\b\u001c\u0010\u001d\"\u0004\b\u001e\u0010\u001fR\u0019\u0010!\u001a\u00020 8\u0006@\u0006¢\u0006\f\n\u0004\b!\u0010\"\u001a\u0004\b#\u0010$R\u001a\u0010&\u001a\u00060%R\u00020\u00008\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b&\u0010'R\u0016\u0010(\u001a\u00020\u00128\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b(\u0010\u001bR\u0018\u0010*\u001a\u0004\u0018\u00010)8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b*\u0010+R\u0018\u0010-\u001a\u0004\u0018\u00010,8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b-\u0010.R\u0019\u00100\u001a\u00020/8\u0006@\u0006¢\u0006\f\n\u0004\b0\u00101\u001a\u0004\b2\u00103R\u0019\u00105\u001a\u0002048\u0006@\u0006¢\u0006\f\n\u0004\b5\u00106\u001a\u0004\b7\u00108R\u0016\u00109\u001a\u00020 8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b9\u0010\"R\u0016\u0010:\u001a\u00020\u00128\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b:\u0010\u001b¨\u0006>"}, d2 = {"Lokhttp3/internal/ws/WebSocketWriter;", HttpUrl.FRAGMENT_ENCODE_SET, "formatOpcode", HttpUrl.FRAGMENT_ENCODE_SET, "contentLength", "Lokio/Sink;", "newMessageSink", "(IJ)Lokio/Sink;", "code", "Lokio/ByteString;", "reason", HttpUrl.FRAGMENT_ENCODE_SET, "writeClose", "(ILokio/ByteString;)V", "opcode", "payload", "writeControlFrame", "byteCount", HttpUrl.FRAGMENT_ENCODE_SET, "isFirstFrame", "isFinal", "writeMessageFrame", "(IJZZ)V", "writePing", "(Lokio/ByteString;)V", "writePong", "activeWriter", "Z", "getActiveWriter", "()Z", "setActiveWriter", "(Z)V", "Lokio/Buffer;", "buffer", "Lokio/Buffer;", "getBuffer", "()Lokio/Buffer;", "Lokhttp3/internal/ws/WebSocketWriter$FrameSink;", "frameSink", "Lokhttp3/internal/ws/WebSocketWriter$FrameSink;", "isClient", "Lokio/Buffer$UnsafeCursor;", "maskCursor", "Lokio/Buffer$UnsafeCursor;", HttpUrl.FRAGMENT_ENCODE_SET, "maskKey", "[B", "Ljava/util/Random;", "random", "Ljava/util/Random;", "getRandom", "()Ljava/util/Random;", "Lokio/BufferedSink;", "sink", "Lokio/BufferedSink;", "getSink", "()Lokio/BufferedSink;", "sinkBuffer", "writerClosed", "<init>", "(ZLokio/BufferedSink;Ljava/util/Random;)V", "FrameSink", "okhttp"}, k = 1, mv = {1, 1, 15}, pn = HttpUrl.FRAGMENT_ENCODE_SET, xi = 0, xs = HttpUrl.FRAGMENT_ENCODE_SET)
public final class WebSocketWriter {
    private boolean activeWriter;
    private final f buffer;
    private final FrameSink frameSink;
    private final boolean isClient;
    private final f.a maskCursor;
    private final byte[] maskKey;
    private final Random random;
    private final g sink;
    private final f sinkBuffer;
    private boolean writerClosed;

    /* JADX INFO: compiled from: WebSocketWriter.kt */
    @n(bv = {1, 0, 3}, d1 = {"\u00006\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\f\n\u0002\u0010\b\n\u0002\b\u000b\b\u0080\u0004\u0018\u00002\u00020\u0001B\u0007¢\u0006\u0004\b%\u0010&J\u000f\u0010\u0003\u001a\u00020\u0002H\u0016¢\u0006\u0004\b\u0003\u0010\u0004J\u000f\u0010\u0005\u001a\u00020\u0002H\u0016¢\u0006\u0004\b\u0005\u0010\u0004J\u000f\u0010\u0007\u001a\u00020\u0006H\u0016¢\u0006\u0004\b\u0007\u0010\bJ\u001f\u0010\r\u001a\u00020\u00022\u0006\u0010\n\u001a\u00020\t2\u0006\u0010\f\u001a\u00020\u000bH\u0016¢\u0006\u0004\b\r\u0010\u000eR\"\u0010\u0010\u001a\u00020\u000f8\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b\u0010\u0010\u0011\u001a\u0004\b\u0012\u0010\u0013\"\u0004\b\u0014\u0010\u0015R\"\u0010\u0016\u001a\u00020\u000b8\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b\u0016\u0010\u0017\u001a\u0004\b\u0018\u0010\u0019\"\u0004\b\u001a\u0010\u001bR\"\u0010\u001d\u001a\u00020\u001c8\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b\u001d\u0010\u001e\u001a\u0004\b\u001f\u0010 \"\u0004\b!\u0010\"R\"\u0010#\u001a\u00020\u000f8\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b#\u0010\u0011\u001a\u0004\b#\u0010\u0013\"\u0004\b$\u0010\u0015¨\u0006'"}, d2 = {"Lokhttp3/internal/ws/WebSocketWriter$FrameSink;", "Ll/y;", HttpUrl.FRAGMENT_ENCODE_SET, "close", "()V", "flush", "Lokio/Timeout;", "timeout", "()Lokio/Timeout;", "Lokio/Buffer;", "source", HttpUrl.FRAGMENT_ENCODE_SET, "byteCount", "write", "(Lokio/Buffer;J)V", HttpUrl.FRAGMENT_ENCODE_SET, "closed", "Z", "getClosed", "()Z", "setClosed", "(Z)V", "contentLength", "J", "getContentLength", "()J", "setContentLength", "(J)V", HttpUrl.FRAGMENT_ENCODE_SET, "formatOpcode", "I", "getFormatOpcode", "()I", "setFormatOpcode", "(I)V", "isFirstFrame", "setFirstFrame", "<init>", "(Lokhttp3/internal/ws/WebSocketWriter;)V", "okhttp"}, k = 1, mv = {1, 1, 15}, pn = HttpUrl.FRAGMENT_ENCODE_SET, xi = 0, xs = HttpUrl.FRAGMENT_ENCODE_SET)
    public final class FrameSink implements y {
        private boolean closed;
        private long contentLength;
        private int formatOpcode;
        private boolean isFirstFrame;

        public FrameSink() {
        }

        @Override // l.y, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
        public void close() throws IOException {
            if (this.closed) {
                throw new IOException("closed");
            }
            WebSocketWriter webSocketWriter = WebSocketWriter.this;
            webSocketWriter.writeMessageFrame(this.formatOpcode, webSocketWriter.getBuffer().K(), this.isFirstFrame, true);
            this.closed = true;
            WebSocketWriter.this.setActiveWriter(false);
        }

        @Override // l.y, java.io.Flushable
        public void flush() throws IOException {
            if (this.closed) {
                throw new IOException("closed");
            }
            WebSocketWriter webSocketWriter = WebSocketWriter.this;
            webSocketWriter.writeMessageFrame(this.formatOpcode, webSocketWriter.getBuffer().K(), this.isFirstFrame, false);
            this.isFirstFrame = false;
        }

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

        public final long getContentLength() {
            return this.contentLength;
        }

        public final int getFormatOpcode() {
            return this.formatOpcode;
        }

        public final boolean isFirstFrame() {
            return this.isFirstFrame;
        }

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

        public final void setContentLength(long j2) {
            this.contentLength = j2;
        }

        public final void setFirstFrame(boolean z) {
            this.isFirstFrame = z;
        }

        public final void setFormatOpcode(int i2) {
            this.formatOpcode = i2;
        }

        @Override // l.y
        public b0 timeout() {
            return WebSocketWriter.this.getSink().timeout();
        }

        @Override // l.y
        public void write(f fVar, long j2) throws IOException {
            r.g(fVar, "source");
            if (this.closed) {
                throw new IOException("closed");
            }
            WebSocketWriter.this.getBuffer().write(fVar, j2);
            boolean z = this.isFirstFrame && this.contentLength != -1 && WebSocketWriter.this.getBuffer().K() > this.contentLength - ((long) InvokeCmd.CMD_TOUCH_CTRL_START);
            long jI = WebSocketWriter.this.getBuffer().i();
            if (jI <= 0 || z) {
                return;
            }
            WebSocketWriter.this.writeMessageFrame(this.formatOpcode, jI, this.isFirstFrame, false);
            this.isFirstFrame = false;
        }
    }

    public WebSocketWriter(boolean z, g gVar, Random random) {
        r.g(gVar, "sink");
        r.g(random, "random");
        this.isClient = z;
        this.sink = gVar;
        this.random = random;
        this.sinkBuffer = gVar.a();
        this.buffer = new f();
        this.frameSink = new FrameSink();
        this.maskKey = this.isClient ? new byte[4] : null;
        this.maskCursor = this.isClient ? new f.a() : null;
    }

    private final void writeControlFrame(int i2, i iVar) throws IOException {
        if (this.writerClosed) {
            throw new IOException("closed");
        }
        int iS = iVar.s();
        if (!(((long) iS) <= 125)) {
            throw new IllegalArgumentException("Payload size must be less than or equal to 125".toString());
        }
        this.sinkBuffer.X(i2 | 128);
        if (this.isClient) {
            this.sinkBuffer.X(iS | 128);
            Random random = this.random;
            byte[] bArr = this.maskKey;
            if (bArr == null) {
                r.p();
                throw null;
            }
            random.nextBytes(bArr);
            this.sinkBuffer.V(this.maskKey);
            if (iS > 0) {
                long jK = this.sinkBuffer.K();
                this.sinkBuffer.S(iVar);
                f fVar = this.sinkBuffer;
                f.a aVar = this.maskCursor;
                if (aVar == null) {
                    r.p();
                    throw null;
                }
                fVar.x(aVar);
                this.maskCursor.f(jK);
                WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
                this.maskCursor.close();
            }
        } else {
            this.sinkBuffer.X(iS);
            this.sinkBuffer.S(iVar);
        }
        this.sink.flush();
    }

    public final boolean getActiveWriter() {
        return this.activeWriter;
    }

    public final f getBuffer() {
        return this.buffer;
    }

    public final Random getRandom() {
        return this.random;
    }

    public final g getSink() {
        return this.sink;
    }

    public final y newMessageSink(int i2, long j2) {
        if (!(!this.activeWriter)) {
            throw new IllegalStateException("Another message writer is active. Did you call close()?".toString());
        }
        this.activeWriter = true;
        this.frameSink.setFormatOpcode(i2);
        this.frameSink.setContentLength(j2);
        this.frameSink.setFirstFrame(true);
        this.frameSink.setClosed(false);
        return this.frameSink;
    }

    public final void setActiveWriter(boolean z) {
        this.activeWriter = z;
    }

    public final void writeClose(int i2, i iVar) throws IOException {
        i iVarG = i.f;
        if (i2 != 0 || iVar != null) {
            if (i2 != 0) {
                WebSocketProtocol.INSTANCE.validateCloseCode(i2);
            }
            f fVar = new f();
            fVar.d0(i2);
            if (iVar != null) {
                fVar.S(iVar);
            }
            iVarG = fVar.g();
        }
        try {
            writeControlFrame(8, iVarG);
        } finally {
            this.writerClosed = true;
        }
    }

    public final void writeMessageFrame(int i2, long j2, boolean z, boolean z2) throws IOException {
        if (this.writerClosed) {
            throw new IOException("closed");
        }
        if (!z) {
            i2 = 0;
        }
        if (z2) {
            i2 |= 128;
        }
        this.sinkBuffer.X(i2);
        int i3 = this.isClient ? 128 : 0;
        if (j2 <= 125) {
            this.sinkBuffer.X(((int) j2) | i3);
        } else if (j2 <= WebSocketProtocol.PAYLOAD_SHORT_MAX) {
            this.sinkBuffer.X(i3 | 126);
            this.sinkBuffer.d0((int) j2);
        } else {
            this.sinkBuffer.X(i3 | 127);
            this.sinkBuffer.c0(j2);
        }
        if (this.isClient) {
            Random random = this.random;
            byte[] bArr = this.maskKey;
            if (bArr == null) {
                r.p();
                throw null;
            }
            random.nextBytes(bArr);
            this.sinkBuffer.V(this.maskKey);
            if (j2 > 0) {
                long jK = this.sinkBuffer.K();
                this.sinkBuffer.write(this.buffer, j2);
                f fVar = this.sinkBuffer;
                f.a aVar = this.maskCursor;
                if (aVar == null) {
                    r.p();
                    throw null;
                }
                fVar.x(aVar);
                this.maskCursor.f(jK);
                WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
                this.maskCursor.close();
            }
        } else {
            this.sinkBuffer.write(this.buffer, j2);
        }
        this.sink.m();
    }

    public final void writePing(i iVar) throws IOException {
        r.g(iVar, "payload");
        writeControlFrame(9, iVar);
    }

    public final void writePong(i iVar) throws IOException {
        r.g(iVar, "payload");
        writeControlFrame(10, iVar);
    }
}
