下面列出了怎么用android.net.LocalSocketAddress的API类实例代码及写法,或者点击链接到github查看源代码。
private LocalSocket connectService() {
LocalSocket socket = new LocalSocket();
boolean done = false;
// The uncrypt socket will be created by init upon receiving the
// service request. It may not be ready by this point. So we will
// keep retrying until success or reaching timeout.
for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
try {
socket.connect(new LocalSocketAddress(UNCRYPT_SOCKET,
LocalSocketAddress.Namespace.RESERVED));
done = true;
break;
} catch (IOException ignored) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Slog.w(TAG, "Interrupted:", e);
}
}
}
if (!done) {
Slog.e(TAG, "Timed out connecting to uncrypt socket");
return null;
}
return socket;
}
/**
* Try connecting to the Zygote over and over again until we hit a time-out.
* @param address The name of the socket to connect to.
*/
public static void waitForConnectionToZygote(LocalSocketAddress address) {
for (int n = 20; n >= 0; n--) {
try {
final ZygoteState zs = ZygoteState.connect(address);
zs.close();
return;
} catch (IOException ioe) {
Log.w(LOG_TAG,
"Got error connecting to zygote, retrying. msg= " + ioe.getMessage());
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
}
Slog.wtf(LOG_TAG, "Failed to connect to Zygote through socket " + address.getName());
}
/**
* Starts a new zygote process as a child of this zygote. This is used to create
* secondary zygotes that inherit data from the zygote that this object
* communicates with. This returns a new ZygoteProcess representing a connection
* to the newly created zygote. Throws an exception if the zygote cannot be started.
*/
public ChildZygoteProcess startChildZygote(final String processClass,
final String niceName,
int uid, int gid, int[] gids,
int runtimeFlags,
String seInfo,
String abi,
String instructionSet) {
// Create an unguessable address in the global abstract namespace.
final LocalSocketAddress serverAddress = new LocalSocketAddress(
processClass + "/" + UUID.randomUUID().toString());
final String[] extraArgs = {Zygote.CHILD_ZYGOTE_SOCKET_NAME_ARG + serverAddress.getName()};
Process.ProcessStartResult result;
try {
result = startViaZygote(processClass, niceName, uid, gid,
gids, runtimeFlags, 0 /* mountExternal */, 0 /* targetSdkVersion */, seInfo,
abi, instructionSet, null /* appDataDir */, null /* invokeWith */,
true /* startChildZygote */, extraArgs);
} catch (ZygoteStartFailedEx ex) {
throw new RuntimeException("Starting child-zygote through Zygote failed", ex);
}
return new ChildZygoteProcess(serverAddress, result.pid);
}
/**
* init server socket
*
* @return init failed return false.
*/
private boolean initServerSocket() {
// if not running, do not init
if (!isRunning) {
return false;
}
try {
LocalSocket localSocket = new LocalSocket();
localSocket.bind(new LocalSocketAddress(PATH, LocalSocketAddress.Namespace.FILESYSTEM));
serverSocket = new LocalServerSocket(localSocket.getFileDescriptor());
return true;
} catch (IOException e) {
VayLog.e(TAG, "unable to bind", e);
return false;
}
}
/**
* init server socket
*
* @return init failed return false.
*/
private boolean initServerSocket() {
// if not running, do not init
if (!isRunning) {
return false;
}
try {
LocalSocket localSocket = new LocalSocket();
localSocket.bind(new LocalSocketAddress(PATH, LocalSocketAddress.Namespace.FILESYSTEM));
serverSocket = new LocalServerSocket(localSocket.getFileDescriptor());
return true;
} catch (IOException e) {
VayLog.e(TAG, "unable to bind", e);
app.track(e);
return false;
}
}
/**
* init server socket
*
* @return init failed return false.
*/
private boolean initServerSocket() {
// if not running, do not init
if (!isRunning) {
return false;
}
try {
LocalSocket localSocket = new LocalSocket();
localSocket.bind(new LocalSocketAddress(PATH, LocalSocketAddress.Namespace.FILESYSTEM));
serverSocket = new LocalServerSocket(localSocket.getFileDescriptor());
return true;
} catch (IOException e) {
VayLog.e(TAG, "unable to bind", e);
return false;
}
}
/**
* init server socket
*
* @return init failed return false.
*/
private boolean initServerSocket() {
// if not running, do not init
if (!isRunning) {
return false;
}
try {
LocalSocket localSocket = new LocalSocket();
localSocket.bind(new LocalSocketAddress(PATH, LocalSocketAddress.Namespace.FILESYSTEM));
serverSocket = new LocalServerSocket(localSocket.getFileDescriptor());
return true;
} catch (IOException e) {
VayLog.e(TAG, "unable to bind", e);
ShadowsocksApplication.app.track(e);
return false;
}
}
protected void createSockets() throws IOException {
final String LOCAL_ADDR = "net.majorkernelpanic.streaming-";
for (int i=0;i<10;i++) {
try {
mSocketId = new Random().nextInt();
mLss = new LocalServerSocket(LOCAL_ADDR+mSocketId);
break;
} catch (IOException e1) {}
}
mReceiver = new LocalSocket();
mReceiver.connect( new LocalSocketAddress(LOCAL_ADDR+mSocketId));
mReceiver.setReceiveBufferSize(500000);
mReceiver.setSoTimeout(3000);
mSender = mLss.accept();
mSender.setSendBufferSize(500000);
}
private LocalSocketAddress determineSocketAddress() {
// If we're testing, set up a socket in a namespace that's accessible to test code.
// In order to ensure that unprivileged apps aren't able to impersonate native daemons on
// production devices, even if said native daemons ill-advisedly pick a socket name that
// starts with __test__, only allow this on debug builds.
if (mSocket.startsWith("__test__") && Build.IS_DEBUGGABLE) {
return new LocalSocketAddress(mSocket);
} else {
return new LocalSocketAddress(mSocket, LocalSocketAddress.Namespace.RESERVED);
}
}
private static boolean openLmkdSocket() {
try {
sLmkdSocket = new LocalSocket(LocalSocket.SOCKET_SEQPACKET);
sLmkdSocket.connect(
new LocalSocketAddress("lmkd",
LocalSocketAddress.Namespace.RESERVED));
sLmkdOutputStream = sLmkdSocket.getOutputStream();
} catch (IOException ex) {
Slog.w(TAG, "lowmemorykiller daemon socket open failed");
sLmkdSocket = null;
return false;
}
return true;
}
protected void createSockets() throws IOException {
if (sPipeApi == PIPE_API_LS) {
final String LOCAL_ADDR = "net.majorkernelpanic.streaming-";
for (int i=0;i<10;i++) {
try {
mSocketId = new Random().nextInt();
mLss = new LocalServerSocket(LOCAL_ADDR+mSocketId);
break;
} catch (IOException e1) {}
}
mReceiver = new LocalSocket();
mReceiver.connect( new LocalSocketAddress(LOCAL_ADDR+mSocketId));
mReceiver.setReceiveBufferSize(500000);
mReceiver.setSoTimeout(3000);
mSender = mLss.accept();
mSender.setSendBufferSize(500000);
} else {
Log.e(TAG, "parcelFileDescriptors createPipe version = Lollipop");
mParcelFileDescriptors = ParcelFileDescriptor.createPipe();
mParcelRead = new ParcelFileDescriptor(mParcelFileDescriptors[0]);
mParcelWrite = new ParcelFileDescriptor(mParcelFileDescriptors[1]);
}
}
ChildZygoteProcess(LocalSocketAddress socketAddress, int pid) {
super(socketAddress, null);
mPid = pid;
}
public ZygoteProcess(String primarySocket, String secondarySocket) {
this(new LocalSocketAddress(primarySocket, LocalSocketAddress.Namespace.RESERVED),
new LocalSocketAddress(secondarySocket, LocalSocketAddress.Namespace.RESERVED));
}
public ZygoteProcess(LocalSocketAddress primarySocket, LocalSocketAddress secondarySocket) {
mSocket = primarySocket;
mSecondarySocket = secondarySocket;
}
public LocalSocketAddress getPrimarySocketAddress() {
return mSocket;
}
/**
* Try connecting to the Zygote over and over again until we hit a time-out.
* @param socketName The name of the socket to connect to.
*/
public static void waitForConnectionToZygote(String socketName) {
final LocalSocketAddress address =
new LocalSocketAddress(socketName, LocalSocketAddress.Namespace.RESERVED);
waitForConnectionToZygote(address);
}
public LocalSocketUtil(String name) throws UnknownHostException,
IOException {
socket = new LocalSocket();
socket.connect(new LocalSocketAddress(name));
}
public LocalSocketThread(String socketPath) {
mSocketPath = new LocalSocketAddress(socketPath);
mInputStream = null;
mOutputStream = null;
mMessages = new HashMap<>();
}
public void connect() throws IOException {
localSocket.connect(new LocalSocketAddress(LOCAL_ABSTRACT_NAME));
readClientId(localSocket.getInputStream());
}