/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Unix implementation of AsynchronousServerSocketChannel
*/
implements Port.PollableChannel
{
private final int fdVal;
// flag to indicate an accept is outstanding
private void enableAccept() {
}
// used to ensure that the context for an asynchronous accept is visible
// the pooled thread that handles the I/O event
// pending accept
private boolean acceptPending;
// context for permission check when security manager set
throws IOException
{
super(port);
try {
} catch (IOException x) {
throw x;
}
// add mapping from file descriptor to this channel
}
// remove the mapping
// close file descriptor
// if there is a pending accept then complete it
synchronized (updateLock) {
if (!acceptPending)
return; // no pending accept
acceptPending = false;
}
// discard the stack trace as otherwise it may appear that implClose
// has thrown the exception.
future.setFailure(x);
} else {
// invoke by submitting task rather than directly
}
}
return port;
}
/**
* Invoked by event handling thread when listener socket is polled
*/
synchronized (updateLock) {
if (!acceptPending)
return; // may have been grabbed by asynchronous close
acceptPending = false;
}
// attempt to accept connection
try {
begin();
// spurious wakeup, is this possible?
if (n == IOStatus.UNAVAILABLE) {
synchronized (updateLock) {
acceptPending = true;
}
return;
}
} catch (Throwable x) {
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
exc = x;
} finally {
end();
}
// Connection accepted so finish it when not holding locks.
try {
} catch (Throwable x) {
if (!(x instanceof IOException) && !(x instanceof SecurityException))
x = new IOException(x);
exc = x;
}
}
// copy field befores accept is re-renabled
// re-enable accepting and invoke handler
enableAccept();
// if an async cancel has already cancelled the operation then
// close the new channel so as to free resources
try {
} catch (IOException ignore) { }
}
} else {
}
}
/**
* Completes the accept by creating the AsynchronousSocketChannel for
* the given file descriptor and remote address. If this method completes
* will be closed.
*/
final InetSocketAddress remote,
throws IOException, SecurityException
{
try {
} catch (IOException x) {
throw x;
}
// permission check must always be in initiator's context
try {
}
return null;
}
}, acc);
} else {
}
}
} catch (SecurityException x) {
try {
} catch (Throwable suppressed) {
}
throw x;
}
return ch;
}
{
// complete immediately if channel is closed
if (!isOpen()) {
Throwable e = new ClosedChannelException();
return CompletedFuture.withFailure(e);
} else {
return null;
}
}
if (localAddress == null)
throw new NotYetBoundException();
// cancel was invoked with pending accept so connection may have been
// dropped.
if (isAcceptKilled())
throw new RuntimeException("Accept not allowed due cancellation");
// check and set flag to prevent concurrent accepting
if (!accepting.compareAndSet(false, true))
throw new AcceptPendingException();
// attempt accept
try {
begin();
if (n == IOStatus.UNAVAILABLE) {
// need calling context when there is security manager as
// permission check may be done in a different thread without
// any application call frames on the stack
synchronized (updateLock) {
this.acceptHandler = null;
this.acceptFuture = result;
} else {
this.acceptHandler = handler;
this.acceptAttachment = att;
}
this.acceptPending = true;
}
// register for connections
return result;
}
} catch (Throwable x) {
// accept failed
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
exc = x;
} finally {
end();
}
// connection accepted immediately
try {
} catch (Throwable x) {
exc = x;
}
}
// re-enable accepting before invoking handler
enableAccept();
} else {
return null;
}
}
// -- Native methods --
private static native void initIDs();
// Accepts a new connection, setting the given file descriptor to refer to
// the new socket and setting isaa[0] to the socket's remote address.
// Returns 1 on success, or IOStatus.UNAVAILABLE.
//
throws IOException;
static {
initIDs();
}
}