jdk/src/share/classes/sun/nio/ch/DatagramChannelImpl.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 2001-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
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * An implementation of DatagramChannels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class DatagramChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    extends DatagramChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // Used to make native read and write calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static NativeDispatcher nd = new DatagramDispatcher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // Our file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    FileDescriptor fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // fd value needed for dev/poll. This value will remain valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // even after the value in the file descriptor object has been set to -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    int fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // IDs of native threads doing reads and writes, for signalling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private volatile long readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private volatile long writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Cached InetAddress and port for unconnected DatagramChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // used by receive0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private InetAddress cachedSenderInetAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int cachedSenderPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Lock held by current reading or connecting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final Object readLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Lock held by current writing or connecting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final Object writeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // DO NOT invoke a blocking I/O operation while holding this lock!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final Object stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // State (does not necessarily increase monotonically)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int ST_UNINITIALIZED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static int ST_UNCONNECTED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static int ST_CONNECTED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final int ST_KILLED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private int state = ST_UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private SocketAddress localAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    SocketAddress remoteAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Options
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private SocketOpts.IP options = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // Our socket adaptor, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private DatagramSocket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public DatagramChannelImpl(SelectorProvider sp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.fd = Net.socket(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.state = ST_UNCONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.state = ST_UNCONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public DatagramSocket socket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (socket == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                socket = DatagramSocketAdaptor.create(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private void ensureOpen() throws ClosedChannelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new ClosedChannelException();
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 SocketAddress sender;       // Set by receive0 (## ugh)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public SocketAddress receive(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (dst.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new IllegalArgumentException("Read-only buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (dst == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // If socket is not bound then behave as if nothing received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (!isBound())             // ## NotYetBoundException ??
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (isConnected() || (security == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        n = receive(fd, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    if (n == IOStatus.UNAVAILABLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    bb = Util.getTemporaryDirectBuffer(dst.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            n = receive(fd, bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        if (n == IOStatus.UNAVAILABLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        InetSocketAddress isa = (InetSocketAddress)sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                            security.checkAccept(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                isa.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                            // Ignore packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                            bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                            n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                return sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (bb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private int receive(FileDescriptor fd, ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int pos = dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        int lim = dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (dst instanceof DirectBuffer && rem > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return receiveIntoNativeBuffer(fd, dst, rem, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // Substitute a native buffer. If the supplied buffer is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // we must instead use a nonempty buffer, otherwise the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // will not block waiting for a datagram on some platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int newSize = Math.max(rem, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            bb = Util.getTemporaryDirectBuffer(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            int n = receiveIntoNativeBuffer(fd, bb, newSize, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (n > 0 && rem > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private int receiveIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                        int rem, int pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int n = receive0(fd, ((DirectBuffer)bb).address() + pos, rem,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                         isConnected());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            bb.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public int send(ByteBuffer src, SocketAddress target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (src == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            InetSocketAddress isa = (InetSocketAddress)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            InetAddress ia = isa.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (ia == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                throw new IOException("Target address not resolved");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (!isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    if (target == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        if (ia.isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            sm.checkMulticast(isa.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            sm.checkConnect(isa.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                            isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                } else { // Connected case; Check address then write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    if (!target.equals(remoteAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                            "Connected address not equal to target address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    return write(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    n = send(fd, src, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private int send(FileDescriptor fd, ByteBuffer src, SocketAddress target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (src instanceof DirectBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return sendFromNativeBuffer(fd, src, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // Substitute a native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        int pos = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        int lim = src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            bb = Util.getTemporaryDirectBuffer(rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            bb.put(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            // Do not update src until we see how many bytes were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            src.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            int n = sendFromNativeBuffer(fd, bb, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                // now update src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                src.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                            SocketAddress target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int written = send0(fd, ((DirectBuffer)bb).address() + pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                            rem, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (written > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            bb.position(pos + written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public int read(ByteBuffer buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    n = IOUtil.read(fd, buf, -1, nd, readLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private long read0(ByteBuffer[] bufs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (bufs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    n = IOUtil.read(fd, bufs, nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public long read(ByteBuffer[] dsts, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
           throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        // ## Fix IOUtil.write so that we can avoid this array copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return read0(Util.subsequence(dsts, offset, length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public int write(ByteBuffer buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    n = IOUtil.write(fd, buf, -1, nd, writeLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    private long write0(ByteBuffer[] bufs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (bufs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    n = IOUtil.write(fd, bufs, nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public long write(ByteBuffer[] srcs, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // ## Fix IOUtil.write so that we can avoid this array copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        return write0(Util.subsequence(srcs, offset, length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    protected void implConfigureBlocking(boolean block) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        IOUtil.configureBlocking(fd, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public SocketOpts options() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (options == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                SocketOptsImpl.Dispatcher d
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    = new SocketOptsImpl.Dispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                            int getInt(int opt) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                return Net.getIntOption(fd, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                            void setInt(int opt, int arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                Net.setIntOption(fd, opt, arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                options = new SocketOptsImpl.IP(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            return options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return Net.localPortNumber(fd) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public SocketAddress localAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if (isConnected() && (localAddress == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                // Socket was not bound before connecting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                // so ask what the address turned out to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                localAddress = Net.localAddress(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                InetSocketAddress isa = (InetSocketAddress)localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                sm.checkConnect(isa.getAddress().getHostAddress(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            return localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public SocketAddress remoteAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public void bind(SocketAddress local) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    if (isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        throw new AlreadyBoundException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    InetSocketAddress isa = Net.checkAddress(local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        sm.checkListen(isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    Net.bind(fd, isa.getAddress(), isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    localAddress = Net.localAddress(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return (state == ST_CONNECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    void ensureOpenAndUnconnected() throws IOException { // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (state != ST_UNCONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                throw new IllegalStateException("Connect already invoked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public DatagramChannel connect(SocketAddress sa) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        int trafficClass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        int localPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        synchronized(readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            synchronized(writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    ensureOpenAndUnconnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    InetSocketAddress isa = Net.checkAddress(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        sm.checkConnect(isa.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                        isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    int n = Net.connect(fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                        isa.getAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                                        isa.getPort(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                        trafficClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    if (n <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        throw new Error();      // Can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    // Connection succeeded; disallow further invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    state = ST_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    remoteAddress = sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    sender = isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    cachedSenderInetAddress = isa.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    cachedSenderPort = isa.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public DatagramChannel disconnect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        synchronized(readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            synchronized(writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    if (!isConnected() || !isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    InetSocketAddress isa = (InetSocketAddress)remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        sm.checkConnect(isa.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                                        isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    disconnect0(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    remoteAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    state = ST_UNCONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    protected void implCloseSelectableChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            nd.preClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            long th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            if ((th = readerThread) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                NativeThread.signal(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if ((th = writerThread) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                NativeThread.signal(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (!isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    public void kill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            if (state == ST_KILLED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            if (state == ST_UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            assert !isOpen() && !isRegistered();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            nd.close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // fd is null if constructor threw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if (fd != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * Translates native poll revent set into a ready operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    public boolean translateReadyOps(int ops, int initialOps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                     SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        int oldOps = sk.nioReadyOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if ((ops & PollArrayWrapper.POLLNVAL) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if ((ops & (PollArrayWrapper.POLLERR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    | PollArrayWrapper.POLLHUP)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            newOps = intOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (((ops & PollArrayWrapper.POLLIN) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            ((intOps & SelectionKey.OP_READ) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if (((ops & PollArrayWrapper.POLLOUT) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            ((intOps & SelectionKey.OP_WRITE) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        return translateReadyOps(ops, 0, sk);
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
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if ((ops & SelectionKey.OP_READ) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            newOps |= PollArrayWrapper.POLLIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if ((ops & SelectionKey.OP_WRITE) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            newOps |= PollArrayWrapper.POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if ((ops & SelectionKey.OP_CONNECT) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            newOps |= PollArrayWrapper.POLLIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        sk.selector.putEventOps(sk, newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        return fdVal;
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
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    private static native void disconnect0(FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    private native int receive0(FileDescriptor fd, long address, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                boolean connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    private native int send0(FileDescriptor fd, long address, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                     SocketAddress sa)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
}