package m1;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import l1.C0622e;

/* JADX INFO: renamed from: m1.c, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public abstract class AbstractRunnableC0631c implements Runnable {

    /* JADX INFO: renamed from: H, reason: collision with root package name */
    static int f5400H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    static C0622e f5401I = C0622e.a();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    int f5402b = 0;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    String f5403g;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    Thread f5404r;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    C0632d f5405x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    protected HashMap f5406y;

    public AbstractRunnableC0631c() {
        StringBuilder sb = new StringBuilder();
        sb.append("Transport");
        int i2 = f5400H;
        f5400H = i2 + 1;
        sb.append(i2);
        this.f5403g = sb.toString();
        this.f5406y = new HashMap(4);
    }

    private void h() {
        while (this.f5404r == Thread.currentThread()) {
            try {
                InterfaceC0629a interfaceC0629aJ = j();
                if (interfaceC0629aJ == null) {
                    throw new IOException("end of stream");
                }
                synchronized (this) {
                    try {
                        AbstractC0630b abstractC0630b = (AbstractC0630b) this.f5406y.get(interfaceC0629aJ);
                        if (abstractC0630b == null) {
                            if (C0622e.f5384g >= 4) {
                                f5401I.println("Invalid key, skipping message");
                            }
                            g();
                        } else {
                            e(abstractC0630b);
                            abstractC0630b.f5399g = true;
                            notifyAll();
                        }
                    } finally {
                    }
                }
            } catch (Exception e2) {
                String message = e2.getMessage();
                boolean z2 = message != null && message.equals("Read timed out");
                boolean z3 = !z2;
                if (!z2 && C0622e.f5384g >= 3) {
                    e2.printStackTrace(f5401I);
                }
                try {
                    b(z3);
                } catch (IOException e3) {
                    e3.printStackTrace(f5401I);
                }
            }
        }
    }

    public static int k(InputStream inputStream, byte[] bArr, int i2, int i3) throws IOException {
        int i4 = 0;
        while (i4 < i3) {
            int i5 = inputStream.read(bArr, i2 + i4, i3 - i4);
            if (i5 <= 0) {
                break;
            }
            i4 += i5;
        }
        return i4;
    }

    public synchronized void a(long j2) {
        try {
            try {
                int i2 = this.f5402b;
                if (i2 != 0) {
                    if (i2 != 3) {
                        if (i2 == 4) {
                            this.f5402b = 0;
                            throw new C0632d("Connection in error", this.f5405x);
                        }
                        C0632d c0632d = new C0632d("Invalid state: " + this.f5402b);
                        this.f5402b = 0;
                        throw c0632d;
                    }
                    if (i2 != 0 && i2 != 3 && i2 != 4) {
                        if (C0622e.f5384g >= 1) {
                            f5401I.println("Invalid state: " + this.f5402b);
                        }
                        this.f5402b = 0;
                        this.f5404r = null;
                    }
                    return;
                }
                this.f5402b = 1;
                this.f5405x = null;
                Thread thread = new Thread(this, this.f5403g);
                this.f5404r = thread;
                thread.setDaemon(true);
                synchronized (this.f5404r) {
                    this.f5404r.start();
                    this.f5404r.wait(j2);
                    int i3 = this.f5402b;
                    if (i3 == 1) {
                        this.f5402b = 0;
                        this.f5404r = null;
                        throw new C0632d("Connection timeout");
                    }
                    if (i3 == 2) {
                        C0632d c0632d2 = this.f5405x;
                        if (c0632d2 == null) {
                            this.f5402b = 3;
                            return;
                        } else {
                            this.f5402b = 4;
                            this.f5404r = null;
                            throw c0632d2;
                        }
                    }
                    if (i3 != 0 && i3 != 3 && i3 != 4) {
                        if (C0622e.f5384g >= 1) {
                            f5401I.println("Invalid state: " + this.f5402b);
                        }
                        this.f5402b = 0;
                        this.f5404r = null;
                    }
                }
            } catch (InterruptedException e2) {
                this.f5402b = 0;
                this.f5404r = null;
                throw new C0632d(e2);
            }
        } catch (Throwable th) {
            int i4 = this.f5402b;
            if (i4 != 0 && i4 != 3 && i4 != 4) {
                if (C0622e.f5384g >= 1) {
                    f5401I.println("Invalid state: " + this.f5402b);
                }
                this.f5402b = 0;
                this.f5404r = null;
            }
            throw th;
        } finally {
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:28:0x004e A[DONT_GENERATE] */
    /* JADX WARN: Removed duplicated region for block: B:30:0x0050 A[Catch: all -> 0x002e, TRY_ENTER, TRY_LEAVE, TryCatch #1 {all -> 0x002e, blocks: (B:3:0x0001, B:11:0x0011, B:13:0x0015, B:16:0x0030, B:30:0x0050, B:25:0x0047, B:18:0x0036, B:22:0x0041), top: B:37:0x0001 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public synchronized void b(boolean r6) {
        /*
            r5 = this;
            monitor-enter(r5)
            int r0 = r5.f5402b     // Catch: java.lang.Throwable -> L2e
            if (r0 == 0) goto L51
            r1 = 2
            r2 = 0
            r3 = 1
            r4 = 0
            if (r0 == r1) goto L35
            r1 = 3
            if (r0 == r1) goto L36
            r6 = 4
            if (r0 == r6) goto L44
            int r6 = l1.C0622e.f5384g     // Catch: java.lang.Throwable -> L2e
            if (r6 < r3) goto L30
            l1.e r6 = m1.AbstractRunnableC0631c.f5401I     // Catch: java.lang.Throwable -> L2e
            java.lang.StringBuilder r0 = new java.lang.StringBuilder     // Catch: java.lang.Throwable -> L2e
            r0.<init>()     // Catch: java.lang.Throwable -> L2e
            java.lang.String r1 = "Invalid state: "
            r0.append(r1)     // Catch: java.lang.Throwable -> L2e
            int r1 = r5.f5402b     // Catch: java.lang.Throwable -> L2e
            r0.append(r1)     // Catch: java.lang.Throwable -> L2e
            java.lang.String r0 = r0.toString()     // Catch: java.lang.Throwable -> L2e
            r6.println(r0)     // Catch: java.lang.Throwable -> L2e
            goto L30
        L2e:
            r6 = move-exception
            goto L53
        L30:
            r5.f5404r = r4     // Catch: java.lang.Throwable -> L2e
            r5.f5402b = r2     // Catch: java.lang.Throwable -> L2e
            goto L4c
        L35:
            r6 = r3
        L36:
            java.util.HashMap r0 = r5.f5406y     // Catch: java.lang.Throwable -> L2e
            int r0 = r0.size()     // Catch: java.lang.Throwable -> L2e
            if (r0 == 0) goto L41
            if (r6 != 0) goto L41
            goto L4c
        L41:
            r5.d(r6)     // Catch: java.lang.Throwable -> L2e java.io.IOException -> L46
        L44:
            r6 = r4
            goto L47
        L46:
            r6 = move-exception
        L47:
            r5.f5404r = r4     // Catch: java.lang.Throwable -> L2e
            r5.f5402b = r2     // Catch: java.lang.Throwable -> L2e
            r4 = r6
        L4c:
            if (r4 != 0) goto L50
            monitor-exit(r5)
            return
        L50:
            throw r4     // Catch: java.lang.Throwable -> L2e
        L51:
            monitor-exit(r5)
            return
        L53:
            monitor-exit(r5)     // Catch: java.lang.Throwable -> L2e
            throw r6
        */
        throw new UnsupportedOperationException("Method not decompiled: m1.AbstractRunnableC0631c.b(boolean):void");
    }

    protected abstract void c();

    protected abstract void d(boolean z2);

    protected abstract void e(AbstractC0630b abstractC0630b);

    protected abstract void f(InterfaceC0629a interfaceC0629a);

    protected abstract void g();

    protected abstract void i(InterfaceC0629a interfaceC0629a);

    protected abstract InterfaceC0629a j();

    public synchronized void l(InterfaceC0629a interfaceC0629a, AbstractC0630b abstractC0630b, long j2) {
        i(interfaceC0629a);
        abstractC0630b.f5399g = false;
        try {
            try {
                try {
                    this.f5406y.put(interfaceC0629a, abstractC0630b);
                    f(interfaceC0629a);
                    abstractC0630b.f5398b = System.currentTimeMillis() + j2;
                    while (!abstractC0630b.f5399g) {
                        wait(j2);
                        j2 = abstractC0630b.f5398b - System.currentTimeMillis();
                        if (j2 <= 0) {
                            throw new C0632d(this.f5403g + " timedout waiting for response to " + interfaceC0629a);
                        }
                    }
                } catch (InterruptedException e2) {
                    throw new C0632d(e2);
                }
            } catch (IOException e3) {
                if (C0622e.f5384g > 2) {
                    e3.printStackTrace(f5401I);
                }
                try {
                    b(true);
                } catch (IOException e4) {
                    e4.printStackTrace(f5401I);
                }
                throw e3;
            }
        } finally {
            this.f5406y.remove(interfaceC0629a);
        }
    }

    @Override // java.lang.Runnable
    public void run() {
        Thread threadCurrentThread = Thread.currentThread();
        try {
            c();
            synchronized (threadCurrentThread) {
                try {
                    if (threadCurrentThread != this.f5404r) {
                        return;
                    }
                    this.f5402b = 2;
                    threadCurrentThread.notify();
                    h();
                } finally {
                }
            }
        } catch (Exception e2) {
            synchronized (threadCurrentThread) {
                try {
                    if (threadCurrentThread != this.f5404r) {
                        if (C0622e.f5384g >= 2) {
                            e2.printStackTrace(f5401I);
                        }
                    } else {
                        this.f5405x = new C0632d(e2);
                        this.f5402b = 2;
                        threadCurrentThread.notify();
                    }
                } finally {
                }
            }
        } catch (Throwable th) {
            synchronized (threadCurrentThread) {
                try {
                    if (threadCurrentThread != this.f5404r) {
                        return;
                    }
                    this.f5402b = 2;
                    threadCurrentThread.notify();
                    throw th;
                } finally {
                }
            }
        }
    }

    public String toString() {
        return this.f5403g;
    }
}
