src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
author michaelm
Fri, 25 Oct 2019 15:56:35 +0100
branchunixdomainchannels
changeset 58801 119ac9128c1b
parent 55081 dd321e3596c0
child 58847 692de65ab293
permissions -rw-r--r--
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53419
eac105e3ec13 8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents: 50722
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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;
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
    35
import java.net.SocketException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    36
import java.net.SocketOption;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    37
import java.net.SocketTimeoutException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    38
import java.net.StandardProtocolFamily;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    39
import java.net.StandardSocketOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.nio.ByteBuffer;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    41
import java.nio.channels.AlreadyBoundException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    42
import java.nio.channels.AlreadyConnectedException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    43
import java.nio.channels.AsynchronousCloseException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    44
import java.nio.channels.ClosedChannelException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    45
import java.nio.channels.ConnectionPendingException;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    46
import java.nio.channels.IllegalBlockingModeException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    47
import java.nio.channels.NoConnectionPendingException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    48
import java.nio.channels.NotYetConnectedException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    49
import java.nio.channels.SelectionKey;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    50
import java.nio.channels.SocketChannel;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    51
import java.nio.channels.spi.SelectorProvider;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    52
import java.util.Collections;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    53
import java.util.HashSet;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    54
import java.util.Objects;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    55
import java.util.Set;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    56
import java.util.concurrent.locks.ReentrantLock;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    57
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
    58
import sun.net.ConnectionResetException;
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
    59
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
    60
import sun.net.ext.ExtendedSocketOptions;
50722
bc104aaf24e9 8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents: 50303
diff changeset
    61
import sun.net.util.SocketExceptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * An implementation of SocketChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    67
public abstract class SocketChannelImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    extends SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Used to make native read and write calls
53445
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53419
diff changeset
    72
    private static final NativeDispatcher nd = new SocketDispatcher();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // Our file descriptor object
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    75
    final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final int fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // Lock held by current reading or connecting thread
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    79
    final ReentrantLock readLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Lock held by current writing or connecting thread
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    82
    final ReentrantLock writeLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // DO NOT invoke a blocking I/O operation while holding this lock!
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    86
    final Object stateLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    88
    // Input/Output closed
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    89
    volatile boolean isInputClosed;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    90
    volatile boolean isOutputClosed;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    91
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
    92
    // Connection reset protected by readLock
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
    93
    private boolean connectionReset;
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // State, increases monotonically
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    98
    static final int ST_UNCONNECTED = 0;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    99
    static final int ST_CONNECTIONPENDING = 1;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   100
    static final int ST_CONNECTED = 2;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   101
    static final int ST_CLOSING = 3;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   102
    static final int ST_CLOSED = 4;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   103
    volatile int state;  // need stateLock to change
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   104
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   105
    // IDs of native threads doing reads and writes, for signalling
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   106
    private long readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   107
    private long writerThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // Binding
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   110
    SocketAddress localAddress;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   111
    SocketAddress remoteAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // Socket adaptor, created on demand
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   114
    Socket socket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // Constructor for normal connecting sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    //
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   121
    public SocketChannelImpl(SelectorProvider sp) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.fd = Net.socket(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   127
    public SocketChannelImpl(SelectorProvider sp, FileDescriptor fd)
6525
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   128
        throws IOException
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   129
    {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   130
        super(sp);
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   131
        this.fd = fd;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   132
        this.fdVal = IOUtil.fdVal(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   135
    /**
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   136
     * Checks that the channel is open.
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   137
     *
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   138
     * @throws ClosedChannelException if channel is closed (or closing)
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   139
     */
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   140
    void ensureOpen() throws ClosedChannelException {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   141
        if (!isOpen())
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   142
            throw new ClosedChannelException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   143
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   144
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   145
    /**
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   146
     * Checks that the channel is open and connected.
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   147
     *
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   148
     * @apiNote This method uses the "state" field to check if the channel is
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   149
     * open. It should never be used in conjuncion with isOpen or ensureOpen
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   150
     * as these methods check AbstractInterruptibleChannel's closed field - that
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   151
     * field is set before implCloseSelectableChannel is called and so before
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   152
     * the state is changed.
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   153
     *
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   154
     * @throws ClosedChannelException if channel is closed (or closing)
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   155
     * @throws NotYetConnectedException if open and not connected
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   156
     */
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   157
    private void ensureOpenAndConnected() throws ClosedChannelException {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   158
        int state = this.state;
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   159
        if (state < ST_CONNECTED) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   160
            throw new NotYetConnectedException();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   161
        } else if (state > ST_CONNECTED) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   162
            throw new ClosedChannelException();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   163
        }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   164
    }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   165
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   166
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   167
     * Marks the beginning of a read operation that might block.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   168
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   169
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   170
     * @throws NotYetConnectedException if the channel is not yet connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   171
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   172
    private void beginRead(boolean blocking) throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   173
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   174
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   175
            begin();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   176
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   177
            synchronized (stateLock) {
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   178
                ensureOpenAndConnected();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   179
                // record thread so it can be signalled if needed
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   180
                readerThread = NativeThread.current();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   181
            }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   182
        } else {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   183
            ensureOpenAndConnected();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   187
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   188
     * Marks the end of a read operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   189
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   190
     * @throws AsynchronousCloseException if the channel was closed due to this
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   191
     * thread being interrupted on a blocking read operation.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   192
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   193
    private void endRead(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   194
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   195
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   196
        if (blocking) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   197
            synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   198
                readerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   199
                if (state == ST_CLOSING) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   200
                    tryFinishClose();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   201
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   202
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   203
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   204
            end(completed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   208
    private void throwConnectionReset() throws SocketException {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   209
        throw new SocketException("Connection reset");
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   210
    }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   211
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   212
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   213
    public int read(ByteBuffer buf) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   214
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   215
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   216
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   217
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   218
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   219
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   220
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   221
                beginRead(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   222
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   223
                // check if connection has been reset
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   224
                if (connectionReset)
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   225
                    throwConnectionReset();
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   226
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   227
                // check if input is shutdown
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   228
                if (isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   229
                    return IOStatus.EOF;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   230
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   231
                n = IOUtil.read(fd, buf, -1, nd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   232
                if (blocking) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   233
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   234
                        park(Net.POLLIN);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   235
                        n = IOUtil.read(fd, buf, -1, nd);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   236
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   237
                }
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   238
            } catch (ConnectionResetException e) {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   239
                connectionReset = true;
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   240
                throwConnectionReset();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   241
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   242
                endRead(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   243
                if (n <= 0 && isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   244
                    return IOStatus.EOF;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   245
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   246
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   247
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   248
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   252
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   253
    public long read(ByteBuffer[] dsts, int offset, int length)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   254
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   255
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   256
        Objects.checkFromIndexSize(offset, length, dsts.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   258
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   259
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   260
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   261
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   262
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   263
                beginRead(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   264
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   265
                // check if connection has been reset
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   266
                if (connectionReset)
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   267
                    throwConnectionReset();
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   268
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   269
                // check if input is shutdown
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   270
                if (isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   271
                    return IOStatus.EOF;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   272
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   273
                n = IOUtil.read(fd, dsts, offset, length, nd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   274
                if (blocking) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   275
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   276
                        park(Net.POLLIN);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   277
                        n = IOUtil.read(fd, dsts, offset, length, nd);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   278
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   279
                }
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   280
            } catch (ConnectionResetException e) {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   281
                connectionReset = true;
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   282
                throwConnectionReset();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   283
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   284
                endRead(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   285
                if (n <= 0 && isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   286
                    return IOStatus.EOF;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   287
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   288
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   289
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   290
            readLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   291
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   292
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   293
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   294
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   295
     * Marks the beginning of a write operation that might block.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   296
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   297
     * @throws ClosedChannelException if the channel is closed or output shutdown
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   298
     * @throws NotYetConnectedException if the channel is not yet connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   299
     */
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   300
    void beginWrite(boolean blocking) throws ClosedChannelException {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   301
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   302
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   303
            begin();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   304
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   305
            synchronized (stateLock) {
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   306
                ensureOpenAndConnected();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   307
                if (isOutputClosed)
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   308
                    throw new ClosedChannelException();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   309
                // record thread so it can be signalled if needed
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   310
                writerThread = NativeThread.current();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   311
            }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   312
        } else {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   313
            ensureOpenAndConnected();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   314
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   315
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   316
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   317
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   318
     * Marks the end of a write operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   319
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   320
     * @throws AsynchronousCloseException if the channel was closed due to this
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   321
     * thread being interrupted on a blocking write operation.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   322
     */
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   323
    void endWrite(boolean blocking, boolean completed)
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   324
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   325
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   326
        if (blocking) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   327
            synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   328
                writerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   329
                if (state == ST_CLOSING) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   330
                    tryFinishClose();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   331
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   332
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   333
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   334
            end(completed);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   335
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   336
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   337
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   338
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   339
    public int write(ByteBuffer buf) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   340
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   341
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   342
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   343
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   344
            boolean blocking = isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   347
                beginWrite(blocking);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   348
                n = IOUtil.write(fd, buf, -1, nd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   349
                if (blocking) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   350
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   351
                        park(Net.POLLOUT);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   352
                        n = IOUtil.write(fd, buf, -1, nd);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   353
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   354
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   355
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   356
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   357
                if (n <= 0 && isOutputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   358
                    throw new AsynchronousCloseException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   359
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   360
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   361
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   362
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   363
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   364
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   366
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   367
    public long write(ByteBuffer[] srcs, int offset, int length)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   368
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   369
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   370
        Objects.checkFromIndexSize(offset, length, srcs.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   372
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   373
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   374
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   375
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   376
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   377
                beginWrite(blocking);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   378
                n = IOUtil.write(fd, srcs, offset, length, nd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   379
                if (blocking) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   380
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   381
                        park(Net.POLLOUT);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   382
                        n = IOUtil.write(fd, srcs, offset, length, nd);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   383
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   386
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   387
                if (n <= 0 && isOutputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   388
                    throw new AsynchronousCloseException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   389
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   390
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   391
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   392
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   393
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   394
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   396
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   397
    protected void implConfigureBlocking(boolean block) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   398
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   399
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   400
            writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   401
            try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   402
                lockedConfigureBlocking(block);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   403
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   404
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   406
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   407
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   411
    /**
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   412
     * Adjust the blocking mode while holding the readLock or writeLock.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   413
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   414
    private void lockedConfigureBlocking(boolean block) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   415
        assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   416
        synchronized (stateLock) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   417
            ensureOpen();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   418
            IOUtil.configureBlocking(fd, block);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   419
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   420
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   421
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   422
    /**
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   423
     * Returns the local address, or null if not bound
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   424
     */
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   425
    SocketAddress localAddress() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   426
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   431
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   432
     * Returns the remote address, or null if not connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   433
     */
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   434
    SocketAddress remoteAddress() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   435
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   440
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public boolean isConnected() {
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   442
        return (state == ST_CONNECTED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   445
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public boolean isConnectionPending() {
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   447
        return (state == ST_CONNECTIONPENDING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   450
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   451
     * Marks the beginning of a connect operation that might block.
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   452
     * @param blocking true if configured blocking
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   453
     * @param isa the remote address
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   454
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   455
     * @throws AlreadyConnectedException if already connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   456
     * @throws ConnectionPendingException is a connection is pending
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   457
     * @throws IOException if the pre-connect hook fails
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   458
     */
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   459
    private void beginConnect(boolean blocking, SocketAddress sa)
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   460
        throws IOException
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   461
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   462
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   463
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   464
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   465
        }
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   466
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   467
            ensureOpen();
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   468
            int state = this.state;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (state == ST_CONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                throw new AlreadyConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   471
            if (state == ST_CONNECTIONPENDING)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                throw new ConnectionPendingException();
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   473
            assert state == ST_UNCONNECTED;
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   474
            this.state = ST_CONNECTIONPENDING;
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   475
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   476
            if (localAddress == null && sa instanceof InetSocketAddress) {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   477
                InetSocketAddress isa = (InetSocketAddress)sa;
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   478
                NetHooks.beforeTcpConnect(fd, isa.getAddress(), isa.getPort());
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   479
            }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   480
            remoteAddress = sa;
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   481
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   482
            if (blocking) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   483
                // record thread so it can be signalled if needed
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   484
                readerThread = NativeThread.current();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   485
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   489
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   490
     * Marks the end of a connect operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   491
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   492
     * @throws AsynchronousCloseException if the channel was closed due to this
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   493
     * thread being interrupted on a blocking connect operation.
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   494
     * @throws IOException if completed and unable to obtain the local address
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   495
     */
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   496
    void endConnect(boolean blocking, boolean completed)
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   497
        throws IOException
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   498
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   499
        endRead(blocking, completed);
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   500
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   501
        if (completed) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   502
            synchronized (stateLock) {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   503
                if (state == ST_CONNECTIONPENDING) {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   504
                    localAddress = localAddressImpl(fd);
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   505
                    state = ST_CONNECTED;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   506
                }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   507
            }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   508
        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   509
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   510
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   511
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   512
     * Checks the remote address to which this channel is to be connected.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   513
     */
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   514
    abstract SocketAddress checkRemote(SocketAddress sa) throws IOException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   515
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   516
    abstract int connectImpl(FileDescriptor fd,SocketAddress sa) throws IOException;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   517
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   518
    @Override
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   519
    public boolean connect(SocketAddress remote) throws IOException {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   520
        SocketAddress sa = checkRemote(remote);
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   521
        try {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   522
            readLock.lock();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   523
            try {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   524
                writeLock.lock();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   525
                try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   526
                    boolean blocking = isBlocking();
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   527
                    boolean connected = false;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   528
                    try {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   529
                        beginConnect(blocking, sa);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   530
                        int n = connectImpl(fd, sa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   531
                        if (n > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   532
                            connected = true;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   533
                        } else if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   534
                            assert IOStatus.okayToRetry(n);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   535
                            boolean polled = false;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   536
                            while (!polled && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   537
                                park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   538
                                polled = Net.pollConnectNow(fd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   539
                            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   540
                            connected = polled && isOpen();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   541
                        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   542
                    } finally {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   543
                        endConnect(blocking, connected);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   544
                    }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   545
                    return connected;
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   546
                } finally {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   547
                    writeLock.unlock();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   548
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   549
            } finally {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   550
                readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   552
        } catch (IOException ioe) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   553
            // connect failed, close the channel
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   554
            close();
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   555
            throw SocketExceptions.of(ioe, sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   559
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   560
     * Marks the beginning of a finishConnect operation that might block.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   561
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   562
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   563
     * @throws NoConnectionPendingException if no connection is pending
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   564
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   565
    private void beginFinishConnect(boolean blocking) throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   566
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   567
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   568
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   569
        }
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   570
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   571
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   572
            if (state != ST_CONNECTIONPENDING)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   573
                throw new NoConnectionPendingException();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   574
            if (blocking) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   575
                // record thread so it can be signalled if needed
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   576
                readerThread = NativeThread.current();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   577
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   578
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   579
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   580
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   581
    abstract SocketAddress localAddressImpl(FileDescriptor fd)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   582
        throws IOException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   583
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   584
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   585
     * Marks the end of a finishConnect operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   586
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   587
     * @throws AsynchronousCloseException if the channel was closed due to this
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   588
     * thread being interrupted on a blocking connect operation.
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   589
     * @throws IOException if completed and unable to obtain the local address
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   590
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   591
    private void endFinishConnect(boolean blocking, boolean completed)
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   592
        throws IOException
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   593
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   594
        endRead(blocking, completed);
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   595
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   596
        if (completed) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   597
            synchronized (stateLock) {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   598
                if (state == ST_CONNECTIONPENDING) {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   599
                    localAddress = localAddressImpl(fd);
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   600
                    state = ST_CONNECTED;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   601
                }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   602
            }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   603
        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   604
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   605
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   606
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public boolean finishConnect() throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   608
        try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   609
            readLock.lock();
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   610
            try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   611
                writeLock.lock();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   612
                try {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   613
                    // no-op if already connected
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   614
                    if (isConnected())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                        return true;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   616
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   617
                    boolean blocking = isBlocking();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   618
                    boolean connected = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   620
                        beginFinishConnect(blocking);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   621
                        boolean polled = Net.pollConnectNow(fd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   622
                        if (blocking) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   623
                            while (!polled && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   624
                                park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   625
                                polled = Net.pollConnectNow(fd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   626
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                        }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   628
                        connected = polled && isOpen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    } finally {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   630
                        endFinishConnect(blocking, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   632
                    assert (blocking && connected) ^ !blocking;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   633
                    return connected;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   634
                } finally {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   635
                    writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   637
            } finally {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   638
                readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   640
        } catch (IOException ioe) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   641
            // connect failed, close the channel
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   642
            close();
50722
bc104aaf24e9 8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents: 50303
diff changeset
   643
            throw SocketExceptions.of(ioe, remoteAddress);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   647
    /**
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   648
     * Closes the socket if there are no I/O operations in progress and the
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   649
     * channel is not registered with a Selector.
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   650
     */
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   651
    private boolean tryClose() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   652
        assert Thread.holdsLock(stateLock) && state == ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   653
        if ((readerThread == 0) && (writerThread == 0) && !isRegistered()) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   654
            state = ST_CLOSED;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   655
            nd.close(fd);
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   656
            return true;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   657
        } else {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   658
            return false;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   659
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   660
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   661
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   662
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   663
     * Invokes tryClose to attempt to close the socket.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   664
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   665
     * This method is used for deferred closing by I/O and Selector operations.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   666
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   667
    private void tryFinishClose() {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   668
        try {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   669
            tryClose();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   670
        } catch (IOException ignore) { }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   671
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   672
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   673
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   674
     * Closes this channel when configured in blocking mode.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   675
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   676
     * If there is an I/O operation in progress then the socket is pre-closed
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   677
     * and the I/O threads signalled, in which case the final close is deferred
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   678
     * until all I/O operations complete.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   679
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   680
     * Note that a channel configured blocking may be registered with a Selector
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   681
     * This arises when a key is canceled and the channel configured to blocking
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   682
     * mode before the key is flushed from the Selector.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   683
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   684
    private void implCloseBlockingMode() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   685
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   686
            assert state < ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   687
            state = ST_CLOSING;
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   688
            if (!tryClose()) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   689
                long reader = readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   690
                long writer = writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   691
                if (reader != 0 || writer != 0) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   692
                    nd.preClose(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   693
                    if (reader != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   694
                        NativeThread.signal(reader);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   695
                    if (writer != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   696
                        NativeThread.signal(writer);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   697
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   698
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   699
        }
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   700
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   701
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   702
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   703
     * Closes this channel when configured in non-blocking mode.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   704
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   705
     * If the channel is registered with a Selector then the close is deferred
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   706
     * until the channel is flushed from all Selectors.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   707
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   708
     * If the socket is connected and the channel is registered with a Selector
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   709
     * then the socket is shutdown for writing so that the peer reads EOF. In
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   710
     * addition, if SO_LINGER is set to a non-zero value then it is disabled so
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   711
     * that the deferred close does not wait.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   712
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   713
    private void implCloseNonBlockingMode() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   714
        boolean connected;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   715
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   716
            assert state < ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   717
            connected = (state == ST_CONNECTED);
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   718
            state = ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   719
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   720
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   721
        // wait for any read/write operations to complete
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   722
        readLock.lock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   723
        readLock.unlock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   724
        writeLock.lock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   725
        writeLock.unlock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   726
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   727
        // if the socket cannot be closed because it's registered with a Selector
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   728
        // then shutdown the socket for writing.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   729
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   730
            if (state == ST_CLOSING && !tryClose() && connected && isRegistered()) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   731
                try {
50109
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   732
                    SocketOption<Integer> opt = StandardSocketOptions.SO_LINGER;
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   733
                    int interval = (int) Net.getSocketOption(fd, Net.UNSPEC, opt);
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   734
                    if (interval != 0) {
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   735
                        if (interval > 0) {
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   736
                            // disable SO_LINGER
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   737
                            Net.setSocketOption(fd, Net.UNSPEC, opt, -1);
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   738
                        }
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   739
                        Net.shutdown(fd, Net.SHUT_WR);
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   740
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   741
                } catch (IOException ignore) { }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   742
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   743
        }
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   744
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   745
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   746
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   747
     * Invoked by implCloseChannel to close the channel.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   748
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   749
    @Override
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   750
    protected void implCloseSelectableChannel() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   751
        assert !isOpen();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   752
        if (isBlocking()) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   753
            implCloseBlockingMode();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   754
        } else {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   755
            implCloseNonBlockingMode();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   756
        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   757
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   758
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   759
    @Override
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   760
    public void kill() {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   761
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   762
            if (state == ST_CLOSING) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   763
                tryFinishClose();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   764
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   765
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   766
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   767
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   768
    /**
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   769
     * Package private version called from InheritedChannel
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   770
     */
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   771
    void localImplCloseSelectableChannel() throws IOException {
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   772
        implCloseSelectableChannel();
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   773
    }
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   774
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   775
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   776
    public SocketChannel shutdownInput() throws IOException {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   777
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   778
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   779
            if (!isConnected())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   780
                throw new NotYetConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   781
            if (!isInputClosed) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   782
                Net.shutdown(fd, Net.SHUT_RD);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   783
                long thread = readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   784
                if (thread != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   785
                    NativeThread.signal(thread);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   786
                isInputClosed = true;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   787
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   788
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   792
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   793
    public SocketChannel shutdownOutput() throws IOException {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   794
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   795
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   796
            if (!isConnected())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   797
                throw new NotYetConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   798
            if (!isOutputClosed) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   799
                Net.shutdown(fd, Net.SHUT_WR);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   800
                long thread = writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   801
                if (thread != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   802
                    NativeThread.signal(thread);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   803
                isOutputClosed = true;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   804
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   805
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   809
    boolean isInputOpen() {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   810
        return !isInputClosed;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   811
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   812
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   813
    boolean isOutputOpen() {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   814
        return !isOutputClosed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   817
    /**
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   818
     * Waits for a connection attempt to finish with a timeout
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   819
     * @throws SocketTimeoutException if the connect timeout elapses
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   820
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   821
    private boolean finishTimedConnect(long nanos) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   822
        long startNanos = System.nanoTime();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   823
        boolean polled = Net.pollConnectNow(fd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   824
        while (!polled && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   825
            long remainingNanos = nanos - (System.nanoTime() - startNanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   826
            if (remainingNanos <= 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   827
                throw new SocketTimeoutException("Connect timed out");
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   828
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   829
            park(Net.POLLOUT, remainingNanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   830
            polled = Net.pollConnectNow(fd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   831
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   832
        return polled && isOpen();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   833
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   834
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   835
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   836
     * Attempts to establish a connection to the given socket address with a
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   837
     * timeout. Closes the socket if connection cannot be established.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   838
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   839
     * @apiNote This method is for use by the socket adaptor.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   840
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   841
     * @throws IllegalBlockingModeException if the channel is non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   842
     * @throws SocketTimeoutException if the read timeout elapses
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   843
     */
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   844
    void blockingConnect(SocketAddress remote, long nanos) throws IOException {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   845
        SocketAddress sa = checkRemote(remote);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   846
        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   847
            readLock.lock();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   848
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   849
                writeLock.lock();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   850
                try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   851
                    if (!isBlocking())
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   852
                        throw new IllegalBlockingModeException();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   853
                    boolean connected = false;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   854
                    try {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   855
                        beginConnect(true, sa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   856
                        // change socket to non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   857
                        lockedConfigureBlocking(false);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   858
                        try {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   859
                            int n = connectImpl(fd, sa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   860
                            connected = (n > 0) ? true : finishTimedConnect(nanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   861
                        } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   862
                            // restore socket to blocking mode
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   863
                            lockedConfigureBlocking(true);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   864
                        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   865
                    } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   866
                        endConnect(true, connected);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   867
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   868
                } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   869
                    writeLock.unlock();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   870
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   871
            } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   872
                readLock.unlock();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   873
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   874
        } catch (IOException ioe) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   875
            // connect failed, close the channel
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   876
            close();
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   877
            throw SocketExceptions.of(ioe, sa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   878
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   879
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   880
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   881
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   882
     * Attempts to read bytes from the socket into the given byte array.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   883
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   884
    private int tryRead(byte[] b, int off, int len) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   885
        ByteBuffer dst = Util.getTemporaryDirectBuffer(len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   886
        assert dst.position() == 0;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   887
        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   888
            int n = nd.read(fd, ((DirectBuffer)dst).address(), len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   889
            if (n > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   890
                dst.get(b, off, n);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   891
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   892
            return n;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   893
        } finally{
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   894
            Util.offerFirstTemporaryDirectBuffer(dst);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   895
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   896
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   897
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   898
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   899
     * Reads bytes from the socket into the given byte array with a timeout.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   900
     * @throws SocketTimeoutException if the read timeout elapses
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   901
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   902
    private int timedRead(byte[] b, int off, int len, long nanos) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   903
        long startNanos = System.nanoTime();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   904
        int n = tryRead(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   905
        while (n == IOStatus.UNAVAILABLE && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   906
            long remainingNanos = nanos - (System.nanoTime() - startNanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   907
            if (remainingNanos <= 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   908
                throw new SocketTimeoutException("Read timed out");
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   909
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   910
            park(Net.POLLIN, remainingNanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   911
            n = tryRead(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   912
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   913
        return n;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   914
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   915
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   916
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   917
     * Reads bytes from the socket into the given byte array.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   918
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   919
     * @apiNote This method is for use by the socket adaptor.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   920
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   921
     * @throws IllegalBlockingModeException if the channel is non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   922
     * @throws SocketTimeoutException if the read timeout elapses
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   923
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   924
    int blockingRead(byte[] b, int off, int len, long nanos) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   925
        Objects.checkFromIndexSize(off, len, b.length);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   926
        if (len == 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   927
            // nothing to do
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   928
            return 0;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   929
        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   930
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   931
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   932
        try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   933
            // check that channel is configured blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   934
            if (!isBlocking())
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   935
                throw new IllegalBlockingModeException();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   936
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   937
            int n = 0;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   938
            try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   939
                beginRead(true);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   940
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   941
                // check if connection has been reset
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   942
                if (connectionReset)
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   943
                    throwConnectionReset();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   944
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   945
                // check if input is shutdown
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   946
                if (isInputClosed)
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   947
                    return IOStatus.EOF;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   948
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   949
                if (nanos > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   950
                    // change socket to non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   951
                    lockedConfigureBlocking(false);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   952
                    try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   953
                        n = timedRead(b, off, len, nanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   954
                    } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   955
                        // restore socket to blocking mode
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   956
                        lockedConfigureBlocking(true);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   957
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   958
                } else {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   959
                    // read, no timeout
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   960
                    n = tryRead(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   961
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   962
                        park(Net.POLLIN);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   963
                        n = tryRead(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   964
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   965
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   966
            } catch (ConnectionResetException e) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   967
                connectionReset = true;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   968
                throwConnectionReset();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   969
            } finally {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   970
                endRead(true, n > 0);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   971
                if (n <= 0 && isInputClosed)
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   972
                    return IOStatus.EOF;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   973
            }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   974
            assert n > 0 || n == -1;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   975
            return n;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   976
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   977
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   981
    /**
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   982
     * Attempts to write a sequence of bytes to the socket from the given
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   983
     * byte array.
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   984
     */
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   985
    private int tryWrite(byte[] b, int off, int len) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   986
        ByteBuffer src = Util.getTemporaryDirectBuffer(len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   987
        assert src.position() == 0;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   988
        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   989
            src.put(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   990
            return nd.write(fd, ((DirectBuffer)src).address(), len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   991
        } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   992
            Util.offerFirstTemporaryDirectBuffer(src);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   993
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   994
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   996
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   997
     * Writes a sequence of bytes to the socket from the given byte array.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   998
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   999
     * @apiNote This method is for use by the socket adaptor.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1000
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1001
    void blockingWriteFully(byte[] b, int off, int len) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1002
        Objects.checkFromIndexSize(off, len, b.length);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1003
        if (len == 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1004
            // nothing to do
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1005
            return;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1006
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1007
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1008
        writeLock.lock();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1009
        try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1010
            // check that channel is configured blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1011
            if (!isBlocking())
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1012
                throw new IllegalBlockingModeException();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1013
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1014
            // loop until all bytes have been written
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1015
            int pos = off;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1016
            int end = off + len;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1017
            beginWrite(true);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1018
            try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1019
                while (pos < end && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1020
                    int size = end - pos;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1021
                    int n = tryWrite(b, pos, size);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1022
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1023
                        park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1024
                        n = tryWrite(b, pos, size);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1025
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1026
                    if (n > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1027
                        pos += n;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1028
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1029
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1030
            } finally {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1031
                endWrite(true, pos >= end);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1033
        } finally {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1034
            writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /**
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1039
     * Return the number of bytes in the socket input buffer.
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1040
     */
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1041
    int available() throws IOException {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1042
        synchronized (stateLock) {
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1043
            ensureOpenAndConnected();
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1044
            if (isInputClosed) {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1045
                return 0;
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1046
            } else {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1047
                return Net.available(fd);
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1048
            }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1049
        }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1050
    }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1051
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1052
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * Translates native poll revent ops into a ready operation ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1055
    public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1056
        int intOps = ski.nioInterestOps();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1057
        int oldOps = ski.nioReadyOps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1060
        if ((ops & Net.POLLNVAL) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1067
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            newOps = intOps;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1069
            ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1073
        boolean connected = isConnected();
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1074
        if (((ops & Net.POLLIN) != 0) &&
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1075
            ((intOps & SelectionKey.OP_READ) != 0) && connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1078
        if (((ops & Net.POLLCONN) != 0) &&
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1079
            ((intOps & SelectionKey.OP_CONNECT) != 0) && isConnectionPending())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            newOps |= SelectionKey.OP_CONNECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1082
        if (((ops & Net.POLLOUT) != 0) &&
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1083
            ((intOps & SelectionKey.OP_WRITE) != 0) && connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1086
        ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1090
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1091
        return translateReadyOps(ops, ski.nioReadyOps(), ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1094
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1095
        return translateReadyOps(ops, 0, ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1101
    public int translateInterestOps(int ops) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        if ((ops & SelectionKey.OP_READ) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1104
            newOps |= Net.POLLIN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        if ((ops & SelectionKey.OP_WRITE) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1106
            newOps |= Net.POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if ((ops & SelectionKey.OP_CONNECT) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1108
            newOps |= Net.POLLCONN;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1109
        return newOps;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
  1120
    abstract String getRevealedLocalAddressAsString(SocketAddress sa);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
  1121
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1122
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23879
diff changeset
  1124
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        sb.append(this.getClass().getSuperclass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            sb.append("closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        else {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1130
            synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                case ST_UNCONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    sb.append("unconnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    break;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1135
                case ST_CONNECTIONPENDING:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    sb.append("connection-pending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                case ST_CONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                    sb.append("connected");
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1140
                    if (isInputClosed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                        sb.append(" ishut");
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1142
                    if (isOutputClosed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                        sb.append(" oshut");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                }
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
  1146
                SocketAddress addr = localAddress();
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1147
                if (addr != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                    sb.append(" local=");
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
  1149
                    sb.append(getRevealedLocalAddressAsString(addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                if (remoteAddress() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                    sb.append(" remote=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                    sb.append(remoteAddress().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
}