jdk/src/share/classes/sun/nio/ch/SocketChannelImpl.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1152 29d6145d1097
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * An implementation of SocketChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class SocketChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    extends SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // Used to make native read and write calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static NativeDispatcher nd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // Our file descriptor object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final FileDescriptor fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // fd value needed for dev/poll. This value will remain valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // even after the value in the file descriptor object has been set to -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private final int fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // IDs of native threads doing reads and writes, for signalling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private volatile long readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private volatile long writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // Lock held by current reading or connecting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final Object readLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Lock held by current writing or connecting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final Object writeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // DO NOT invoke a blocking I/O operation while holding this lock!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final Object stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // State, increases monotonically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final int ST_UNINITIALIZED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static final int ST_UNCONNECTED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final int ST_PENDING = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final int ST_CONNECTED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final int ST_KILLPENDING = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final int ST_KILLED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private int state = ST_UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // Binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private SocketAddress localAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private SocketAddress remoteAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Input/Output open
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private boolean isInputOpen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private boolean isOutputOpen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private boolean readyToConnect = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // Options, created on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private SocketOpts.IP.TCP options = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // Socket adaptor, created on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private Socket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // Constructor for normal connecting sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    SocketChannelImpl(SelectorProvider sp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.fd = Net.socket(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.state = ST_UNCONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // Constructor for sockets obtained from server sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    SocketChannelImpl(SelectorProvider sp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                      FileDescriptor fd, InetSocketAddress remote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.state = ST_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.remoteAddress = remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public Socket socket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (socket == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                socket = SocketAdaptor.create(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private boolean ensureReadOpen() throws ClosedChannelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (!isInputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private void ensureWriteOpen() throws ClosedChannelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (!isOutputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private void readerCleanup() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (state == ST_KILLPENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private void writerCleanup() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (state == ST_KILLPENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public int read(ByteBuffer buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (!ensureReadOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                // Set up the interruption machinery; see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                // AbstractInterruptibleChannel for details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    if (!isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    // Either the current thread is already interrupted, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    // begin() closed the channel, or another thread closed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    // channel since we checked it a few bytecodes ago.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    // either case the value returned here is irrelevant since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    // the invocation of end() in the finally block will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    // an appropriate exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    // Save this thread so that it can be signalled on those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    // platforms that require it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                // Between the previous test of isOpen() and the return of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                // IOUtil.read invocation below, this channel might be closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                // or this thread might be interrupted.  We rely upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                // implicit synchronization point in the kernel read() call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                // make sure that the right thing happens.  In either case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                // implCloseSelectableChannel method is ultimately invoked in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                // some other thread, so there are three possibilities:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                //   - implCloseSelectableChannel() invokes nd.preClose()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                //     before this thread invokes read(), in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                //     read returns immediately with either EOF or an error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                //     the latter of which will cause an IOException to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                //     thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                //   - implCloseSelectableChannel() invokes nd.preClose() after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                //     this thread is blocked in read().  On some operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                //     systems (e.g., Solaris and Windows) this causes the read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                //     to return immediately with either EOF or an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                //     indication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                //   - implCloseSelectableChannel() invokes nd.preClose() after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                //     this thread is blocked in read() but the operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                //     system (e.g., Linux) doesn't support preemptive close,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                //     so implCloseSelectableChannel() proceeds to signal this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                //     thread, thereby causing the read to return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                //     with IOStatus.INTERRUPTED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                // In all three cases the invocation of end() in the finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                // clause will notice that the channel has been closed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                // throw an appropriate exception (AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                // or ClosedByInterruptException) if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                // *There is A fourth possibility. implCloseSelectableChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                // invokes nd.preClose(), signals reader/writer thred and quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                // moves on to nd.close() in kill(), which does a real close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                // Then a third thread accepts a new connection, opens file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                // whatever that causes the released "fd" to be recycled. All
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                // above happens just between our last isOpen() check and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                // next kernel read reached, with the recycled "fd". The solution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                // is to postpone the real kill() if there is a reader or/and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                // writer thread(s) over there "waiting", leave the cleanup/kill
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                // to the reader or writer thread. (the preClose() still happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                // so the connection gets cut off as usual).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                // For socket channels there is the additional wrinkle that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                // asynchronous shutdown works much like asynchronous close,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                // except that the channel is shutdown rather than completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                // closed.  This is analogous to the first two cases above,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                // except that the shutdown operation plays the role of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                // nd.preClose().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    n = IOUtil.read(fd, buf, -1, nd, readLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    if ((n == IOStatus.INTERRUPTED) && isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        // The system call was interrupted but the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        // is still open, so retry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                readerCleanup();        // Clear reader thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                // The end method, which is defined in our superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                // AbstractInterruptibleChannel, resets the interruption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                // machinery.  If its argument is true then it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                // normally; otherwise it checks the interrupt and open state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                // of this channel and throws an appropriate exception if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                // necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                // So, if we actually managed to do any I/O in the above try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                // block then we pass true to the end method.  We also pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                // true if the channel was in non-blocking mode when the I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                // operation was initiated but no data could be transferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                // this prevents spurious exceptions from being thrown in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                // rare event that a channel is closed or a thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                // interrupted at the exact moment that a non-blocking I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                // request is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                end(n > 0 || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                // Extra case for socket channels: Asynchronous shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    if ((n <= 0) && (!isInputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        return IOStatus.EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    private long read0(ByteBuffer[] bufs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (bufs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (!ensureReadOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    n = IOUtil.read(fd, bufs, nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                readerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                end(n > 0 || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    if ((n <= 0) && (!isInputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        return IOStatus.EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public long read(ByteBuffer[] dsts, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // ## Fix IOUtil.write so that we can avoid this array copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return read0(Util.subsequence(dsts, offset, length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public int write(ByteBuffer buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            ensureWriteOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    n = IOUtil.write(fd, buf, -1, nd, writeLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                writerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                end(n > 0 || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    if ((n <= 0) && (!isOutputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        throw new AsynchronousCloseException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public long write0(ByteBuffer[] bufs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (bufs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            ensureWriteOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    n = IOUtil.write(fd, bufs, nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                writerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    if ((n <= 0) && (!isOutputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        throw new AsynchronousCloseException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public long write(ByteBuffer[] srcs, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // ## Fix IOUtil.write so that we can avoid this array copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return write0(Util.subsequence(srcs, offset, length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    protected void implConfigureBlocking(boolean block) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        IOUtil.configureBlocking(fd, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public SocketOpts options() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (options == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                SocketOptsImpl.Dispatcher d
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    = new SocketOptsImpl.Dispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                            int getInt(int opt) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                return Net.getIntOption(fd, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                            void setInt(int opt, int arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                                throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                Net.setIntOption(fd, opt, arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                options = new SocketOptsImpl.IP.TCP(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if (state == ST_CONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return localAddress != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public SocketAddress localAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            if (state == ST_CONNECTED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                (localAddress == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                 ((InetSocketAddress)localAddress).getAddress().isAnyLocalAddress())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    // Socket was not bound before connecting or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    // Socket was bound with an "anyLocalAddress"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    localAddress = Net.localAddress(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public SocketAddress remoteAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public void bind(SocketAddress local) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    ensureOpenAndUnconnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    if (localAddress != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        throw new AlreadyBoundException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    InetSocketAddress isa = Net.checkAddress(local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    Net.bind(fd, isa.getAddress(), isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    localAddress = Net.localAddress(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return (state == ST_CONNECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public boolean isConnectionPending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            return (state == ST_PENDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    void ensureOpenAndUnconnected() throws IOException { // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (state == ST_CONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                throw new AlreadyConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (state == ST_PENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                throw new ConnectionPendingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public boolean connect(SocketAddress sa) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        int trafficClass = 0;           // ## Pick up from options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        int localPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                ensureOpenAndUnconnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                InetSocketAddress isa = Net.checkAddress(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    sm.checkConnect(isa.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                                    isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                synchronized (blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                            begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                if (!isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                InetAddress ia = isa.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                                if (ia.isAnyLocalAddress())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                    ia = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                n = Net.connect(fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                                ia,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                                isa.getPort(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                                trafficClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                                if (  (n == IOStatus.INTERRUPTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                                      && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                            readerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                            end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                            assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        // If an exception was thrown, close the channel after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        // invoking end() so as to avoid bogus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                        // AsynchronousCloseExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                        throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        remoteAddress = isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                            // Connection succeeded; disallow further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                            // invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                            state = ST_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        // If nonblocking and no exception then connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                        // pending; disallow another invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        if (!isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                            state = ST_PENDING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public boolean finishConnect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    if (state == ST_CONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    if (state != ST_PENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        throw new NoConnectionPendingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        synchronized (blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                                if (!isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                            if (!isBlocking()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                                    n = checkConnect(fd, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                                                     readyToConnect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                    if (  (n == IOStatus.INTERRUPTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                          && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                    n = checkConnect(fd, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                                                     readyToConnect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                    if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                        // Loop in case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                        // spurious notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                                    if (  (n == IOStatus.INTERRUPTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                                          && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                            readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                            if (state == ST_KILLPENDING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                // poll()/getsockopt() does not report
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                // error (throws exception, with n = 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                // on Linux platform after dup2 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                // signal-wakeup. Force n to 0 so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                // end() can throw appropriate exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    // If an exception was thrown, close the channel after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    // invoking end() so as to avoid bogus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    // AsynchronousCloseExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                        state = ST_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    public final static int SHUT_RD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public final static int SHUT_WR = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    public final static int SHUT_RDWR = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public void shutdownInput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            isInputOpen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            shutdown(fd, SHUT_RD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            if (readerThread != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                NativeThread.signal(readerThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    public void shutdownOutput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            isOutputOpen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            shutdown(fd, SHUT_WR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            if (writerThread != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                NativeThread.signal(writerThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public boolean isInputOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return isInputOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    public boolean isOutputOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            return isOutputOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    // AbstractInterruptibleChannel synchronizes invocations of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    // using AbstractInterruptibleChannel.closeLock, and also ensures that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    // method is only ever invoked once.  Before we get to this method, isOpen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    // (which is volatile) will have been set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    protected void implCloseSelectableChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            isInputOpen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            isOutputOpen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            // Close the underlying file descriptor and dup it to a known fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            // that's already closed.  This prevents other operations on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            // channel from using the old fd, which might be recycled in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            // meantime and allocated to an entirely different channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            nd.preClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            // Signal native threads, if needed.  If a target thread is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            // currently blocked in an I/O operation then no harm is done since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            // the signal handler doesn't actually do anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (readerThread != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                NativeThread.signal(readerThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            if (writerThread != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                NativeThread.signal(writerThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            // If this channel is not registered then it's safe to close the fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            // immediately since we know at this point that no thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            // blocked in an I/O operation upon the channel and, since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            // channel is marked closed, no thread will start another such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            // operation.  If this channel is registered then we don't close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            // the fd since it might be in use by a selector.  In that case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            // closing this channel caused its keys to be cancelled, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            // last selector to deregister a key for this channel will invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            // kill() to close the fd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            if (!isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    public void kill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (state == ST_KILLED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (state == ST_UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            assert !isOpen() && !isRegistered();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            // Postpone the kill if there is a waiting reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            // or writer thread. See the comments in read() for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            // more detailed explanation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            if (readerThread == 0 && writerThread == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                nd.close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                state = ST_KILLPENDING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Translates native poll revent ops into a ready operation ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    public boolean translateReadyOps(int ops, int initialOps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                                     SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        int oldOps = sk.nioReadyOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if ((ops & PollArrayWrapper.POLLNVAL) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if ((ops & (PollArrayWrapper.POLLERR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    | PollArrayWrapper.POLLHUP)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            newOps = intOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            // No need to poll again in checkConnect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            // the error will be detected there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            readyToConnect = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if (((ops & PollArrayWrapper.POLLIN) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            ((intOps & SelectionKey.OP_READ) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            (state == ST_CONNECTED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (((ops & PollArrayWrapper.POLLCONN) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            ((intOps & SelectionKey.OP_CONNECT) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            ((state == ST_UNCONNECTED) || (state == ST_PENDING))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            newOps |= SelectionKey.OP_CONNECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            readyToConnect = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (((ops & PollArrayWrapper.POLLOUT) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            ((intOps & SelectionKey.OP_WRITE) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            (state == ST_CONNECTED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        return translateReadyOps(ops, 0, sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if ((ops & SelectionKey.OP_READ) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            newOps |= PollArrayWrapper.POLLIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        if ((ops & SelectionKey.OP_WRITE) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            newOps |= PollArrayWrapper.POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        if ((ops & SelectionKey.OP_CONNECT) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            newOps |= PollArrayWrapper.POLLCONN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        sk.selector.putEventOps(sk, newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        sb.append(this.getClass().getSuperclass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            sb.append("closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                case ST_UNCONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    sb.append("unconnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                case ST_PENDING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    sb.append("connection-pending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                case ST_CONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    sb.append("connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    if (!isInputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        sb.append(" ishut");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    if (!isOutputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                        sb.append(" oshut");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                if (localAddress() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    sb.append(" local=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                    sb.append(localAddress().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                if (remoteAddress() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    sb.append(" remote=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    sb.append(remoteAddress().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    private static native int checkConnect(FileDescriptor fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                                           boolean block, boolean ready)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    private static native void shutdown(FileDescriptor fd, int how)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        nd = new SocketDispatcher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
}