< prev index next >
src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java
Print this page
rev 55750 : UDS support, temporary commit
*** 26,40 ****
--- 26,42 ----
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,103 ****
// ID of native thread currently blocked in this channel, for signalling
private long thread;
// Binding
! private InetSocketAddress 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) {
super(sp);
this.fd = Net.serverSocket(true);
this.fdVal = IOUtil.fdVal(fd);
}
--- 85,111 ----
// ID of native thread currently blocked in this channel, for signalling
private long thread;
// Binding
! 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,144 ****
public SocketAddress getLocalAddress() throws IOException {
synchronized (stateLock) {
ensureOpen();
return (localAddress == null)
? null
! : Net.getRevealedLocalAddress(localAddress);
}
}
@Override
public <T> ServerSocketChannel setOption(SocketOption<T> name, T value)
--- 142,152 ----
public SocketAddress getLocalAddress() throws IOException {
synchronized (stateLock) {
ensureOpen();
return (localAddress == null)
? null
! : localAddress instanceof InetSocketAddress ? Net.getRevealedLocalAddress((InetSocketAddress)localAddress) : localAddress;
}
}
@Override
public <T> ServerSocketChannel setOption(SocketOption<T> name, T value)
*** 208,217 ****
--- 216,231 ----
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,228 ****
--- 233,243 ----
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,274 ****
@Override
public SocketChannel accept() throws IOException {
int n = 0;
FileDescriptor newfd = new FileDescriptor();
! InetSocketAddress[] isaa = new InetSocketAddress[1];
acceptLock.lock();
try {
boolean blocking = isBlocking();
try {
--- 279,289 ----
@Override
public SocketChannel accept() throws IOException {
int n = 0;
FileDescriptor newfd = new FileDescriptor();
! SocketAddress[] isaa = new SocketAddress[1];
acceptLock.lock();
try {
boolean blocking = isBlocking();
try {
*** 306,316 ****
* @throws SocketTimeoutException if the timeout expires
*/
SocketChannel blockingAccept(long nanos) throws IOException {
int n = 0;
FileDescriptor newfd = new FileDescriptor();
! InetSocketAddress[] isaa = new InetSocketAddress[1];
acceptLock.lock();
try {
// check that channel is configured blocking
if (!isBlocking())
--- 321,331 ----
* @throws SocketTimeoutException if the timeout expires
*/
SocketChannel blockingAccept(long nanos) throws IOException {
int n = 0;
FileDescriptor newfd = new FileDescriptor();
! SocketAddress[] isaa = new SocketAddress[1];
acceptLock.lock();
try {
// check that channel is configured blocking
if (!isBlocking())
*** 344,364 ****
assert n > 0;
return finishAccept(newfd, isaa[0]);
}
! private SocketChannel finishAccept(FileDescriptor newfd, InetSocketAddress 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());
}
return new SocketChannelImpl(provider(), newfd, isa);
} catch (Exception e) {
nd.close(newfd);
throw e;
--- 359,379 ----
assert n > 0;
return finishAccept(newfd, isaa[0]);
}
! 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 && 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,498 ****
/**
* Returns the local address, or null if not bound
*/
InetSocketAddress localAddress() {
synchronized (stateLock) {
! return localAddress;
}
}
/**
* Translates native poll revent set into a ready operation set
--- 503,513 ----
/**
* Returns the local address, or null if not bound
*/
InetSocketAddress localAddress() {
synchronized (stateLock) {
! return localAddress instanceof InetSocketAddress ? (InetSocketAddress)localAddress : null; // TODO: refactoring/javadoc
}
}
/**
* Translates native poll revent set into a ready operation set
*** 547,569 ****
public int getFDVal() {
return fdVal;
}
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) {
sb.append("unbound");
} else {
! sb.append(Net.getRevealedLocalAddressAsString(addr));
}
}
}
sb.append(']');
return sb.toString();
--- 562,586 ----
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) {
! if (localAddress == null) {
sb.append("unbound");
+ } else if (localAddress instanceof InetSocketAddress) {
+ sb.append(Net.getRevealedLocalAddressAsString((InetSocketAddress)localAddress));
} else {
! sb.append(localAddress);
}
}
}
sb.append(']');
return sb.toString();
< prev index next >