jdk/src/share/classes/sun/nio/ch/DatagramChannelImpl.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2427 f35f516befc3
child 6301 c90a67d75c9f
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2427
diff changeset
     2
 * Copyright (c) 2001, 2009, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2427
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2427
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2427
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2427
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2427
diff changeset
    23
 * questions.
2
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.*;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    34
import java.util.*;
2
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
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    50
    private final FileDescriptor fd;
2
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
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    54
    private final int fdVal;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    55
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    56
    // The protocol family of the socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    57
    private final ProtocolFamily family;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // IDs of native threads doing reads and writes, for signalling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private volatile long readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private volatile long writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Cached InetAddress and port for unconnected DatagramChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // used by receive0
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    65
    private InetAddress cachedSenderInetAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    66
    private int cachedSenderPort;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Lock held by current reading or connecting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final Object readLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Lock held by current writing or connecting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final Object writeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // DO NOT invoke a blocking I/O operation while holding this lock!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final Object stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // State (does not necessarily increase monotonically)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final int ST_UNINITIALIZED = -1;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    82
    private static final int ST_UNCONNECTED = 0;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    83
    private static final int ST_CONNECTED = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final int ST_KILLED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private int state = ST_UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // Binding
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    88
    private SocketAddress localAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    89
    private SocketAddress remoteAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // Our socket adaptor, if any
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    92
    private DatagramSocket socket;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    93
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    94
    // Multicast support
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    95
    private MembershipRegistry registry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public DatagramChannelImpl(SelectorProvider sp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        super(sp);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   104
        this.family = Net.isIPv6Available() ?
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   105
            StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   106
        this.fd = Net.socket(family, false);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   107
        this.fdVal = IOUtil.fdVal(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   108
        this.state = ST_UNCONNECTED;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   109
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   110
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   111
    public DatagramChannelImpl(SelectorProvider sp, ProtocolFamily family) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   112
        super(sp);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   113
        if ((family != StandardProtocolFamily.INET) &&
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   114
            (family != StandardProtocolFamily.INET6))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   115
        {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   116
            if (family == null)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   117
                throw new NullPointerException("'family' is null");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   118
            else
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   119
                throw new UnsupportedOperationException("Protocol family not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   120
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   121
        if (family == StandardProtocolFamily.INET6) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   122
            if (!Net.isIPv6Available()) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   123
                throw new UnsupportedOperationException("IPv6 not available");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   124
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   125
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   126
        this.family = family;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   127
        this.fd = Net.socket(family, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.state = ST_UNCONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        super(sp);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   136
        this.family = Net.isIPv6Available() ?
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   137
            StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.state = ST_UNCONNECTED;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   141
        this.localAddress = Net.localAddress(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public DatagramSocket socket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (socket == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                socket = DatagramSocketAdaptor.create(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   152
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   153
    public SocketAddress getLocalAddress() throws IOException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   154
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   155
            if (!isOpen())
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   156
                throw new ClosedChannelException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   157
            return localAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   158
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   159
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   160
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   161
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   162
    public SocketAddress getRemoteAddress() throws IOException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   163
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   164
            if (!isOpen())
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   165
                throw new ClosedChannelException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   166
            return remoteAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   167
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   168
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   169
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   170
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   171
    public <T> DatagramChannel setOption(SocketOption<T> name, T value)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   172
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   173
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   174
        if (name == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   175
            throw new NullPointerException();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   176
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   177
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   178
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   179
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   180
            ensureOpen();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   181
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   182
            if (name == StandardSocketOption.IP_TOS) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   183
                // IPv4 only; no-op for IPv6
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   184
                if (family == StandardProtocolFamily.INET) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   185
                    Net.setSocketOption(fd, family, name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   186
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   187
                return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   188
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   189
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   190
            if (name == StandardSocketOption.IP_MULTICAST_TTL ||
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   191
                name == StandardSocketOption.IP_MULTICAST_LOOP)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   192
            {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   193
                // options are protocol dependent
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   194
                Net.setSocketOption(fd, family, name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   195
                return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   196
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   197
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   198
            if (name == StandardSocketOption.IP_MULTICAST_IF) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   199
                if (value == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   200
                    throw new IllegalArgumentException("Cannot set IP_MULTICAST_IF to 'null'");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   201
                NetworkInterface interf = (NetworkInterface)value;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   202
                if (family == StandardProtocolFamily.INET6) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   203
                    int index = interf.getIndex();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   204
                    if (index == -1)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   205
                        throw new IOException("Network interface cannot be identified");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   206
                    Net.setInterface6(fd, index);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   207
                } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   208
                    // need IPv4 address to identify interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   209
                    Inet4Address target = Net.anyInet4Address(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   210
                    if (target == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   211
                        throw new IOException("Network interface not configured for IPv4");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   212
                    int targetAddress = Net.inet4AsInt(target);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   213
                    Net.setInterface4(fd, targetAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   214
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   215
                return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   216
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   217
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   218
            // remaining options don't need any special handling
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   219
            Net.setSocketOption(fd, Net.UNSPEC, name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   220
            return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   221
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   222
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   223
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   224
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   225
    @SuppressWarnings("unchecked")
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   226
    public <T> T getOption(SocketOption<T> name)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   227
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   228
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   229
        if (name == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   230
            throw new NullPointerException();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   231
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   232
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   233
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   234
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   235
            ensureOpen();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   236
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   237
            if (name == StandardSocketOption.IP_TOS) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   238
                // IPv4 only; always return 0 on IPv6
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   239
                if (family == StandardProtocolFamily.INET) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   240
                    return (T) Net.getSocketOption(fd, family, name);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   241
                } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   242
                    return (T) Integer.valueOf(0);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   243
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   244
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   245
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   246
            if (name == StandardSocketOption.IP_MULTICAST_TTL ||
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   247
                name == StandardSocketOption.IP_MULTICAST_LOOP)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   248
            {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   249
                return (T) Net.getSocketOption(fd, family, name);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   250
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   251
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   252
            if (name == StandardSocketOption.IP_MULTICAST_IF) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   253
                if (family == StandardProtocolFamily.INET) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   254
                    int address = Net.getInterface4(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   255
                    if (address == 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   256
                        return null;    // default interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   257
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   258
                    InetAddress ia = Net.inet4FromInt(address);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   259
                    NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   260
                    if (ni == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   261
                        throw new IOException("Unable to map address to interface");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   262
                    return (T) ni;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   263
                } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   264
                    int index = Net.getInterface6(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   265
                    if (index == 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   266
                        return null;    // default interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   267
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   268
                    NetworkInterface ni = NetworkInterface.getByIndex(index);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   269
                    if (ni == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   270
                        throw new IOException("Unable to map index to interface");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   271
                    return (T) ni;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   272
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   273
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   274
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   275
            // no special handling
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   276
            return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   277
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   278
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   279
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   280
    private static class DefaultOptionsHolder {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   281
        static final Set<SocketOption<?>> defaultOptions = defaultOptions();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   282
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   283
        private static Set<SocketOption<?>> defaultOptions() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   284
            HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(8);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   285
            set.add(StandardSocketOption.SO_SNDBUF);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   286
            set.add(StandardSocketOption.SO_RCVBUF);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   287
            set.add(StandardSocketOption.SO_REUSEADDR);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   288
            set.add(StandardSocketOption.SO_BROADCAST);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   289
            set.add(StandardSocketOption.IP_TOS);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   290
            set.add(StandardSocketOption.IP_MULTICAST_IF);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   291
            set.add(StandardSocketOption.IP_MULTICAST_TTL);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   292
            set.add(StandardSocketOption.IP_MULTICAST_LOOP);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   293
            return Collections.unmodifiableSet(set);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   294
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   295
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   296
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   297
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   298
    public final Set<SocketOption<?>> supportedOptions() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   299
        return DefaultOptionsHolder.defaultOptions;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   300
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   301
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private void ensureOpen() throws ClosedChannelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private SocketAddress sender;       // Set by receive0 (## ugh)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public SocketAddress receive(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (dst.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throw new IllegalArgumentException("Read-only buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (dst == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            ensureOpen();
2427
f35f516befc3 6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents: 2057
diff changeset
   316
            // Socket was not bound before attempting receive
f35f516befc3 6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents: 2057
diff changeset
   317
            if (localAddress() == null)
f35f516befc3 6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents: 2057
diff changeset
   318
                bind(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                if (isConnected() || (security == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        n = receive(fd, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    if (n == IOStatus.UNAVAILABLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    bb = Util.getTemporaryDirectBuffer(dst.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            n = receive(fd, bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        if (n == IOStatus.UNAVAILABLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        InetSocketAddress isa = (InetSocketAddress)sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                            security.checkAccept(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                isa.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                            // Ignore packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                            bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                return sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                if (bb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
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
    private int receive(FileDescriptor fd, ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        int pos = dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int lim = dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (dst instanceof DirectBuffer && rem > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return receiveIntoNativeBuffer(fd, dst, rem, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // Substitute a native buffer. If the supplied buffer is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // we must instead use a nonempty buffer, otherwise the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // will not block waiting for a datagram on some platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int newSize = Math.max(rem, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            bb = Util.getTemporaryDirectBuffer(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            int n = receiveIntoNativeBuffer(fd, bb, newSize, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (n > 0 && rem > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    private int receiveIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                        int rem, int pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        int n = receive0(fd, ((DirectBuffer)bb).address() + pos, rem,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                         isConnected());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            bb.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public int send(ByteBuffer src, SocketAddress target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (src == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            InetSocketAddress isa = (InetSocketAddress)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            InetAddress ia = isa.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (ia == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                throw new IOException("Target address not resolved");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                if (!isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    if (target == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        if (ia.isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            sm.checkMulticast(isa.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            sm.checkConnect(isa.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                                            isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                } else { // Connected case; Check address then write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    if (!target.equals(remoteAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                            "Connected address not equal to target address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    return write(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    n = send(fd, src, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   449
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   450
                synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   451
                    if (isOpen() && (localAddress == null)) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   452
                        localAddress = Net.localAddress(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   453
                    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   454
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    private int send(FileDescriptor fd, ByteBuffer src, SocketAddress target)
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
        if (src instanceof DirectBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return sendFromNativeBuffer(fd, src, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // Substitute a native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        int pos = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int lim = src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            bb = Util.getTemporaryDirectBuffer(rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            bb.put(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            // Do not update src until we see how many bytes were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            src.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            int n = sendFromNativeBuffer(fd, bb, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                // now update src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                src.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                            SocketAddress target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   504
        boolean preferIPv6 = (family != StandardProtocolFamily.INET);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   505
        int written = send0(preferIPv6, fd, ((DirectBuffer)bb).address() + pos,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                            rem, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (written > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            bb.position(pos + written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    public int read(ByteBuffer buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    n = IOUtil.read(fd, buf, -1, nd, readLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    private long read0(ByteBuffer[] bufs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (bufs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    n = IOUtil.read(fd, bufs, nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public long read(ByteBuffer[] dsts, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
           throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        // ## Fix IOUtil.write so that we can avoid this array copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        return read0(Util.subsequence(dsts, offset, length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    public int write(ByteBuffer buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    n = IOUtil.write(fd, buf, -1, nd, writeLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private long write0(ByteBuffer[] bufs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (bufs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    n = IOUtil.write(fd, bufs, nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public long write(ByteBuffer[] srcs, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        // ## Fix IOUtil.write so that we can avoid this array copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return write0(Util.subsequence(srcs, offset, length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    protected void implConfigureBlocking(boolean block) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        IOUtil.configureBlocking(fd, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    public SocketAddress localAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            return localAddress;
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
    public SocketAddress remoteAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   654
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   655
    public DatagramChannel bind(SocketAddress local) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   660
                    if (localAddress != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                        throw new AlreadyBoundException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   662
                    InetSocketAddress isa;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   663
                    if (local == null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   664
                        isa = new InetSocketAddress(0);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   665
                    } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   666
                        isa = Net.checkAddress(local);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   667
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   668
                        // only Inet4Address allowed with IPv4 socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   669
                        if (family == StandardProtocolFamily.INET) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   670
                            InetAddress addr = isa.getAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   671
                            if (!(addr instanceof Inet4Address))
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   672
                                throw new UnsupportedAddressTypeException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   673
                        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   674
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    SecurityManager sm = System.getSecurityManager();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   676
                    if (sm != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        sm.checkListen(isa.getPort());
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   678
                    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   679
                    Net.bind(family, fd, isa.getAddress(), isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    localAddress = Net.localAddress(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   684
        return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            return (state == ST_CONNECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    void ensureOpenAndUnconnected() throws IOException { // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (state != ST_UNCONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                throw new IllegalStateException("Connect already invoked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    public DatagramChannel connect(SocketAddress sa) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        int localPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        synchronized(readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            synchronized(writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    ensureOpenAndUnconnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    InetSocketAddress isa = Net.checkAddress(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        sm.checkConnect(isa.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                                        isa.getPort());
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   714
                    int n = Net.connect(family,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   715
                                        fd,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                                        isa.getAddress(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   717
                                        isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    if (n <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        throw new Error();      // Can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    // Connection succeeded; disallow further invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    state = ST_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    remoteAddress = sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    sender = isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    cachedSenderInetAddress = isa.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    cachedSenderPort = isa.getPort();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   727
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   728
                    // Socket was not bound before connecting,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   729
                    if (localAddress == null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   730
                        localAddress = Net.localAddress(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   731
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    public DatagramChannel disconnect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        synchronized(readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            synchronized(writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    if (!isConnected() || !isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    InetSocketAddress isa = (InetSocketAddress)remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                        sm.checkConnect(isa.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                                        isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    disconnect0(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    remoteAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    state = ST_UNCONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   758
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   759
     * Joins channel's socket to the given group/interface and
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   760
     * optional source address.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   761
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   762
    private MembershipKey innerJoin(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   763
                                    NetworkInterface interf,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   764
                                    InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   765
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   766
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   767
        if (!group.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   768
            throw new IllegalArgumentException("Group not a multicast address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   769
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   770
        // check multicast address is compatible with this socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   771
        if (!(group instanceof Inet4Address)) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   772
            if (family == StandardProtocolFamily.INET)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   773
                throw new IllegalArgumentException("Group is not IPv4 address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   774
            if (!(group instanceof Inet6Address))
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   775
                throw new IllegalArgumentException("Address type not supported");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   776
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   777
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   778
        // check source address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   779
        if (source != null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   780
            if (source.isAnyLocalAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   781
                throw new IllegalArgumentException("Source address is a wildcard address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   782
            if (source.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   783
                throw new IllegalArgumentException("Source address is multicast address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   784
            if (source.getClass() != group.getClass())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   785
                throw new IllegalArgumentException("Source address is different type to group");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   786
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   787
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   788
        SecurityManager sm = System.getSecurityManager();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   789
        if (sm != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   790
            sm.checkMulticast(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   791
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   792
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   793
            if (!isOpen())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   794
                throw new ClosedChannelException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   795
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   796
            // check the registry to see if we are already a member of the group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   797
            if (registry == null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   798
                registry = new MembershipRegistry();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   799
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   800
                // return existing membership key
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   801
                MembershipKey key = registry.checkMembership(group, interf, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   802
                if (key != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   803
                    return key;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   804
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   805
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   806
            MembershipKeyImpl key;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   807
            if (family == StandardProtocolFamily.INET6) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   808
                int index = interf.getIndex();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   809
                if (index == -1)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   810
                    throw new IOException("Network interface cannot be identified");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   811
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   812
                // need multicast and source address as byte arrays
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   813
                byte[] groupAddress = Net.inet6AsByteArray(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   814
                byte[] sourceAddress = (source == null) ? null :
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   815
                    Net.inet6AsByteArray(source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   816
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   817
                // join the group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   818
                int n = Net.join6(fd, groupAddress, index, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   819
                if (n == IOStatus.UNAVAILABLE)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   820
                    throw new UnsupportedOperationException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   821
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   822
                key = new MembershipKeyImpl.Type6(this, group, interf, source,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   823
                                                  groupAddress, index, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   824
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   825
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   826
                // need IPv4 address to identify interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   827
                Inet4Address target = Net.anyInet4Address(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   828
                if (target == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   829
                    throw new IOException("Network interface not configured for IPv4");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   830
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   831
                int groupAddress = Net.inet4AsInt(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   832
                int targetAddress = Net.inet4AsInt(target);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   833
                int sourceAddress = (source == null) ? 0 : Net.inet4AsInt(source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   834
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   835
                // join the group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   836
                int n = Net.join4(fd, groupAddress, targetAddress, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   837
                if (n == IOStatus.UNAVAILABLE)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   838
                    throw new UnsupportedOperationException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   839
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   840
                key = new MembershipKeyImpl.Type4(this, group, interf, source,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   841
                                                  groupAddress, targetAddress, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   842
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   843
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   844
            registry.add(key);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   845
            return key;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   846
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   847
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   848
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   849
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   850
    public MembershipKey join(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   851
                              NetworkInterface interf)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   852
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   853
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   854
        return innerJoin(group, interf, null);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   855
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   856
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   857
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   858
    public MembershipKey join(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   859
                              NetworkInterface interf,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   860
                              InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   861
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   862
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   863
        if (source == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   864
            throw new NullPointerException("source address is null");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   865
        return innerJoin(group, interf, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   866
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   867
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   868
    // package-private
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   869
    void drop(MembershipKeyImpl key) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   870
        assert key.channel() == this;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   871
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   872
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   873
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   874
                return;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   875
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   876
            try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   877
                if (family == StandardProtocolFamily.INET6) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   878
                    MembershipKeyImpl.Type6 key6 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   879
                        (MembershipKeyImpl.Type6)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   880
                    Net.drop6(fd, key6.groupAddress(), key6.index(), key6.source());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   881
                } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   882
                    MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   883
                    Net.drop4(fd, key4.groupAddress(), key4.interfaceAddress(),
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   884
                        key4.source());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   885
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   886
            } catch (IOException ioe) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   887
                // should not happen
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   888
                throw new AssertionError(ioe);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   889
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   890
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   891
            key.invalidate();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   892
            registry.remove(key);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   893
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   894
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   895
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   896
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   897
     * Block datagrams from given source if a memory to receive all
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   898
     * datagrams.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   899
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   900
    void block(MembershipKeyImpl key, InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   901
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   902
    {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   903
        assert key.channel() == this;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   904
        assert key.sourceAddress() == null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   905
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   906
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   907
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   908
                throw new IllegalStateException("key is no longer valid");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   909
            if (source.isAnyLocalAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   910
                throw new IllegalArgumentException("Source address is a wildcard address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   911
            if (source.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   912
                throw new IllegalArgumentException("Source address is multicast address");
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   913
            if (source.getClass() != key.group().getClass())
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   914
                throw new IllegalArgumentException("Source address is different type to group");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   915
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   916
            int n;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   917
            if (family == StandardProtocolFamily.INET6) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   918
                 MembershipKeyImpl.Type6 key6 =
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   919
                    (MembershipKeyImpl.Type6)key;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   920
                n = Net.block6(fd, key6.groupAddress(), key6.index(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   921
                               Net.inet6AsByteArray(source));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   922
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   923
                MembershipKeyImpl.Type4 key4 =
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   924
                    (MembershipKeyImpl.Type4)key;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   925
                n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   926
                               Net.inet4AsInt(source));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   927
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   928
            if (n == IOStatus.UNAVAILABLE) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   929
                // ancient kernel
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   930
                throw new UnsupportedOperationException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   931
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   932
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   933
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   934
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   935
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   936
     * Unblock given source.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   937
     */
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   938
    void unblock(MembershipKeyImpl key, InetAddress source) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   939
        assert key.channel() == this;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   940
        assert key.sourceAddress() == null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   941
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   942
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   943
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   944
                throw new IllegalStateException("key is no longer valid");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   945
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   946
            try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   947
                if (family == StandardProtocolFamily.INET6) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   948
                    MembershipKeyImpl.Type6 key6 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   949
                        (MembershipKeyImpl.Type6)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   950
                    Net.unblock6(fd, key6.groupAddress(), key6.index(),
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   951
                                 Net.inet6AsByteArray(source));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   952
                } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   953
                    MembershipKeyImpl.Type4 key4 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   954
                        (MembershipKeyImpl.Type4)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   955
                    Net.unblock4(fd, key4.groupAddress(), key4.interfaceAddress(),
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   956
                                 Net.inet4AsInt(source));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   957
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   958
            } catch (IOException ioe) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   959
                // should not happen
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   960
                throw new AssertionError(ioe);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   961
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   962
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   963
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   964
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    protected void implCloseSelectableChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            nd.preClose(fd);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   968
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   969
            // if member of mulitcast group then invalidate all keys
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   970
            if (registry != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   971
                registry.invalidateAll();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   972
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            long th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            if ((th = readerThread) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                NativeThread.signal(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            if ((th = writerThread) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                NativeThread.signal(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            if (!isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    public void kill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            if (state == ST_KILLED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            if (state == ST_UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            assert !isOpen() && !isRegistered();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            nd.close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        // fd is null if constructor threw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        if (fd != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * Translates native poll revent set into a ready operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    public boolean translateReadyOps(int ops, int initialOps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                                     SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        int oldOps = sk.nioReadyOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        if ((ops & PollArrayWrapper.POLLNVAL) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if ((ops & (PollArrayWrapper.POLLERR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    | PollArrayWrapper.POLLHUP)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            newOps = intOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        if (((ops & PollArrayWrapper.POLLIN) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            ((intOps & SelectionKey.OP_READ) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (((ops & PollArrayWrapper.POLLOUT) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            ((intOps & SelectionKey.OP_WRITE) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        return translateReadyOps(ops, 0, sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        if ((ops & SelectionKey.OP_READ) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            newOps |= PollArrayWrapper.POLLIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        if ((ops & SelectionKey.OP_WRITE) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            newOps |= PollArrayWrapper.POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        if ((ops & SelectionKey.OP_CONNECT) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            newOps |= PollArrayWrapper.POLLIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        sk.selector.putEventOps(sk, newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    private static native void disconnect0(FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    private native int receive0(FileDescriptor fd, long address, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                                boolean connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1081
    private native int send0(boolean preferIPv6, FileDescriptor fd, long address, int len,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1082
                             SocketAddress sa)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
}