package ir;

import com.google.android.gms.dynamite.descriptors.com.google.android.gms.measurement.dynamite.ModuleDescriptor;
import com.google.firebase.perf.util.Constants;
import java.io.Closeable;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.t;
import kr.d;
import vq.m;
import vq.p;

/* loaded from: classes3.dex */
public final class g implements Closeable {
    private final kr.d A;
    private final kr.d B;
    private c C;
    private final byte[] D;
    private final d.a E;

    /* renamed from: p, reason: collision with root package name */
    private final boolean f23425p;

    /* renamed from: q, reason: collision with root package name */
    private final kr.f f23426q;

    /* renamed from: r, reason: collision with root package name */
    private final a f23427r;

    /* renamed from: s, reason: collision with root package name */
    private final boolean f23428s;

    /* renamed from: t, reason: collision with root package name */
    private final boolean f23429t;

    /* renamed from: u, reason: collision with root package name */
    private boolean f23430u;

    /* renamed from: v, reason: collision with root package name */
    private int f23431v;

    /* renamed from: w, reason: collision with root package name */
    private long f23432w;

    /* renamed from: x, reason: collision with root package name */
    private boolean f23433x;

    /* renamed from: y, reason: collision with root package name */
    private boolean f23434y;

    /* renamed from: z, reason: collision with root package name */
    private boolean f23435z;

    /* loaded from: classes3.dex */
    public interface a {
        void b(String str);

        void d(kr.g gVar);

        void e(kr.g gVar);

        void f(kr.g gVar);

        void g(int i10, String str);
    }

    public g(boolean z10, kr.f source, a frameCallback, boolean z11, boolean z12) {
        t.g(source, "source");
        t.g(frameCallback, "frameCallback");
        this.f23425p = z10;
        this.f23426q = source;
        this.f23427r = frameCallback;
        this.f23428s = z11;
        this.f23429t = z12;
        this.A = new kr.d();
        this.B = new kr.d();
        this.D = z10 ? null : new byte[4];
        this.E = z10 ? null : new d.a();
    }

    private final void b() {
        short s10;
        String str;
        long j10 = this.f23432w;
        if (j10 > 0) {
            this.f23426q.R(this.A, j10);
            if (!this.f23425p) {
                kr.d dVar = this.A;
                d.a aVar = this.E;
                t.d(aVar);
                dVar.z(aVar);
                this.E.d(0L);
                f fVar = f.f23424a;
                d.a aVar2 = this.E;
                byte[] bArr = this.D;
                t.d(bArr);
                fVar.b(aVar2, bArr);
                this.E.close();
            }
        }
        switch (this.f23431v) {
            case 8:
                long X = this.A.X();
                if (X == 1) {
                    throw new ProtocolException("Malformed close payload length of 1.");
                }
                if (X != 0) {
                    s10 = this.A.readShort();
                    str = this.A.N();
                    String a10 = f.f23424a.a(s10);
                    if (a10 != null) {
                        throw new ProtocolException(a10);
                    }
                } else {
                    s10 = 1005;
                    str = "";
                }
                this.f23427r.g(s10, str);
                this.f23430u = true;
                return;
            case 9:
                this.f23427r.e(this.A.C());
                return;
            case 10:
                this.f23427r.f(this.A.C());
                return;
            default:
                throw new ProtocolException("Unknown control opcode: " + p.s(this.f23431v));
        }
    }

    private final void c() {
        boolean z10;
        if (this.f23430u) {
            throw new IOException("closed");
        }
        long h10 = this.f23426q.l().h();
        this.f23426q.l().b();
        try {
            int b10 = m.b(this.f23426q.readByte(), Constants.MAX_HOST_LENGTH);
            this.f23426q.l().g(h10, TimeUnit.NANOSECONDS);
            int i10 = b10 & 15;
            this.f23431v = i10;
            boolean z11 = (b10 & 128) != 0;
            this.f23433x = z11;
            boolean z12 = (b10 & 8) != 0;
            this.f23434y = z12;
            if (z12 && !z11) {
                throw new ProtocolException("Control frames must be final.");
            }
            boolean z13 = (b10 & 64) != 0;
            if (i10 == 1 || i10 == 2) {
                if (!z13) {
                    z10 = false;
                } else {
                    if (!this.f23428s) {
                        throw new ProtocolException("Unexpected rsv1 flag");
                    }
                    z10 = true;
                }
                this.f23435z = z10;
            } else if (z13) {
                throw new ProtocolException("Unexpected rsv1 flag");
            }
            if ((b10 & 32) != 0) {
                throw new ProtocolException("Unexpected rsv2 flag");
            }
            if ((b10 & 16) != 0) {
                throw new ProtocolException("Unexpected rsv3 flag");
            }
            int b11 = m.b(this.f23426q.readByte(), Constants.MAX_HOST_LENGTH);
            boolean z14 = (b11 & 128) != 0;
            if (z14 == this.f23425p) {
                throw new ProtocolException(this.f23425p ? "Server-sent frames must not be masked." : "Client-sent frames must be masked.");
            }
            long j10 = b11 & ModuleDescriptor.MODULE_VERSION;
            this.f23432w = j10;
            if (j10 == 126) {
                this.f23432w = m.c(this.f23426q.readShort(), 65535);
            } else if (j10 == 127) {
                long readLong = this.f23426q.readLong();
                this.f23432w = readLong;
                if (readLong < 0) {
                    throw new ProtocolException("Frame length 0x" + p.t(this.f23432w) + " > 0x7FFFFFFFFFFFFFFF");
                }
            }
            if (this.f23434y && this.f23432w > 125) {
                throw new ProtocolException("Control frame must be less than 125B.");
            }
            if (z14) {
                kr.f fVar = this.f23426q;
                byte[] bArr = this.D;
                t.d(bArr);
                fVar.readFully(bArr);
            }
        } catch (Throwable th2) {
            this.f23426q.l().g(h10, TimeUnit.NANOSECONDS);
            throw th2;
        }
    }

    private final void d() {
        while (!this.f23430u) {
            long j10 = this.f23432w;
            if (j10 > 0) {
                this.f23426q.R(this.B, j10);
                if (!this.f23425p) {
                    kr.d dVar = this.B;
                    d.a aVar = this.E;
                    t.d(aVar);
                    dVar.z(aVar);
                    this.E.d(this.B.X() - this.f23432w);
                    f fVar = f.f23424a;
                    d.a aVar2 = this.E;
                    byte[] bArr = this.D;
                    t.d(bArr);
                    fVar.b(aVar2, bArr);
                    this.E.close();
                }
            }
            if (this.f23433x) {
                return;
            }
            f();
            if (this.f23431v != 0) {
                throw new ProtocolException("Expected continuation opcode. Got: " + p.s(this.f23431v));
            }
        }
        throw new IOException("closed");
    }

    private final void e() {
        int i10 = this.f23431v;
        if (i10 != 1 && i10 != 2) {
            throw new ProtocolException("Unknown opcode: " + p.s(i10));
        }
        d();
        if (this.f23435z) {
            c cVar = this.C;
            if (cVar == null) {
                cVar = new c(this.f23429t);
                this.C = cVar;
            }
            cVar.a(this.B);
        }
        if (i10 == 1) {
            this.f23427r.b(this.B.N());
        } else {
            this.f23427r.d(this.B.C());
        }
    }

    private final void f() {
        while (!this.f23430u) {
            c();
            if (!this.f23434y) {
                return;
            } else {
                b();
            }
        }
    }

    public final void a() {
        c();
        if (this.f23434y) {
            b();
        } else {
            e();
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        c cVar = this.C;
        if (cVar != null) {
            cVar.close();
        }
    }
}
