src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
author alanb
Thu, 08 Feb 2018 10:55:21 +0000
changeset 48761 74c1fa26435a
parent 48750 ffbb784a8873
child 49001 ce06058197a4
permissions -rw-r--r--
8196956: (ch) More channels cleanup Reviewed-by: rriggs, prappo, bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
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: 2446
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: 2446
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    30
import java.net.InetAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    31
import java.net.InetSocketAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    32
import java.net.ProtocolFamily;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    33
import java.net.Socket;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    34
import java.net.SocketAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    35
import java.net.SocketOption;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    36
import java.net.StandardProtocolFamily;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    37
import java.net.StandardSocketOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.ByteBuffer;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    39
import java.nio.channels.AlreadyBoundException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    40
import java.nio.channels.AlreadyConnectedException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    41
import java.nio.channels.AsynchronousCloseException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    42
import java.nio.channels.ClosedChannelException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    43
import java.nio.channels.ConnectionPendingException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    44
import java.nio.channels.NoConnectionPendingException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    45
import java.nio.channels.NotYetConnectedException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    46
import java.nio.channels.SelectionKey;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    47
import java.nio.channels.SocketChannel;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    48
import java.nio.channels.spi.SelectorProvider;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    49
import java.util.Collections;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    50
import java.util.HashSet;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    51
import java.util.Set;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    52
import java.util.concurrent.locks.ReentrantLock;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    53
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
    54
import sun.net.NetHooks;
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    55
import sun.net.ext.ExtendedSocketOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * An implementation of SocketChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
class SocketChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    extends SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // Used to make native read and write calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static NativeDispatcher nd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // Our file descriptor object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final FileDescriptor fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final int fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // IDs of native threads doing reads and writes, for signalling
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 29986
diff changeset
    74
    private volatile long readerThread;
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 29986
diff changeset
    75
    private volatile long writerThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // 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
    78
    private final ReentrantLock readLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // 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
    81
    private final ReentrantLock writeLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // DO NOT invoke a blocking I/O operation while holding this lock!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private final Object stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
    89
    // set true when exclusive binding is on and SO_REUSEADDR is emulated
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
    90
    private boolean isReuseAddress;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
    91
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // State, increases monotonically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final int ST_UNINITIALIZED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static final int ST_UNCONNECTED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final int ST_PENDING = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final int ST_CONNECTED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static final int ST_KILLPENDING = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final int ST_KILLED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private int state = ST_UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // Binding
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   102
    private InetSocketAddress localAddress;
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   103
    private InetSocketAddress remoteAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // Input/Output open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private boolean isInputOpen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private boolean isOutputOpen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // Socket adaptor, created on demand
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   110
    private Socket socket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // Constructor for normal connecting sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    SocketChannelImpl(SelectorProvider sp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.fd = Net.socket(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.state = ST_UNCONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
6525
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   124
    SocketChannelImpl(SelectorProvider sp,
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   125
                      FileDescriptor fd,
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   126
                      boolean bound)
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   127
        throws IOException
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   128
    {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   129
        super(sp);
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   130
        this.fd = fd;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   131
        this.fdVal = IOUtil.fdVal(fd);
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   132
        this.state = ST_UNCONNECTED;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   133
        if (bound)
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   134
            this.localAddress = Net.localAddress(fd);
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   135
    }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   136
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // Constructor for sockets obtained from server sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    SocketChannelImpl(SelectorProvider sp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                      FileDescriptor fd, InetSocketAddress remote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.state = ST_CONNECTED;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   147
        this.localAddress = Net.localAddress(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this.remoteAddress = remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Socket socket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (socket == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                socket = SocketAdaptor.create(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   159
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   160
    public SocketAddress getLocalAddress() throws IOException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   161
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   162
            if (!isOpen())
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   163
                throw new ClosedChannelException();
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   164
            return  Net.getRevealedLocalAddress(localAddress);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   165
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   166
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   167
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   168
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   169
    public SocketAddress getRemoteAddress() throws IOException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   170
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   171
            if (!isOpen())
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   172
                throw new ClosedChannelException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   173
            return remoteAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   174
        }
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   177
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   178
    public <T> SocketChannel setOption(SocketOption<T> name, T value)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   179
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   180
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   181
        if (name == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   182
            throw new NullPointerException();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   183
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   184
            throw new UnsupportedOperationException("'" + name + "' 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
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   187
            if (!isOpen())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   188
                throw new ClosedChannelException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   189
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   190
            if (name == StandardSocketOptions.IP_TOS) {
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   191
                ProtocolFamily family = Net.isIPv6Available() ?
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   192
                    StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   193
                Net.setSocketOption(fd, family, name, value);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   194
                return this;
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   195
            }
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   196
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   197
            if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   198
                // SO_REUSEADDR emulated when using exclusive bind
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   199
                isReuseAddress = (Boolean)value;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   200
                return this;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   201
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   202
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   203
            // no options that require special handling
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   204
            Net.setSocketOption(fd, Net.UNSPEC, name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   205
            return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   206
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   207
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   208
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   209
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   210
    @SuppressWarnings("unchecked")
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   211
    public <T> T getOption(SocketOption<T> name)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   212
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   213
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   214
        if (name == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   215
            throw new NullPointerException();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   216
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   217
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   218
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   219
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   220
            if (!isOpen())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   221
                throw new ClosedChannelException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   222
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   223
            if (name == StandardSocketOptions.SO_REUSEADDR &&
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   224
                    Net.useExclusiveBind())
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   225
            {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   226
                // SO_REUSEADDR emulated when using exclusive bind
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   227
                return (T)Boolean.valueOf(isReuseAddress);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   228
            }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   229
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   230
            // special handling for IP_TOS: always return 0 when IPv6
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   231
            if (name == StandardSocketOptions.IP_TOS) {
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   232
                ProtocolFamily family = Net.isIPv6Available() ?
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   233
                    StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   234
                return (T) Net.getSocketOption(fd, family, name);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   235
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   236
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   237
            // no options that require special handling
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   238
            return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   239
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   240
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   241
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   242
    private static class DefaultOptionsHolder {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   243
        static final Set<SocketOption<?>> defaultOptions = defaultOptions();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   244
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   245
        private static Set<SocketOption<?>> defaultOptions() {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   246
            HashSet<SocketOption<?>> set = new HashSet<>(8);
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   247
            set.add(StandardSocketOptions.SO_SNDBUF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   248
            set.add(StandardSocketOptions.SO_RCVBUF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   249
            set.add(StandardSocketOptions.SO_KEEPALIVE);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   250
            set.add(StandardSocketOptions.SO_REUSEADDR);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   251
            if (Net.isReusePortAvailable()) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   252
                set.add(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   253
            }
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   254
            set.add(StandardSocketOptions.SO_LINGER);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   255
            set.add(StandardSocketOptions.TCP_NODELAY);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   256
            // additional options required by socket adaptor
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   257
            set.add(StandardSocketOptions.IP_TOS);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   258
            set.add(ExtendedSocketOption.SO_OOBINLINE);
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   259
            ExtendedSocketOptions extendedOptions =
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   260
                    ExtendedSocketOptions.getInstance();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   261
            set.addAll(extendedOptions.options());
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   262
            return Collections.unmodifiableSet(set);
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   266
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   267
    public final Set<SocketOption<?>> supportedOptions() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   268
        return DefaultOptionsHolder.defaultOptions;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   269
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   270
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private boolean ensureReadOpen() throws ClosedChannelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (!isInputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private void ensureWriteOpen() throws ClosedChannelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (!isOutputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private void readerCleanup() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (state == ST_KILLPENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    private void writerCleanup() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (state == ST_KILLPENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public int read(ByteBuffer buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   316
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   317
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (!ensureReadOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                // Set up the interruption machinery; see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                // AbstractInterruptibleChannel for details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    if (!isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    // Either the current thread is already interrupted, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    // begin() closed the channel, or another thread closed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    // channel since we checked it a few bytecodes ago.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    // either case the value returned here is irrelevant since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    // the invocation of end() in the finally block will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    // an appropriate exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    // Save this thread so that it can be signalled on those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    // platforms that require it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                // Between the previous test of isOpen() and the return of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                // IOUtil.read invocation below, this channel might be closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                // or this thread might be interrupted.  We rely upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                // implicit synchronization point in the kernel read() call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                // make sure that the right thing happens.  In either case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                // implCloseSelectableChannel method is ultimately invoked in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                // some other thread, so there are three possibilities:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                //   - implCloseSelectableChannel() invokes nd.preClose()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                //     before this thread invokes read(), in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                //     read returns immediately with either EOF or an error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                //     the latter of which will cause an IOException to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                //     thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                //   - implCloseSelectableChannel() invokes nd.preClose() after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                //     this thread is blocked in read().  On some operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                //     systems (e.g., Solaris and Windows) this causes the read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                //     to return immediately with either EOF or an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                //     indication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                //   - implCloseSelectableChannel() invokes nd.preClose() after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                //     this thread is blocked in read() but the operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                //     system (e.g., Linux) doesn't support preemptive close,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                //     so implCloseSelectableChannel() proceeds to signal this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                //     thread, thereby causing the read to return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                //     with IOStatus.INTERRUPTED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                // In all three cases the invocation of end() in the finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                // clause will notice that the channel has been closed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                // throw an appropriate exception (AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                // or ClosedByInterruptException) if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                // *There is A fourth possibility. implCloseSelectableChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                // invokes nd.preClose(), signals reader/writer thred and quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                // moves on to nd.close() in kill(), which does a real close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                // Then a third thread accepts a new connection, opens file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                // whatever that causes the released "fd" to be recycled. All
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                // above happens just between our last isOpen() check and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                // next kernel read reached, with the recycled "fd". The solution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                // is to postpone the real kill() if there is a reader or/and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                // writer thread(s) over there "waiting", leave the cleanup/kill
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                // to the reader or writer thread. (the preClose() still happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                // so the connection gets cut off as usual).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                // For socket channels there is the additional wrinkle that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                // asynchronous shutdown works much like asynchronous close,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                // except that the channel is shutdown rather than completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                // closed.  This is analogous to the first two cases above,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                // except that the shutdown operation plays the role of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                // nd.preClose().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                for (;;) {
16921
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents: 14025
diff changeset
   398
                    n = IOUtil.read(fd, buf, -1, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    if ((n == IOStatus.INTERRUPTED) && isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        // The system call was interrupted but the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        // is still open, so retry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                readerCleanup();        // Clear reader thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                // The end method, which is defined in our superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                // AbstractInterruptibleChannel, resets the interruption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                // machinery.  If its argument is true then it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                // normally; otherwise it checks the interrupt and open state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                // of this channel and throws an appropriate exception if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                // necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                // So, if we actually managed to do any I/O in the above try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                // block then we pass true to the end method.  We also pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                // true if the channel was in non-blocking mode when the I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                // operation was initiated but no data could be transferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                // this prevents spurious exceptions from being thrown in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                // rare event that a channel is closed or a thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                // interrupted at the exact moment that a non-blocking I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                // request is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                end(n > 0 || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                // Extra case for socket channels: Asynchronous shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    if ((n <= 0) && (!isInputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        return IOStatus.EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   437
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   438
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   442
    public long read(ByteBuffer[] dsts, int offset, int length)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   443
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   444
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   445
        if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   446
            throw new IndexOutOfBoundsException();
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   447
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   448
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (!ensureReadOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                for (;;) {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   461
                    n = IOUtil.read(fd, dsts, offset, length, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                readerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                end(n > 0 || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    if ((n <= 0) && (!isInputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        return IOStatus.EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   475
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   476
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public int write(ByteBuffer buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            throw new NullPointerException();
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   483
        writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   484
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            ensureWriteOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                for (;;) {
16921
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents: 14025
diff changeset
   495
                    n = IOUtil.write(fd, buf, -1, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                writerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                end(n > 0 || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    if ((n <= 0) && (!isOutputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        throw new AsynchronousCloseException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   509
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   510
            writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   514
    public long write(ByteBuffer[] srcs, int offset, int length)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   515
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   516
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   517
        if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   518
            throw new IndexOutOfBoundsException();
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   519
        writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   520
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            ensureWriteOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                for (;;) {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 6117
diff changeset
   531
                    n = IOUtil.write(fd, srcs, offset, length, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                writerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    if ((n <= 0) && (!isOutputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        throw new AsynchronousCloseException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   545
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   546
            writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
6117
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   550
    // package-private
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   551
    int sendOutOfBandData(byte b) throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   552
        writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   553
        try {
6117
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   554
            ensureWriteOpen();
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   555
            int n = 0;
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   556
            try {
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   557
                begin();
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   558
                synchronized (stateLock) {
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   559
                    if (!isOpen())
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   560
                        return 0;
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   561
                    writerThread = NativeThread.current();
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   562
                }
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   563
                for (;;) {
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   564
                    n = sendOutOfBandData(fd, b);
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   565
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   566
                        continue;
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   567
                    return IOStatus.normalize(n);
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   568
                }
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   569
            } finally {
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   570
                writerCleanup();
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   571
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   572
                synchronized (stateLock) {
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   573
                    if ((n <= 0) && (!isOutputOpen))
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   574
                        throw new AsynchronousCloseException();
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   575
                }
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   576
                assert IOStatus.check(n);
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   577
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   578
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   579
            writeLock.unlock();
6117
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   580
        }
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   581
    }
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
   582
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    protected void implConfigureBlocking(boolean block) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        IOUtil.configureBlocking(fd, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   587
    public InetSocketAddress localAddress() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public SocketAddress remoteAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   599
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   600
    public SocketChannel bind(SocketAddress local) throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   601
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   602
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   603
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   604
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   606
                    if (!isOpen())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   607
                        throw new ClosedChannelException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   608
                    if (state == ST_PENDING)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   609
                        throw new ConnectionPendingException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    if (localAddress != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        throw new AlreadyBoundException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   612
                    InetSocketAddress isa = (local == null) ?
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   613
                        new InetSocketAddress(0) : Net.checkAddress(local);
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   614
                    SecurityManager sm = System.getSecurityManager();
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   615
                    if (sm != null) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   616
                        sm.checkListen(isa.getPort());
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   617
                    }
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
   618
                    NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    Net.bind(fd, isa.getAddress(), isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    localAddress = Net.localAddress(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   622
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   623
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   625
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   626
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   628
        return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return (state == ST_CONNECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public boolean isConnectionPending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            return (state == ST_PENDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    void ensureOpenAndUnconnected() throws IOException { // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            if (state == ST_CONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                throw new AlreadyConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (state == ST_PENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                throw new ConnectionPendingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public boolean connect(SocketAddress sa) throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   655
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   656
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   657
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   658
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                ensureOpenAndUnconnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                InetSocketAddress isa = Net.checkAddress(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    sm.checkConnect(isa.getAddress().getHostAddress(),
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   664
                            isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                synchronized (blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                            begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                                if (!isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                }
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
   674
                                // notify hook only if unbound
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
   675
                                if (localAddress == null) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
   676
                                    NetHooks.beforeTcpConnect(fd,
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   677
                                            isa.getAddress(),
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   678
                                            isa.getPort());
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
   679
                                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                InetAddress ia = isa.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                if (ia.isAnyLocalAddress())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                                    ia = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                                n = Net.connect(fd,
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   687
                                        ia,
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   688
                                        isa.getPort());
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   689
                                if ((n == IOStatus.INTERRUPTED) && isOpen())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                            }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   693
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                            readerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                            end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                            assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        // If an exception was thrown, close the channel after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                        // invoking end() so as to avoid bogus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                        // AsynchronousCloseExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        remoteAddress = isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                            // Connection succeeded; disallow further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                            // invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                            state = ST_CONNECTED;
12683
5c743840dd8a 7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents: 12440
diff changeset
   713
                            if (isOpen())
5c743840dd8a 7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents: 12440
diff changeset
   714
                                localAddress = Net.localAddress(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                        // If nonblocking and no exception then connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                        // pending; disallow another invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        if (!isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                            state = ST_PENDING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                return false;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   726
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   727
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   729
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   730
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    public boolean finishConnect() throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   735
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   736
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   737
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   738
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    if (state == ST_CONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    if (state != ST_PENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                        throw new NoConnectionPendingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                        begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                        synchronized (blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                                if (!isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                            if (!isBlocking()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                for (;;) {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   760
                                    n = checkConnect(fd, false);
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   761
                                    if ((n == IOStatus.INTERRUPTED) && isOpen())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                                for (;;) {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   767
                                    n = checkConnect(fd, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                                    if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                                        // Loop in case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                                        // spurious notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                                    }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   773
                                    if ((n == IOStatus.INTERRUPTED) && isOpen())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                            readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                            if (state == ST_KILLPENDING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                                // poll()/getsockopt() does not report
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                // error (throws exception, with n = 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                                // on Linux platform after dup2 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                                // signal-wakeup. Force n to 0 so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                                // end() can throw appropriate exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                                n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                        end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                        assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    // If an exception was thrown, close the channel after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    // invoking end() so as to avoid bogus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    // AsynchronousCloseExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        state = ST_CONNECTED;
12683
5c743840dd8a 7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents: 12440
diff changeset
   805
                        if (isOpen())
5c743840dd8a 7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents: 12440
diff changeset
   806
                            localAddress = Net.localAddress(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                return false;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   811
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   812
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   814
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   815
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   819
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   820
    public SocketChannel shutdownInput() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                throw new ClosedChannelException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   824
            if (!isConnected())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   825
                throw new NotYetConnectedException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   826
            if (isInputOpen) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   827
                Net.shutdown(fd, Net.SHUT_RD);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   828
                if (readerThread != 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   829
                    NativeThread.signal(readerThread);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   830
                isInputOpen = false;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   831
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   832
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   836
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   837
    public SocketChannel shutdownOutput() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                throw new ClosedChannelException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   841
            if (!isConnected())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   842
                throw new NotYetConnectedException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   843
            if (isOutputOpen) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   844
                Net.shutdown(fd, Net.SHUT_WR);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   845
                if (writerThread != 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   846
                    NativeThread.signal(writerThread);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   847
                isOutputOpen = false;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   848
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   849
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public boolean isInputOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            return isInputOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    public boolean isOutputOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            return isOutputOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    // AbstractInterruptibleChannel synchronizes invocations of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    // using AbstractInterruptibleChannel.closeLock, and also ensures that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    // method is only ever invoked once.  Before we get to this method, isOpen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    // (which is volatile) will have been set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    protected void implCloseSelectableChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            isInputOpen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            isOutputOpen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            // Close the underlying file descriptor and dup it to a known fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            // that's already closed.  This prevents other operations on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            // channel from using the old fd, which might be recycled in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            // meantime and allocated to an entirely different channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            //
12440
2689ca179e22 7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents: 9679
diff changeset
   880
            if (state != ST_KILLED)
2689ca179e22 7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents: 9679
diff changeset
   881
                nd.preClose(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            // Signal native threads, if needed.  If a target thread is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            // currently blocked in an I/O operation then no harm is done since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            // the signal handler doesn't actually do anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            if (readerThread != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                NativeThread.signal(readerThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            if (writerThread != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                NativeThread.signal(writerThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            // If this channel is not registered then it's safe to close the fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            // immediately since we know at this point that no thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            // blocked in an I/O operation upon the channel and, since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            // channel is marked closed, no thread will start another such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            // operation.  If this channel is registered then we don't close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            // the fd since it might be in use by a selector.  In that case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            // closing this channel caused its keys to be cancelled, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            // last selector to deregister a key for this channel will invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            // kill() to close the fd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            if (!isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    public void kill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            if (state == ST_KILLED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            if (state == ST_UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            assert !isOpen() && !isRegistered();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            // Postpone the kill if there is a waiting reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            // or writer thread. See the comments in read() for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            // more detailed explanation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            if (readerThread == 0 && writerThread == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                nd.close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                state = ST_KILLPENDING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * Translates native poll revent ops into a ready operation ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    public boolean translateReadyOps(int ops, int initialOps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                                     SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        int oldOps = sk.nioReadyOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   939
        if ((ops & Net.POLLNVAL) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   946
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            newOps = intOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   952
        if (((ops & Net.POLLIN) != 0) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            ((intOps & SelectionKey.OP_READ) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            (state == ST_CONNECTED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   957
        if (((ops & Net.POLLCONN) != 0) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            ((intOps & SelectionKey.OP_CONNECT) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            ((state == ST_UNCONNECTED) || (state == ST_PENDING))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            newOps |= SelectionKey.OP_CONNECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   963
        if (((ops & Net.POLLOUT) != 0) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            ((intOps & SelectionKey.OP_WRITE) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            (state == ST_CONNECTED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        return translateReadyOps(ops, 0, sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   980
    // package-private
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   981
    int poll(int events, long timeout) throws IOException {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   982
        assert Thread.holdsLock(blockingLock()) && !isBlocking();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   983
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   984
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   985
        try {
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   986
            int n = 0;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   987
            try {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   988
                begin();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   989
                synchronized (stateLock) {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   990
                    if (!isOpen())
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   991
                        return 0;
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   992
                    readerThread = NativeThread.current();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   993
                }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   994
                n = Net.poll(fd, events, timeout);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   995
            } finally {
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   996
                readerCleanup();
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   997
                end(n > 0);
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   998
            }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
   999
            return n;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1000
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1001
            readLock.unlock();
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
  1002
        }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
  1003
    }
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 12683
diff changeset
  1004
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        if ((ops & SelectionKey.OP_READ) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1011
            newOps |= Net.POLLIN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        if ((ops & SelectionKey.OP_WRITE) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1013
            newOps |= Net.POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        if ((ops & SelectionKey.OP_CONNECT) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1015
            newOps |= Net.POLLCONN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        sk.selector.putEventOps(sk, newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1027
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23879
diff changeset
  1029
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        sb.append(this.getClass().getSuperclass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            sb.append("closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                case ST_UNCONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                    sb.append("unconnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                case ST_PENDING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    sb.append("connection-pending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                case ST_CONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    sb.append("connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    if (!isInputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                        sb.append(" ishut");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    if (!isOutputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                        sb.append(" oshut");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                }
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1051
                InetSocketAddress addr = localAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1052
                if (addr != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    sb.append(" local=");
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1054
                    sb.append(Net.getRevealedLocalAddressAsString(addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                if (remoteAddress() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    sb.append(" remote=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    sb.append(remoteAddress().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1069
    private static native int checkConnect(FileDescriptor fd, boolean block)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
6117
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
  1072
    private static native int sendOutOfBandData(FileDescriptor fd, byte data)
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
  1073
        throws IOException;
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
  1074
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 18212
diff changeset
  1076
        IOUtil.load();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        nd = new SocketDispatcher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
}