package a.c.a.b;

import a.c.b.z.b;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public abstract class h extends Thread {
    public static final int m = 327680;
    public static final int n = 327680;
    private static final int o = 500;
    private static final String p = "AOASocketReadThread";
    private static final String q = "SocketReadThread";
    public static final int r = 8;
    private static final AtomicInteger s = new AtomicInteger(16);

    /* renamed from: a  reason: collision with root package name */
    private final CountDownLatch f62a = new CountDownLatch(1);
    private final byte[] b = new byte[8];
    private final int c;
    private boolean d;
    private final String e;
    private final String f;
    private Socket g = null;
    private BufferedInputStream h = null;
    private BufferedOutputStream i = null;
    private volatile int j = -1;
    private int k = -1;
    private int l = 2;

    public static /* synthetic */ class a {

        /* renamed from: a  reason: collision with root package name */
        public static final /* synthetic */ int[] f63a;

        /* JADX WARNING: Can't wrap try/catch for region: R(18:0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|18) */
        /* JADX WARNING: Code restructure failed: missing block: B:19:?, code lost:
            return;
         */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:11:0x002a */
        /* JADX WARNING: Missing exception handler attribute for start block: B:13:0x0031 */
        /* JADX WARNING: Missing exception handler attribute for start block: B:15:0x0038 */
        /* JADX WARNING: Missing exception handler attribute for start block: B:3:0x000f */
        /* JADX WARNING: Missing exception handler attribute for start block: B:5:0x0016 */
        /* JADX WARNING: Missing exception handler attribute for start block: B:7:0x001c */
        /* JADX WARNING: Missing exception handler attribute for start block: B:9:0x0023 */
        static {
            b.values();
            int[] iArr = new int[10];
            f63a = iArr;
            b bVar = b.RTP;
            iArr[3] = 1;
            int[] iArr2 = f63a;
            b bVar2 = b.RTSP;
            iArr2[2] = 2;
            int[] iArr3 = f63a;
            b bVar3 = b.UIBC;
            iArr3[1] = 3;
            int[] iArr4 = f63a;
            b bVar4 = b.AUTH;
            iArr4[4] = 4;
            int[] iArr5 = f63a;
            b bVar5 = b.CONTROL;
            iArr5[5] = 5;
            int[] iArr6 = f63a;
            b bVar6 = b.MEDIA;
            iArr6[6] = 6;
            int[] iArr7 = f63a;
            b bVar7 = b.SENSOR;
            iArr7[7] = 7;
            int[] iArr8 = f63a;
            b bVar8 = b.CERT;
            iArr8[8] = 8;
        }
    }

    public h(int i2, String str, int i3) {
        this.c = i2;
        String str2 = str + q;
        this.f = str2;
        setName(str2);
        a.a.a.b.a(p, "Create " + str2 + ",ChannelId " + i3);
        this.e = str;
        if (i3 != -1) {
            b(i3);
        }
        this.d = true;
    }

    public int a(int i2) {
        switch (b.a(i2).ordinal()) {
            case 1:
                return 5;
            case 2:
                return 3;
            case 3:
                return 4;
            case 4:
                return 7;
            case 5:
                return 2;
            case 6:
                return 6;
            case 7:
                return 8;
            case 8:
                return 9;
            default:
                return s.getAndIncrement();
        }
    }

    public int a(byte[] bArr, int i2, int i3) {
        try {
            if (this.h != null) {
                int i4 = 0;
                int i5 = i3;
                while (i5 > 0) {
                    int read = this.h.read(bArr, i2 + i4, i5);
                    if (read > 0) {
                        i5 -= read;
                        i4 += read;
                    } else {
                        throw new IOException("Receive Data Error: ret = " + read);
                    }
                }
                if (i4 == i3) {
                    return i4;
                }
                throw new IOException("Expect receive " + i3 + " bytes, but received " + i4 + " bytes");
            }
            throw new IOException("mInputStream is null");
        } catch (Exception e2) {
            a.a.a.b.b(p, this.e + " readData exception:" + e2.getMessage(), e2);
            return -1;
        }
    }

    public void a() {
        try {
            Socket socket = this.g;
            if (socket != null) {
                socket.close();
                this.g = null;
            }
            BufferedInputStream bufferedInputStream = this.h;
            if (bufferedInputStream != null) {
                bufferedInputStream.close();
                this.h = null;
            }
            BufferedOutputStream bufferedOutputStream = this.i;
            if (bufferedOutputStream != null) {
                bufferedOutputStream.close();
                this.i = null;
            }
            this.d = false;
        } catch (Exception e2) {
            a.a.a.b.b(p, "Close " + this.f + " fail. ", e2);
        }
    }

    public int b(byte[] bArr, int i2, int i3) {
        try {
            BufferedInputStream bufferedInputStream = this.h;
            if (bufferedInputStream != null) {
                return bufferedInputStream.read(bArr, i2, i3);
            }
            throw new IOException("mInputStream is null");
        } catch (Exception e2) {
            a.a.a.b.b(p, this.e + " tryReadData exception:" + e2.getMessage(), e2);
            return -1;
        }
    }

    public String b() {
        return "--------------------------------\n" + "Socket name(" + e() + ")\nSocket port(" + f() + ")\nChannel ID(" + c() + ")\nSocket type(" + (j() ? "server" : "client") + ")\nMsg type(" + (k() ? "ucar" : "raw") + ")\n";
    }

    public void b(int i2) {
        if (i2 == -1) {
            a.a.a.b.b(p, "set " + this.e + " channel id failed, use invalid channel id");
        } else if (this.j == -1) {
            this.j = i2;
            System.arraycopy(a.c.c.a.a(this.j), 0, this.b, 0, 4);
        } else {
            a.a.a.b.e(p, "set " + this.e + " channel id failed, channel id already set");
        }
    }

    public int c() {
        return this.j;
    }

    public int c(byte[] bArr, int i2, int i3) {
        try {
            BufferedOutputStream bufferedOutputStream = this.i;
            if (bufferedOutputStream != null) {
                bufferedOutputStream.write(bArr, i2, i3);
                this.i.flush();
                return i3;
            }
            throw new IOException("mOutputStream is null");
        } catch (Exception e2) {
            a.a.a.b.b(p, this.e + " writeData exception:" + e2.getMessage(), e2);
            return -1;
        }
    }

    public void c(int i2) {
        if (i2 == 1 || i2 == 2) {
            this.l = i2;
        } else {
            a.a.a.b.b(p, "invalid msg type");
        }
    }

    public int d() {
        return this.l;
    }

    public void d(int i2) {
        this.k = i2;
    }

    public String e() {
        return this.e;
    }

    public int f() {
        return this.c;
    }

    public int g() {
        return this.k;
    }

    public String h() {
        return this.f;
    }

    public boolean i() {
        return this.d;
    }

    public boolean j() {
        return g() == 1;
    }

    public boolean k() {
        return d() == 1;
    }

    public void l() {
        this.f62a.countDown();
    }

    public void m() {
    }

    public void n() {
        try {
            a.a.a.b.c(p, "Waiting for socket running");
            if (!this.f62a.await(500, TimeUnit.MILLISECONDS)) {
                a.a.a.b.c(p, "Waiting for socket ready timeout");
            }
        } catch (InterruptedException e2) {
            a.a.a.b.b(p, "Socket READY.await() failed.", e2);
        }
    }

    public abstract Socket o();

    public void run() {
        String str;
        a.a.a.b.a(p, "Begin to run in " + this.f);
        if (g() != 2) {
            l();
        }
        Socket o2 = o();
        this.g = o2;
        if (o2 != null) {
            try {
                this.h = new BufferedInputStream(this.g.getInputStream());
                this.i = new BufferedOutputStream(this.g.getOutputStream());
                m();
            } catch (IOException e2) {
                a.a.a.b.b(p, "Get Exception in " + this.f, e2);
            }
        }
        if (g() == 2) {
            l();
        }
        try {
            byte[] bArr = new byte[20];
            byte[] bArr2 = new byte[65536];
            while (true) {
                Socket socket = this.g;
                if (socket != null && this.d) {
                    if (!socket.isConnected()) {
                        str = "socket is disconnected when read data";
                        break;
                    } else if (this.l != 1) {
                        int b2 = b(bArr2, 0, 65536);
                        if (b2 >= 0) {
                            a.c.c.a.a(b2, this.b, 4);
                            if (c.e().a(this.b, 8, bArr2, Math.max(b2, 8)) < 0) {
                                str = "raw data bulkTransferOut fail";
                                break;
                            }
                        } else {
                            return;
                        }
                    } else if (a(bArr, 0, 20) >= 0) {
                        int a2 = a.c.c.a.a(bArr, 0);
                        int i2 = a2 - 20;
                        a.c.c.a.a(a2, this.b, 4);
                        if (bArr.length < a2) {
                            byte[] bArr3 = new byte[a2];
                            System.arraycopy(bArr, 0, bArr3, 0, 20);
                            bArr = bArr3;
                        }
                        if (a(bArr, 20, i2) >= 0) {
                            if (c.e().a(this.b, 8, bArr, a2) < 0) {
                                str = "ucar data bulkTransferOut fail";
                                break;
                            }
                        } else {
                            return;
                        }
                    } else {
                        return;
                    }
                } else {
                    return;
                }
            }
            a.a.a.b.b(p, str);
        } catch (Exception e3) {
            a.a.a.b.b(p, "get Exception in ReadThread", e3);
        }
    }

    public String toString() {
        return b();
    }
}
