Merge "LocalSocket: Add support for SOCK_DGRAM and SOCK_SEQPACKET"
This commit is contained in:
committed by
Android (Google) Code Review
commit
757ec7837d
@@ -46,7 +46,7 @@ public class LocalServerSocket {
|
||||
{
|
||||
impl = new LocalSocketImpl();
|
||||
|
||||
impl.create(true);
|
||||
impl.create(LocalSocket.SOCKET_STREAM);
|
||||
|
||||
localAddress = new LocalSocketAddress(name);
|
||||
impl.bind(localAddress);
|
||||
@@ -93,7 +93,7 @@ public class LocalServerSocket {
|
||||
|
||||
impl.accept (acceptedImpl);
|
||||
|
||||
return new LocalSocket(acceptedImpl);
|
||||
return new LocalSocket(acceptedImpl, LocalSocket.SOCKET_UNKNOWN);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,21 +34,42 @@ public class LocalSocket implements Closeable {
|
||||
private LocalSocketAddress localAddress;
|
||||
private boolean isBound;
|
||||
private boolean isConnected;
|
||||
private final int sockType;
|
||||
|
||||
/** unknown socket type (used for constructor with existing file descriptor) */
|
||||
/* package */ static final int SOCKET_UNKNOWN = 0;
|
||||
/** Datagram socket type */
|
||||
public static final int SOCKET_DGRAM = 1;
|
||||
/** Stream socket type */
|
||||
public static final int SOCKET_STREAM = 2;
|
||||
/** Sequential packet socket type */
|
||||
public static final int SOCKET_SEQPACKET = 3;
|
||||
|
||||
/**
|
||||
* Creates a AF_LOCAL/UNIX domain stream socket.
|
||||
*/
|
||||
public LocalSocket() {
|
||||
this(new LocalSocketImpl());
|
||||
this(SOCKET_STREAM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a AF_LOCAL/UNIX domain stream socket with given socket type
|
||||
*
|
||||
* @param sockType either {@link #SOCKET_DGRAM}, {@link #SOCKET_STREAM}
|
||||
* or {@link #SOCKET_SEQPACKET}
|
||||
*/
|
||||
public LocalSocket(int sockType) {
|
||||
this(new LocalSocketImpl(), sockType);
|
||||
isBound = false;
|
||||
isConnected = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a AF_LOCAL/UNIX domain stream socket with FileDescriptor.
|
||||
* @hide
|
||||
*/
|
||||
public LocalSocket(FileDescriptor fd) throws IOException {
|
||||
this(new LocalSocketImpl(fd));
|
||||
this(new LocalSocketImpl(fd), SOCKET_UNKNOWN);
|
||||
isBound = true;
|
||||
isConnected = true;
|
||||
}
|
||||
@@ -57,8 +78,9 @@ public class LocalSocket implements Closeable {
|
||||
* for use with AndroidServerSocket
|
||||
* @param impl a SocketImpl
|
||||
*/
|
||||
/*package*/ LocalSocket(LocalSocketImpl impl) {
|
||||
/*package*/ LocalSocket(LocalSocketImpl impl, int sockType) {
|
||||
this.impl = impl;
|
||||
this.sockType = sockType;
|
||||
this.isConnected = false;
|
||||
this.isBound = false;
|
||||
}
|
||||
@@ -81,7 +103,7 @@ public class LocalSocket implements Closeable {
|
||||
synchronized (this) {
|
||||
if (!implCreated) {
|
||||
try {
|
||||
impl.create(true);
|
||||
impl.create(sockType);
|
||||
} finally {
|
||||
implCreated = true;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,10 @@ import java.io.InputStream;
|
||||
import java.io.FileDescriptor;
|
||||
import java.net.SocketOptions;
|
||||
|
||||
import libcore.io.ErrnoException;
|
||||
import libcore.io.Libcore;
|
||||
import libcore.io.OsConstants;
|
||||
|
||||
/**
|
||||
* Socket implementation used for android.net.LocalSocket and
|
||||
* android.net.LocalServerSocket. Supports only AF_LOCAL sockets.
|
||||
@@ -159,7 +163,6 @@ class LocalSocketImpl
|
||||
|
||||
private native int pending_native(FileDescriptor fd) throws IOException;
|
||||
private native int available_native(FileDescriptor fd) throws IOException;
|
||||
private native void close_native(FileDescriptor fd) throws IOException;
|
||||
private native int read_native(FileDescriptor fd) throws IOException;
|
||||
private native int readba_native(byte[] b, int off, int len,
|
||||
FileDescriptor fd) throws IOException;
|
||||
@@ -171,8 +174,6 @@ class LocalSocketImpl
|
||||
int namespace) throws IOException;
|
||||
private native void bindLocal(FileDescriptor fd, String name, int namespace)
|
||||
throws IOException;
|
||||
private native FileDescriptor create_native(boolean stream)
|
||||
throws IOException;
|
||||
private native void listen_native(FileDescriptor fd, int backlog)
|
||||
throws IOException;
|
||||
private native void shutdown(FileDescriptor fd, boolean shutdownInput);
|
||||
@@ -222,15 +223,33 @@ class LocalSocketImpl
|
||||
/**
|
||||
* Creates a socket in the underlying OS.
|
||||
*
|
||||
* @param stream true if this should be a stream socket, false for
|
||||
* datagram.
|
||||
* @param sockType either {@link LocalSocket#SOCKET_DGRAM}, {@link LocalSocket#SOCKET_STREAM}
|
||||
* or {@link LocalSocket#SOCKET_SEQPACKET}
|
||||
* @throws IOException
|
||||
*/
|
||||
public void create (boolean stream) throws IOException {
|
||||
public void create (int sockType) throws IOException {
|
||||
// no error if socket already created
|
||||
// need this for LocalServerSocket.accept()
|
||||
if (fd == null) {
|
||||
fd = create_native(stream);
|
||||
int osType;
|
||||
switch (sockType) {
|
||||
case LocalSocket.SOCKET_DGRAM:
|
||||
osType = OsConstants.SOCK_DGRAM;
|
||||
break;
|
||||
case LocalSocket.SOCKET_STREAM:
|
||||
osType = OsConstants.SOCK_STREAM;
|
||||
break;
|
||||
case LocalSocket.SOCKET_SEQPACKET:
|
||||
osType = OsConstants.SOCK_SEQPACKET;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("unknown sockType");
|
||||
}
|
||||
try {
|
||||
fd = Libcore.os.socket(OsConstants.AF_UNIX, osType, 0);
|
||||
} catch (ErrnoException e) {
|
||||
e.rethrowAsIOException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,7 +261,11 @@ class LocalSocketImpl
|
||||
public void close() throws IOException {
|
||||
synchronized (LocalSocketImpl.this) {
|
||||
if (fd == null) return;
|
||||
close_native(fd);
|
||||
try {
|
||||
Libcore.os.close(fd);
|
||||
} catch (ErrnoException e) {
|
||||
e.rethrowAsIOException();
|
||||
}
|
||||
fd = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user