< prev index next >
src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java
Print this page
rev 55750 : UDS support, temporary commit
@@ -26,15 +26,17 @@
package sun.nio.ch;
import java.io.FileDescriptor;
import java.io.IOException;
import java.net.InetSocketAddress;
+import java.net.ProtocolFamily;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.net.SocketOption;
import java.net.SocketTimeoutException;
import java.net.StandardSocketOptions;
+import java.net.UnixSocketAddress;
import java.nio.channels.AlreadyBoundException;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.IllegalBlockingModeException;
import java.nio.channels.NotYetBoundException;
@@ -83,21 +85,27 @@
// ID of native thread currently blocked in this channel, for signalling
private long thread;
// Binding
- private InetSocketAddress localAddress; // null => unbound
+ private SocketAddress localAddress; // null => unbound
// set true when exclusive binding is on and SO_REUSEADDR is emulated
private boolean isReuseAddress;
// Our socket adaptor, if any
private ServerSocket socket;
// -- End of fields protected by stateLock
+ ServerSocketChannelImpl(SelectorProvider sp, ProtocolFamily family) {
+ super(sp);
+ this.fd = Net.serverSocket(true, family);
+ this.fdVal = IOUtil.fdVal(fd);
+ }
+
ServerSocketChannelImpl(SelectorProvider sp) {
super(sp);
this.fd = Net.serverSocket(true);
this.fdVal = IOUtil.fdVal(fd);
}
@@ -134,11 +142,11 @@
public SocketAddress getLocalAddress() throws IOException {
synchronized (stateLock) {
ensureOpen();
return (localAddress == null)
? null
- : Net.getRevealedLocalAddress(localAddress);
+ : localAddress instanceof InetSocketAddress ? Net.getRevealedLocalAddress((InetSocketAddress)localAddress) : localAddress;
}
}
@Override
public <T> ServerSocketChannel setOption(SocketOption<T> name, T value)
@@ -208,10 +216,16 @@
public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException {
synchronized (stateLock) {
ensureOpen();
if (localAddress != null)
throw new AlreadyBoundException();
+ if (local instanceof UnixSocketAddress) {
+ // TODO: SecurityManager?
+ Net.bind(fd, (UnixSocketAddress)local);
+ Net.listen(fd, backlog < 1 ? 50 : backlog);
+ localAddress = local;
+ } else {
InetSocketAddress isa = (local == null)
? new InetSocketAddress(0)
: Net.checkAddress(local);
SecurityManager sm = System.getSecurityManager();
if (sm != null)
@@ -219,10 +233,11 @@
NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort());
Net.bind(fd, isa.getAddress(), isa.getPort());
Net.listen(fd, backlog < 1 ? 50 : backlog);
localAddress = Net.localAddress(fd);
}
+ }
return this;
}
/**
* Marks the beginning of an I/O operation that might block.
@@ -264,11 +279,11 @@
@Override
public SocketChannel accept() throws IOException {
int n = 0;
FileDescriptor newfd = new FileDescriptor();
- InetSocketAddress[] isaa = new InetSocketAddress[1];
+ SocketAddress[] isaa = new SocketAddress[1];
acceptLock.lock();
try {
boolean blocking = isBlocking();
try {
@@ -306,11 +321,11 @@
* @throws SocketTimeoutException if the timeout expires
*/
SocketChannel blockingAccept(long nanos) throws IOException {
int n = 0;
FileDescriptor newfd = new FileDescriptor();
- InetSocketAddress[] isaa = new InetSocketAddress[1];
+ SocketAddress[] isaa = new SocketAddress[1];
acceptLock.lock();
try {
// check that channel is configured blocking
if (!isBlocking())
@@ -344,21 +359,21 @@
assert n > 0;
return finishAccept(newfd, isaa[0]);
}
- private SocketChannel finishAccept(FileDescriptor newfd, InetSocketAddress isa)
+ private SocketChannel finishAccept(FileDescriptor newfd, SocketAddress isa)
throws IOException
{
try {
// newly accepted socket is initially in blocking mode
IOUtil.configureBlocking(newfd, true);
// check permitted to accept connections from the remote address
SecurityManager sm = System.getSecurityManager();
- if (sm != null) {
- sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort());
+ if (sm != null && isa instanceof InetSocketAddress) { // TODO: SecurityManager: permissions for UDS?
+ sm.checkAccept(((InetSocketAddress) isa).getAddress().getHostAddress(), ((InetSocketAddress) isa).getPort());
}
return new SocketChannelImpl(provider(), newfd, isa);
} catch (Exception e) {
nd.close(newfd);
throw e;
@@ -488,11 +503,11 @@
/**
* Returns the local address, or null if not bound
*/
InetSocketAddress localAddress() {
synchronized (stateLock) {
- return localAddress;
+ return localAddress instanceof InetSocketAddress ? (InetSocketAddress)localAddress : null; // TODO: refactoring/javadoc
}
}
/**
* Translates native poll revent set into a ready operation set
@@ -547,23 +562,25 @@
public int getFDVal() {
return fdVal;
}
+ @Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.getClass().getName());
sb.append('[');
if (!isOpen()) {
sb.append("closed");
} else {
synchronized (stateLock) {
- InetSocketAddress addr = localAddress;
- if (addr == null) {
+ if (localAddress == null) {
sb.append("unbound");
+ } else if (localAddress instanceof InetSocketAddress) {
+ sb.append(Net.getRevealedLocalAddressAsString((InetSocketAddress)localAddress));
} else {
- sb.append(Net.getRevealedLocalAddressAsString(addr));
+ sb.append(localAddress);
}
}
}
sb.append(']');
return sb.toString();
< prev index next >