package g.i0.i;

import com.seewo.vcommons.constants.Platform;
import g.i0.i.o;
import java.io.Closeable;
import java.io.IOException;
import java.net.Socket;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public final class g implements Closeable {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final ExecutorService f6282b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final boolean f6283c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final d f6284d;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final String f6286f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public int f6287g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f6288h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public boolean f6289i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final ScheduledExecutorService f6290j;
    public final ExecutorService k;
    public final s l;
    public boolean m;
    public long o;
    public final t q;
    public boolean r;
    public final Socket s;
    public final q t;
    public final f u;
    public final Set<Integer> v;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Map<Integer, p> f6285e = new LinkedHashMap();
    public long n = 0;
    public t p = new t();

    public class a extends g.i0.b {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final /* synthetic */ int f6291c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final /* synthetic */ g.i0.i.b f6292d;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public a(String str, Object[] objArr, int i2, g.i0.i.b bVar) {
            super(str, objArr);
            this.f6291c = i2;
            this.f6292d = bVar;
        }

        @Override // g.i0.b
        public void a() {
            try {
                g gVar = g.this;
                gVar.t.K(this.f6291c, this.f6292d);
            } catch (IOException unused) {
                g.a(g.this);
            }
        }
    }

    public class b extends g.i0.b {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final /* synthetic */ int f6294c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final /* synthetic */ long f6295d;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public b(String str, Object[] objArr, int i2, long j2) {
            super(str, objArr);
            this.f6294c = i2;
            this.f6295d = j2;
        }

        @Override // g.i0.b
        public void a() {
            try {
                g.this.t.L(this.f6294c, this.f6295d);
            } catch (IOException unused) {
                g.a(g.this);
            }
        }
    }

    public static class c {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public Socket f6297a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public String f6298b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public h.h f6299c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public h.g f6300d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public d f6301e = d.f6303a;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public int f6302f;

        public c(boolean z) {
        }
    }

    public static abstract class d {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public static final d f6303a = new a();

        public class a extends d {
            @Override // g.i0.i.g.d
            public void b(p pVar) {
                pVar.c(g.i0.i.b.REFUSED_STREAM);
            }
        }

        public void a(g gVar) {
        }

        public abstract void b(p pVar);
    }

    public final class e extends g.i0.b {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final boolean f6304c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final int f6305d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final int f6306e;

        public e(boolean z, int i2, int i3) {
            super("OkHttp %s ping %08x%08x", g.this.f6286f, Integer.valueOf(i2), Integer.valueOf(i3));
            this.f6304c = z;
            this.f6305d = i2;
            this.f6306e = i3;
        }

        /* JADX WARN: Removed duplicated region for block: B:17:0x001f A[EXC_TOP_SPLITTER, SYNTHETIC] */
        @Override // g.i0.b
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public void a() {
            /*
                r6 = this;
                g.i0.i.g r0 = g.i0.i.g.this
                boolean r1 = r6.f6304c
                int r2 = r6.f6305d
                int r3 = r6.f6306e
                java.util.Objects.requireNonNull(r0)
                if (r1 != 0) goto L1f
                monitor-enter(r0)
                boolean r4 = r0.m     // Catch: java.lang.Throwable -> L1c
                r5 = 1
                r0.m = r5     // Catch: java.lang.Throwable -> L1c
                monitor-exit(r0)     // Catch: java.lang.Throwable -> L1c
                if (r4 == 0) goto L1f
            L16:
                g.i0.i.b r1 = g.i0.i.b.PROTOCOL_ERROR     // Catch: java.io.IOException -> L24
                r0.i(r1, r1)     // Catch: java.io.IOException -> L24
                goto L24
            L1c:
                r1 = move-exception
                monitor-exit(r0)     // Catch: java.lang.Throwable -> L1c
                throw r1
            L1f:
                g.i0.i.q r4 = r0.t     // Catch: java.io.IOException -> L16
                r4.E(r1, r2, r3)     // Catch: java.io.IOException -> L16
            L24:
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: g.i0.i.g.e.a():void");
        }
    }

    public class f extends g.i0.b implements o.b {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final o f6308c;

        public f(o oVar) {
            super("OkHttp %s", g.this.f6286f);
            this.f6308c = oVar;
        }

        @Override // g.i0.b
        public void a() throws Throwable {
            g.i0.i.b bVar;
            g.i0.i.b bVar2 = g.i0.i.b.INTERNAL_ERROR;
            try {
                try {
                    this.f6308c.o(this);
                    while (this.f6308c.i(false, this)) {
                    }
                    bVar = g.i0.i.b.NO_ERROR;
                    try {
                        try {
                            g.this.i(bVar, g.i0.i.b.CANCEL);
                        } catch (IOException unused) {
                            g.i0.i.b bVar3 = g.i0.i.b.PROTOCOL_ERROR;
                            g.this.i(bVar3, bVar3);
                        }
                    } catch (Throwable th) {
                        th = th;
                        try {
                            g.this.i(bVar, bVar2);
                        } catch (IOException unused2) {
                        }
                        g.i0.c.d(this.f6308c);
                        throw th;
                    }
                } catch (IOException unused3) {
                }
            } catch (IOException unused4) {
                bVar = bVar2;
            } catch (Throwable th2) {
                th = th2;
                bVar = bVar2;
                g.this.i(bVar, bVar2);
                g.i0.c.d(this.f6308c);
                throw th;
            }
            g.i0.c.d(this.f6308c);
        }
    }

    static {
        TimeUnit timeUnit = TimeUnit.SECONDS;
        SynchronousQueue synchronousQueue = new SynchronousQueue();
        byte[] bArr = g.i0.c.f6140a;
        f6282b = new ThreadPoolExecutor(0, Platform.UNKNOWN, 60L, timeUnit, synchronousQueue, new g.i0.d("OkHttp Http2Connection", true));
    }

    public g(c cVar) {
        t tVar = new t();
        this.q = tVar;
        this.r = false;
        this.v = new LinkedHashSet();
        this.l = s.f6375a;
        this.f6283c = true;
        this.f6284d = cVar.f6301e;
        this.f6288h = 1;
        this.f6288h = 3;
        this.p.b(7, 16777216);
        String str = cVar.f6298b;
        this.f6286f = str;
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, new g.i0.d(g.i0.c.l("OkHttp %s Writer", str), false));
        this.f6290j = scheduledThreadPoolExecutor;
        if (cVar.f6302f != 0) {
            e eVar = new e(false, 0, 0);
            long j2 = cVar.f6302f;
            scheduledThreadPoolExecutor.scheduleAtFixedRate(eVar, j2, j2, TimeUnit.MILLISECONDS);
        }
        this.k = new ThreadPoolExecutor(0, 1, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue(), new g.i0.d(g.i0.c.l("OkHttp %s Push Observer", str), true));
        tVar.b(7, 65535);
        tVar.b(5, 16384);
        this.o = tVar.a();
        this.s = cVar.f6297a;
        this.t = new q(cVar.f6300d, true);
        this.u = new f(new o(cVar.f6299c, true));
    }

    public static void a(g gVar) {
        Objects.requireNonNull(gVar);
        try {
            g.i0.i.b bVar = g.i0.i.b.PROTOCOL_ERROR;
            gVar.i(bVar, bVar);
        } catch (IOException unused) {
        }
    }

    public final synchronized void B(g.i0.b bVar) {
        synchronized (this) {
        }
        if (!this.f6289i) {
            this.k.execute(bVar);
        }
    }

    public boolean E(int i2) {
        return i2 != 0 && (i2 & 1) == 0;
    }

    public synchronized p K(int i2) {
        p pVarRemove;
        pVarRemove = this.f6285e.remove(Integer.valueOf(i2));
        notifyAll();
        return pVarRemove;
    }

    public void L(g.i0.i.b bVar) {
        synchronized (this.t) {
            synchronized (this) {
                if (this.f6289i) {
                    return;
                }
                this.f6289i = true;
                this.t.z(this.f6287g, bVar, g.i0.c.f6140a);
            }
        }
    }

    public synchronized void M(long j2) {
        long j3 = this.n + j2;
        this.n = j3;
        if (j3 >= this.p.a() / 2) {
            P(0, this.n);
            this.n = 0L;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:16:0x0030, code lost:
    
        r2 = java.lang.Math.min((int) java.lang.Math.min(r12, r4), r8.t.f6365f);
        r6 = r2;
        r8.o -= r6;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void N(int r9, boolean r10, h.f r11, long r12) {
        /*
            r8 = this;
            r0 = 0
            int r2 = (r12 > r0 ? 1 : (r12 == r0 ? 0 : -1))
            r3 = 0
            if (r2 != 0) goto Ld
            g.i0.i.q r12 = r8.t
            r12.i(r10, r9, r11, r3)
            return
        Ld:
            int r2 = (r12 > r0 ? 1 : (r12 == r0 ? 0 : -1))
            if (r2 <= 0) goto L65
            monitor-enter(r8)
        L12:
            long r4 = r8.o     // Catch: java.lang.Throwable -> L54 java.lang.InterruptedException -> L56
            int r2 = (r4 > r0 ? 1 : (r4 == r0 ? 0 : -1))
            if (r2 > 0) goto L30
            java.util.Map<java.lang.Integer, g.i0.i.p> r2 = r8.f6285e     // Catch: java.lang.Throwable -> L54 java.lang.InterruptedException -> L56
            java.lang.Integer r4 = java.lang.Integer.valueOf(r9)     // Catch: java.lang.Throwable -> L54 java.lang.InterruptedException -> L56
            boolean r2 = r2.containsKey(r4)     // Catch: java.lang.Throwable -> L54 java.lang.InterruptedException -> L56
            if (r2 == 0) goto L28
            r8.wait()     // Catch: java.lang.Throwable -> L54 java.lang.InterruptedException -> L56
            goto L12
        L28:
            java.io.IOException r9 = new java.io.IOException     // Catch: java.lang.Throwable -> L54 java.lang.InterruptedException -> L56
            java.lang.String r10 = "stream closed"
            r9.<init>(r10)     // Catch: java.lang.Throwable -> L54 java.lang.InterruptedException -> L56
            throw r9     // Catch: java.lang.Throwable -> L54 java.lang.InterruptedException -> L56
        L30:
            long r4 = java.lang.Math.min(r12, r4)     // Catch: java.lang.Throwable -> L54
            int r2 = (int) r4     // Catch: java.lang.Throwable -> L54
            g.i0.i.q r4 = r8.t     // Catch: java.lang.Throwable -> L54
            int r4 = r4.f6365f     // Catch: java.lang.Throwable -> L54
            int r2 = java.lang.Math.min(r2, r4)     // Catch: java.lang.Throwable -> L54
            long r4 = r8.o     // Catch: java.lang.Throwable -> L54
            long r6 = (long) r2     // Catch: java.lang.Throwable -> L54
            long r4 = r4 - r6
            r8.o = r4     // Catch: java.lang.Throwable -> L54
            monitor-exit(r8)     // Catch: java.lang.Throwable -> L54
            long r12 = r12 - r6
            g.i0.i.q r4 = r8.t
            if (r10 == 0) goto L4f
            int r5 = (r12 > r0 ? 1 : (r12 == r0 ? 0 : -1))
            if (r5 != 0) goto L4f
            r5 = 1
            goto L50
        L4f:
            r5 = r3
        L50:
            r4.i(r5, r9, r11, r2)
            goto Ld
        L54:
            r9 = move-exception
            goto L63
        L56:
            java.lang.Thread r9 = java.lang.Thread.currentThread()     // Catch: java.lang.Throwable -> L54
            r9.interrupt()     // Catch: java.lang.Throwable -> L54
            java.io.InterruptedIOException r9 = new java.io.InterruptedIOException     // Catch: java.lang.Throwable -> L54
            r9.<init>()     // Catch: java.lang.Throwable -> L54
            throw r9     // Catch: java.lang.Throwable -> L54
        L63:
            monitor-exit(r8)     // Catch: java.lang.Throwable -> L54
            throw r9
        L65:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i0.i.g.N(int, boolean, h.f, long):void");
    }

    public void O(int i2, g.i0.i.b bVar) {
        try {
            this.f6290j.execute(new a("OkHttp %s stream %d", new Object[]{this.f6286f, Integer.valueOf(i2)}, i2, bVar));
        } catch (RejectedExecutionException unused) {
        }
    }

    public void P(int i2, long j2) {
        try {
            this.f6290j.execute(new b("OkHttp Window Update %s stream %d", new Object[]{this.f6286f, Integer.valueOf(i2)}, i2, j2));
        } catch (RejectedExecutionException unused) {
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        i(g.i0.i.b.NO_ERROR, g.i0.i.b.CANCEL);
    }

    public void flush() {
        this.t.flush();
    }

    public void i(g.i0.i.b bVar, g.i0.i.b bVar2) throws IOException {
        p[] pVarArr = null;
        try {
            L(bVar);
            e = null;
        } catch (IOException e2) {
            e = e2;
        }
        synchronized (this) {
            if (!this.f6285e.isEmpty()) {
                pVarArr = (p[]) this.f6285e.values().toArray(new p[this.f6285e.size()]);
                this.f6285e.clear();
            }
        }
        if (pVarArr != null) {
            for (p pVar : pVarArr) {
                try {
                    pVar.c(bVar2);
                } catch (IOException e3) {
                    if (e != null) {
                        e = e3;
                    }
                }
            }
        }
        try {
            this.t.close();
        } catch (IOException e4) {
            if (e == null) {
                e = e4;
            }
        }
        try {
            this.s.close();
        } catch (IOException e5) {
            e = e5;
        }
        this.f6290j.shutdown();
        this.k.shutdown();
        if (e != null) {
            throw e;
        }
    }

    public synchronized p o(int i2) {
        return this.f6285e.get(Integer.valueOf(i2));
    }

    public synchronized int z() {
        int i2;
        t tVar = this.q;
        i2 = Platform.UNKNOWN;
        if ((tVar.f6376a & 16) != 0) {
            i2 = tVar.f6377b[4];
        }
        return i2;
    }
}
