package okhttp3.internal.ws;

import com.cvte.vman.impl.AvManager;
import com.cvte.vman.instance.InvokeCmd;
import f.c;
import f.d;
import f.f;
import f.s;
import f.u;
import java.io.IOException;
import java.util.Objects;
import java.util.Random;

/* JADX INFO: loaded from: classes.dex */
final class WebSocketWriter {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    boolean activeWriter;
    final c buffer = new c();
    final FrameSink frameSink = new FrameSink();
    final boolean isClient;
    final byte[] maskBuffer;
    final byte[] maskKey;
    final Random random;
    final d sink;
    boolean writerClosed;

    final class FrameSink implements s {
        boolean closed;
        long contentLength;
        int formatOpcode;
        boolean isFirstFrame;

        FrameSink() {
        }

        @Override // f.s, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (this.closed) {
                throw new IOException("closed");
            }
            synchronized (WebSocketWriter.this) {
                WebSocketWriter webSocketWriter = WebSocketWriter.this;
                webSocketWriter.writeMessageFrameSynchronized(this.formatOpcode, webSocketWriter.buffer.x(), this.isFirstFrame, true);
            }
            this.closed = true;
            WebSocketWriter.this.activeWriter = false;
        }

        @Override // f.s, java.io.Flushable
        public void flush() throws IOException {
            if (this.closed) {
                throw new IOException("closed");
            }
            synchronized (WebSocketWriter.this) {
                WebSocketWriter webSocketWriter = WebSocketWriter.this;
                webSocketWriter.writeMessageFrameSynchronized(this.formatOpcode, webSocketWriter.buffer.x(), this.isFirstFrame, false);
            }
            this.isFirstFrame = false;
        }

        @Override // f.s
        public u timeout() {
            return WebSocketWriter.this.sink.timeout();
        }

        @Override // f.s
        public void write(c cVar, long j) throws IOException {
            if (this.closed) {
                throw new IOException("closed");
            }
            WebSocketWriter.this.buffer.write(cVar, j);
            boolean z = this.isFirstFrame && this.contentLength != -1 && WebSocketWriter.this.buffer.x() > this.contentLength - 8192;
            long jE = WebSocketWriter.this.buffer.e();
            if (jE <= 0 || z) {
                return;
            }
            synchronized (WebSocketWriter.this) {
                WebSocketWriter.this.writeMessageFrameSynchronized(this.formatOpcode, jE, this.isFirstFrame, false);
            }
            this.isFirstFrame = false;
        }
    }

    WebSocketWriter(boolean z, d dVar, Random random) {
        Objects.requireNonNull(dVar, "sink == null");
        Objects.requireNonNull(random, "random == null");
        this.isClient = z;
        this.sink = dVar;
        this.random = random;
        this.maskKey = z ? new byte[4] : null;
        this.maskBuffer = z ? new byte[InvokeCmd.CMD_TOUCH_CTRL_START] : null;
    }

    private void writeControlFrameSynchronized(int i, f fVar) throws IOException {
        if (this.writerClosed) {
            throw new IOException("closed");
        }
        int iQ = fVar.q();
        if (iQ > 125) {
            throw new IllegalArgumentException("Payload size must be less than or equal to 125");
        }
        this.sink.writeByte(i | 128);
        if (this.isClient) {
            this.sink.writeByte(iQ | 128);
            this.random.nextBytes(this.maskKey);
            this.sink.write(this.maskKey);
            byte[] bArrU = fVar.u();
            WebSocketProtocol.toggleMask(bArrU, bArrU.length, this.maskKey, 0L);
            this.sink.write(bArrU);
        } else {
            this.sink.writeByte(iQ);
            this.sink.U(fVar);
        }
        this.sink.flush();
    }

    s newMessageSink(int i, long j) {
        if (this.activeWriter) {
            throw new IllegalStateException("Another message writer is active. Did you call close()?");
        }
        this.activeWriter = true;
        FrameSink frameSink = this.frameSink;
        frameSink.formatOpcode = i;
        frameSink.contentLength = j;
        frameSink.isFirstFrame = true;
        frameSink.closed = false;
        return frameSink;
    }

    void writeClose(int i, f fVar) throws IOException {
        f fVarN = f.f3079d;
        if (i != 0 || fVar != null) {
            if (i != 0) {
                WebSocketProtocol.validateCloseCode(i);
            }
            c cVar = new c();
            cVar.writeShort(i);
            if (fVar != null) {
                cVar.U(fVar);
            }
            fVarN = cVar.n();
        }
        synchronized (this) {
            try {
                try {
                    writeControlFrameSynchronized(8, fVarN);
                } finally {
                    this.writerClosed = true;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    void writeMessageFrameSynchronized(int i, long j, boolean z, boolean z2) throws IOException {
        if (this.writerClosed) {
            throw new IOException("closed");
        }
        if (!z) {
            i = 0;
        }
        if (z2) {
            i |= 128;
        }
        this.sink.writeByte(i);
        int i2 = this.isClient ? 128 : 0;
        if (j <= 125) {
            this.sink.writeByte(i2 | ((int) j));
        } else if (j <= 65535) {
            this.sink.writeByte(i2 | AvManager.MIC_Gain_Control_DISABLED);
            this.sink.writeShort((int) j);
        } else {
            this.sink.writeByte(i2 | 127);
            this.sink.writeLong(j);
        }
        if (this.isClient) {
            this.random.nextBytes(this.maskKey);
            this.sink.write(this.maskKey);
            long j2 = 0;
            while (j2 < j) {
                int i3 = this.buffer.read(this.maskBuffer, 0, (int) Math.min(j, this.maskBuffer.length));
                if (i3 == -1) {
                    throw new AssertionError();
                }
                long j3 = i3;
                WebSocketProtocol.toggleMask(this.maskBuffer, j3, this.maskKey, j2);
                this.sink.write(this.maskBuffer, 0, i3);
                j2 += j3;
            }
        } else {
            this.sink.write(this.buffer, j);
        }
        this.sink.f();
    }

    void writePing(f fVar) throws IOException {
        synchronized (this) {
            writeControlFrameSynchronized(9, fVar);
        }
    }

    void writePong(f fVar) throws IOException {
        synchronized (this) {
            writeControlFrameSynchronized(10, fVar);
        }
    }
}
