0N/A/*
4963N/A * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.nio.ch;
0N/A
0N/Aimport java.io.FileDescriptor;
0N/Aimport java.io.IOException;
0N/Aimport java.net.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.nio.channels.spi.*;
524N/Aimport java.util.*;
1040N/Aimport sun.net.NetHooks;
0N/A
0N/A
0N/A/**
0N/A * An implementation of ServerSocketChannels
0N/A */
0N/A
0N/Aclass ServerSocketChannelImpl
0N/A extends ServerSocketChannel
0N/A implements SelChImpl
0N/A{
0N/A
0N/A // Used to make native close and configure calls
0N/A private static NativeDispatcher nd;
0N/A
0N/A // Our file descriptor
0N/A private final FileDescriptor fd;
0N/A
0N/A // fd value needed for dev/poll. This value will remain valid
0N/A // even after the value in the file descriptor object has been set to -1
0N/A private int fdVal;
0N/A
0N/A // ID of native thread currently blocked in this channel, for signalling
0N/A private volatile long thread = 0;
0N/A
0N/A // Lock held by thread currently blocked in this channel
0N/A private final Object lock = new Object();
0N/A
0N/A // Lock held by any thread that modifies the state fields declared below
0N/A // DO NOT invoke a blocking I/O operation while holding this lock!
0N/A private final Object stateLock = new Object();
0N/A
0N/A // -- The following fields are protected by stateLock
0N/A
0N/A // Channel state, increases monotonically
0N/A private static final int ST_UNINITIALIZED = -1;
0N/A private static final int ST_INUSE = 0;
0N/A private static final int ST_KILLED = 1;
0N/A private int state = ST_UNINITIALIZED;
0N/A
0N/A // Binding
6319N/A private InetSocketAddress localAddress; // null => unbound
0N/A
6272N/A // set true when exclusive binding is on and SO_REUSEADDR is emulated
6272N/A private boolean isReuseAddress;
6272N/A
0N/A // Our socket adaptor, if any
0N/A ServerSocket socket;
0N/A
0N/A // -- End of fields protected by stateLock
0N/A
0N/A
2736N/A ServerSocketChannelImpl(SelectorProvider sp) throws IOException {
0N/A super(sp);
0N/A this.fd = Net.serverSocket(true);
0N/A this.fdVal = IOUtil.fdVal(fd);
0N/A this.state = ST_INUSE;
0N/A }
0N/A
2736N/A ServerSocketChannelImpl(SelectorProvider sp,
2736N/A FileDescriptor fd,
2736N/A boolean bound)
0N/A throws IOException
0N/A {
0N/A super(sp);
0N/A this.fd = fd;
0N/A this.fdVal = IOUtil.fdVal(fd);
0N/A this.state = ST_INUSE;
2736N/A if (bound)
2736N/A localAddress = Net.localAddress(fd);
0N/A }
0N/A
0N/A public ServerSocket socket() {
0N/A synchronized (stateLock) {
0N/A if (socket == null)
0N/A socket = ServerSocketAdaptor.create(this);
0N/A return socket;
0N/A }
0N/A }
0N/A
524N/A @Override
524N/A public SocketAddress getLocalAddress() throws IOException {
524N/A synchronized (stateLock) {
524N/A if (!isOpen())
893N/A throw new ClosedChannelException();
6319N/A return localAddress == null? localAddress
6319N/A : Net.getRevealedLocalAddress(
6319N/A Net.asInetSocketAddress(localAddress));
524N/A }
524N/A }
524N/A
524N/A @Override
893N/A public <T> ServerSocketChannel setOption(SocketOption<T> name, T value)
524N/A throws IOException
524N/A {
524N/A if (name == null)
524N/A throw new NullPointerException();
893N/A if (!supportedOptions().contains(name))
893N/A throw new UnsupportedOperationException("'" + name + "' not supported");
524N/A synchronized (stateLock) {
524N/A if (!isOpen())
524N/A throw new ClosedChannelException();
6272N/A if (name == StandardSocketOptions.SO_REUSEADDR &&
6272N/A Net.useExclusiveBind())
6272N/A {
6272N/A // SO_REUSEADDR emulated when using exclusive bind
6272N/A isReuseAddress = (Boolean)value;
6272N/A } else {
6272N/A // no options that require special handling
6272N/A Net.setSocketOption(fd, Net.UNSPEC, name, value);
6272N/A }
524N/A return this;
524N/A }
524N/A }
524N/A
524N/A @Override
524N/A @SuppressWarnings("unchecked")
524N/A public <T> T getOption(SocketOption<T> name)
524N/A throws IOException
524N/A {
524N/A if (name == null)
524N/A throw new NullPointerException();
893N/A if (!supportedOptions().contains(name))
893N/A throw new UnsupportedOperationException("'" + name + "' not supported");
524N/A
524N/A synchronized (stateLock) {
524N/A if (!isOpen())
524N/A throw new ClosedChannelException();
6272N/A if (name == StandardSocketOptions.SO_REUSEADDR &&
6272N/A Net.useExclusiveBind())
6272N/A {
6272N/A // SO_REUSEADDR emulated when using exclusive bind
6272N/A return (T)Boolean.valueOf(isReuseAddress);
6272N/A }
524N/A // no options that require special handling
524N/A return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
524N/A }
524N/A }
524N/A
893N/A private static class DefaultOptionsHolder {
524N/A static final Set<SocketOption<?>> defaultOptions = defaultOptions();
524N/A
524N/A private static Set<SocketOption<?>> defaultOptions() {
524N/A HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(2);
4216N/A set.add(StandardSocketOptions.SO_RCVBUF);
4216N/A set.add(StandardSocketOptions.SO_REUSEADDR);
524N/A return Collections.unmodifiableSet(set);
524N/A }
524N/A }
524N/A
524N/A @Override
893N/A public final Set<SocketOption<?>> supportedOptions() {
893N/A return DefaultOptionsHolder.defaultOptions;
524N/A }
524N/A
0N/A public boolean isBound() {
0N/A synchronized (stateLock) {
0N/A return localAddress != null;
0N/A }
0N/A }
0N/A
6319N/A public InetSocketAddress localAddress() {
0N/A synchronized (stateLock) {
0N/A return localAddress;
0N/A }
0N/A }
0N/A
524N/A @Override
524N/A public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException {
0N/A synchronized (lock) {
0N/A if (!isOpen())
0N/A throw new ClosedChannelException();
0N/A if (isBound())
0N/A throw new AlreadyBoundException();
524N/A InetSocketAddress isa = (local == null) ? new InetSocketAddress(0) :
524N/A Net.checkAddress(local);
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null)
0N/A sm.checkListen(isa.getPort());
1040N/A NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort());
0N/A Net.bind(fd, isa.getAddress(), isa.getPort());
524N/A Net.listen(fd, backlog < 1 ? 50 : backlog);
0N/A synchronized (stateLock) {
0N/A localAddress = Net.localAddress(fd);
0N/A }
0N/A }
524N/A return this;
0N/A }
0N/A
0N/A public SocketChannel accept() throws IOException {
0N/A synchronized (lock) {
0N/A if (!isOpen())
0N/A throw new ClosedChannelException();
0N/A if (!isBound())
0N/A throw new NotYetBoundException();
0N/A SocketChannel sc = null;
0N/A
0N/A int n = 0;
0N/A FileDescriptor newfd = new FileDescriptor();
0N/A InetSocketAddress[] isaa = new InetSocketAddress[1];
0N/A
0N/A try {
0N/A begin();
0N/A if (!isOpen())
0N/A return null;
0N/A thread = NativeThread.current();
0N/A for (;;) {
0N/A n = accept0(this.fd, newfd, isaa);
0N/A if ((n == IOStatus.INTERRUPTED) && isOpen())
0N/A continue;
0N/A break;
0N/A }
0N/A } finally {
0N/A thread = 0;
0N/A end(n > 0);
0N/A assert IOStatus.check(n);
0N/A }
0N/A
0N/A if (n < 1)
0N/A return null;
0N/A
0N/A IOUtil.configureBlocking(newfd, true);
0N/A InetSocketAddress isa = isaa[0];
0N/A sc = new SocketChannelImpl(provider(), newfd, isa);
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A try {
0N/A sm.checkAccept(isa.getAddress().getHostAddress(),
0N/A isa.getPort());
0N/A } catch (SecurityException x) {
0N/A sc.close();
0N/A throw x;
0N/A }
0N/A }
0N/A return sc;
0N/A
0N/A }
0N/A }
0N/A
0N/A protected void implConfigureBlocking(boolean block) throws IOException {
0N/A IOUtil.configureBlocking(fd, block);
0N/A }
0N/A
0N/A protected void implCloseSelectableChannel() throws IOException {
0N/A synchronized (stateLock) {
4963N/A if (state != ST_KILLED)
4963N/A nd.preClose(fd);
0N/A long th = thread;
0N/A if (th != 0)
0N/A NativeThread.signal(th);
0N/A if (!isRegistered())
0N/A kill();
0N/A }
0N/A }
0N/A
0N/A public void kill() throws IOException {
0N/A synchronized (stateLock) {
0N/A if (state == ST_KILLED)
0N/A return;
0N/A if (state == ST_UNINITIALIZED) {
0N/A state = ST_KILLED;
0N/A return;
0N/A }
0N/A assert !isOpen() && !isRegistered();
0N/A nd.close(fd);
0N/A state = ST_KILLED;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Translates native poll revent set into a ready operation set
0N/A */
0N/A public boolean translateReadyOps(int ops, int initialOps,
0N/A SelectionKeyImpl sk) {
0N/A int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes
0N/A int oldOps = sk.nioReadyOps();
0N/A int newOps = initialOps;
0N/A
0N/A if ((ops & PollArrayWrapper.POLLNVAL) != 0) {
0N/A // This should only happen if this channel is pre-closed while a
0N/A // selection operation is in progress
0N/A // ## Throw an error if this channel has not been pre-closed
0N/A return false;
0N/A }
0N/A
0N/A if ((ops & (PollArrayWrapper.POLLERR
0N/A | PollArrayWrapper.POLLHUP)) != 0) {
0N/A newOps = intOps;
0N/A sk.nioReadyOps(newOps);
0N/A return (newOps & ~oldOps) != 0;
0N/A }
0N/A
0N/A if (((ops & PollArrayWrapper.POLLIN) != 0) &&
0N/A ((intOps & SelectionKey.OP_ACCEPT) != 0))
0N/A newOps |= SelectionKey.OP_ACCEPT;
0N/A
0N/A sk.nioReadyOps(newOps);
0N/A return (newOps & ~oldOps) != 0;
0N/A }
0N/A
0N/A public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
0N/A return translateReadyOps(ops, sk.nioReadyOps(), sk);
0N/A }
0N/A
0N/A public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
0N/A return translateReadyOps(ops, 0, sk);
0N/A }
0N/A
0N/A /**
0N/A * Translates an interest operation set into a native poll event set
0N/A */
0N/A public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
0N/A int newOps = 0;
0N/A
0N/A // Translate ops
0N/A if ((ops & SelectionKey.OP_ACCEPT) != 0)
0N/A newOps |= PollArrayWrapper.POLLIN;
0N/A // Place ops into pollfd array
0N/A sk.selector.putEventOps(sk, newOps);
0N/A }
0N/A
0N/A public FileDescriptor getFD() {
0N/A return fd;
0N/A }
0N/A
0N/A public int getFDVal() {
0N/A return fdVal;
0N/A }
0N/A
0N/A public String toString() {
0N/A StringBuffer sb = new StringBuffer();
0N/A sb.append(this.getClass().getName());
0N/A sb.append('[');
6319N/A if (!isOpen()) {
0N/A sb.append("closed");
6319N/A } else {
0N/A synchronized (stateLock) {
6319N/A InetSocketAddress addr = localAddress();
6319N/A if (addr == null) {
0N/A sb.append("unbound");
0N/A } else {
6319N/A sb.append(Net.getRevealedLocalAddressAsString(addr));
0N/A }
0N/A }
0N/A }
0N/A sb.append(']');
0N/A return sb.toString();
0N/A }
0N/A
0N/A // -- Native methods --
0N/A
0N/A // Accepts a new connection, setting the given file descriptor to refer to
0N/A // the new socket and setting isaa[0] to the socket's remote address.
0N/A // Returns 1 on success, or IOStatus.UNAVAILABLE (if non-blocking and no
0N/A // connections are pending) or IOStatus.INTERRUPTED.
0N/A //
0N/A private native int accept0(FileDescriptor ssfd, FileDescriptor newfd,
0N/A InetSocketAddress[] isaa)
0N/A throws IOException;
0N/A
0N/A private static native void initIDs();
0N/A
0N/A static {
0N/A Util.load();
0N/A initIDs();
0N/A nd = new SocketDispatcher();
0N/A }
0N/A
0N/A}