src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java
author dfuchs
Wed, 09 Oct 2019 17:38:58 +0100
changeset 58518 705c3f88a409
parent 55375 96c7427456f9
child 58679 9c3209ff7550
child 58808 9261ad32cba9
permissions -rw-r--r--
8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx) Summary: DatagramChannel::disconnect will attempt to rebind to the original port if the local port switches back to 0 after the association is disolved by the system. Reviewed-by: alanb, chegar, fweimer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53419
eac105e3ec13 8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents: 50303
diff changeset
     2
 * Copyright (c) 2001, 2019, 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;
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
    60
import sun.net.util.IPAddressUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * An implementation of DatagramChannels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
class DatagramChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    extends DatagramChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // Used to make native read and write calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static NativeDispatcher nd = new DatagramDispatcher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    73
    // The protocol family of the socket
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    74
    private final ProtocolFamily family;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    75
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Our file descriptor
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    77
    private final FileDescriptor fd;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    78
    private final int fdVal;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    79
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // Cached InetAddress and port for unconnected DatagramChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // used by receive0
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    82
    private InetAddress cachedSenderInetAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    83
    private int cachedSenderPort;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // 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
    86
    private final ReentrantLock readLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // 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
    89
    private final ReentrantLock writeLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // DO NOT invoke a blocking I/O operation while holding this lock!
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
    93
    private final Object stateLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // State (does not necessarily increase monotonically)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    98
    private static final int ST_UNCONNECTED = 0;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    99
    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
   100
    private static final int ST_CLOSING = 2;
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   101
    private static final int ST_CLOSED = 3;
49001
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);
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   182
        synchronized (stateLock) {
49001
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() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   195
        synchronized (stateLock) {
2
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 {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
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 {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
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");
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54754
diff changeset
   226
        if (!name.type().isInstance(value))
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54754
diff changeset
   227
            throw new IllegalArgumentException("Invalid value '" + value + "'");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   228
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   229
        synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   230
            ensureOpen();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   231
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23879
diff changeset
   232
            if (name == StandardSocketOptions.IP_TOS ||
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23879
diff changeset
   233
                name == StandardSocketOptions.IP_MULTICAST_TTL ||
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   234
                name == StandardSocketOptions.IP_MULTICAST_LOOP)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   235
            {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   236
                // options are protocol dependent
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   237
                Net.setSocketOption(fd, family, name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   238
                return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   239
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   240
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   241
            if (name == StandardSocketOptions.IP_MULTICAST_IF) {
1152
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
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   279
        synchronized (stateLock) {
1152
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);
53419
eac105e3ec13 8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents: 50303
diff changeset
   337
            set.addAll(ExtendedSocketOptions.datagramSocketOptions());
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;
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   365
        synchronized (stateLock) {
49001
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) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   387
            synchronized (stateLock) {
49001
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
                if (state == ST_CLOSING) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   390
                    tryFinishClose();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   391
                }
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
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   394
            end(completed);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   395
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    private SocketAddress sender;       // Set by receive0 (## ugh)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   400
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public SocketAddress receive(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (dst.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            throw new IllegalArgumentException("Read-only buffer");
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   404
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   405
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   406
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   407
            boolean blocking = isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   411
                SocketAddress remote = beginRead(blocking, false);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   412
                boolean connected = (remote != null);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   413
                SecurityManager sm = System.getSecurityManager();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   414
                if (connected || (sm == null)) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   415
                    // connected or no security manager
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   416
                    n = receive(fd, dst, connected);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   417
                    if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   418
                        while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   419
                            park(Net.POLLIN);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   420
                            n = receive(fd, dst, connected);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   421
                        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   422
                    } else if (n == IOStatus.UNAVAILABLE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        return null;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   424
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                } else {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   426
                    // 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
   427
                    // security manager and not connected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    bb = Util.getTemporaryDirectBuffer(dst.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    for (;;) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   430
                        n = receive(fd, bb, connected);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   431
                        if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   432
                            while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   433
                                park(Net.POLLIN);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   434
                                n = receive(fd, bb, connected);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   435
                            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   436
                        } else if (n == IOStatus.UNAVAILABLE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                            return null;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   438
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        InetSocketAddress isa = (InetSocketAddress)sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   441
                            sm.checkAccept(isa.getAddress().getHostAddress(),
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   442
                                           isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                            // Ignore packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                            n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   454
                assert sender != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                return sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (bb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    Util.releaseTemporaryDirectBuffer(bb);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   459
                endRead(blocking, n > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   462
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   463
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   467
    private int receive(FileDescriptor fd, ByteBuffer dst, boolean connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        int pos = dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        int lim = dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (dst instanceof DirectBuffer && rem > 0)
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   475
            return receiveIntoNativeBuffer(fd, dst, rem, pos, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        // Substitute a native buffer. If the supplied buffer is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        // we must instead use a nonempty buffer, otherwise the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // will not block waiting for a datagram on some platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        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
   481
        ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   483
            int n = receiveIntoNativeBuffer(fd, bb, newSize, 0, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (n > 0 && rem > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    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
   494
                                        int rem, int pos, boolean connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   497
        int n = receive0(fd, ((DirectBuffer)bb).address() + pos, rem, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            bb.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   503
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public int send(ByteBuffer src, SocketAddress target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   507
        Objects.requireNonNull(src);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   508
        InetSocketAddress isa = Net.checkAddress(target, family);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   510
        writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   511
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   512
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   513
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   514
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   515
                SocketAddress remote = beginWrite(blocking, false);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   516
                if (remote != null) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   517
                    // connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   518
                    if (!target.equals(remote)) {
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 49001
diff changeset
   519
                        throw new AlreadyConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   520
                    }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   521
                    n = IOUtil.write(fd, src, -1, nd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   522
                    if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   523
                        while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   524
                            park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   525
                            n = IOUtil.write(fd, src, -1, nd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   526
                        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   527
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   528
                } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   529
                    // not connected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    SecurityManager sm = System.getSecurityManager();
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   531
                    InetAddress ia = isa.getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        if (ia.isMulticastAddress()) {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   534
                            sm.checkMulticast(ia);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        } else {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   536
                            sm.checkConnect(ia.getHostAddress(), isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    }
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   539
                    if (ia.isLinkLocalAddress())
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   540
                        isa = IPAddressUtil.toScopedAddress(isa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   541
                    n = send(fd, src, isa);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   542
                    if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   543
                        while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   544
                            park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   545
                            n = send(fd, src, isa);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   546
                        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   547
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   550
                endWrite(blocking, n > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   553
            return IOStatus.normalize(n);
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   554
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   555
            writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   559
    private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (src instanceof DirectBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            return sendFromNativeBuffer(fd, src, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        // Substitute a native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        int pos = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        int lim = src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
9237
cbb5753e87e7 7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents: 8817
diff changeset
   571
        ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            bb.put(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            // Do not update src until we see how many bytes were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            src.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            int n = sendFromNativeBuffer(fd, bb, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                // now update src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                src.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   590
                                     InetSocketAddress target)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   598
        boolean preferIPv6 = (family != StandardProtocolFamily.INET);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   599
        int written;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   600
        try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   601
            written = send0(preferIPv6, fd, ((DirectBuffer)bb).address() + pos,
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   602
                            rem, target.getAddress(), target.getPort());
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   603
        } catch (PortUnreachableException pue) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   604
            if (isConnected())
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   605
                throw pue;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   606
            written = rem;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   607
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (written > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            bb.position(pos + written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
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
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    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
   615
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   616
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   617
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   618
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   619
            boolean blocking = isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   622
                beginRead(blocking, true);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   623
                n = IOUtil.read(fd, buf, -1, nd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   624
                if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   625
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   626
                        park(Net.POLLIN);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   627
                        n = IOUtil.read(fd, buf, -1, nd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   628
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   629
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   631
                endRead(blocking, n > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   634
            return IOStatus.normalize(n);
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
            readLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   637
        }
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
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   640
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   641
    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
   642
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   643
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   644
        Objects.checkFromIndexSize(offset, length, dsts.length);
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
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   647
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   648
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   649
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   650
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   651
                beginRead(blocking, true);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   652
                n = IOUtil.read(fd, dsts, offset, length, nd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   653
                if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   654
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   655
                        park(Net.POLLIN);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   656
                        n = IOUtil.read(fd, dsts, offset, length, nd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   657
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   658
                }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   659
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   660
                endRead(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   661
                assert IOStatus.check(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   662
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   663
            return IOStatus.normalize(n);
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   664
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   665
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   669
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   670
     * 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
   671
     * @param blocking true if configured blocking
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   672
     * @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
   673
     * @return remote address if connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   674
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   675
     * @throws NotYetConnectedException if mustBeConnected and not connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   676
     * @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
   677
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   678
    private SocketAddress beginWrite(boolean blocking, boolean mustBeConnected)
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   679
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   680
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   681
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   682
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   683
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   684
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   685
        SocketAddress remote;
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   686
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   687
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   688
            remote = remoteAddress;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   689
            if ((remote == null) && mustBeConnected)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   690
                throw new NotYetConnectedException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   691
            if (localAddress == null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   692
                bindInternal(null);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   693
            if (blocking)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   694
                writerThread = NativeThread.current();
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
        return remote;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   697
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   698
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
     * 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
   701
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   702
     * @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
   703
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   704
    private void endWrite(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   705
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   706
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   707
        if (blocking) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   708
            synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   709
                writerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   710
                if (state == ST_CLOSING) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   711
                    tryFinishClose();
49001
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
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   714
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   715
            end(completed);
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 int write(ByteBuffer buf) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   721
        Objects.requireNonNull(buf);
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
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   724
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   725
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   726
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   727
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   728
                beginWrite(blocking, true);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   729
                n = IOUtil.write(fd, buf, -1, nd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   730
                if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   731
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   732
                        park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   733
                        n = IOUtil.write(fd, buf, -1, nd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   734
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   735
                }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   736
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   737
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   738
                assert IOStatus.check(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   739
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   740
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   741
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   742
            writeLock.unlock();
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
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   745
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   746
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   747
    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
   748
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   749
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   750
        Objects.checkFromIndexSize(offset, length, srcs.length);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   751
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   752
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   753
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   754
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   755
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   756
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   757
                beginWrite(blocking, true);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   758
                n = IOUtil.write(fd, srcs, offset, length, nd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   759
                if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   760
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   761
                        park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   762
                        n = IOUtil.write(fd, srcs, offset, length, nd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   763
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   764
                }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   765
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   766
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   767
                assert IOStatus.check(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   768
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   769
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   770
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   771
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   772
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   773
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   774
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   775
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   776
    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
   777
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   778
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   779
            writeLock.lock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            try {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   781
                synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   782
                    ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   783
                    IOUtil.configureBlocking(fd, block);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   784
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   786
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   788
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   789
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   793
    InetSocketAddress localAddress() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   794
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            return localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   799
    InetSocketAddress remoteAddress() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   800
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   805
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   806
    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
   807
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   808
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   809
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   810
            try {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   811
                synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   813
                    if (localAddress != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                        throw new AlreadyBoundException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   815
                    bindInternal(local);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   817
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   818
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   820
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   821
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   823
        return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   826
    private void bindInternal(SocketAddress local) throws IOException {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   827
        assert Thread.holdsLock(stateLock )&& (localAddress == null);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   828
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   829
        InetSocketAddress isa;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   830
        if (local == null) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   831
            // only Inet4Address allowed with IPv4 socket
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   832
            if (family == StandardProtocolFamily.INET) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   833
                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
   834
            } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   835
                isa = new InetSocketAddress(0);
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
        } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   838
            isa = Net.checkAddress(local, family);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   839
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   840
        SecurityManager sm = System.getSecurityManager();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   841
        if (sm != null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   842
            sm.checkListen(isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   843
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   844
        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
   845
        localAddress = Net.localAddress(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   846
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   847
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   848
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    public boolean isConnected() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   850
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            return (state == ST_CONNECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   855
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    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
   857
        InetSocketAddress isa = Net.checkAddress(sa, family);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   858
        SecurityManager sm = System.getSecurityManager();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   859
        if (sm != null) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   860
            InetAddress ia = isa.getAddress();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   861
            if (ia.isMulticastAddress()) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   862
                sm.checkMulticast(ia);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   863
            } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   864
                sm.checkConnect(ia.getHostAddress(), isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   865
                sm.checkAccept(ia.getHostAddress(), isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   866
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   867
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   868
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   869
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   870
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   871
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   872
            try {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   873
                synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   874
                    ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   875
                    if (state == ST_CONNECTED)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   876
                        throw new AlreadyConnectedException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   877
58518
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   878
                    // ensure that the socket is bound
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   879
                    if (localAddress == null) {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   880
                        bindInternal(null);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   881
                    }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   882
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   883
                    int n = Net.connect(family,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   884
                                        fd,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                                        isa.getAddress(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   886
                                        isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    if (n <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                        throw new Error();      // Can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   890
                    // connected
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   891
                    remoteAddress = isa;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   892
                    state = ST_CONNECTED;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   893
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   894
                    // refresh local address
7045
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents: 6301
diff changeset
   895
                    localAddress = Net.localAddress(fd);
27078
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
   896
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
   897
                    // flush any packets already received.
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   898
                    boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   899
                    if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   900
                        IOUtil.configureBlocking(fd, false);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   901
                    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   902
                    try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   903
                        ByteBuffer buf = ByteBuffer.allocate(100);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   904
                        while (receive(fd, buf, false) > 0) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   905
                            buf.clear();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   906
                        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   907
                    } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   908
                        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   909
                            IOUtil.configureBlocking(fd, true);
27078
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
   910
                        }
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
   911
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   913
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   914
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   916
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   917
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   922
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    public DatagramChannel disconnect() throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   924
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   925
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   926
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   927
            try {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   928
                synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   929
                    if (!isOpen() || (state != ST_CONNECTED))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                        return this;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   931
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   932
                    // disconnect socket
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 12440
diff changeset
   933
                    boolean isIPv6 = (family == StandardProtocolFamily.INET6);
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 12440
diff changeset
   934
                    disconnect0(fd, isIPv6);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   935
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   936
                    // no longer connected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    remoteAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    state = ST_UNCONNECTED;
7045
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents: 6301
diff changeset
   939
58518
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   940
                    // check whether rebind is needed
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   941
                    InetSocketAddress isa = Net.localAddress(fd);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   942
                    if (isa.getPort() == 0) {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   943
                        // On Linux, if bound to ephemeral port,
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   944
                        // disconnect does not preserve that port.
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   945
                        // In this case, try to rebind to the previous port.
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   946
                        int port = localAddress.getPort();
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   947
                        localAddress = isa; // in case Net.bind fails
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   948
                        Net.bind(family, fd, isa.getAddress(), port);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   949
                        isa = Net.localAddress(fd); // refresh address
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   950
                        assert isa.getPort() == port;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   951
                    }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   952
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   953
                    // refresh localAddress
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
   954
                    localAddress = isa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   956
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   957
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   959
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   960
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
1152
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
     * Joins channel's socket to the given group/interface and
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   967
     * optional source address.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   968
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   969
    private MembershipKey innerJoin(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   970
                                    NetworkInterface interf,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   971
                                    InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   972
        throws IOException
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
        if (!group.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   975
            throw new IllegalArgumentException("Group not a multicast address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   976
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   977
        // 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
   978
        if (group instanceof Inet4Address) {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   979
            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
   980
                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
   981
        } else if (group instanceof Inet6Address) {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   982
            if (family != StandardProtocolFamily.INET6)
9237
cbb5753e87e7 7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents: 8817
diff changeset
   983
                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
   984
        } else {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
   985
            throw new IllegalArgumentException("Address type not supported");
1152
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   988
        // check source address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   989
        if (source != null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   990
            if (source.isAnyLocalAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   991
                throw new IllegalArgumentException("Source address is a wildcard address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   992
            if (source.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   993
                throw new IllegalArgumentException("Source address is multicast address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   994
            if (source.getClass() != group.getClass())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   995
                throw new IllegalArgumentException("Source address is different type to group");
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   998
        SecurityManager sm = System.getSecurityManager();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   999
        if (sm != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1000
            sm.checkMulticast(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1001
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1002
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1003
            ensureOpen();
1152
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
            // 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
  1006
            if (registry == null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1007
                registry = new MembershipRegistry();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1008
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1009
                // return existing membership key
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1010
                MembershipKey key = registry.checkMembership(group, interf, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1011
                if (key != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1012
                    return key;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1013
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1014
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1015
            MembershipKeyImpl key;
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1016
            if ((family == StandardProtocolFamily.INET6) &&
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1017
                ((group instanceof Inet6Address) || Net.canJoin6WithIPv4Group()))
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1018
            {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1019
                int index = interf.getIndex();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1020
                if (index == -1)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1021
                    throw new IOException("Network interface cannot be identified");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1022
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1023
                // need multicast and source address as byte arrays
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1024
                byte[] groupAddress = Net.inet6AsByteArray(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1025
                byte[] sourceAddress = (source == null) ? null :
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1026
                    Net.inet6AsByteArray(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
                // join the group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1029
                int n = Net.join6(fd, groupAddress, index, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1030
                if (n == IOStatus.UNAVAILABLE)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1031
                    throw new UnsupportedOperationException();
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
                key = new MembershipKeyImpl.Type6(this, group, interf, source,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1034
                                                  groupAddress, index, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1035
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1036
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1037
                // need IPv4 address to identify interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1038
                Inet4Address target = Net.anyInet4Address(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1039
                if (target == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1040
                    throw new IOException("Network interface not configured for IPv4");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1041
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1042
                int groupAddress = Net.inet4AsInt(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1043
                int targetAddress = Net.inet4AsInt(target);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1044
                int sourceAddress = (source == null) ? 0 : Net.inet4AsInt(source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1045
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1046
                // join the group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1047
                int n = Net.join4(fd, groupAddress, targetAddress, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1048
                if (n == IOStatus.UNAVAILABLE)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1049
                    throw new UnsupportedOperationException();
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
                key = new MembershipKeyImpl.Type4(this, group, interf, source,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1052
                                                  groupAddress, targetAddress, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1053
            }
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
            registry.add(key);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1056
            return key;
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
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1059
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1060
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1061
    public MembershipKey join(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1062
                              NetworkInterface interf)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1063
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1064
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1065
        return innerJoin(group, interf, null);
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1068
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1069
    public MembershipKey join(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1070
                              NetworkInterface interf,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1071
                              InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1072
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1073
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1074
        Objects.requireNonNull(source);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1075
        return innerJoin(group, interf, source);
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1078
    // package-private
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1079
    void drop(MembershipKeyImpl key) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1080
        assert key.channel() == this;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1081
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1082
        synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1083
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1084
                return;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1085
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1086
            try {
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1087
                if (key instanceof MembershipKeyImpl.Type6) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1088
                    MembershipKeyImpl.Type6 key6 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1089
                        (MembershipKeyImpl.Type6)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1090
                    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
  1091
                } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1092
                    MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1093
                    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
  1094
                        key4.source());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1095
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1096
            } catch (IOException ioe) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1097
                // should not happen
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1098
                throw new AssertionError(ioe);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1099
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1100
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1101
            key.invalidate();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1102
            registry.remove(key);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1103
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1104
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1105
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1106
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1107
     * 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
  1108
     * datagrams.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1109
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1110
    void block(MembershipKeyImpl key, InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1111
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1112
    {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1113
        assert key.channel() == this;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1114
        assert key.sourceAddress() == null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1115
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1116
        synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1117
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1118
                throw new IllegalStateException("key is no longer valid");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1119
            if (source.isAnyLocalAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1120
                throw new IllegalArgumentException("Source address is a wildcard address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1121
            if (source.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1122
                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
  1123
            if (source.getClass() != key.group().getClass())
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1124
                throw new IllegalArgumentException("Source address is different type to group");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1125
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1126
            int n;
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1127
            if (key instanceof MembershipKeyImpl.Type6) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1128
                 MembershipKeyImpl.Type6 key6 =
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1129
                    (MembershipKeyImpl.Type6)key;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1130
                n = Net.block6(fd, key6.groupAddress(), key6.index(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1131
                               Net.inet6AsByteArray(source));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1132
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1133
                MembershipKeyImpl.Type4 key4 =
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1134
                    (MembershipKeyImpl.Type4)key;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1135
                n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1136
                               Net.inet4AsInt(source));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1137
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1138
            if (n == IOStatus.UNAVAILABLE) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1139
                // ancient kernel
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1140
                throw new UnsupportedOperationException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1141
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1142
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1143
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1144
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1145
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1146
     * Unblock given source.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1147
     */
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1148
    void unblock(MembershipKeyImpl key, InetAddress source) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1149
        assert key.channel() == this;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1150
        assert key.sourceAddress() == null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1151
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1152
        synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1153
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1154
                throw new IllegalStateException("key is no longer valid");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1155
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1156
            try {
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1157
                if (key instanceof MembershipKeyImpl.Type6) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1158
                    MembershipKeyImpl.Type6 key6 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1159
                        (MembershipKeyImpl.Type6)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1160
                    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
  1161
                                 Net.inet6AsByteArray(source));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1162
                } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1163
                    MembershipKeyImpl.Type4 key4 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1164
                        (MembershipKeyImpl.Type4)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1165
                    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
  1166
                                 Net.inet4AsInt(source));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1167
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1168
            } catch (IOException ioe) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1169
                // should not happen
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1170
                throw new AssertionError(ioe);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1171
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1172
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1173
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1174
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1175
    /**
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1176
     * Closes the socket if there are no I/O operations in progress and the
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1177
     * channel is not registered with a Selector.
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1178
     */
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1179
    private boolean tryClose() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1180
        assert Thread.holdsLock(stateLock) && state == ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1181
        if ((readerThread == 0) && (writerThread == 0) && !isRegistered()) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1182
            state = ST_CLOSED;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1183
            try {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1184
                nd.close(fd);
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1185
            } finally {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1186
                // notify resource manager
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1187
                ResourceManager.afterUdpClose();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1188
            }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1189
            return true;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1190
        } else {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1191
            return false;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1192
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1193
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1194
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1195
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1196
     * Invokes tryClose to attempt to close the socket.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1197
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1198
     * This method is used for deferred closing by I/O and Selector operations.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1199
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1200
    private void tryFinishClose() {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1201
        try {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1202
            tryClose();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1203
        } catch (IOException ignore) { }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1204
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1205
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1206
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1207
     * Closes this channel when configured in blocking mode.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1208
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1209
     * If there is an I/O operation in progress then the socket is pre-closed
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1210
     * and the I/O threads signalled, in which case the final close is deferred
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1211
     * until all I/O operations complete.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1212
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1213
    private void implCloseBlockingMode() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1214
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1215
            assert state < ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1216
            state = ST_CLOSING;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1217
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1218
            // 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
  1219
            if (registry != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1220
                registry.invalidateAll();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1221
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1222
            if (!tryClose()) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1223
                long reader = readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1224
                long writer = writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1225
                if (reader != 0 || writer != 0) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1226
                    nd.preClose(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1227
                    if (reader != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1228
                        NativeThread.signal(reader);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1229
                    if (writer != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1230
                        NativeThread.signal(writer);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1231
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1232
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        }
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1234
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1235
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1236
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1237
     * Closes this channel when configured in non-blocking mode.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1238
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1239
     * If the channel is registered with a Selector then the close is deferred
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1240
     * until the channel is flushed from all Selectors.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1241
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1242
    private void implCloseNonBlockingMode() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1243
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1244
            assert state < ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1245
            state = ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1246
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1247
            // if member of any multicast groups then invalidate the keys
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1248
            if (registry != null)
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1249
                registry.invalidateAll();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1250
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1251
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1252
        // wait for any read/write operations to complete before trying to close
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1253
        readLock.lock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1254
        readLock.unlock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1255
        writeLock.lock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1256
        writeLock.unlock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1257
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1258
            if (state == ST_CLOSING) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1259
                tryClose();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1260
            }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1261
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1262
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1263
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1264
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1265
     * Invoked by implCloseChannel to close the channel.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1266
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1267
    @Override
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1268
    protected void implCloseSelectableChannel() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1269
        assert !isOpen();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1270
        if (isBlocking()) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1271
            implCloseBlockingMode();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1272
        } else {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1273
            implCloseNonBlockingMode();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1274
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1277
    @Override
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1278
    public void kill() {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1279
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1280
            if (state == ST_CLOSING) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1281
                tryFinishClose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 41134
diff changeset
  1286
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        // fd is null if constructor threw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        if (fd != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * Translates native poll revent set into a ready operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1296
    public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1297
        int intOps = ski.nioInterestOps();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1298
        int oldOps = ski.nioReadyOps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1301
        if ((ops & Net.POLLNVAL) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1308
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            newOps = intOps;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1310
            ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1314
        if (((ops & Net.POLLIN) != 0) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            ((intOps & SelectionKey.OP_READ) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1318
        if (((ops & Net.POLLOUT) != 0) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            ((intOps & SelectionKey.OP_WRITE) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1322
        ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1326
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1327
        return translateReadyOps(ops, ski.nioReadyOps(), ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1330
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1331
        return translateReadyOps(ops, 0, ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1334
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1335
     * 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
  1336
     * @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
  1337
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1338
    boolean pollRead(long timeout) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1339
        boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1340
        assert Thread.holdsLock(blockingLock()) && blocking;
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1341
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1342
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1343
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1344
            boolean polled = false;
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1345
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1346
                beginRead(blocking, false);
49702
09c01737ad27 8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails
alanb
parents: 49493
diff changeset
  1347
                int events = Net.poll(fd, Net.POLLIN, timeout);
09c01737ad27 8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails
alanb
parents: 49493
diff changeset
  1348
                polled = (events != 0);
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1349
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1350
                endRead(blocking, polled);
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1351
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1352
            return polled;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1353
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1354
            readLock.unlock();
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1355
        }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1356
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12890
diff changeset
  1357
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1361
    public int translateInterestOps(int ops) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        if ((ops & SelectionKey.OP_READ) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1364
            newOps |= Net.POLLIN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        if ((ops & SelectionKey.OP_WRITE) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1366
            newOps |= Net.POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        if ((ops & SelectionKey.OP_CONNECT) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1368
            newOps |= Net.POLLIN;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1369
        return newOps;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 12440
diff changeset
  1385
    private static native void disconnect0(FileDescriptor fd, boolean isIPv6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    private native int receive0(FileDescriptor fd, long address, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                                boolean connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
  1392
    private native int send0(boolean preferIPv6, FileDescriptor fd, long address,
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
  1393
                             int len, InetAddress addr, int port)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 18212
diff changeset
  1397
        IOUtil.load();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
}