src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java
author alanb
Sat, 30 Nov 2019 16:21:19 +0000
changeset 59329 289000934908
parent 59146 455612b3161a
permissions -rw-r--r--
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation Reviewed-by: dfuchs, chegar
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;
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
    30
import java.io.UncheckedIOException;
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
    31
import java.lang.invoke.MethodHandles;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
    32
import java.lang.invoke.VarHandle;
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
    33
import java.lang.ref.Cleaner.Cleanable;
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
    34
import java.lang.reflect.Method;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    35
import java.net.DatagramSocket;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    36
import java.net.Inet4Address;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    37
import java.net.Inet6Address;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    38
import java.net.InetAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    39
import java.net.InetSocketAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    40
import java.net.NetworkInterface;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    41
import java.net.PortUnreachableException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    42
import java.net.ProtocolFamily;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    43
import java.net.SocketAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    44
import java.net.SocketOption;
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
    45
import java.net.SocketTimeoutException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    46
import java.net.StandardProtocolFamily;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    47
import java.net.StandardSocketOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.nio.ByteBuffer;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    49
import java.nio.channels.AlreadyBoundException;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    50
import java.nio.channels.AlreadyConnectedException;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    51
import java.nio.channels.AsynchronousCloseException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    52
import java.nio.channels.ClosedChannelException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    53
import java.nio.channels.DatagramChannel;
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
    54
import java.nio.channels.IllegalBlockingModeException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    55
import java.nio.channels.MembershipKey;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    56
import java.nio.channels.NotYetConnectedException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    57
import java.nio.channels.SelectionKey;
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
    58
import java.nio.channels.spi.AbstractSelectableChannel;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    59
import java.nio.channels.spi.SelectorProvider;
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
    60
import java.security.AccessController;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
    61
import java.security.PrivilegedExceptionAction;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    62
import java.util.Collections;
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
    63
import java.util.HashMap;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    64
import java.util.HashSet;
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
    65
import java.util.Map;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    66
import java.util.Objects;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    67
import java.util.Set;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    68
import java.util.concurrent.locks.ReentrantLock;
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
    69
import java.util.function.Consumer;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    70
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
    71
import jdk.internal.ref.CleanerFactory;
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
    72
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
    73
import sun.net.ext.ExtendedSocketOptions;
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
    74
import sun.net.util.IPAddressUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * An implementation of DatagramChannels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
class DatagramChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    extends DatagramChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Used to make native read and write calls
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
    85
    private static final NativeDispatcher nd = new DatagramDispatcher();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    87
    // The protocol family of the socket
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    88
    private final ProtocolFamily family;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    89
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // Our file descriptor
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    91
    private final FileDescriptor fd;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    92
    private final int fdVal;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    93
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
    94
    // Native buffer for socket address used by receive0, protected by readLock
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
    95
    private final NativeSocketAddress socketAddress;
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
    96
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
    97
    // Cleaner to close file descriptor and free native socket address
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
    98
    private final Cleanable cleaner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // 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
   101
    private final ReentrantLock readLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // 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
   104
    private final ReentrantLock writeLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // 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
   108
    private final Object stateLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // State (does not necessarily increase monotonically)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   113
    private static final int ST_UNCONNECTED = 0;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   114
    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
   115
    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
   116
    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
   117
    private int state;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   119
    // 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
   120
    private long readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   121
    private long writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   122
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
   123
    // Local and remote (connected) address
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   124
    private InetSocketAddress localAddress;
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   125
    private InetSocketAddress remoteAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
   127
    // Local address prior to connecting
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
   128
    private InetSocketAddress initialLocalAddress;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
   129
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   130
    // Socket adaptor, created lazily
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   131
    private static final VarHandle SOCKET;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   132
    static {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   133
        try {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   134
            MethodHandles.Lookup l = MethodHandles.lookup();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   135
            SOCKET = l.findVarHandle(DatagramChannelImpl.class, "socket", DatagramSocket.class);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   136
        } catch (Exception e) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   137
            throw new InternalError(e);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   138
        }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   139
    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   140
    private volatile DatagramSocket socket;
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
    // Multicast support
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   143
    private MembershipRegistry registry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   145
    // set true when socket is bound and SO_REUSEADDRESS is emulated
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   146
    private boolean reuseAddressEmulated;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   147
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   148
    // 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
   149
    private boolean isReuseAddress;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   150
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public DatagramChannelImpl(SelectorProvider sp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        super(sp);
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   157
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   158
        this.socketAddress = new NativeSocketAddress();
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   159
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   160
        ResourceManager.beforeUdpCreate();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   161
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   162
            this.family = Net.isIPv6Available()
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   163
                    ? StandardProtocolFamily.INET6
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   164
                    : StandardProtocolFamily.INET;
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   165
            this.fd = Net.socket(family, false);
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   166
            this.fdVal = IOUtil.fdVal(fd);
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   167
        } catch (IOException ioe) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   168
            ResourceManager.afterUdpClose();
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   169
            socketAddress.free();
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   170
            throw ioe;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   171
        }
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   172
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   173
        Runnable releaser = releaserFor(fd, socketAddress);
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   174
        this.cleaner = CleanerFactory.cleaner().register(this, releaser);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   175
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   176
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   177
    public DatagramChannelImpl(SelectorProvider sp, ProtocolFamily family)
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   178
        throws IOException
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 8788
diff changeset
   179
    {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   180
        super(sp);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   181
        Objects.requireNonNull(family, "'family' is null");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   182
        if ((family != StandardProtocolFamily.INET) &&
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   183
            (family != StandardProtocolFamily.INET6)) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   184
            throw new UnsupportedOperationException("Protocol family not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   185
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   186
        if (family == StandardProtocolFamily.INET6) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   187
            if (!Net.isIPv6Available()) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   188
                throw new UnsupportedOperationException("IPv6 not available");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   189
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   190
        }
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   191
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   192
        this.socketAddress = new NativeSocketAddress();
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   193
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   194
        ResourceManager.beforeUdpCreate();
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   195
        try {
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   196
            this.family = family;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   197
            this.fd = Net.socket(family, false);
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   198
            this.fdVal = IOUtil.fdVal(fd);
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   199
        } catch (IOException ioe) {
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   200
            ResourceManager.afterUdpClose();
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   201
            socketAddress.free();
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   202
            throw ioe;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   203
        }
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   204
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   205
        Runnable releaser = releaserFor(fd, socketAddress);
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   206
        this.cleaner = CleanerFactory.cleaner().register(this, releaser);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        super(sp);
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   213
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   214
        try {
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   215
            this.socketAddress = new NativeSocketAddress();
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   216
        } catch (OutOfMemoryError e) {
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   217
            nd.close(fd);
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   218
            throw e;
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   219
        }
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   220
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   221
        // increment UDP count to match decrement when closing
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   222
        ResourceManager.beforeUdpCreate();
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 48750
diff changeset
   223
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   224
        this.family = Net.isIPv6Available()
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   225
                ? StandardProtocolFamily.INET6
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   226
                : StandardProtocolFamily.INET;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        this.fdVal = IOUtil.fdVal(fd);
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   229
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   230
        Runnable releaser = releaserFor(fd, socketAddress);
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   231
        this.cleaner = CleanerFactory.cleaner().register(this, releaser);
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   232
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   233
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   234
            this.localAddress = Net.localAddress(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   235
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   238
    // @throws ClosedChannelException if channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   239
    private void ensureOpen() throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   240
        if (!isOpen())
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   241
            throw new ClosedChannelException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   242
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   243
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   244
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public DatagramSocket socket() {
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   246
        DatagramSocket socket = this.socket;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   247
        if (socket == null) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   248
            socket = DatagramSocketAdaptor.create(this);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   249
            if (!SOCKET.compareAndSet(this, null, socket)) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   250
                socket = this.socket;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   251
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   253
        return socket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   256
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   257
    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
   258
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   259
            ensureOpen();
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   260
            // Perform security check before returning address
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   261
            return Net.getRevealedLocalAddress(localAddress);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   262
        }
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   265
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   266
    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
   267
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   268
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   269
            return remoteAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   270
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   271
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   272
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   273
    /**
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   274
     * Returns the protocol family to specify to set/getSocketOption for the
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   275
     * given socket option.
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   276
     */
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   277
    private ProtocolFamily familyFor(SocketOption<?> name) {
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   278
        assert Thread.holdsLock(stateLock);
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   279
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   280
        // unspecified (most options)
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   281
        if (SocketOptionRegistry.findOption(name, Net.UNSPEC) != null)
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   282
            return Net.UNSPEC;
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   283
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   284
        // IPv4 socket
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   285
        if (family == StandardProtocolFamily.INET)
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   286
            return StandardProtocolFamily.INET;
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   287
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   288
        // IPv6 socket that is unbound
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   289
        if (localAddress == null)
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   290
            return StandardProtocolFamily.INET6;
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   291
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   292
        // IPv6 socket bound to wildcard or IPv6 address
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   293
        InetAddress address = localAddress.getAddress();
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   294
        if (address.isAnyLocalAddress() || (address instanceof Inet6Address))
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   295
            return StandardProtocolFamily.INET6;
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   296
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   297
        // IPv6 socket bound to IPv4 address
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   298
        if (Net.canUseIPv6OptionsWithIPv4LocalAddress()) {
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   299
            // IPV6_XXX options can be used
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   300
            return StandardProtocolFamily.INET6;
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   301
        } else {
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   302
            // IPV6_XXX options cannot be used
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   303
            return StandardProtocolFamily.INET;
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   304
        }
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   305
    }
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   306
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   307
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   308
    public <T> DatagramChannel setOption(SocketOption<T> name, T value)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   309
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   310
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   311
        Objects.requireNonNull(name);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   312
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   313
            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
   314
        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
   315
            throw new IllegalArgumentException("Invalid value '" + value + "'");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   316
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   317
        synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   318
            ensureOpen();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   319
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   320
            ProtocolFamily family = familyFor(name);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   321
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   322
            if (name == StandardSocketOptions.IP_MULTICAST_IF) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   323
                NetworkInterface interf = (NetworkInterface)value;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   324
                if (family == StandardProtocolFamily.INET6) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   325
                    int index = interf.getIndex();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   326
                    if (index == -1)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   327
                        throw new IOException("Network interface cannot be identified");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   328
                    Net.setInterface6(fd, index);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   329
                } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   330
                    // need IPv4 address to identify interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   331
                    Inet4Address target = Net.anyInet4Address(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   332
                    if (target == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   333
                        throw new IOException("Network interface not configured for IPv4");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   334
                    int targetAddress = Net.inet4AsInt(target);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   335
                    Net.setInterface4(fd, targetAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   336
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   337
                return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   338
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   339
            if (name == StandardSocketOptions.SO_REUSEADDR
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   340
                && Net.useExclusiveBind() && localAddress != null) {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   341
                reuseAddressEmulated = true;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   342
                this.isReuseAddress = (Boolean)value;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   343
            }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   344
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   345
            // remaining options don't need any special handling
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   346
            Net.setSocketOption(fd, family, name, value);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   347
            return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   348
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   349
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   350
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   351
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   352
    @SuppressWarnings("unchecked")
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   353
    public <T> T getOption(SocketOption<T> name)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   354
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   355
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   356
        Objects.requireNonNull(name);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   357
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   358
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   359
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   360
        synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   361
            ensureOpen();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   362
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   363
            ProtocolFamily family = familyFor(name);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   364
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   365
            if (name == StandardSocketOptions.IP_MULTICAST_IF) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   366
                if (family == StandardProtocolFamily.INET) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   367
                    int address = Net.getInterface4(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   368
                    if (address == 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   369
                        return null;    // default interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   370
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   371
                    InetAddress ia = Net.inet4FromInt(address);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   372
                    NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   373
                    if (ni == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   374
                        throw new IOException("Unable to map address to interface");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   375
                    return (T) ni;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   376
                } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   377
                    int index = Net.getInterface6(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   378
                    if (index == 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   379
                        return null;    // default interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   380
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   381
                    NetworkInterface ni = NetworkInterface.getByIndex(index);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   382
                    if (ni == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   383
                        throw new IOException("Unable to map index to interface");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   384
                    return (T) ni;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   385
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   386
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   387
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   388
            if (name == StandardSocketOptions.SO_REUSEADDR && reuseAddressEmulated) {
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   389
                return (T) Boolean.valueOf(isReuseAddress);
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   390
            }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   391
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   392
            // no special handling
59000
612c58965775 8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents: 58899
diff changeset
   393
            return (T) Net.getSocketOption(fd, family, name);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   394
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   395
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   396
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   397
    private static class DefaultOptionsHolder {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   398
        static final Set<SocketOption<?>> defaultOptions = defaultOptions();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   399
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   400
        private static Set<SocketOption<?>> defaultOptions() {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   401
            HashSet<SocketOption<?>> set = new HashSet<>();
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   402
            set.add(StandardSocketOptions.SO_SNDBUF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   403
            set.add(StandardSocketOptions.SO_RCVBUF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   404
            set.add(StandardSocketOptions.SO_REUSEADDR);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   405
            if (Net.isReusePortAvailable()) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   406
                set.add(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   407
            }
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   408
            set.add(StandardSocketOptions.SO_BROADCAST);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   409
            set.add(StandardSocketOptions.IP_TOS);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   410
            set.add(StandardSocketOptions.IP_MULTICAST_IF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   411
            set.add(StandardSocketOptions.IP_MULTICAST_TTL);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9275
diff changeset
   412
            set.add(StandardSocketOptions.IP_MULTICAST_LOOP);
53419
eac105e3ec13 8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents: 50303
diff changeset
   413
            set.addAll(ExtendedSocketOptions.datagramSocketOptions());
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   414
            return Collections.unmodifiableSet(set);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   415
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   416
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   417
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   418
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   419
    public final Set<SocketOption<?>> supportedOptions() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   420
        return DefaultOptionsHolder.defaultOptions;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   421
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   422
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   423
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   424
     * 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
   425
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   426
     * @param blocking true if configured blocking
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   427
     * @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
   428
     * @return remote address if connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   429
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   430
     * @throws NotYetConnectedException if mustBeConnected and not connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   431
     * @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
   432
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   433
    private SocketAddress beginRead(boolean blocking, boolean mustBeConnected)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   434
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   435
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   436
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   437
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   438
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   439
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   440
        SocketAddress remote;
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   441
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   442
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   443
            remote = remoteAddress;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   444
            if ((remote == null) && mustBeConnected)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   445
                throw new NotYetConnectedException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   446
            if (localAddress == null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   447
                bindInternal(null);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   448
            if (blocking)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   449
                readerThread = NativeThread.current();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   450
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   451
        return remote;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   452
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   453
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   454
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   455
     * 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
   456
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   457
     * @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
   458
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   459
    private void endRead(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   460
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   461
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   462
        if (blocking) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   463
            synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   464
                readerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   465
                if (state == ST_CLOSING) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   466
                    tryFinishClose();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   467
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   468
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   469
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   470
            end(completed);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   471
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   474
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public SocketAddress receive(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (dst.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            throw new IllegalArgumentException("Read-only buffer");
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   478
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   479
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   480
            boolean blocking = isBlocking();
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   481
            SocketAddress sender = null;
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
                SocketAddress remote = beginRead(blocking, false);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   484
                boolean connected = (remote != null);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   485
                SecurityManager sm = System.getSecurityManager();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   486
                if (connected || (sm == null)) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   487
                    // connected or no security manager
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   488
                    int n = receive(dst, connected);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   489
                    if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   490
                        while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   491
                            park(Net.POLLIN);
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   492
                            n = receive(dst, connected);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   493
                        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   494
                    }
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   495
                    if (n >= 0) {
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   496
                        // sender address is in socket address buffer
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   497
                        sender = socketAddress.toInetSocketAddress();
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   498
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                } else {
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   500
                    // security manager and unconnected
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   501
                    sender = untrustedReceive(dst);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                return sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            } finally {
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   505
                endRead(blocking, (sender != null));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   507
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   508
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   512
    /**
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   513
     * Receives a datagram into an untrusted buffer. When there is a security
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   514
     * manager set, and the socket is not connected, datagrams have to be received
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   515
     * into a buffer that is not accessible to the user. The datagram is copied
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   516
     * into the user's buffer when the sender address is accepted by the security
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   517
     * manager.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   518
     */
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   519
    private SocketAddress untrustedReceive(ByteBuffer dst) throws IOException {
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   520
        SecurityManager sm = System.getSecurityManager();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   521
        assert readLock.isHeldByCurrentThread()
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   522
                && sm != null && remoteAddress == null;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   523
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   524
        ByteBuffer bb = Util.getTemporaryDirectBuffer(dst.remaining());
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   525
        try {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   526
            boolean blocking = isBlocking();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   527
            for (;;) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   528
                int n = receive(bb, false);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   529
                if (blocking) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   530
                    while (IOStatus.okayToRetry(n) && isOpen()) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   531
                        park(Net.POLLIN);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   532
                        n = receive(bb, false);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   533
                    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   534
                }
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   535
                if (n >= 0) {
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   536
                    // sender address is in socket address buffer
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   537
                    InetSocketAddress isa = socketAddress.toInetSocketAddress();
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   538
                    try {
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   539
                        sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort());
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   540
                        bb.flip();
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   541
                        dst.put(bb);
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   542
                        return isa;
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   543
                    } catch (SecurityException se) {
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   544
                        // ignore datagram
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   545
                        bb.clear();
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   546
                    }
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   547
                } else {
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   548
                    return null;
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   549
                }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   550
            }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   551
        } finally {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   552
            Util.releaseTemporaryDirectBuffer(bb);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   553
        }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   554
    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   555
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   556
    /**
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   557
     * Receives a datagram into the given buffer.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   558
     *
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   559
     * @apiNote This method is for use by the socket adaptor. The buffer is
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   560
     * assumed to be trusted, meaning it is not accessible to user code.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   561
     *
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   562
     * @throws IllegalBlockingModeException if the channel is non-blocking
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   563
     * @throws SocketTimeoutException if the timeout elapses
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   564
     */
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   565
    SocketAddress blockingReceive(ByteBuffer dst, long nanos) throws IOException {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   566
        readLock.lock();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   567
        try {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   568
            ensureOpen();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   569
            if (!isBlocking())
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   570
                throw new IllegalBlockingModeException();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   571
            SecurityManager sm = System.getSecurityManager();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   572
            boolean connected = isConnected();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   573
            SocketAddress sender;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   574
            do {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   575
                if (nanos > 0) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   576
                    sender = trustedBlockingReceive(dst, nanos);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   577
                } else {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   578
                    sender = trustedBlockingReceive(dst);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   579
                }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   580
                // check sender when security manager set and not connected
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   581
                if (sm != null && !connected) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   582
                    InetSocketAddress isa = (InetSocketAddress) sender;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   583
                    try {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   584
                        sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort());
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   585
                    } catch (SecurityException e) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   586
                        sender = null;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   587
                    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   588
                }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   589
            } while (sender == null);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   590
            return sender;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   591
        } finally {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   592
            readLock.unlock();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   593
        }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   594
    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   595
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   596
    /**
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   597
     * Receives a datagram into given buffer. This method is used to support
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   598
     * the socket adaptor. The buffer is assumed to be trusted.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   599
     * @throws SocketTimeoutException if the timeout elapses
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   600
     */
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   601
    private SocketAddress trustedBlockingReceive(ByteBuffer dst)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    {
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   604
        assert readLock.isHeldByCurrentThread() && isBlocking();
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   605
        SocketAddress sender = null;
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   606
        try {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   607
            SocketAddress remote = beginRead(true, false);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   608
            boolean connected = (remote != null);
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   609
            int n = receive(dst, connected);
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   610
            while (IOStatus.okayToRetry(n) && isOpen()) {
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   611
                park(Net.POLLIN);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   612
                n = receive(dst, connected);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   613
            }
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   614
            if (n >= 0) {
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   615
                // sender address is in socket address buffer
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   616
                sender = socketAddress.toInetSocketAddress();
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   617
            }
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   618
            return sender;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   619
        } finally {
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   620
            endRead(true, (sender != null));
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   621
        }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   622
    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   623
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   624
    /**
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   625
     * Receives a datagram into given buffer with a timeout. This method is
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   626
     * used to support the socket adaptor. The buffer is assumed to be trusted.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   627
     * @throws SocketTimeoutException if the timeout elapses
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   628
     */
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   629
    private SocketAddress trustedBlockingReceive(ByteBuffer dst, long nanos)
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   630
        throws IOException
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   631
    {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   632
        assert readLock.isHeldByCurrentThread() && isBlocking();
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   633
        SocketAddress sender = null;
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   634
        try {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   635
            SocketAddress remote = beginRead(true, false);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   636
            boolean connected = (remote != null);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   637
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   638
            // change socket to non-blocking
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   639
            lockedConfigureBlocking(false);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   640
            try {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   641
                long startNanos = System.nanoTime();
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   642
                int n = receive(dst, connected);
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   643
                while (n == IOStatus.UNAVAILABLE && isOpen()) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   644
                    long remainingNanos = nanos - (System.nanoTime() - startNanos);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   645
                    if (remainingNanos <= 0) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   646
                        throw new SocketTimeoutException("Receive timed out");
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   647
                    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   648
                    park(Net.POLLIN, remainingNanos);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   649
                    n = receive(dst, connected);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   650
                }
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   651
                if (n >= 0) {
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   652
                    // sender address is in socket address buffer
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   653
                    sender = socketAddress.toInetSocketAddress();
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   654
                }
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   655
                return sender;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   656
            } finally {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   657
                // restore socket to blocking mode (if channel is open)
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   658
                tryLockedConfigureBlocking(true);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   659
            }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   660
        } finally {
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   661
            endRead(true, (sender != null));
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   662
        }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   663
    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   664
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   665
    private int receive(ByteBuffer dst, boolean connected) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        int pos = dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        int lim = dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        if (dst instanceof DirectBuffer && rem > 0)
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   671
            return receiveIntoNativeBuffer(dst, rem, pos, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        // Substitute a native buffer. If the supplied buffer is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        // we must instead use a nonempty buffer, otherwise the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        // will not block waiting for a datagram on some platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        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
   677
        ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        try {
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   679
            int n = receiveIntoNativeBuffer(bb, newSize, 0, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            if (n > 0 && rem > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                dst.put(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   689
    private int receiveIntoNativeBuffer(ByteBuffer bb, int rem, int pos,
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   690
                                        boolean connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    {
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   693
        int n = receive0(fd,
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   694
                         ((DirectBuffer)bb).address() + pos, rem,
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   695
                         socketAddress.address(),
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
   696
                         connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            bb.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   702
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    public int send(ByteBuffer src, SocketAddress target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   706
        Objects.requireNonNull(src);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   707
        InetSocketAddress isa = Net.checkAddress(target, family);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   709
        writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   710
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   711
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   712
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   713
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   714
                SocketAddress remote = beginWrite(blocking, false);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   715
                if (remote != null) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   716
                    // connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   717
                    if (!target.equals(remote)) {
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 49001
diff changeset
   718
                        throw new AlreadyConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   719
                    }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   720
                    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
   721
                    if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   722
                        while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   723
                            park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   724
                            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
   725
                        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   726
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   727
                } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   728
                    // not connected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    SecurityManager sm = System.getSecurityManager();
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   730
                    InetAddress ia = isa.getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        if (ia.isMulticastAddress()) {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   733
                            sm.checkMulticast(ia);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                        } else {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   735
                            sm.checkConnect(ia.getHostAddress(), isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    }
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   738
                    if (ia.isLinkLocalAddress())
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   739
                        isa = IPAddressUtil.toScopedAddress(isa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   740
                    n = send(fd, src, isa);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   741
                    if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   742
                        while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   743
                            park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   744
                            n = send(fd, src, isa);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   745
                        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   746
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   749
                endWrite(blocking, n > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   752
            return IOStatus.normalize(n);
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   753
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   754
            writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   758
    /**
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   759
     * Sends a datagram from the bytes in given buffer.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   760
     *
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   761
     * @apiNote This method is for use by the socket adaptor.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   762
     *
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   763
     * @throws IllegalBlockingModeException if the channel is non-blocking
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   764
     */
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   765
    void blockingSend(ByteBuffer src, SocketAddress target) throws IOException {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   766
        writeLock.lock();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   767
        try {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   768
            ensureOpen();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   769
            if (!isBlocking())
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   770
                throw new IllegalBlockingModeException();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   771
            send(src, target);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   772
        } finally {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   773
            writeLock.unlock();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   774
        }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   775
    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   776
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   777
    private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (src instanceof DirectBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            return sendFromNativeBuffer(fd, src, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        // Substitute a native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        int pos = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        int lim = src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
9237
cbb5753e87e7 7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents: 8817
diff changeset
   789
        ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            bb.put(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            // Do not update src until we see how many bytes were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            src.position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            int n = sendFromNativeBuffer(fd, bb, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                // now update src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                src.position(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   808
                                     InetSocketAddress target)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   816
        boolean preferIPv6 = (family != StandardProtocolFamily.INET);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   817
        int written;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   818
        try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   819
            written = send0(preferIPv6, fd, ((DirectBuffer)bb).address() + pos,
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 14025
diff changeset
   820
                            rem, target.getAddress(), target.getPort());
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   821
        } catch (PortUnreachableException pue) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   822
            if (isConnected())
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   823
                throw pue;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   824
            written = rem;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9679
diff changeset
   825
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (written > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            bb.position(pos + written);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   831
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    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
   833
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   834
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   835
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   836
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   837
            boolean blocking = isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   840
                beginRead(blocking, true);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   841
                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
   842
                if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   843
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   844
                        park(Net.POLLIN);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   845
                        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
   846
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   847
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   849
                endRead(blocking, n > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   852
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   853
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   854
            readLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   855
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   856
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   857
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   858
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   859
    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
   860
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   861
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   862
        Objects.checkFromIndexSize(offset, length, dsts.length);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   863
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   864
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   865
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   866
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   867
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   868
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   869
                beginRead(blocking, true);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   870
                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
   871
                if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   872
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   873
                        park(Net.POLLIN);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   874
                        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
   875
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   876
                }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   877
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   878
                endRead(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   879
                assert IOStatus.check(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   880
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   881
            return IOStatus.normalize(n);
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   882
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   883
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   887
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   888
     * 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
   889
     * @param blocking true if configured blocking
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   890
     * @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
   891
     * @return remote address if connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   892
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   893
     * @throws NotYetConnectedException if mustBeConnected and not connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   894
     * @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
   895
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   896
    private SocketAddress beginWrite(boolean blocking, boolean mustBeConnected)
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   897
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   898
    {
49001
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
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   901
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   902
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   903
        SocketAddress remote;
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   904
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   905
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   906
            remote = remoteAddress;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   907
            if ((remote == null) && mustBeConnected)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   908
                throw new NotYetConnectedException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   909
            if (localAddress == null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   910
                bindInternal(null);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   911
            if (blocking)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   912
                writerThread = NativeThread.current();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   913
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   914
        return remote;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   915
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   916
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   917
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   918
     * 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
   919
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   920
     * @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
   921
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   922
    private void endWrite(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   923
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   924
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   925
        if (blocking) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   926
            synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   927
                writerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   928
                if (state == ST_CLOSING) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   929
                    tryFinishClose();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   930
                }
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
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   933
            end(completed);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   934
        }
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
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   937
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   938
    public int write(ByteBuffer buf) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   939
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   940
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   941
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   942
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   943
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   944
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   945
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   946
                beginWrite(blocking, true);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   947
                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
   948
                if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   949
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   950
                        park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   951
                        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
   952
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   953
                }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   954
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   955
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   956
                assert IOStatus.check(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   957
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   958
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   959
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   960
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   961
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   962
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   963
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   964
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   965
    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
   966
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   967
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   968
        Objects.checkFromIndexSize(offset, length, srcs.length);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   969
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   970
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   971
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   972
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   973
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   974
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   975
                beginWrite(blocking, true);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   976
                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
   977
                if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   978
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   979
                        park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   980
                        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
   981
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 53419
diff changeset
   982
                }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   983
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   984
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   985
                assert IOStatus.check(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   986
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   987
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   988
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   989
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   990
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   991
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   992
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   993
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   994
    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
   995
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   996
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   997
            writeLock.lock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            try {
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
   999
                lockedConfigureBlocking(block);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1001
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1003
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1004
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1008
    /**
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1009
     * Adjusts the blocking mode. readLock or writeLock must already be held.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1010
     */
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1011
    private void lockedConfigureBlocking(boolean block) throws IOException {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1012
        assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1013
        synchronized (stateLock) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1014
            ensureOpen();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1015
            IOUtil.configureBlocking(fd, block);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1016
        }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1017
    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1018
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1019
    /**
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1020
     * Adjusts the blocking mode if the channel is open. readLock or writeLock
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1021
     * must already be held.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1022
     *
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1023
     * @return {@code true} if the blocking mode was adjusted, {@code false} if
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1024
     *         the blocking mode was not adjusted because the channel is closed
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1025
     */
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1026
    private boolean tryLockedConfigureBlocking(boolean block) throws IOException {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1027
        assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1028
        synchronized (stateLock) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1029
            if (isOpen()) {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1030
                IOUtil.configureBlocking(fd, block);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1031
                return true;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1032
            } else {
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1033
                return false;
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1034
            }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1035
        }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1036
    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1037
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1038
    InetSocketAddress localAddress() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1039
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            return localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1044
    InetSocketAddress remoteAddress() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1045
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1050
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1051
    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
  1052
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1053
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1054
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1055
            try {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1056
                synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1058
                    if (localAddress != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                        throw new AlreadyBoundException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1060
                    bindInternal(local);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1062
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1063
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1065
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1066
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1068
        return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1071
    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
  1072
        assert Thread.holdsLock(stateLock )&& (localAddress == null);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1073
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1074
        InetSocketAddress isa;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1075
        if (local == null) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1076
            // only Inet4Address allowed with IPv4 socket
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1077
            if (family == StandardProtocolFamily.INET) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1078
                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
  1079
            } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1080
                isa = new InetSocketAddress(0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1081
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1082
        } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1083
            isa = Net.checkAddress(local, family);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1084
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1085
        SecurityManager sm = System.getSecurityManager();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1086
        if (sm != null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1087
            sm.checkListen(isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1088
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1089
        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
  1090
        localAddress = Net.localAddress(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1091
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1092
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1093
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    public boolean isConnected() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1095
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            return (state == ST_CONNECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1100
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    public DatagramChannel connect(SocketAddress sa) throws IOException {
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1102
        return connect(sa, true);
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1103
    }
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1104
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1105
    /**
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1106
     * Connects the channel's socket.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1107
     *
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1108
     * @param sa the remote address to which this channel is to be connected
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1109
     * @param check true to check if the channel is already connected.
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1110
     */
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1111
    DatagramChannel connect(SocketAddress sa, boolean check) throws IOException {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1112
        InetSocketAddress isa = Net.checkAddress(sa, family);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1113
        SecurityManager sm = System.getSecurityManager();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1114
        if (sm != null) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1115
            InetAddress ia = isa.getAddress();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1116
            if (ia.isMulticastAddress()) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1117
                sm.checkMulticast(ia);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1118
            } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1119
                sm.checkConnect(ia.getHostAddress(), isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1120
                sm.checkAccept(ia.getHostAddress(), isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1121
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1122
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1123
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1124
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1125
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1126
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1127
            try {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1128
                synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1129
                    ensureOpen();
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1130
                    if (check && state == ST_CONNECTED)
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1131
                        throw new AlreadyConnectedException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1132
58518
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
  1133
                    // 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
  1134
                    if (localAddress == null) {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
  1135
                        bindInternal(null);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
  1136
                    }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
  1137
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1138
                    // capture local address before connect
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1139
                    initialLocalAddress = localAddress;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1140
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1141
                    int n = Net.connect(family,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1142
                                        fd,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                                        isa.getAddress(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1144
                                        isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                    if (n <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                        throw new Error();      // Can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1148
                    // connected
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1149
                    remoteAddress = isa;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1150
                    state = ST_CONNECTED;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1151
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1152
                    // refresh local address
7045
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents: 6301
diff changeset
  1153
                    localAddress = Net.localAddress(fd);
27078
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
  1154
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
  1155
                    // flush any packets already received.
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1156
                    boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1157
                    if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1158
                        IOUtil.configureBlocking(fd, false);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1159
                    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1160
                    try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1161
                        ByteBuffer buf = ByteBuffer.allocate(100);
58899
5573a7098439 8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents: 58808
diff changeset
  1162
                        while (receive(buf, false) >= 0) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1163
                            buf.clear();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1164
                        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1165
                    } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1166
                        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1167
                            IOUtil.configureBlocking(fd, true);
27078
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
  1168
                        }
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
  1169
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1171
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1172
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1174
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1175
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1180
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    public DatagramChannel disconnect() throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1182
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1183
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1184
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1185
            try {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1186
                synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1187
                    if (!isOpen() || (state != ST_CONNECTED))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                        return this;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1189
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1190
                    // disconnect socket
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 12440
diff changeset
  1191
                    boolean isIPv6 = (family == StandardProtocolFamily.INET6);
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 12440
diff changeset
  1192
                    disconnect0(fd, isIPv6);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1193
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1194
                    // no longer connected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    remoteAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    state = ST_UNCONNECTED;
7045
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents: 6301
diff changeset
  1197
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1198
                    // refresh localAddress, should be same as it was prior to connect
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1199
                    localAddress = Net.localAddress(fd);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1200
                    try {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1201
                        if (!localAddress.equals(initialLocalAddress)) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1202
                            // Workaround connect(2) issues on Linux and macOS
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1203
                            repairSocket(initialLocalAddress);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1204
                            assert (localAddress != null)
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1205
                                    && localAddress.equals(Net.localAddress(fd))
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1206
                                    && localAddress.equals(initialLocalAddress);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1207
                        }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1208
                    } finally {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1209
                        initialLocalAddress = null;
58518
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents: 55375
diff changeset
  1210
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1212
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1213
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1215
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1216
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1221
    /**
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1222
     * "Repair" the channel's socket after a disconnect that didn't restore the
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1223
     * local address.
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1224
     *
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1225
     * On Linux, connect(2) dissolves the association but changes the local port
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1226
     * to 0 when it was initially bound to an ephemeral port. The workaround here
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1227
     * is to rebind to the original port.
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1228
     *
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1229
     * On macOS, connect(2) dissolves the association but rebinds the socket to
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1230
     * the wildcard address when it was initially bound to a specific address.
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1231
     * The workaround here is to re-create the socket.
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1232
     */
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1233
    private void repairSocket(InetSocketAddress target)
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1234
        throws IOException
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1235
    {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1236
        assert Thread.holdsLock(stateLock);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1237
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1238
        // Linux: try to bind the socket to the original address/port
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1239
        if (localAddress.getPort() == 0) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1240
            assert localAddress.getAddress().equals(target.getAddress());
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1241
            Net.bind(family, fd, target.getAddress(), target.getPort());
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1242
            localAddress = Net.localAddress(fd);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1243
            return;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1244
        }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1245
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1246
        // capture the value of all existing socket options
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1247
        Map<SocketOption<?>, Object> map = new HashMap<>();
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1248
        for (SocketOption<?> option : supportedOptions()) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1249
            Object value = getOption(option);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1250
            if (value != null) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1251
                map.put(option, value);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1252
            }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1253
        }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1254
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1255
        // macOS: re-create the socket.
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1256
        FileDescriptor newfd = Net.socket(family, false);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1257
        try {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1258
            // copy the socket options that are protocol family agnostic
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1259
            for (Map.Entry<SocketOption<?>, Object> e : map.entrySet()) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1260
                SocketOption<?> option = e.getKey();
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1261
                if (SocketOptionRegistry.findOption(option, Net.UNSPEC) != null) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1262
                    Object value = e.getValue();
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1263
                    try {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1264
                        Net.setSocketOption(newfd, Net.UNSPEC, option, value);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1265
                    } catch (IOException ignore) { }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1266
                }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1267
            }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1268
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1269
            // copy the blocking mode
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1270
            if (!isBlocking()) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1271
                IOUtil.configureBlocking(newfd, false);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1272
            }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1273
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1274
            // dup this channel's socket to the new socket. If this succeeds then
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1275
            // fd will reference the new socket. If it fails then it will still
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1276
            // reference the old socket.
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1277
            nd.dup(newfd, fd);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1278
        } finally {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1279
            // release the file descriptor
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1280
            nd.close(newfd);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1281
        }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1282
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1283
        // bind to the original local address
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1284
        try {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1285
            Net.bind(family, fd, target.getAddress(), target.getPort());
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1286
        } catch (IOException ioe) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1287
            // bind failed, socket is left unbound
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1288
            localAddress = null;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1289
            throw ioe;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1290
        }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1291
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1292
        // restore local address
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1293
        localAddress = Net.localAddress(fd);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1294
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1295
        // restore all socket options (including those set in first pass)
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1296
        for (Map.Entry<SocketOption<?>, Object> e : map.entrySet()) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1297
            @SuppressWarnings("unchecked")
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1298
            SocketOption<Object> option = (SocketOption<Object>) e.getKey();
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1299
            Object value = e.getValue();
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1300
            try {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1301
                setOption(option, value);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1302
            } catch (IOException ignore) { }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1303
        }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1304
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1305
        // restore multicast group membership
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1306
        MembershipRegistry registry = this.registry;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1307
        if (registry != null) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1308
            registry.forEach(k -> {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1309
                if (k instanceof MembershipKeyImpl.Type6) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1310
                    MembershipKeyImpl.Type6 key6 = (MembershipKeyImpl.Type6) k;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1311
                    Net.join6(fd, key6.groupAddress(), key6.index(), key6.source());
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1312
                } else {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1313
                    MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4) k;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1314
                    Net.join4(fd, key4.groupAddress(), key4.interfaceAddress(), key4.source());
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1315
                }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1316
            });
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1317
        }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1318
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1319
        // reset registration in all Selectors that this channel is registered with
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1320
        AbstractSelectableChannels.forEach(this, SelectionKeyImpl::reset);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1321
    }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1322
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1323
    /**
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1324
     * Defines static methods to access AbstractSelectableChannel non-public members.
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1325
     */
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1326
    private static class AbstractSelectableChannels {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1327
        private static final Method FOREACH;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1328
        static {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1329
            try {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1330
                PrivilegedExceptionAction<Method> pae = () -> {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1331
                    Method m = AbstractSelectableChannel.class.getDeclaredMethod("forEach", Consumer.class);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1332
                    m.setAccessible(true);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1333
                    return m;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1334
                };
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1335
                FOREACH = AccessController.doPrivileged(pae);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1336
            } catch (Exception e) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1337
                throw new InternalError(e);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1338
            }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1339
        }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1340
        static void forEach(AbstractSelectableChannel ch, Consumer<SelectionKeyImpl> action) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1341
            try {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1342
                FOREACH.invoke(ch, action);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1343
            } catch (Exception e) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1344
                throw new InternalError(e);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1345
            }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1346
        }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1347
    }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1348
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 59000
diff changeset
  1349
    /**
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1350
     * Joins channel's socket to the given group/interface and
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1351
     * optional source address.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1352
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1353
    private MembershipKey innerJoin(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1354
                                    NetworkInterface interf,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1355
                                    InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1356
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1357
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1358
        if (!group.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1359
            throw new IllegalArgumentException("Group not a multicast address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1360
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1361
        // 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
  1362
        if (group instanceof Inet4Address) {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1363
            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
  1364
                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
  1365
        } else if (group instanceof Inet6Address) {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1366
            if (family != StandardProtocolFamily.INET6)
9237
cbb5753e87e7 7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents: 8817
diff changeset
  1367
                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
  1368
        } else {
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1369
            throw new IllegalArgumentException("Address type not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1370
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1371
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1372
        // check source address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1373
        if (source != null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1374
            if (source.isAnyLocalAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1375
                throw new IllegalArgumentException("Source address is a wildcard address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1376
            if (source.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1377
                throw new IllegalArgumentException("Source address is multicast address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1378
            if (source.getClass() != group.getClass())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1379
                throw new IllegalArgumentException("Source address is different type to group");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1380
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1381
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1382
        SecurityManager sm = System.getSecurityManager();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1383
        if (sm != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1384
            sm.checkMulticast(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1385
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1386
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1387
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1388
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1389
            // 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
  1390
            if (registry == null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1391
                registry = new MembershipRegistry();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1392
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1393
                // return existing membership key
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1394
                MembershipKey key = registry.checkMembership(group, interf, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1395
                if (key != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1396
                    return key;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1397
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1398
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1399
            MembershipKeyImpl key;
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1400
            if ((family == StandardProtocolFamily.INET6) &&
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1401
                ((group instanceof Inet6Address) || Net.canJoin6WithIPv4Group()))
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1402
            {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1403
                int index = interf.getIndex();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1404
                if (index == -1)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1405
                    throw new IOException("Network interface cannot be identified");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1406
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1407
                // need multicast and source address as byte arrays
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1408
                byte[] groupAddress = Net.inet6AsByteArray(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1409
                byte[] sourceAddress = (source == null) ? null :
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1410
                    Net.inet6AsByteArray(source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1411
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1412
                // join the group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1413
                int n = Net.join6(fd, groupAddress, index, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1414
                if (n == IOStatus.UNAVAILABLE)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1415
                    throw new UnsupportedOperationException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1416
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1417
                key = new MembershipKeyImpl.Type6(this, group, interf, source,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1418
                                                  groupAddress, index, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1419
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1420
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1421
                // need IPv4 address to identify interface
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1422
                Inet4Address target = Net.anyInet4Address(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1423
                if (target == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1424
                    throw new IOException("Network interface not configured for IPv4");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1425
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1426
                int groupAddress = Net.inet4AsInt(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1427
                int targetAddress = Net.inet4AsInt(target);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1428
                int sourceAddress = (source == null) ? 0 : Net.inet4AsInt(source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1429
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1430
                // join the group
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1431
                int n = Net.join4(fd, groupAddress, targetAddress, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1432
                if (n == IOStatus.UNAVAILABLE)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1433
                    throw new UnsupportedOperationException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1434
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1435
                key = new MembershipKeyImpl.Type4(this, group, interf, source,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1436
                                                  groupAddress, targetAddress, sourceAddress);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1437
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1438
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1439
            registry.add(key);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1440
            return key;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1441
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1442
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1443
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1444
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1445
    public MembershipKey join(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1446
                              NetworkInterface interf)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1447
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1448
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1449
        return innerJoin(group, interf, null);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1450
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1451
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1452
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1453
    public MembershipKey join(InetAddress group,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1454
                              NetworkInterface interf,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1455
                              InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1456
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1457
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1458
        Objects.requireNonNull(source);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1459
        return innerJoin(group, interf, source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1460
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1461
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1462
    // package-private
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1463
    void drop(MembershipKeyImpl key) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1464
        assert key.channel() == this;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1465
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1466
        synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1467
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1468
                return;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1469
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1470
            try {
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1471
                if (key instanceof MembershipKeyImpl.Type6) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1472
                    MembershipKeyImpl.Type6 key6 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1473
                        (MembershipKeyImpl.Type6)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1474
                    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
  1475
                } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1476
                    MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1477
                    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
  1478
                        key4.source());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1479
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1480
            } catch (IOException ioe) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1481
                // should not happen
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1482
                throw new AssertionError(ioe);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1483
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1484
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1485
            key.invalidate();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1486
            registry.remove(key);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1487
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1488
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1489
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1490
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1491
     * 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
  1492
     * datagrams.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1493
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1494
    void block(MembershipKeyImpl key, InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1495
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1496
    {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1497
        assert key.channel() == this;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1498
        assert key.sourceAddress() == null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1499
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1500
        synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1501
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1502
                throw new IllegalStateException("key is no longer valid");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1503
            if (source.isAnyLocalAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1504
                throw new IllegalArgumentException("Source address is a wildcard address");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1505
            if (source.isMulticastAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1506
                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
  1507
            if (source.getClass() != key.group().getClass())
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1508
                throw new IllegalArgumentException("Source address is different type to group");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1509
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1510
            int n;
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1511
            if (key instanceof MembershipKeyImpl.Type6) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1512
                 MembershipKeyImpl.Type6 key6 =
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1513
                    (MembershipKeyImpl.Type6)key;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1514
                n = Net.block6(fd, key6.groupAddress(), key6.index(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1515
                               Net.inet6AsByteArray(source));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1516
            } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1517
                MembershipKeyImpl.Type4 key4 =
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1518
                    (MembershipKeyImpl.Type4)key;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1519
                n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(),
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1520
                               Net.inet4AsInt(source));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1521
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1522
            if (n == IOStatus.UNAVAILABLE) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1523
                // ancient kernel
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1524
                throw new UnsupportedOperationException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1525
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1526
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1527
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1528
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1529
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1530
     * Unblock given source.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1531
     */
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1532
    void unblock(MembershipKeyImpl key, InetAddress source) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1533
        assert key.channel() == this;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1534
        assert key.sourceAddress() == null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1535
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1536
        synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1537
            if (!key.isValid())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1538
                throw new IllegalStateException("key is no longer valid");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1539
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1540
            try {
8788
b98f18278dc4 7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents: 7668
diff changeset
  1541
                if (key instanceof MembershipKeyImpl.Type6) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1542
                    MembershipKeyImpl.Type6 key6 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1543
                        (MembershipKeyImpl.Type6)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1544
                    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
  1545
                                 Net.inet6AsByteArray(source));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1546
                } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1547
                    MembershipKeyImpl.Type4 key4 =
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1548
                        (MembershipKeyImpl.Type4)key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1549
                    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
  1550
                                 Net.inet4AsInt(source));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1551
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1552
            } catch (IOException ioe) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1553
                // should not happen
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
  1554
                throw new AssertionError(ioe);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1555
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1556
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1557
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1558
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1559
    /**
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1560
     * 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
  1561
     * 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
  1562
     */
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1563
    private boolean tryClose() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1564
        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
  1565
        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
  1566
            state = ST_CLOSED;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1567
            try {
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1568
                // close socket
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1569
                cleaner.clean();
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1570
            } catch (UncheckedIOException ioe) {
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1571
                throw ioe.getCause();
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1572
            }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1573
            return true;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1574
        } else {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1575
            return false;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1576
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1577
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1578
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1579
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1580
     * 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
  1581
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1582
     * 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
  1583
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1584
    private void tryFinishClose() {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1585
        try {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1586
            tryClose();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1587
        } catch (IOException ignore) { }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1588
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1589
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1590
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1591
     * 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
  1592
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1593
     * 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
  1594
     * 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
  1595
     * 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
  1596
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1597
    private void implCloseBlockingMode() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1598
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1599
            assert state < ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1600
            state = ST_CLOSING;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1601
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1602
            // 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
  1603
            if (registry != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1604
                registry.invalidateAll();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1605
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1606
            if (!tryClose()) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1607
                long reader = readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1608
                long writer = writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1609
                if (reader != 0 || writer != 0) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1610
                    nd.preClose(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1611
                    if (reader != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1612
                        NativeThread.signal(reader);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1613
                    if (writer != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1614
                        NativeThread.signal(writer);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1615
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1616
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        }
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1618
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1619
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1620
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1621
     * 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
  1622
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1623
     * 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
  1624
     * 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
  1625
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1626
    private void implCloseNonBlockingMode() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1627
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1628
            assert state < ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1629
            state = ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1630
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1631
            // 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
  1632
            if (registry != null)
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1633
                registry.invalidateAll();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1634
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1635
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1636
        // 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
  1637
        readLock.lock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1638
        readLock.unlock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1639
        writeLock.lock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1640
        writeLock.unlock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1641
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1642
            if (state == ST_CLOSING) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1643
                tryClose();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1644
            }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1645
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1646
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1647
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1648
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1649
     * 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
  1650
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1651
    @Override
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1652
    protected void implCloseSelectableChannel() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1653
        assert !isOpen();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1654
        if (isBlocking()) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1655
            implCloseBlockingMode();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1656
        } else {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1657
            implCloseNonBlockingMode();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1658
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1661
    @Override
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1662
    public void kill() {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1663
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1664
            if (state == ST_CLOSING) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1665
                tryFinishClose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * Translates native poll revent set into a ready operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1673
    public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1674
        int intOps = ski.nioInterestOps();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1675
        int oldOps = ski.nioReadyOps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1678
        if ((ops & Net.POLLNVAL) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1685
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            newOps = intOps;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1687
            ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1691
        if (((ops & Net.POLLIN) != 0) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            ((intOps & SelectionKey.OP_READ) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1695
        if (((ops & Net.POLLOUT) != 0) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            ((intOps & SelectionKey.OP_WRITE) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1699
        ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1703
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1704
        return translateReadyOps(ops, ski.nioReadyOps(), ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1707
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1708
        return translateReadyOps(ops, 0, ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1711
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1714
    public int translateInterestOps(int ops) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        if ((ops & SelectionKey.OP_READ) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1717
            newOps |= Net.POLLIN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        if ((ops & SelectionKey.OP_WRITE) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1719
            newOps |= Net.POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        if ((ops & SelectionKey.OP_CONNECT) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1721
            newOps |= Net.POLLIN;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49284
diff changeset
  1722
        return newOps;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1733
    /**
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
  1734
     * Returns an action to release a the given file descriptor and free the
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
  1735
     * given native socket address.
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1736
     */
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
  1737
    private static Runnable releaserFor(FileDescriptor fd, NativeSocketAddress sa) {
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1738
        return () -> {
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1739
            try {
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1740
                nd.close(fd);
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1741
            } catch (IOException ioe) {
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1742
                throw new UncheckedIOException(ioe);
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1743
            } finally {
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
  1744
                // decrement socket count and release memory
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1745
                ResourceManager.afterUdpClose();
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
  1746
                sa.free();
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1747
            }
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1748
        };
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 58518
diff changeset
  1749
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents: 12440
diff changeset
  1753
    private static native void disconnect0(FileDescriptor fd, boolean isIPv6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
  1756
    private static native int receive0(FileDescriptor fd, long address, int len,
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
  1757
                                       long senderAddress, boolean connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
59329
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
  1760
    private static native int send0(boolean preferIPv6, FileDescriptor fd,
289000934908 8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents: 59146
diff changeset
  1761
                                    long address, int len, InetAddress addr, int port)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 18212
diff changeset
  1765
        IOUtil.load();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
}