/*
* 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.
*/
/**
* Windows implementation of AsynchronousServerSocketChannel using overlapped I/O.
*/
{
// 2 * (sizeof(SOCKET_ADDRESS) + 16)
private final long handle;
private final int completionKey;
// typically there will be zero, or one I/O operations pending. In rare
// cases there may be more. These rare cases arise when a sequence of accept
// operations complete immediately and handled by the initiating thread.
// event has been posted.
private final long dataBuffer;
// flag to indicate that an accept operation is outstanding
super(iocp);
// associate socket with given completion port
int key;
try {
} catch (IOException x) {
closesocket0(h); // prevent leak
throw x;
}
this.handle = h;
this.completionKey = key;
this.ioCache = new PendingIoCache();
}
}
// close socket (which may cause outstanding accept to be aborted).
// waits until the accept operations have completed
// finally disassociate from the completion port
// release other resources
}
return iocp;
}
/**
* Task to initiate accept operation and to handle result.
*/
{
}
void enableAccept() {
}
void closeChildChannel() {
try {
} catch (IOException ignore) { }
}
// caller must have acquired read lock for the listener and child channel.
/**
* in that it requires 2 calls to getsockname and 2 calls to getpeername.
* (should change this to use GetAcceptExSockaddrs)
*/
// permission check (in context of initiating thread)
return null;
}
}, acc);
}
}
/**
* Initiates the accept operation.
*/
public void run() {
long overlapped = 0L;
try {
// begin usage of listener socket
begin();
try {
// begin usage of child socket (as it is registered with
// completion port and so may be closed in the event that
// the group is forcefully closed).
synchronized (result) {
if (n == IOStatus.UNAVAILABLE) {
return;
}
// connection accepted immediately
finishAccept();
// allow another accept before the result is set
enableAccept();
}
} finally {
// end usage on child socket
}
} catch (Throwable x) {
// failed to initiate accept so release resources
if (overlapped != 0L)
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
if (!(x instanceof IOException) && !(x instanceof SecurityException))
x = new IOException(x);
enableAccept();
result.setFailure(x);
} finally {
// end of usage of listener socket
end();
}
// accept completed immediately but may not have executed on
// initiating thread in which case the operation may have been
// cancelled.
if (result.isCancelled()) {
}
// invoke completion handler
}
/**
* Executed when the I/O has completed
*/
try {
// connection accept after group has shutdown
if (iocp.isShutdown()) {
throw new IOException(new ShutdownChannelGroupException());
}
// finish the accept
try {
begin();
try {
finishAccept();
} finally {
}
} finally {
end();
}
// allow another accept before the result is set
enableAccept();
} catch (Throwable x) {
enableAccept();
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
if (!(x instanceof IOException) && !(x instanceof SecurityException))
x = new IOException(x);
result.setFailure(x);
}
// if an async cancel has already cancelled the operation then
// close the new channel so as to free resources
if (result.isCancelled()) {
}
// invoke handler (but not directly)
}
enableAccept();
// release waiters
if (isOpen()) {
result.setFailure(x);
} else {
}
}
}
{
if (!isOpen()) {
return null;
}
if (isAcceptKilled())
throw new RuntimeException("Accept not allowed due to cancellation");
// ensure channel is bound to local address
if (localAddress == null)
throw new NotYetBoundException();
// create the socket that will be accepted. The creation of the socket
// we check that the listener is open and also to prevent the I/O
// port from being closed as the new socket is registered.
try {
begin();
} catch (IOException x) {
ioe = x;
} finally {
end();
}
return null;
}
// 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
// check and set flag to prevent concurrent accepting
if (!accepting.compareAndSet(false, true))
throw new AcceptPendingException();
// initiate I/O
if (Iocp.supportsThreadAgnosticIo()) {
} else {
}
return result;
}
// -- Native methods --
private static native void initIDs();
long acceptSocket) throws IOException;
static {
initIDs();
}
}