src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java
author bpb
Thu, 22 Mar 2018 12:29:52 -0700
changeset 49284 a51ca91c2cde
parent 49001 ce06058197a4
child 49493 814bd31f8da0
permissions -rw-r--r--
8198753: (dc) DatagramChannel throws unspecified exceptions Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, 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;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    30
import java.net.DatagramSocket;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    31
import java.net.Inet4Address;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    32
import java.net.Inet6Address;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    33
import java.net.InetAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    34
import java.net.InetSocketAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    35
import java.net.NetworkInterface;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    36
import java.net.PortUnreachableException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    37
import java.net.ProtocolFamily;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    38
import java.net.SocketAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    39
import java.net.SocketOption;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    40
import java.net.StandardProtocolFamily;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    41
import java.net.StandardSocketOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.nio.ByteBuffer;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    43
import java.nio.channels.AlreadyBoundException;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    44
import java.nio.channels.AlreadyConnectedException;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    45
import java.nio.channels.AsynchronousCloseException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    46
import java.nio.channels.ClosedChannelException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    47
import java.nio.channels.DatagramChannel;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    48
import java.nio.channels.MembershipKey;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    49
import java.nio.channels.NotYetConnectedException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    50
import java.nio.channels.SelectionKey;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    51
import java.nio.channels.spi.SelectorProvider;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    52
import java.util.Collections;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    53
import java.util.HashSet;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    54
import java.util.Objects;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    55
import java.util.Set;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    56
import java.util.concurrent.locks.ReentrantLock;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    57
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
    58
import sun.net.ResourceManager;
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    59
import sun.net.ext.ExtendedSocketOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * An implementation of DatagramChannels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
class DatagramChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    extends DatagramChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // Used to make native read and write calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static NativeDispatcher nd = new DatagramDispatcher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    72
    // The protocol family of the socket
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    73
    private final ProtocolFamily family;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    74
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // Our file descriptor
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    76
    private final FileDescriptor fd;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    77
    private final int fdVal;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    78
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // Cached InetAddress and port for unconnected DatagramChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // used by receive0
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    81
    private InetAddress cachedSenderInetAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    82
    private int cachedSenderPort;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Lock held by current reading or connecting thread
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    85
    private final ReentrantLock readLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // Lock held by current writing or connecting thread
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    88
    private final ReentrantLock writeLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // DO NOT invoke a blocking I/O operation while holding this lock!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private final Object stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // State (does not necessarily increase monotonically)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    97
    private static final int ST_UNCONNECTED = 0;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    98
    private static final int ST_CONNECTED = 1;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    99
    private static final int ST_CLOSING = 2;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   100
    private static final int ST_KILLPENDING = 3;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   101
    private static final int ST_KILLED = 4;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   102
    private int state;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   104
    // IDs of native threads doing reads and writes, for signalling
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   105
    private long readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   106
    private long writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   107
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   108
    // Binding and remote address (when connected)
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   109
    private InetSocketAddress localAddress;
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   110
    private InetSocketAddress remoteAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // Our socket adaptor, if any
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   113
    private DatagramSocket socket;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   114
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   115
    // Multicast support
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   116
    private MembershipRegistry registry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   118
    // set true when socket is bound and SO_REUSEADDRESS is emulated
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   119
    private boolean reuseAddressEmulated;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   120
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   121
    // set true/false when socket is already bound and SO_REUSEADDR is emulated
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   122
    private boolean isReuseAddress;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   123
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public DatagramChannelImpl(SelectorProvider sp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        super(sp);
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   130
        ResourceManager.beforeUdpCreate();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   131
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   132
            this.family = Net.isIPv6Available()
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   133
                    ? StandardProtocolFamily.INET6
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   134
                    : StandardProtocolFamily.INET;
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   135
            this.fd = Net.socket(family, false);
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   136
            this.fdVal = IOUtil.fdVal(fd);
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   137
        } catch (IOException ioe) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   138
            ResourceManager.afterUdpClose();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   139
            throw ioe;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   140
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   141
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   142
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   143
    public DatagramChannelImpl(SelectorProvider sp, ProtocolFamily family)
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   144
        throws IOException
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   145
    {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   146
        super(sp);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   147
        Objects.requireNonNull(family, "'family' is null");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   148
        if ((family != StandardProtocolFamily.INET) &&
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   149
            (family != StandardProtocolFamily.INET6)) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   150
            throw new UnsupportedOperationException("Protocol family not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   151
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   152
        if (family == StandardProtocolFamily.INET6) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   153
            if (!Net.isIPv6Available()) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   154
                throw new UnsupportedOperationException("IPv6 not available");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   155
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   156
        }
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   157
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   158
        ResourceManager.beforeUdpCreate();
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   159
        try {
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   160
            this.family = family;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   161
            this.fd = Net.socket(family, false);
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   162
            this.fdVal = IOUtil.fdVal(fd);
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   163
        } catch (IOException ioe) {
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   164
            ResourceManager.afterUdpClose();
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   165
            throw ioe;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   166
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        super(sp);
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   173
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   174
        // increment UDP count to match decrement when closing
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   175
        ResourceManager.beforeUdpCreate();
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   176
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   177
        this.family = Net.isIPv6Available()
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   178
                ? StandardProtocolFamily.INET6
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   179
                : StandardProtocolFamily.INET;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        this.fdVal = IOUtil.fdVal(fd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   182
        synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   183
            this.localAddress = Net.localAddress(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   184
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   187
    // @throws ClosedChannelException if channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   188
    private void ensureOpen() throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   189
        if (!isOpen())
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   190
            throw new ClosedChannelException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   191
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   192
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   193
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public DatagramSocket socket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (socket == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                socket = DatagramSocketAdaptor.create(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   202
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   203
    public SocketAddress getLocalAddress() throws IOException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   204
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   205
            ensureOpen();
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   206
            // Perform security check before returning address
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   207
            return Net.getRevealedLocalAddress(localAddress);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   208
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   209
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   210
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   211
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   212
    public SocketAddress getRemoteAddress() throws IOException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   213
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   214
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   215
            return remoteAddress;
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   219
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   220
    public <T> DatagramChannel setOption(SocketOption<T> name, T value)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   221
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   222
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   223
        Objects.requireNonNull(name);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   224
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   225
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   226
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   227
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   228
            ensureOpen();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   229
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23879
diff changeset
   230
            if (name == StandardSocketOptions.IP_TOS ||
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23879
diff changeset
   231
                name == StandardSocketOptions.IP_MULTICAST_TTL ||
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   232
                name == StandardSocketOptions.IP_MULTICAST_LOOP)
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
                // options are protocol dependent
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   235
                Net.setSocketOption(fd, family, name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   236
                return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   237
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   238
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   239
            if (name == StandardSocketOptions.IP_MULTICAST_IF) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   240
                if (value == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   241
                    throw new IllegalArgumentException("Cannot set IP_MULTICAST_IF to 'null'");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   242
                NetworkInterface interf = (NetworkInterface)value;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   243
                if (family == StandardProtocolFamily.INET6) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   244
                    int index = interf.getIndex();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   245
                    if (index == -1)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   246
                        throw new IOException("Network interface cannot be identified");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   247
                    Net.setInterface6(fd, index);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   248
                } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   249
                    // need IPv4 address to identify interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   250
                    Inet4Address target = Net.anyInet4Address(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   251
                    if (target == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   252
                        throw new IOException("Network interface not configured for IPv4");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   253
                    int targetAddress = Net.inet4AsInt(target);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   254
                    Net.setInterface4(fd, targetAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   255
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   256
                return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   257
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   258
            if (name == StandardSocketOptions.SO_REUSEADDR
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   259
                && Net.useExclusiveBind() && localAddress != null) {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   260
                reuseAddressEmulated = true;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   261
                this.isReuseAddress = (Boolean)value;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   262
            }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   263
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   264
            // remaining options don't need any special handling
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   265
            Net.setSocketOption(fd, Net.UNSPEC, name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   266
            return this;
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
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   269
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   270
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   271
    @SuppressWarnings("unchecked")
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   272
    public <T> T getOption(SocketOption<T> name)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   273
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   274
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   275
        Objects.requireNonNull(name);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   276
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   277
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
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
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   280
            ensureOpen();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   281
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23879
diff changeset
   282
            if (name == StandardSocketOptions.IP_TOS ||
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23879
diff changeset
   283
                name == StandardSocketOptions.IP_MULTICAST_TTL ||
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   284
                name == StandardSocketOptions.IP_MULTICAST_LOOP)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   285
            {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   286
                return (T) Net.getSocketOption(fd, family, name);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   287
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   288
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   289
            if (name == StandardSocketOptions.IP_MULTICAST_IF) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   290
                if (family == StandardProtocolFamily.INET) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   291
                    int address = Net.getInterface4(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   292
                    if (address == 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   293
                        return null;    // default interface
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
                    InetAddress ia = Net.inet4FromInt(address);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   296
                    NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   297
                    if (ni == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   298
                        throw new IOException("Unable to map address to interface");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   299
                    return (T) ni;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   300
                } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   301
                    int index = Net.getInterface6(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   302
                    if (index == 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   303
                        return null;    // default interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   304
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   305
                    NetworkInterface ni = NetworkInterface.getByIndex(index);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   306
                    if (ni == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   307
                        throw new IOException("Unable to map index to interface");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   308
                    return (T) ni;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   309
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   310
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   311
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   312
            if (name == StandardSocketOptions.SO_REUSEADDR && reuseAddressEmulated) {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   313
                return (T)Boolean.valueOf(isReuseAddress);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   314
            }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   315
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   316
            // no special handling
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   317
            return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   318
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   319
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   320
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   321
    private static class DefaultOptionsHolder {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   322
        static final Set<SocketOption<?>> defaultOptions = defaultOptions();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   323
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   324
        private static Set<SocketOption<?>> defaultOptions() {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   325
            HashSet<SocketOption<?>> set = new HashSet<>();
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   326
            set.add(StandardSocketOptions.SO_SNDBUF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   327
            set.add(StandardSocketOptions.SO_RCVBUF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   328
            set.add(StandardSocketOptions.SO_REUSEADDR);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   329
            if (Net.isReusePortAvailable()) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   330
                set.add(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   331
            }
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   332
            set.add(StandardSocketOptions.SO_BROADCAST);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   333
            set.add(StandardSocketOptions.IP_TOS);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   334
            set.add(StandardSocketOptions.IP_MULTICAST_IF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   335
            set.add(StandardSocketOptions.IP_MULTICAST_TTL);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   336
            set.add(StandardSocketOptions.IP_MULTICAST_LOOP);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   337
            set.addAll(ExtendedSocketOptions.getInstance().options());
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   338
            return Collections.unmodifiableSet(set);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   339
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   340
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   341
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   342
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   343
    public final Set<SocketOption<?>> supportedOptions() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   344
        return DefaultOptionsHolder.defaultOptions;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   345
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   346
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   347
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   348
     * Marks the beginning of a read operation that might block.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   349
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   350
     * @param blocking true if configured blocking
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   351
     * @param mustBeConnected true if the socket must be connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   352
     * @return remote address if connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   353
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   354
     * @throws NotYetConnectedException if mustBeConnected and not connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   355
     * @throws IOException if socket not bound and cannot be bound
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   356
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   357
    private SocketAddress beginRead(boolean blocking, boolean mustBeConnected)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   358
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   359
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   360
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   361
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   362
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   363
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   364
        SocketAddress remote;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   365
        synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   366
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   367
            remote = remoteAddress;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   368
            if ((remote == null) && mustBeConnected)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   369
                throw new NotYetConnectedException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   370
            if (localAddress == null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   371
                bindInternal(null);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   372
            if (blocking)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   373
                readerThread = NativeThread.current();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   374
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   375
        return remote;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   376
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   377
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   378
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   379
     * Marks the end of a read operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   380
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   381
     * @throws AsynchronousCloseException if the channel was closed asynchronously
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   382
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   383
    private void endRead(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   384
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   385
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   386
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   387
            synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   388
                readerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   389
                // notify any thread waiting in implCloseSelectableChannel
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   390
                if (state == ST_CLOSING) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   391
                    stateLock.notifyAll();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   392
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   393
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   394
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   395
            end(completed);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   396
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private SocketAddress sender;       // Set by receive0 (## ugh)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   401
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public SocketAddress receive(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (dst.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throw new IllegalArgumentException("Read-only buffer");
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   405
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   406
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   407
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   408
            boolean blocking = isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   412
                SocketAddress remote = beginRead(blocking, false);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   413
                boolean connected = (remote != null);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   414
                SecurityManager sm = System.getSecurityManager();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   415
                if (connected || (sm == null)) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   416
                    // connected or no security manager
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    do {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   418
                        n = receive(fd, dst, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    if (n == IOStatus.UNAVAILABLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                } else {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   423
                    // Cannot receive into user's buffer when running with a
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   424
                    // security manager and not connected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    bb = Util.getTemporaryDirectBuffer(dst.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        do {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   428
                            n = receive(fd, bb, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        if (n == IOStatus.UNAVAILABLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        InetSocketAddress isa = (InetSocketAddress)sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   434
                            sm.checkAccept(isa.getAddress().getHostAddress(),
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   435
                                           isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                            // Ignore packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                            bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   447
                assert sender != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                return sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                if (bb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    Util.releaseTemporaryDirectBuffer(bb);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   452
                endRead(blocking, n > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   455
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   456
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   460
    private int receive(FileDescriptor fd, ByteBuffer dst, boolean connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        int pos = dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        int lim = dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (dst instanceof DirectBuffer && rem > 0)
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   468
            return receiveIntoNativeBuffer(fd, dst, rem, pos, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // Substitute a native buffer. If the supplied buffer is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // we must instead use a nonempty buffer, otherwise the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        // will not block waiting for a datagram on some platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        int newSize = Math.max(rem, 1);
9237
cbb5753e87e7 7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents: 8817
diff changeset
   474
        ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   476
            int n = receiveIntoNativeBuffer(fd, bb, newSize, 0, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (n > 0 && rem > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    private int receiveIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   487
                                        int rem, int pos, boolean connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   490
        int n = receive0(fd, ((DirectBuffer)bb).address() + pos, rem, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            bb.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public int send(ByteBuffer src, SocketAddress target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   499
        Objects.requireNonNull(src);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   500
        InetSocketAddress isa = Net.checkAddress(target, family);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   502
        writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   503
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   504
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   505
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   506
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   507
                SocketAddress remote = beginWrite(blocking, false);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   508
                if (remote != null) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   509
                    // connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   510
                    if (!target.equals(remote)) {
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 49001
diff changeset
   511
                        throw new AlreadyConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   512
                    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   513
                    do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   514
                        n = IOUtil.write(fd, src, -1, nd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   515
                    } while ((n == IOStatus.INTERRUPTED) && isOpen());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   516
                } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   517
                    // not connected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    if (sm != null) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   520
                        InetAddress ia = isa.getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        if (ia.isMulticastAddress()) {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   522
                            sm.checkMulticast(ia);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        } else {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   524
                            sm.checkConnect(ia.getHostAddress(), isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   527
                    do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   528
                        n = send(fd, src, isa);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   529
                    } while ((n == IOStatus.INTERRUPTED) && isOpen());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   532
                endWrite(blocking, n > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   535
            return IOStatus.normalize(n);
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   536
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   537
            writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   541
    private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (src instanceof DirectBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            return sendFromNativeBuffer(fd, src, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        // Substitute a native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        int pos = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        int lim = src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
9237
cbb5753e87e7 7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents: 8817
diff changeset
   553
        ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            bb.put(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            // Do not update src until we see how many bytes were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            src.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            int n = sendFromNativeBuffer(fd, bb, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                // now update src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                src.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   572
                                     InetSocketAddress target)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   580
        boolean preferIPv6 = (family != StandardProtocolFamily.INET);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   581
        int written;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   582
        try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   583
            written = send0(preferIPv6, fd, ((DirectBuffer)bb).address() + pos,
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   584
                            rem, target.getAddress(), target.getPort());
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   585
        } catch (PortUnreachableException pue) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   586
            if (isConnected())
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   587
                throw pue;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   588
            written = rem;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   589
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if (written > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            bb.position(pos + written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   595
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public int read(ByteBuffer buf) throws IOException {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   597
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   598
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   599
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   600
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   601
            boolean blocking = isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   604
                beginRead(blocking, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                do {
16921
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents: 16734
diff changeset
   606
                    n = IOUtil.read(fd, buf, -1, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   608
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   610
                endRead(blocking, n > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   613
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   614
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   615
            readLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   616
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   617
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   618
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   619
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   620
    public long read(ByteBuffer[] dsts, int offset, int length)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   621
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   622
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   623
        Objects.checkFromIndexSize(offset, length, dsts.length);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   624
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   625
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   626
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   627
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   628
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   629
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   630
                beginRead(blocking, true);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   631
                do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   632
                    n = IOUtil.read(fd, dsts, offset, length, nd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   633
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   634
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   635
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   636
                endRead(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   637
                assert IOStatus.check(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   638
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   639
            return IOStatus.normalize(n);
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   640
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   641
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   645
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   646
     * Marks the beginning of a write operation that might block.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   647
     * @param blocking true if configured blocking
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   648
     * @param mustBeConnected true if the socket must be connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   649
     * @return remote address if connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   650
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   651
     * @throws NotYetConnectedException if mustBeConnected and not connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   652
     * @throws IOException if socket not bound and cannot be bound
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   653
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   654
    private SocketAddress beginWrite(boolean blocking, boolean mustBeConnected)
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   655
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   656
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   657
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   658
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   659
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   660
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   661
        SocketAddress remote;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   662
        synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   663
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   664
            remote = remoteAddress;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   665
            if ((remote == null) && mustBeConnected)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   666
                throw new NotYetConnectedException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   667
            if (localAddress == null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   668
                bindInternal(null);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   669
            if (blocking)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   670
                writerThread = NativeThread.current();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   671
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   672
        return remote;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   673
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   674
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   675
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   676
     * Marks the end of a write operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   677
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   678
     * @throws AsynchronousCloseException if the channel was closed asynchronously
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   679
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   680
    private void endWrite(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   681
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   682
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   683
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   684
            synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   685
                writerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   686
                // notify any thread waiting in implCloseSelectableChannel
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   687
                if (state == ST_CLOSING) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   688
                    stateLock.notifyAll();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   689
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   690
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   691
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   692
            end(completed);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   693
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   694
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   695
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   696
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   697
    public int write(ByteBuffer buf) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   698
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   699
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   700
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   701
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   702
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   703
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   704
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   705
                beginWrite(blocking, true);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   706
                do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   707
                    n = IOUtil.write(fd, buf, -1, nd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   708
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   709
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   710
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   711
                assert IOStatus.check(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   712
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   713
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   714
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   715
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   716
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   717
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   718
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   719
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   720
    public long write(ByteBuffer[] srcs, int offset, int length)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   721
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   722
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   723
        Objects.checkFromIndexSize(offset, length, srcs.length);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   724
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   725
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   726
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   727
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   728
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   729
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   730
                beginWrite(blocking, true);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   731
                do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   732
                    n = IOUtil.write(fd, srcs, offset, length, nd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   733
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   734
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   735
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   736
                assert IOStatus.check(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   737
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   738
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   739
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   740
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   741
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   742
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   743
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   744
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   745
    protected void implConfigureBlocking(boolean block) throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   746
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   747
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   748
            writeLock.lock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   750
                synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   751
                    ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   752
                    IOUtil.configureBlocking(fd, block);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   753
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   755
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   757
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   758
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   762
    InetSocketAddress localAddress() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            return localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   768
    InetSocketAddress remoteAddress() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   774
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   775
    public DatagramChannel bind(SocketAddress local) throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   776
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   777
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   778
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   779
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   782
                    if (localAddress != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        throw new AlreadyBoundException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   784
                    bindInternal(local);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   786
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   787
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   789
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   790
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   792
        return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   795
    private void bindInternal(SocketAddress local) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   796
        assert Thread.holdsLock(stateLock) && (localAddress == null);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   797
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   798
        InetSocketAddress isa;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   799
        if (local == null) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   800
            // only Inet4Address allowed with IPv4 socket
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   801
            if (family == StandardProtocolFamily.INET) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   802
                isa = new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   803
            } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   804
                isa = new InetSocketAddress(0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   805
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   806
        } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   807
            isa = Net.checkAddress(local, family);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   808
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   809
        SecurityManager sm = System.getSecurityManager();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   810
        if (sm != null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   811
            sm.checkListen(isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   812
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   813
        Net.bind(family, fd, isa.getAddress(), isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   814
        localAddress = Net.localAddress(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   815
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   816
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   817
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            return (state == ST_CONNECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   824
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    public DatagramChannel connect(SocketAddress sa) throws IOException {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   826
        InetSocketAddress isa = Net.checkAddress(sa, family);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   827
        SecurityManager sm = System.getSecurityManager();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   828
        if (sm != null) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   829
            InetAddress ia = isa.getAddress();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   830
            if (ia.isMulticastAddress()) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   831
                sm.checkMulticast(ia);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   832
            } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   833
                sm.checkConnect(ia.getHostAddress(), isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   834
                sm.checkAccept(ia.getHostAddress(), isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   835
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   836
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   837
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   838
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   839
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   840
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   841
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   843
                    ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   844
                    if (state == ST_CONNECTED)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   845
                        throw new AlreadyConnectedException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   846
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   847
                    int n = Net.connect(family,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   848
                                        fd,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                                        isa.getAddress(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   850
                                        isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                    if (n <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                        throw new Error();      // Can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   854
                    // connected
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   855
                    remoteAddress = isa;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   856
                    state = ST_CONNECTED;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   857
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   858
                    // refresh local address
7045
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents: 6301
diff changeset
   859
                    localAddress = Net.localAddress(fd);
27078
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
   860
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
   861
                    // flush any packets already received.
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   862
                    boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   863
                    if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   864
                        IOUtil.configureBlocking(fd, false);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   865
                    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   866
                    try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   867
                        ByteBuffer buf = ByteBuffer.allocate(100);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   868
                        while (receive(buf) != null) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   869
                            buf.clear();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   870
                        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   871
                    } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   872
                        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   873
                            IOUtil.configureBlocking(fd, true);
27078
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
   874
                        }
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
   875
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   877
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   878
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   880
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   881
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   886
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    public DatagramChannel disconnect() throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   888
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   889
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   890
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   891
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   893
                    if (!isOpen() || (state != ST_CONNECTED))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                        return this;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   895
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   896
                    // disconnect socket
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 12440
diff changeset
   897
                    boolean isIPv6 = (family == StandardProtocolFamily.INET6);
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 12440
diff changeset
   898
                    disconnect0(fd, isIPv6);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   899
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   900
                    // no longer connected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    remoteAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    state = ST_UNCONNECTED;
7045
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents: 6301
diff changeset
   903
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents: 6301
diff changeset
   904
                    // refresh local address
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents: 6301
diff changeset
   905
                    localAddress = Net.localAddress(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   907
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   908
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   910
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   911
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   916
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   917
     * Joins channel's socket to the given group/interface and
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   918
     * optional source address.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   919
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   920
    private MembershipKey innerJoin(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   921
                                    NetworkInterface interf,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   922
                                    InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   923
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   924
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   925
        if (!group.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   926
            throw new IllegalArgumentException("Group not a multicast address");
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
        // check multicast address is compatible with this socket
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   929
        if (group instanceof Inet4Address) {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   930
            if (family == StandardProtocolFamily.INET6 && !Net.canIPv6SocketJoinIPv4Group())
9237
cbb5753e87e7 7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents: 8817
diff changeset
   931
                throw new IllegalArgumentException("IPv6 socket cannot join IPv4 multicast group");
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   932
        } else if (group instanceof Inet6Address) {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   933
            if (family != StandardProtocolFamily.INET6)
9237
cbb5753e87e7 7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents: 8817
diff changeset
   934
                throw new IllegalArgumentException("Only IPv6 sockets can join IPv6 multicast group");
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   935
        } else {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   936
            throw new IllegalArgumentException("Address type not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   937
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   938
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   939
        // check source address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   940
        if (source != null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   941
            if (source.isAnyLocalAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   942
                throw new IllegalArgumentException("Source address is a wildcard address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   943
            if (source.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   944
                throw new IllegalArgumentException("Source address is multicast address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   945
            if (source.getClass() != group.getClass())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   946
                throw new IllegalArgumentException("Source address is different type to group");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   947
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   948
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   949
        SecurityManager sm = System.getSecurityManager();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   950
        if (sm != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   951
            sm.checkMulticast(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   952
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   953
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   954
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   955
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   956
            // 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
   957
            if (registry == null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   958
                registry = new MembershipRegistry();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   959
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   960
                // return existing membership key
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   961
                MembershipKey key = registry.checkMembership(group, interf, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   962
                if (key != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   963
                    return key;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   964
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   965
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   966
            MembershipKeyImpl key;
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   967
            if ((family == StandardProtocolFamily.INET6) &&
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   968
                ((group instanceof Inet6Address) || Net.canJoin6WithIPv4Group()))
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   969
            {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   970
                int index = interf.getIndex();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   971
                if (index == -1)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   972
                    throw new IOException("Network interface cannot be identified");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   973
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   974
                // need multicast and source address as byte arrays
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   975
                byte[] groupAddress = Net.inet6AsByteArray(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   976
                byte[] sourceAddress = (source == null) ? null :
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   977
                    Net.inet6AsByteArray(source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   978
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   979
                // join the group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   980
                int n = Net.join6(fd, groupAddress, index, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   981
                if (n == IOStatus.UNAVAILABLE)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   982
                    throw new UnsupportedOperationException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   983
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   984
                key = new MembershipKeyImpl.Type6(this, group, interf, source,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   985
                                                  groupAddress, index, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   986
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   987
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   988
                // need IPv4 address to identify interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   989
                Inet4Address target = Net.anyInet4Address(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   990
                if (target == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   991
                    throw new IOException("Network interface not configured for IPv4");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   992
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   993
                int groupAddress = Net.inet4AsInt(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   994
                int targetAddress = Net.inet4AsInt(target);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   995
                int sourceAddress = (source == null) ? 0 : Net.inet4AsInt(source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   996
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   997
                // join the group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   998
                int n = Net.join4(fd, groupAddress, targetAddress, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   999
                if (n == IOStatus.UNAVAILABLE)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1000
                    throw new UnsupportedOperationException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1001
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1002
                key = new MembershipKeyImpl.Type4(this, group, interf, source,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1003
                                                  groupAddress, targetAddress, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1004
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1005
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1006
            registry.add(key);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1007
            return key;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1008
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1009
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1010
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1011
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1012
    public MembershipKey join(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1013
                              NetworkInterface interf)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1014
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1015
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1016
        return innerJoin(group, interf, null);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1017
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1018
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1019
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1020
    public MembershipKey join(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1021
                              NetworkInterface interf,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1022
                              InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1023
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1024
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1025
        Objects.requireNonNull(source);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1026
        return innerJoin(group, interf, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1027
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1028
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1029
    // package-private
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1030
    void drop(MembershipKeyImpl key) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1031
        assert key.channel() == this;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1032
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1033
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1034
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1035
                return;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1036
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1037
            try {
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1038
                if (key instanceof MembershipKeyImpl.Type6) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1039
                    MembershipKeyImpl.Type6 key6 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1040
                        (MembershipKeyImpl.Type6)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1041
                    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
  1042
                } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1043
                    MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1044
                    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
  1045
                        key4.source());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1046
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1047
            } catch (IOException ioe) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1048
                // should not happen
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1049
                throw new AssertionError(ioe);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1050
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1051
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1052
            key.invalidate();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1053
            registry.remove(key);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1054
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1055
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1056
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1057
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1058
     * 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
  1059
     * datagrams.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1060
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1061
    void block(MembershipKeyImpl key, InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1062
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1063
    {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1064
        assert key.channel() == this;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1065
        assert key.sourceAddress() == null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1066
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1067
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1068
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1069
                throw new IllegalStateException("key is no longer valid");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1070
            if (source.isAnyLocalAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1071
                throw new IllegalArgumentException("Source address is a wildcard address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1072
            if (source.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1073
                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
  1074
            if (source.getClass() != key.group().getClass())
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1075
                throw new IllegalArgumentException("Source address is different type to group");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1076
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1077
            int n;
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1078
            if (key instanceof MembershipKeyImpl.Type6) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1079
                 MembershipKeyImpl.Type6 key6 =
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1080
                    (MembershipKeyImpl.Type6)key;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1081
                n = Net.block6(fd, key6.groupAddress(), key6.index(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1082
                               Net.inet6AsByteArray(source));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1083
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1084
                MembershipKeyImpl.Type4 key4 =
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1085
                    (MembershipKeyImpl.Type4)key;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1086
                n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1087
                               Net.inet4AsInt(source));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1088
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1089
            if (n == IOStatus.UNAVAILABLE) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1090
                // ancient kernel
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1091
                throw new UnsupportedOperationException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1092
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1093
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1094
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1095
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1096
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1097
     * Unblock given source.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1098
     */
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1099
    void unblock(MembershipKeyImpl key, InetAddress source) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1100
        assert key.channel() == this;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1101
        assert key.sourceAddress() == null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1102
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1103
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1104
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1105
                throw new IllegalStateException("key is no longer valid");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1106
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1107
            try {
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1108
                if (key instanceof MembershipKeyImpl.Type6) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1109
                    MembershipKeyImpl.Type6 key6 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1110
                        (MembershipKeyImpl.Type6)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1111
                    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
  1112
                                 Net.inet6AsByteArray(source));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1113
                } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1114
                    MembershipKeyImpl.Type4 key4 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1115
                        (MembershipKeyImpl.Type4)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1116
                    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
  1117
                                 Net.inet4AsInt(source));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1118
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1119
            } catch (IOException ioe) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1120
                // should not happen
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1121
                throw new AssertionError(ioe);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1122
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1123
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1124
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1125
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1126
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1127
     * Invoked by implCloseChannel to close the channel.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1128
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1129
     * This method waits for outstanding I/O operations to complete. When in
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1130
     * blocking mode, the socket is pre-closed and the threads in blocking I/O
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1131
     * operations are signalled to ensure that the outstanding I/O operations
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1132
     * complete quickly.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1133
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1134
     * The socket is closed by this method when it is not registered with a
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1135
     * Selector. Note that a channel configured blocking may be registered with
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1136
     * a Selector. This arises when a key is canceled and the channel configured
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1137
     * to blocking mode before the key is flushed from the Selector.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1138
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1139
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    protected void implCloseSelectableChannel() throws IOException {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1141
        assert !isOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1142
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1143
        boolean blocking;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1144
        boolean interrupted = false;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1145
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1146
        // set state to ST_CLOSING and invalid membership keys
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1148
            assert state < ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1149
            blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1150
            state = ST_CLOSING;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1151
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1152
            // if member of any multicast groups then invalidate the keys
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1153
            if (registry != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1154
                registry.invalidateAll();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1155
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1156
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1157
        // wait for any outstanding I/O operations to complete
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1158
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1159
            synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1160
                assert state == ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1161
                long reader = readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1162
                long writer = writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1163
                if (reader != 0 || writer != 0) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1164
                    nd.preClose(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1165
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1166
                    if (reader != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1167
                        NativeThread.signal(reader);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1168
                    if (writer != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1169
                        NativeThread.signal(writer);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1170
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1171
                    // wait for blocking I/O operations to end
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1172
                    while (readerThread != 0 || writerThread != 0) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1173
                        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1174
                            stateLock.wait();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1175
                        } catch (InterruptedException e) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1176
                            interrupted = true;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1177
                        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1178
                    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1179
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1180
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1181
        } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1182
            // non-blocking mode: wait for read/write to complete
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1183
            readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1184
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1185
                writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1186
                writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1187
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1188
                readLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1189
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1191
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1192
        // set state to ST_KILLPENDING
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1193
        synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1194
            assert state == ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1195
            state = ST_KILLPENDING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1196
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1197
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1198
        // close socket if not registered with Selector
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1199
        if (!isRegistered())
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1200
            kill();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1201
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1202
        // restore interrupt status
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1203
        if (interrupted)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1204
            Thread.currentThread().interrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1207
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    public void kill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1210
            if (state == ST_KILLPENDING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                state = ST_KILLED;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1212
                try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1213
                    nd.close(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1214
                } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1215
                    // notify resource manager
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1216
                    ResourceManager.afterUdpClose();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1217
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 41134
diff changeset
  1222
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        // fd is null if constructor threw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        if (fd != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * Translates native poll revent set into a ready operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    public boolean translateReadyOps(int ops, int initialOps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                                     SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        int oldOps = sk.nioReadyOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1238
        if ((ops & Net.POLLNVAL) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1245
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            newOps = intOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1251
        if (((ops & Net.POLLIN) != 0) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            ((intOps & SelectionKey.OP_READ) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1255
        if (((ops & Net.POLLOUT) != 0) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            ((intOps & SelectionKey.OP_WRITE) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        return translateReadyOps(ops, 0, sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1271
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1272
     * Poll this channel's socket for reading up to the given timeout.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1273
     * @return {@code true} if the socket is polled
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1274
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1275
    boolean pollRead(long timeout) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1276
        boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1277
        assert Thread.holdsLock(blockingLock()) && blocking;
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1278
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1279
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1280
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1281
            boolean polled = false;
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1282
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1283
                beginRead(blocking, false);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1284
                int n = Net.poll(fd, Net.POLLIN, timeout);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1285
                polled = (n > 0);
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1286
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1287
                endRead(blocking, polled);
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1288
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1289
            return polled;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1290
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1291
            readLock.unlock();
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1292
        }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1293
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1294
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        if ((ops & SelectionKey.OP_READ) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1302
            newOps |= Net.POLLIN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        if ((ops & SelectionKey.OP_WRITE) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1304
            newOps |= Net.POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        if ((ops & SelectionKey.OP_CONNECT) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1306
            newOps |= Net.POLLIN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        sk.selector.putEventOps(sk, newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 12440
diff changeset
  1323
    private static native void disconnect0(FileDescriptor fd, boolean isIPv6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    private native int receive0(FileDescriptor fd, long address, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                                boolean connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
  1330
    private native int send0(boolean preferIPv6, FileDescriptor fd, long address,
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
  1331
                             int len, InetAddress addr, int port)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 18212
diff changeset
  1335
        IOUtil.load();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
}