Add a module-lib API for constructing a LocalSocket from an fd. am: 8093d78d1c
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1954386 Change-Id: I9fe0e6a22859a82b128b163bc1036712737aa946
This commit is contained in:
@@ -240,6 +240,10 @@ package android.net {
|
|||||||
method public int getResourceId();
|
method public int getResourceId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class LocalSocket implements java.io.Closeable {
|
||||||
|
ctor public LocalSocket(@NonNull java.io.FileDescriptor);
|
||||||
|
}
|
||||||
|
|
||||||
public class NetworkIdentity {
|
public class NetworkIdentity {
|
||||||
method public int getOemManaged();
|
method public int getOemManaged();
|
||||||
method public int getRatType();
|
method public int getRatType();
|
||||||
|
|||||||
@@ -55,7 +55,9 @@ public class LocalServerSocket implements Closeable {
|
|||||||
* Create a LocalServerSocket from a file descriptor that's already
|
* Create a LocalServerSocket from a file descriptor that's already
|
||||||
* been created and bound. listen() will be called immediately on it.
|
* been created and bound. listen() will be called immediately on it.
|
||||||
* Used for cases where file descriptors are passed in via environment
|
* Used for cases where file descriptors are passed in via environment
|
||||||
* variables
|
* variables. The passed-in FileDescriptor is not managed by this class
|
||||||
|
* and must be closed by the caller. Calling {@link #close()} on a socket
|
||||||
|
* created by this method has no effect.
|
||||||
*
|
*
|
||||||
* @param fd bound file descriptor
|
* @param fd bound file descriptor
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
|
|||||||
@@ -16,7 +16,14 @@
|
|||||||
|
|
||||||
package android.net;
|
package android.net;
|
||||||
|
|
||||||
|
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
import android.annotation.SystemApi;
|
||||||
import android.compat.annotation.UnsupportedAppUsage;
|
import android.compat.annotation.UnsupportedAppUsage;
|
||||||
|
import android.system.ErrnoException;
|
||||||
|
import android.system.Os;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
@@ -74,32 +81,52 @@ public class LocalSocket implements Closeable {
|
|||||||
this.isBound = false;
|
this.isBound = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void checkConnected() {
|
||||||
|
try {
|
||||||
|
Os.getpeername(impl.getFileDescriptor());
|
||||||
|
} catch (ErrnoException e) {
|
||||||
|
throw new IllegalArgumentException("Not a connected socket", e);
|
||||||
|
}
|
||||||
|
isConnected = true;
|
||||||
|
isBound = true;
|
||||||
|
implCreated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a LocalSocket instance using the {@link FileDescriptor} for an already-connected
|
||||||
|
* AF_LOCAL/UNIX domain stream socket. The passed-in FileDescriptor is not managed by this class
|
||||||
|
* and must be closed by the caller. Calling {@link #close()} on a socket created by this
|
||||||
|
* method has no effect.
|
||||||
|
*
|
||||||
|
* @param fd the filedescriptor to adopt
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
|
public LocalSocket(@NonNull @SuppressLint("UseParcelFileDescriptor") FileDescriptor fd) {
|
||||||
|
this(new LocalSocketImpl(fd), SOCKET_UNKNOWN);
|
||||||
|
checkConnected();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a LocalSocket instances using the FileDescriptor for an already-connected
|
* Creates a LocalSocket instances using the FileDescriptor for an already-connected
|
||||||
* AF_LOCAL/UNIX domain stream socket. Note: the FileDescriptor must be closed by the caller:
|
* AF_LOCAL/UNIX domain stream socket. Note: the FileDescriptor must be closed by the caller:
|
||||||
* closing the LocalSocket will not close it.
|
* closing the LocalSocket will not close it.
|
||||||
*
|
*
|
||||||
* @hide - used by BluetoothSocket.
|
* TODO: delete this method when Bluetooth is no longer using it.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
*/
|
*/
|
||||||
public static LocalSocket createConnectedLocalSocket(FileDescriptor fd) {
|
public static LocalSocket createConnectedLocalSocket(FileDescriptor fd) {
|
||||||
return createConnectedLocalSocket(new LocalSocketImpl(fd), SOCKET_UNKNOWN);
|
return new LocalSocket(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* for use with LocalServerSocket.accept()
|
* for use with LocalServerSocket.accept()
|
||||||
*/
|
*/
|
||||||
static LocalSocket createLocalSocketForAccept(LocalSocketImpl impl) {
|
static LocalSocket createLocalSocketForAccept(LocalSocketImpl impl) {
|
||||||
return createConnectedLocalSocket(impl, SOCKET_UNKNOWN);
|
LocalSocket socket = new LocalSocket(impl, SOCKET_UNKNOWN);
|
||||||
}
|
socket.checkConnected();
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a LocalSocket from an existing LocalSocketImpl that is already connected.
|
|
||||||
*/
|
|
||||||
private static LocalSocket createConnectedLocalSocket(LocalSocketImpl impl, int sockType) {
|
|
||||||
LocalSocket socket = new LocalSocket(impl, sockType);
|
|
||||||
socket.isConnected = true;
|
|
||||||
socket.isBound = true;
|
|
||||||
socket.implCreated = true;
|
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user