/*
* 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.
*/
/**
* Base implementation of AsynchronousSocketChannel
*/
abstract class AsynchronousSocketChannelImpl
extends AsynchronousSocketChannel
implements Cancellable, Groupable
{
// protects state, localAddress, and remoteAddress
// State, increases monotonically
// reading state
private boolean reading;
private boolean readShutdown;
// writing state
private boolean writing;
private boolean writeShutdown;
// close support
private volatile boolean open = true;
// set true when exclusive binding is on and SO_REUSEADDR is emulated
private boolean isReuseAddress;
throws IOException
{
this.state = ST_UNCONNECTED;
}
// Constructor for sockets obtained from AsynchronousServerSocketChannelImpl
throws IOException
{
this.state = ST_CONNECTED;
this.remoteAddress = remote;
}
public final boolean isOpen() {
return open;
}
/**
* Marks beginning of access to file descriptor/handle
*/
if (!isOpen())
throw new ClosedChannelException();
}
/**
* Marks end of access to file descriptor/handle
*/
final void end() {
}
/**
* Invoked to close socket and release other resources.
*/
// synchronize with any threads initiating asynchronous operations
try {
if (!open)
return; // already closed
open = false;
} finally {
}
implClose();
}
synchronized (readLock) {
reading = false;
if (killed)
readKilled = true;
}
}
final void enableReading() {
enableReading(false);
}
synchronized (writeLock) {
writing = false;
if (killed)
writeKilled = true;
}
}
final void enableWriting() {
enableWriting(false);
}
final void killReading() {
synchronized (readLock) {
readKilled = true;
}
}
final void killWriting() {
synchronized (writeLock) {
writeKilled = true;
}
}
final void killConnect() {
// when a connect is cancelled then the connection may have been
// established so prevent reading or writing.
killReading();
killWriting();
}
/**
* Invoked by connect to initiate the connect operation.
*/
A attachment,
}
A attachment,
{
throw new NullPointerException("'handler' is null");
}
/**
* Invoked by read to initiate the I/O operation.
*/
ByteBuffer[] dsts,
long timeout,
A attachment,
CompletionHandler<V,? super A> handler);
@SuppressWarnings("unchecked")
ByteBuffer[] dsts,
long timeout,
A att,
CompletionHandler<V,? super A> handler)
{
if (!isOpen()) {
Throwable e = new ClosedChannelException();
return CompletedFuture.withFailure(e);
return null;
}
if (remoteAddress == null)
throw new NotYetConnectedException();
boolean shutdown = false;
// check and update state
synchronized (readLock) {
if (readKilled)
throw new IllegalStateException("Reading not allowed due to timeout or cancellation");
if (reading)
throw new ReadPendingException();
if (readShutdown) {
shutdown = true;
} else {
if (hasSpaceToRead) {
reading = true;
}
}
}
// immediately complete with -1 if shutdown for read
// immediately complete with 0 if no space remaining
if (shutdown || !hasSpaceToRead) {
if (isScatteringRead) {
} else {
}
return null;
}
}
if (dst.isReadOnly())
throw new IllegalArgumentException("Read-only buffer");
}
long timeout,
A attachment,
{
throw new NullPointerException("'handler' is null");
if (dst.isReadOnly())
throw new IllegalArgumentException("Read-only buffer");
}
int offset,
int length,
long timeout,
A attachment,
{
throw new NullPointerException("'handler' is null");
throw new IndexOutOfBoundsException();
if (bufs[i].isReadOnly())
throw new IllegalArgumentException("Read-only buffer");
}
}
/**
* Invoked by write to initiate the I/O operation.
*/
ByteBuffer[] srcs,
long timeout,
A attachment,
CompletionHandler<V,? super A> handler);
@SuppressWarnings("unchecked")
ByteBuffer[] srcs,
long timeout,
A att,
CompletionHandler<V,? super A> handler)
{
boolean closed = false;
if (isOpen()) {
if (remoteAddress == null)
throw new NotYetConnectedException();
// check and update state
synchronized (writeLock) {
if (writeKilled)
throw new IllegalStateException("Writing not allowed due to timeout or cancellation");
if (writing)
throw new WritePendingException();
if (writeShutdown) {
closed = true;
} else {
if (hasDataToWrite)
writing = true;
}
}
} else {
closed = true;
}
// channel is closed or shutdown for write
if (closed) {
Throwable e = new ClosedChannelException();
return CompletedFuture.withFailure(e);
return null;
}
// nothing to write so complete immediately
if (!hasDataToWrite) {
return null;
}
}
}
long timeout,
A attachment,
{
throw new NullPointerException("'handler' is null");
}
int offset,
int length,
long timeout,
A attachment,
{
throw new NullPointerException("'handler' is null");
throw new IndexOutOfBoundsException();
}
throws IOException
{
try {
begin();
synchronized (stateLock) {
if (state == ST_PENDING)
throw new ConnectionPendingException();
if (localAddress != null)
throw new AlreadyBoundException();
}
} finally {
end();
}
return this;
}
if (!isOpen())
throw new ClosedChannelException();
}
throws IOException
{
throw new NullPointerException();
try {
begin();
if (writeShutdown)
throw new IOException("Connection has been shutdown for writing");
{
// SO_REUSEADDR emulated when using exclusive bind
} else {
}
return this;
} finally {
end();
}
}
@SuppressWarnings("unchecked")
throw new NullPointerException();
try {
begin();
{
// SO_REUSEADDR emulated when using exclusive bind
}
} finally {
end();
}
}
private static class DefaultOptionsHolder {
}
}
return DefaultOptionsHolder.defaultOptions;
}
if (!isOpen())
throw new ClosedChannelException();
return remoteAddress;
}
try {
begin();
if (remoteAddress == null)
throw new NotYetConnectedException();
synchronized (readLock) {
if (!readShutdown) {
readShutdown = true;
}
}
} finally {
end();
}
return this;
}
try {
begin();
if (remoteAddress == null)
throw new NotYetConnectedException();
synchronized (writeLock) {
if (!writeShutdown) {
writeShutdown = true;
}
}
} finally {
end();
}
return this;
}
synchronized (stateLock) {
if (!isOpen()) {
} else {
switch (state) {
case ST_UNCONNECTED:
break;
case ST_PENDING:
break;
case ST_CONNECTED:
if (readShutdown)
if (writeShutdown)
break;
}
if (localAddress != null) {
}
if (remoteAddress != null) {
}
}
}
}
}