package okhttp3.internal.ws;

import com.cvte.vman.instance.InvokeCmd;
import com.seewo.adbcommons.model.base.ICmdMsg;
import h.c;
import h.d;
import h.f;
import h.r;
import h.t;
import java.io.IOException;
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 r {
        boolean closed;
        long contentLength;
        int formatOpcode;
        boolean isFirstFrame;

        FrameSink() {
        }

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

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

        @Override // h.r
        public t timeout() {
            return WebSocketWriter.this.sink.timeout();
        }

        @Override // h.r
        public void write(c cVar, long j2) throws IOException {
            if (this.closed) {
                throw new IOException("closed");
            }
            WebSocketWriter.this.buffer.write(cVar, j2);
            boolean z = this.isFirstFrame && this.contentLength != -1 && WebSocketWriter.this.buffer.B() > this.contentLength - 8192;
            long jG = WebSocketWriter.this.buffer.g();
            if (jG <= 0 || z) {
                return;
            }
            synchronized (WebSocketWriter.this) {
                WebSocketWriter.this.writeMessageFrameSynchronized(this.formatOpcode, jG, this.isFirstFrame, false);
            }
            this.isFirstFrame = false;
        }
    }

    WebSocketWriter(boolean z, d dVar, Random random) {
        if (dVar == null) {
            throw new NullPointerException("sink == null");
        }
        if (random == null) {
            throw new NullPointerException("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 i2, 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(i2 | ICmdMsg.ACTION_START_ACTIVITY);
        if (this.isClient) {
            this.sink.writeByte(iQ | ICmdMsg.ACTION_START_ACTIVITY);
            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.E(fVar);
        }
        this.sink.flush();
    }

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

    void writeClose(int i2, f fVar) throws IOException {
        f fVarP = f.f4181i;
        if (i2 != 0 || fVar != null) {
            if (i2 != 0) {
                WebSocketProtocol.validateCloseCode(i2);
            }
            c cVar = new c();
            cVar.T(i2);
            if (fVar != null) {
                cVar.H(fVar);
            }
            fVarP = cVar.p();
        }
        synchronized (this) {
            try {
                try {
                    writeControlFrameSynchronized(8, fVarP);
                } finally {
                    this.writerClosed = true;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

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

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

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