package a.c.a.b;

import a.a.a.b;
import android.text.TextUtils;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class f extends h {
    private static final String t = "AOALocalClientSocketReadThread";
    private static final int u = 20;
    private static final int v = 100;
    private static final String w = "\\.";
    private static final int x = 4;

    public f(int i, String str, int i2, int i3) {
        super(i, str, i2);
        d(2);
        if (c() == -1) {
            b.b(t, "invalid channel id");
        }
        c(i3);
    }

    private static InetSocketAddress a(String str, int i) throws UnknownHostException {
        String[] split = str.split(w);
        if (split.length == 4) {
            byte[] bArr = new byte[4];
            for (int i2 = 0; i2 < 4; i2++) {
                if (TextUtils.isDigitsOnly(split[i2])) {
                    bArr[i2] = (byte) (Integer.parseInt(split[i2]) & 255);
                } else {
                    throw new UnknownHostException("invalid ip address");
                }
            }
            return new InetSocketAddress(InetAddress.getByAddress(bArr), i);
        }
        throw new UnknownHostException("ip address is of illegal length");
    }

    @Override // a.c.a.b.h
    public Socket o() {
        int i = 20;
        while (true) {
            int i2 = i - 1;
            if (i <= 0 || !i()) {
                b.b(t, "Connect socket error: " + e());
            } else {
                try {
                    Socket socket = new Socket();
                    socket.connect(a("127.0.0.1", f()), 100);
                    socket.setTcpNoDelay(true);
                    socket.setSendBufferSize(327680);
                    socket.setReceiveBufferSize(327680);
                    return socket;
                } catch (IOException e) {
                    b.a(t, "start Connect socket fail " + e(), e);
                    try {
                        if (i()) {
                            Thread.sleep(100);
                        }
                    } catch (InterruptedException e2) {
                        b.a(t, "sleep exception: " + e(), e2);
                    }
                    i = i2;
                }
            }
        }
        b.b(t, "Connect socket error: " + e());
        return null;
    }
}
