src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
author alanb
Sat, 13 Apr 2019 07:22:55 +0100
branchniosocketimpl-branch
changeset 57321 eef9324f94cc
parent 57310 c1fad761a86e
child 57336 766140c67efa
permissions -rw-r--r--
stateLock and closeLock need to be ReentrantLock
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
     1
/*
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
     4
 *
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    10
 *
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    15
 * accompanied this code).
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    16
 *
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    20
 *
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    23
 * questions.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    24
 */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    25
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    26
package sun.nio.ch;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    27
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    28
import java.io.FileDescriptor;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    29
import java.io.IOException;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    30
import java.io.InputStream;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    31
import java.io.OutputStream;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    32
import java.lang.invoke.MethodHandles;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    33
import java.lang.invoke.VarHandle;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    34
import java.net.InetAddress;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    35
import java.net.InetSocketAddress;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    36
import java.net.ProtocolFamily;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    37
import java.net.SocketAddress;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    38
import java.net.SocketException;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    39
import java.net.SocketImpl;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    40
import java.net.SocketOption;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    41
import java.net.SocketTimeoutException;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    42
import java.net.StandardProtocolFamily;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    43
import java.net.StandardSocketOptions;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    44
import java.net.UnknownHostException;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    45
import java.nio.ByteBuffer;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    46
import java.util.Collections;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    47
import java.util.HashSet;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    48
import java.util.Objects;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    49
import java.util.Set;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    50
import java.util.concurrent.TimeUnit;
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
    51
import java.util.concurrent.locks.Condition;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    52
import java.util.concurrent.locks.ReentrantLock;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    53
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    54
import jdk.internal.ref.CleanerFactory;
57186
997178749c87 Throw SocketException consistently after connection reset detected
alanb
parents: 57179
diff changeset
    55
import sun.net.ConnectionResetException;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    56
import sun.net.NetHooks;
57176
726630bc6a4c Cleanup
michaelm
parents: 57174
diff changeset
    57
import sun.net.PlatformSocketImpl;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    58
import sun.net.ResourceManager;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    59
import sun.net.ext.ExtendedSocketOptions;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    60
import sun.net.util.SocketExceptions;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    61
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    62
import static java.util.concurrent.TimeUnit.MILLISECONDS;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    63
import static java.util.concurrent.TimeUnit.NANOSECONDS;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    64
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    65
/**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    66
 * NIO based SocketImpl.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    67
 *
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    68
 * This implementation attempts to be compatible with legacy PlainSocketImpl,
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    69
 * including behavior and exceptions that are not specified by SocketImpl.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    70
 *
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    71
 * The underlying socket used by this SocketImpl is initially configured
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    72
 * blocking. If a connect, accept or read is attempted with a timeout then the
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    73
 * socket is changed to non-blocking mode. When in non-blocking mode, operations
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    74
 * that don't complete immediately will poll the socket.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    75
 */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    76
57176
726630bc6a4c Cleanup
michaelm
parents: 57174
diff changeset
    77
public final class NioSocketImpl extends SocketImpl implements PlatformSocketImpl {
57270
3519688a4e4d More improvements to connection reset handling
alanb
parents: 57268
diff changeset
    78
    private static final NativeDispatcher nd = new SocketDispatcher();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    79
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    80
    // The maximum number of bytes to read/write per syscall to avoid needing
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    81
    // a huge buffer from the temporary buffer cache
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    82
    private static final int MAX_BUFFER_SIZE = 128 * 1024;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    83
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    84
    // true if this is a SocketImpl for a ServerSocket
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    85
    private final boolean server;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    86
57111
a57c4dc7e2fe implConnect only requires one lock
alanb
parents: 57110
diff changeset
    87
    // Lock held when reading (also used when accepting or connecting)
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    88
    private final ReentrantLock readLock = new ReentrantLock();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    89
57111
a57c4dc7e2fe implConnect only requires one lock
alanb
parents: 57110
diff changeset
    90
    // Lock held when writing
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    91
    private final ReentrantLock writeLock = new ReentrantLock();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    92
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    93
    // The stateLock for read/changing state
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
    94
    private final ReentrantLock stateLock = new ReentrantLock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
    95
    private final Condition stateCondition = stateLock.newCondition();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    96
    private static final int ST_NEW = 0;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    97
    private static final int ST_UNCONNECTED = 1;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    98
    private static final int ST_CONNECTING = 2;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
    99
    private static final int ST_CONNECTED = 3;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   100
    private static final int ST_CLOSING = 4;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   101
    private static final int ST_CLOSED = 5;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   102
    private volatile int state;  // need stateLock to change
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   103
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   104
    // set by SocketImpl.create, protected by stateLock
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   105
    private boolean stream;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   106
    private FileDescriptorCloser closer;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   107
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   108
    // lazily set to true when the socket is configured non-blocking
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   109
    private volatile boolean nonBlocking;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   111
    // used by connect/read/write/accept, protected by stateLock
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   112
    private long readerThread;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   113
    private long writerThread;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   114
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   115
    // used when SO_REUSEADDR is emulated, protected by stateLock
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   116
    private boolean isReuseAddress;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   117
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   118
    // cached value of IPV6_TCLASS or IP_TOS socket option, protected by stateLock
57187
056911ad3ee7 Allow setTrafficClass on streams socket
alanb
parents: 57186
diff changeset
   119
    private int trafficClass;
056911ad3ee7 Allow setTrafficClass on streams socket
alanb
parents: 57186
diff changeset
   120
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   121
    // read or accept timeout in millis
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   122
    private volatile int timeout;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   123
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   124
    // flags to indicate if the connection is shutdown for input and output
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   125
    private volatile boolean isInputClosed;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   126
    private volatile boolean isOutputClosed;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   127
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   128
    // used by read to emulate legacy behavior, protected by readLock
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   129
    private boolean readEOF;
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   130
    private boolean connectionReset;
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   131
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   132
    /**
57212
28b0946d3b81 Cleanup/comments
alanb
parents: 57211
diff changeset
   133
     * Creates an instance of this SocketImpl.
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   134
     * @param server true if this is a SocketImpl for a ServerSocket
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   135
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   136
    public NioSocketImpl(boolean server) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   137
        this.server = server;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   138
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   139
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   140
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   141
     * Returns true if the socket is open.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   142
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   143
    private boolean isOpen() {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   144
        return state < ST_CLOSING;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   145
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   146
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   147
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   148
     * Throws SocketException if the socket is not open.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   149
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   150
    private void ensureOpen() throws SocketException {
57242
c37938e150b7 More cleanup of implAccept
alanb
parents: 57239
diff changeset
   151
        int state = this.state;
c37938e150b7 More cleanup of implAccept
alanb
parents: 57239
diff changeset
   152
        if (state == ST_NEW)
c37938e150b7 More cleanup of implAccept
alanb
parents: 57239
diff changeset
   153
            throw new SocketException("Socket not created");
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   154
        if (state >= ST_CLOSING)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   155
            throw new SocketException("Socket closed");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   156
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   157
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   158
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   159
     * Throws SocketException if the socket is not open and connected.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   160
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   161
    private void ensureOpenAndConnected() throws SocketException {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   162
        int state = this.state;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   163
        if (state < ST_CONNECTED)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   164
            throw new SocketException("Not connected");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   165
        if (state > ST_CONNECTED)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   166
            throw new SocketException("Socket closed");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   167
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   168
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   169
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   170
     * Disables the current thread for scheduling purposes until the socket is
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   171
     * ready for I/O, or is asynchronously closed, for up to the specified
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   172
     * waiting time.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   173
     * @throws IOException if an I/O error occurs
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   174
     */
57128
3d6cee596b33 Minor cleanup to NioSocketImpl to avoid accessing fd field without stateLock
alanb
parents: 57124
diff changeset
   175
    private void park(FileDescriptor fd, int event, long nanos) throws IOException {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   176
        long millis;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   177
        if (nanos == 0) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   178
            millis = -1;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   179
        } else {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   180
            millis = MILLISECONDS.convert(nanos, NANOSECONDS);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   181
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   182
        Net.poll(fd, event, millis);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   183
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   184
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   185
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   186
     * Disables the current thread for scheduling purposes until the socket is
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   187
     * ready for I/O or is asynchronously closed.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   188
     * @throws IOException if an I/O error occurs
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   189
     */
57128
3d6cee596b33 Minor cleanup to NioSocketImpl to avoid accessing fd field without stateLock
alanb
parents: 57124
diff changeset
   190
    private void park(FileDescriptor fd, int event) throws IOException {
3d6cee596b33 Minor cleanup to NioSocketImpl to avoid accessing fd field without stateLock
alanb
parents: 57124
diff changeset
   191
        park(fd, event, 0);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   192
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   193
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   194
    /**
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   195
     * Configures the socket to be non-blocking (if not already non-blocking)
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   196
     * @throws IOException if there is an I/O error changing the blocking mode
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   197
     */
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   198
    private void configureNonBlocking(FileDescriptor fd) throws IOException {
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   199
        if (!nonBlocking) {
57128
3d6cee596b33 Minor cleanup to NioSocketImpl to avoid accessing fd field without stateLock
alanb
parents: 57124
diff changeset
   200
            assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   201
            IOUtil.configureBlocking(fd, false);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   202
            nonBlocking = true;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   203
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   204
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   205
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   206
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   207
     * Marks the beginning of a read operation that might block.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   208
     * @throws SocketException if the socket is closed or not connected
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   209
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   210
    private FileDescriptor beginRead() throws SocketException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   211
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   212
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   213
            ensureOpenAndConnected();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   214
            readerThread = NativeThread.current();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   215
            return fd;
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   216
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   217
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   218
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   219
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   220
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   221
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   222
     * Marks the end of a read operation that may have blocked.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   223
     * @throws SocketException is the socket is closed
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   224
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   225
    private void endRead(boolean completed) throws SocketException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   226
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   227
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   228
            readerThread = 0;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   229
            int state = this.state;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   230
            if (state == ST_CLOSING)
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   231
                stateCondition.signalAll();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   232
            if (!completed && state >= ST_CLOSING)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   233
                throw new SocketException("Socket closed");
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   234
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   235
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   236
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   237
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   238
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   239
    /**
57227
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   240
     * Attempts to read bytes from the socket into the given byte array.
57124
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   241
     */
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   242
    private int tryRead(FileDescriptor fd, byte[] b, int off, int len)
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   243
        throws IOException
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   244
    {
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   245
        ByteBuffer dst = Util.getTemporaryDirectBuffer(len);
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   246
        assert dst.position() == 0;
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   247
        try {
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   248
            int n = nd.read(fd, ((DirectBuffer)dst).address(), len);
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   249
            if (n > 0) {
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   250
                dst.get(b, off, n);
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   251
            }
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   252
            return n;
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   253
        } finally{
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   254
            Util.offerFirstTemporaryDirectBuffer(dst);
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   255
        }
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   256
    }
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   257
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   258
    /**
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   259
     * Reads bytes from the socket into the given byte array with a timeout.
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   260
     * @throws SocketTimeoutException if the read timeout elapses
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   261
     */
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   262
    private int timedRead(FileDescriptor fd, byte[] b, int off, int len, long nanos)
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   263
        throws IOException
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   264
    {
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   265
        assert nonBlocking;
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   266
        long remainingNanos = nanos;
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   267
        long startNanos = System.nanoTime();
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   268
        int n;
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   269
        do {
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   270
            park(fd, Net.POLLIN, remainingNanos);
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   271
            n = tryRead(fd, b, off, len);
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   272
            if (n == IOStatus.UNAVAILABLE) {
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   273
                remainingNanos = nanos - (System.nanoTime() - startNanos);
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   274
                if (remainingNanos <= 0) {
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   275
                    throw new SocketTimeoutException("Read timed out");
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   276
                }
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   277
            }
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   278
        } while (n == IOStatus.UNAVAILABLE && isOpen());
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   279
        return n;
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   280
    }
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   281
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   282
    /**
57124
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   283
     * Reads bytes from the socket into the given byte array.
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   284
     * @return the number of bytes read or -1 at EOF
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   285
     * @throws SocketException if the socket is closed or a socket I/O error occurs
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   286
     * @throws SocketTimeoutException if the read timeout elapses
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   287
     */
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   288
    private int implRead(byte[] b, int off, int len) throws IOException {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   289
        int n = 0;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   290
        FileDescriptor fd = beginRead();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   291
        try {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   292
            if (connectionReset)
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   293
                throw new SocketException("Connection reset");
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   294
            if (isInputClosed)
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   295
                return -1;
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   296
            int timeout = this.timeout;
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   297
            if (timeout > 0)
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   298
                configureNonBlocking(fd);
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   299
            n = tryRead(fd, b, off, len);
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   300
            if (IOStatus.okayToRetry(n) && isOpen()) {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   301
                if (timeout > 0) {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   302
                    // read with timeout
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   303
                    long nanos = NANOSECONDS.convert(timeout, MILLISECONDS);
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   304
                    n = timedRead(fd, b, off, len, nanos);
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   305
                } else {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   306
                    // read, no timeout
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   307
                    do {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   308
                        park(fd, Net.POLLIN);
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   309
                        n = tryRead(fd, b, off, len);
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   310
                    } while (IOStatus.okayToRetry(n) && isOpen());
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   311
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   312
            }
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   313
            return n;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   314
        } catch (SocketTimeoutException e) {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   315
            throw e;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   316
        } catch (ConnectionResetException e) {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   317
            connectionReset = true;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   318
            throw new SocketException("Connection reset");
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   319
        } catch (IOException ioe) {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   320
            throw new SocketException(ioe.getMessage());
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   321
        } finally {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   322
            endRead(n > 0);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   323
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   324
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   325
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   326
    /**
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   327
     * Reads bytes from the socket into the given byte array.
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   328
     * @return the number of bytes read or -1 at EOF
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   329
     * @throws IndexOutOfBoundsException if the bound checks fail
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   330
     * @throws SocketException if the socket is closed or a socket I/O error occurs
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   331
     * @throws SocketTimeoutException if the read timeout elapses
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   332
     */
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   333
    private int read(byte[] b, int off, int len) throws IOException {
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   334
        Objects.checkFromIndexSize(off, len, b.length);
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   335
        if (len == 0) {
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   336
            return 0;
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   337
        } else {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   338
            readLock.lock();
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   339
            try {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   340
                // emulate legacy behavior to return -1, even if socket is closed
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   341
                if (readEOF)
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   342
                    return -1;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   343
                // read up to MAX_BUFFER_SIZE bytes
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   344
                int size = Math.min(len, MAX_BUFFER_SIZE);
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   345
                int n = implRead(b, off, size);
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   346
                if (n == -1)
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   347
                    readEOF = true;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   348
                return n;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   349
            } finally {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   350
                readLock.unlock();
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   351
            }
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   352
        }
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   353
    }
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   354
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   355
    /**
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   356
     * Marks the beginning of a write operation that might block.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   357
     * @throws SocketException if the socket is closed or not connected
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   358
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   359
    private FileDescriptor beginWrite() throws SocketException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   360
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   361
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   362
            ensureOpenAndConnected();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   363
            writerThread = NativeThread.current();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   364
            return fd;
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   365
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   366
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   367
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   368
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   369
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   370
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   371
     * Marks the end of a write operation that may have blocked.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   372
     * @throws SocketException is the socket is closed
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   373
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   374
    private void endWrite(boolean completed) throws SocketException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   375
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   376
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   377
            writerThread = 0;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   378
            int state = this.state;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   379
            if (state == ST_CLOSING)
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   380
                stateCondition.signalAll();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   381
            if (!completed && state >= ST_CLOSING)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   382
                throw new SocketException("Socket closed");
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   383
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   384
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   385
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   386
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   387
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   388
    /**
57227
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   389
     * Attempts to write a sequence of bytes to the socket from the given
57225
debeede2898e niosocketimpl-branch: minor cleanup
chegar
parents: 57222
diff changeset
   390
     * byte array.
57124
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   391
     */
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   392
    private int tryWrite(FileDescriptor fd, byte[] b, int off, int len)
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   393
        throws IOException
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   394
    {
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   395
        ByteBuffer src = Util.getTemporaryDirectBuffer(len);
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   396
        assert src.position() == 0;
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   397
        try {
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   398
            src.put(b, off, len);
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   399
            return nd.write(fd, ((DirectBuffer)src).address(), len);
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   400
        } finally {
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   401
            Util.offerFirstTemporaryDirectBuffer(src);
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   402
        }
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   403
    }
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   404
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   405
    /**
57227
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   406
     * Writes a sequence of bytes to the socket from the given byte array.
57124
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   407
     * @return the number of bytes written
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   408
     * @throws SocketException if the socket is closed or an socket I/O error occurs
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   409
     */
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   410
    private int implWrite(byte[] b, int off, int len) throws IOException {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   411
        int n = 0;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   412
        FileDescriptor fd = beginWrite();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   413
        try {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   414
            n = tryWrite(fd, b, off, len);
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   415
            while (IOStatus.okayToRetry(n) && isOpen()) {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   416
                park(fd, Net.POLLOUT);
57124
8bb7df86576a Improve read/write performance
alanb
parents: 57114
diff changeset
   417
                n = tryWrite(fd, b, off, len);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   418
            }
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   419
            return n;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   420
        } catch (IOException ioe) {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   421
            throw new SocketException(ioe.getMessage());
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   422
        } finally {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   423
            endWrite(n > 0);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   424
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   425
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   426
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   427
    /**
57227
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   428
     * Writes a sequence of bytes to the socket from the given byte array.
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   429
     * @throws SocketException if the socket is closed or an socket I/O error occurs
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   430
     */
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   431
    private void write(byte[] b, int off, int len) throws IOException {
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   432
        Objects.checkFromIndexSize(off, len, b.length);
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   433
        if (len > 0) {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   434
            writeLock.lock();
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   435
            try {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   436
                int pos = off;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   437
                int end = off + len;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   438
                while (pos < end) {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   439
                    // write up to MAX_BUFFER_SIZE bytes
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   440
                    int size = Math.min((end - pos), MAX_BUFFER_SIZE);
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   441
                    int n = implWrite(b, pos, size);
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   442
                    pos += n;
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   443
                }
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   444
            } finally {
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   445
                writeLock.unlock();
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   446
            }
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   447
        }
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   448
    }
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   449
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   450
    /**
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   451
     * Creates the socket.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   452
     * @param stream {@code true} for a streams socket
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   453
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   454
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   455
    protected void create(boolean stream) throws IOException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   456
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   457
        try {
57227
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   458
            if (state != ST_NEW)
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   459
                throw new IOException("Already created");
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   460
            if (!stream)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   461
                ResourceManager.beforeUdpCreate();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   462
            FileDescriptor fd;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   463
            try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   464
                if (server) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   465
                    assert stream;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   466
                    fd = Net.serverSocket(true);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   467
                } else {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   468
                    fd = Net.socket(stream);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   469
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   470
            } catch (IOException ioe) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   471
                if (!stream)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   472
                    ResourceManager.afterUdpClose();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   473
                throw ioe;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   474
            }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   475
            this.fd = fd;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   476
            this.stream = stream;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   477
            this.closer = FileDescriptorCloser.create(this);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   478
            this.state = ST_UNCONNECTED;
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   479
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   480
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   481
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   482
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   483
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   484
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   485
     * Marks the beginning of a connect operation that might block.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   486
     * @throws SocketException if the socket is closed or already connected
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   487
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   488
    private FileDescriptor beginConnect(InetAddress address, int port)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   489
        throws IOException
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   490
    {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   491
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   492
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   493
            int state = this.state;
57114
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   494
            if (state != ST_UNCONNECTED) {
57310
c1fad761a86e Add tests for invalid usages of SocketImpl
alanb
parents: 57304
diff changeset
   495
                if (state == ST_NEW)
c1fad761a86e Add tests for invalid usages of SocketImpl
alanb
parents: 57304
diff changeset
   496
                    throw new SocketException("Not created");
57114
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   497
                if (state == ST_CONNECTING)
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   498
                    throw new SocketException("Connection in progress");
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   499
                if (state == ST_CONNECTED)
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   500
                    throw new SocketException("Already connected");
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   501
                if (state >= ST_CLOSING)
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   502
                    throw new SocketException("Socket closed");
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   503
                assert false;
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   504
            }
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   505
            this.state = ST_CONNECTING;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   506
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   507
            // invoke beforeTcpConnect hook if not already bound
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   508
            if (localport == 0) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   509
                NetHooks.beforeTcpConnect(fd, address, port);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   510
            }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   511
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   512
            // save the remote address/port
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   513
            this.address = address;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   514
            this.port = port;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   515
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   516
            readerThread = NativeThread.current();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   517
            return fd;
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   518
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   519
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   520
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   521
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   522
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   523
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   524
     * Marks the end of a connect operation that may have blocked.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   525
     * @throws SocketException is the socket is closed
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   526
     */
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   527
    private void endConnect(FileDescriptor fd, boolean completed) throws IOException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   528
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   529
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   530
            readerThread = 0;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   531
            int state = this.state;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   532
            if (state == ST_CLOSING)
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   533
                stateCondition.signalAll();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   534
            if (completed && state == ST_CONNECTING) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   535
                this.state = ST_CONNECTED;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   536
                localport = Net.localAddress(fd).getPort();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   537
            } else if (!completed && state >= ST_CLOSING) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   538
                throw new SocketException("Socket closed");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   539
            }
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   540
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   541
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   542
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   543
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   544
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   545
    /**
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   546
     * Waits for a connection attempt to finish with a timeout
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   547
     * @throws SocketTimeoutException if the connect timeout elapses
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   548
     */
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   549
    private void timedFinishConnect(FileDescriptor fd, long nanos) throws IOException {
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   550
        long remainingNanos = nanos;
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   551
        long startNanos = System.nanoTime();
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   552
        boolean polled;
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   553
        do {
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   554
            park(fd, Net.POLLOUT, remainingNanos);
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   555
            polled = Net.pollConnectNow(fd);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   556
            if (!polled) {
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   557
                remainingNanos = nanos - (System.nanoTime() - startNanos);
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   558
                if (remainingNanos <= 0) {
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   559
                    throw new SocketTimeoutException("Connect timed out");
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   560
                }
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   561
            }
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   562
        } while (!polled && isOpen());
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   563
    }
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   564
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   565
    /**
57227
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   566
     * Attempts to establish a connection to the given socket address with a
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   567
     * timeout. Closes the socket if connection cannot be established.
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   568
     * @throws IOException if the address is not a resolved InetSocketAddress or
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   569
     *         the connection cannot be established
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   570
     */
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   571
    @Override
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   572
    protected void connect(SocketAddress remote, int millis) throws IOException {
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   573
        // SocketImpl connect only specifies IOException
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   574
        if (!(remote instanceof InetSocketAddress))
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   575
            throw new IOException("Unsupported address type");
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   576
        InetSocketAddress isa = (InetSocketAddress) remote;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   577
        if (isa.isUnresolved()) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   578
            throw new UnknownHostException(isa.getHostName());
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   579
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   580
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   581
        InetAddress address = isa.getAddress();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   582
        if (address.isAnyLocalAddress())
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   583
            address = InetAddress.getLocalHost();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   584
        int port = isa.getPort();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   585
57114
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   586
        ReentrantLock connectLock = readLock;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   587
        try {
57114
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   588
            connectLock.lock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   589
            try {
57111
a57c4dc7e2fe implConnect only requires one lock
alanb
parents: 57110
diff changeset
   590
                boolean connected = false;
a57c4dc7e2fe implConnect only requires one lock
alanb
parents: 57110
diff changeset
   591
                FileDescriptor fd = beginConnect(address, port);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   592
                try {
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   593
                    if (millis > 0)
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   594
                        configureNonBlocking(fd);
57111
a57c4dc7e2fe implConnect only requires one lock
alanb
parents: 57110
diff changeset
   595
                    int n = Net.connect(fd, address, port);
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   596
                    if (isOpen()) {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   597
                        if (n > 0) {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   598
                            // connection established
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   599
                            connected = true;
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   600
                        } else if (IOStatus.okayToRetry(n)) {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   601
                            // not established or interrupted
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   602
                            if (millis > 0) {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   603
                                // finish connect with timeout
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   604
                                long nanos = NANOSECONDS.convert(millis, MILLISECONDS);
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   605
                                timedFinishConnect(fd, nanos);
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   606
                            } else {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   607
                                // finish connect, no timeout
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   608
                                boolean polled;
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   609
                                do {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   610
                                    park(fd, Net.POLLOUT);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   611
                                    polled = Net.pollConnectNow(fd);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   612
                                } while (!polled && isOpen());
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   613
                            }
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   614
                            connected = isOpen();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   615
                        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   616
                    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   617
                } finally {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   618
                    endConnect(fd, connected);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   619
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   620
            } finally {
57114
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   621
                connectLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   622
            }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   623
        } catch (IOException ioe) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   624
            close();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   625
            throw SocketExceptions.of(ioe, isa);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   626
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   627
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   628
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   629
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   630
    protected void connect(String host, int port) throws IOException {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   631
        connect(new InetSocketAddress(host, port), 0);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   632
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   633
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   634
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   635
    protected void connect(InetAddress address, int port) throws IOException {
57294
c1126b592df9 Cleanup
alanb
parents: 57281
diff changeset
   636
        connect(new InetSocketAddress(address, port), 0);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   637
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   638
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   639
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   640
    protected void bind(InetAddress host, int port) throws IOException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   641
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   642
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   643
            ensureOpen();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   644
            if (localport != 0)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   645
                throw new SocketException("Already bound");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   646
            NetHooks.beforeTcpBind(fd, host, port);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   647
            Net.bind(fd, host, port);
57227
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   648
            // set the address field to the given host address to keep
57225
debeede2898e niosocketimpl-branch: minor cleanup
chegar
parents: 57222
diff changeset
   649
            // compatibility with PlainSocketImpl. When binding to 0.0.0.0
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   650
            // then the actual local address will be ::0 when IPv6 is enabled.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   651
            address = host;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   652
            localport = Net.localAddress(fd).getPort();
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   653
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   654
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   655
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   656
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   657
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   658
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   659
    protected void listen(int backlog) throws IOException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   660
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   661
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   662
            ensureOpen();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   663
            if (localport == 0)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   664
                throw new SocketException("Not bound");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   665
            Net.listen(fd, backlog < 1 ? 50 : backlog);
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   666
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   667
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   668
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   669
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   670
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   671
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   672
     * Marks the beginning of an accept operation that might block.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   673
     * @throws SocketException if the socket is closed
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   674
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   675
    private FileDescriptor beginAccept() throws SocketException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   676
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   677
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   678
            ensureOpen();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   679
            if (!stream)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   680
                throw new SocketException("Not a stream socket");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   681
            if (localport == 0)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   682
                throw new SocketException("Not bound");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   683
            readerThread = NativeThread.current();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   684
            return fd;
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   685
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   686
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   687
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   688
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   689
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   690
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   691
     * Marks the end of an accept operation that may have blocked.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   692
     * @throws SocketException is the socket is closed
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   693
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   694
    private void endAccept(boolean completed) throws SocketException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   695
        stateLock.lock(); 
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   696
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   697
            int state = this.state;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   698
            readerThread = 0;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   699
            if (state == ST_CLOSING)
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   700
                stateCondition.signalAll();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   701
            if (!completed && state >= ST_CLOSING)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   702
                throw new SocketException("Socket closed");
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   703
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   704
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   705
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   706
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   707
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   708
    /**
57225
debeede2898e niosocketimpl-branch: minor cleanup
chegar
parents: 57222
diff changeset
   709
     * Accepts a new connection with a timeout.
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   710
     * @throws SocketTimeoutException if the accept timeout elapses
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   711
     */
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   712
    private int timedAccept(FileDescriptor fd,
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   713
                            FileDescriptor newfd,
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   714
                            InetSocketAddress[] isaa,
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   715
                            long nanos)
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   716
        throws IOException
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   717
    {
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   718
        assert nonBlocking;
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   719
        long remainingNanos = nanos;
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   720
        long startNanos = System.nanoTime();
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   721
        int n;
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   722
        do {
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   723
            park(fd, Net.POLLIN, remainingNanos);
57268
alanb
parents: 57252
diff changeset
   724
            n = Net.accept(fd, newfd, isaa);
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   725
            if (n == IOStatus.UNAVAILABLE) {
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   726
                remainingNanos = nanos - (System.nanoTime() - startNanos);
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   727
                if (remainingNanos <= 0) {
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   728
                    throw new SocketTimeoutException("Accept timed out");
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   729
                }
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   730
            }
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   731
        } while (n == IOStatus.UNAVAILABLE && isOpen());
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   732
        return n;
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   733
    }
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
   734
57227
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   735
    /**
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   736
     * Accepts a new connection so that the given SocketImpl is connected to
57304
18e98037d493 Remove support for accepting connection with foreign impls
alanb
parents: 57299
diff changeset
   737
     * the peer. The SocketImpl must be a newly created NioSocketImpl.
57227
aebe3e4fd2d1 Change asserts to guarantees
alanb
parents: 57225
diff changeset
   738
     */
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   739
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   740
    protected void accept(SocketImpl si) throws IOException {
57304
18e98037d493 Remove support for accepting connection with foreign impls
alanb
parents: 57299
diff changeset
   741
        NioSocketImpl nsi = (NioSocketImpl) si;
57310
c1fad761a86e Add tests for invalid usages of SocketImpl
alanb
parents: 57304
diff changeset
   742
        if (nsi.state != ST_NEW)
c1fad761a86e Add tests for invalid usages of SocketImpl
alanb
parents: 57304
diff changeset
   743
            throw new SocketException("Not a newly created SocketImpl");
57304
18e98037d493 Remove support for accepting connection with foreign impls
alanb
parents: 57299
diff changeset
   744
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   745
        FileDescriptor newfd = new FileDescriptor();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   746
        InetSocketAddress[] isaa = new InetSocketAddress[1];
57114
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   747
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   748
        // acquire the lock, adjusting the timeout for cases where several
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   749
        // threads are accepting connections and there is a timeout set
57114
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   750
        ReentrantLock acceptLock = readLock;
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   751
        int timeout = this.timeout;
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   752
        long remainingNanos = 0;
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   753
        if (timeout > 0) {
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   754
            remainingNanos = tryLock(acceptLock, timeout, MILLISECONDS);
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   755
            if (remainingNanos <= 0) {
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   756
                assert !acceptLock.isHeldByCurrentThread();
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   757
                throw new SocketTimeoutException("Accept timed out");
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   758
            }
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   759
        } else {
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   760
            acceptLock.lock();
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   761
        }
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   762
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   763
        // accept a connection
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   764
        try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   765
            int n = 0;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   766
            FileDescriptor fd = beginAccept();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   767
            try {
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   768
                if (remainingNanos > 0)
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   769
                    configureNonBlocking(fd);
57268
alanb
parents: 57252
diff changeset
   770
                n = Net.accept(fd, newfd, isaa);
57114
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   771
                if (IOStatus.okayToRetry(n) && isOpen()) {
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   772
                    if (remainingNanos > 0) {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   773
                        // accept with timeout
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
   774
                        n = timedAccept(fd, newfd, isaa, remainingNanos);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   775
                    } else {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   776
                        // accept, no timeout
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   777
                        do {
57128
3d6cee596b33 Minor cleanup to NioSocketImpl to avoid accessing fd field without stateLock
alanb
parents: 57124
diff changeset
   778
                            park(fd, Net.POLLIN);
57268
alanb
parents: 57252
diff changeset
   779
                            n = Net.accept(fd, newfd, isaa);
57114
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   780
                        } while (IOStatus.okayToRetry(n) && isOpen());
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   781
                    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   782
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   783
            } finally {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   784
                endAccept(n > 0);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   785
                assert IOStatus.check(n);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   786
            }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   787
        } finally {
57114
e613cc3bc9d4 NioSocketImpl cleanup
alanb
parents: 57111
diff changeset
   788
            acceptLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   789
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   790
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   791
        // get local address and configure accepted socket to blocking mode
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   792
        InetSocketAddress localAddress;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   793
        try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   794
            localAddress = Net.localAddress(newfd);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   795
            IOUtil.configureBlocking(newfd, true);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   796
        } catch (IOException ioe) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   797
            nd.close(newfd);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   798
            throw ioe;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   799
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   800
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   801
        // set the fields
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   802
        nsi.stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   803
        try {
57304
18e98037d493 Remove support for accepting connection with foreign impls
alanb
parents: 57299
diff changeset
   804
            nsi.fd = newfd;
18e98037d493 Remove support for accepting connection with foreign impls
alanb
parents: 57299
diff changeset
   805
            nsi.stream = true;
18e98037d493 Remove support for accepting connection with foreign impls
alanb
parents: 57299
diff changeset
   806
            nsi.closer = FileDescriptorCloser.create(nsi);
18e98037d493 Remove support for accepting connection with foreign impls
alanb
parents: 57299
diff changeset
   807
            nsi.localport = localAddress.getPort();
18e98037d493 Remove support for accepting connection with foreign impls
alanb
parents: 57299
diff changeset
   808
            nsi.address = isaa[0].getAddress();
18e98037d493 Remove support for accepting connection with foreign impls
alanb
parents: 57299
diff changeset
   809
            nsi.port = isaa[0].getPort();
18e98037d493 Remove support for accepting connection with foreign impls
alanb
parents: 57299
diff changeset
   810
            nsi.state = ST_CONNECTED;
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   811
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   812
            nsi.stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   813
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   814
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   815
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   816
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   817
    protected InputStream getInputStream() {
57188
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   818
        return new InputStream() {
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   819
            @Override
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   820
            public int read() throws IOException {
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   821
                byte[] a = new byte[1];
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   822
                int n = read(a, 0, 1);
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   823
                return (n > 0) ? (a[0] & 0xff) : -1;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   824
            }
57188
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   825
            @Override
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   826
            public int read(byte[] b, int off, int len) throws IOException {
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   827
                return NioSocketImpl.this.read(b, off, len);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   828
            }
57188
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   829
            @Override
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   830
            public int available() throws IOException {
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   831
                return NioSocketImpl.this.available();
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   832
            }
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   833
            @Override
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   834
            public void close() throws IOException {
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   835
                NioSocketImpl.this.close();
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   836
            }
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   837
        };
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   838
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   839
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   840
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   841
    protected OutputStream getOutputStream() {
57188
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   842
        return new OutputStream() {
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   843
            @Override
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   844
            public void write(int b) throws IOException {
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   845
                byte[] a = new byte[]{(byte) b};
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   846
                write(a, 0, 1);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   847
            }
57188
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   848
            @Override
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   849
            public void write(byte[] b, int off, int len) throws IOException {
57211
4503441bec2e More NioSocketImpl.read/write and ServerSocket.implAccept cleanup
alanb
parents: 57207
diff changeset
   850
                NioSocketImpl.this.write(b, off, len);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   851
            }
57188
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   852
            @Override
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   853
            public void close() throws IOException {
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   854
                NioSocketImpl.this.close();
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   855
            }
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
   856
        };
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   857
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   858
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   859
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   860
    protected int available() throws IOException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   861
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   862
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   863
            ensureOpenAndConnected();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   864
            if (isInputClosed) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   865
                return 0;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   866
            } else {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   867
                return Net.available(fd);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   868
            }
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   869
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   870
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   871
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   872
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   873
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   874
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   875
     * Closes the socket, signalling and waiting for blocking I/O operations
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   876
     * to complete.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   877
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   878
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   879
    protected void close() throws IOException {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   880
        boolean interrupted = false;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   881
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   882
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   883
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   884
            int state = this.state;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   885
            if (state >= ST_CLOSING)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   886
                return;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   887
            if (state == ST_NEW) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   888
                // stillborn
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   889
                this.state = ST_CLOSED;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   890
                return;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   891
            }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   892
            this.state = ST_CLOSING;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   893
            assert fd != null && closer != null;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   894
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   895
            // shutdown output when linger interval not set
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   896
            try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   897
                var SO_LINGER = StandardSocketOptions.SO_LINGER;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   898
                if ((int) Net.getSocketOption(fd, SO_LINGER) != 0) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   899
                    Net.shutdown(fd, Net.SHUT_WR);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   900
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   901
            } catch (IOException ignore) { }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   902
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   903
            // interrupt and wait for kernel threads to complete I/O operations
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   904
            long reader = readerThread;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   905
            long writer = writerThread;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   906
            if (reader != 0 || writer != 0) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   907
                nd.preClose(fd);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   908
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   909
                if (reader != 0)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   910
                    NativeThread.signal(reader);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   911
                if (writer != 0)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   912
                    NativeThread.signal(writer);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   913
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   914
                // wait for blocking I/O operations to end
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   915
                while (readerThread != 0 || writerThread != 0) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   916
                    try {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   917
                        stateCondition.await();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   918
                    } catch (InterruptedException e) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   919
                        interrupted = true;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   920
                    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   921
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   922
            }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   923
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   924
            // close file descriptor
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   925
            try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   926
                closer.run();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   927
            } finally {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   928
                this.state = ST_CLOSED;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   929
            }
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   930
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   931
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   932
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   933
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   934
        // restore interrupt status
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   935
        if (interrupted)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   936
            Thread.currentThread().interrupt();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   937
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   938
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   939
    // the socket options supported by client and server sockets
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   940
    private static volatile Set<SocketOption<?>> clientSocketOptions;
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   941
    private static volatile Set<SocketOption<?>> serverSocketOptions;
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   942
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   943
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   944
    protected Set<SocketOption<?>> supportedOptions() {
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   945
        Set<SocketOption<?>> options = (server) ? serverSocketOptions : clientSocketOptions;
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   946
        if (options == null) {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   947
            options = new HashSet<>();
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   948
            options.add(StandardSocketOptions.SO_RCVBUF);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   949
            options.add(StandardSocketOptions.SO_REUSEADDR);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   950
            if (server) {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   951
                // IP_TOS added for server socket to maintain compatibility
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   952
                options.add(StandardSocketOptions.IP_TOS);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   953
                options.addAll(ExtendedSocketOptions.serverSocketOptions());
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   954
            } else {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   955
                options.add(StandardSocketOptions.IP_TOS);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   956
                options.add(StandardSocketOptions.SO_KEEPALIVE);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   957
                options.add(StandardSocketOptions.SO_SNDBUF);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   958
                options.add(StandardSocketOptions.SO_LINGER);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   959
                options.add(StandardSocketOptions.TCP_NODELAY);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   960
                options.addAll(ExtendedSocketOptions.clientSocketOptions());
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   961
            }
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   962
            if (Net.isReusePortAvailable())
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   963
                options.add(StandardSocketOptions.SO_REUSEPORT);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   964
            options = Collections.unmodifiableSet(options);
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   965
            if (server) {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   966
                serverSocketOptions = options;
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   967
            } else {
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   968
                clientSocketOptions = options;
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   969
            }
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   970
        }
57281
c08d024d6bf9 Improve handling of async close during connect and other cleanups
alanb
parents: 57274
diff changeset
   971
        return options;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   972
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
   973
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   974
    @Override
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   975
    protected <T> void setOption(SocketOption<T> opt, T value) throws IOException {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   976
        if (!supportedOptions().contains(opt))
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   977
            throw new UnsupportedOperationException("'" + opt + "' not supported");
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   978
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   979
        try {
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   980
            ensureOpen();
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   981
            if (opt == StandardSocketOptions.IP_TOS) {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   982
                // maps to IP_TOS or IPV6_TCLASS
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   983
                int i = (int) value;
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   984
                Net.setSocketOption(fd, family(), opt, i);
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   985
                trafficClass = i;
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   986
            } else if (opt == StandardSocketOptions.SO_REUSEADDR) {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   987
                boolean b = (boolean) value;
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   988
                if (Net.useExclusiveBind()) {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   989
                    isReuseAddress = b;
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   990
                } else {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   991
                    Net.setSocketOption(fd, opt, b);
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   992
                }
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   993
            } else {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   994
                // option does not need special handling
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   995
                Net.setSocketOption(fd, opt, value);
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   996
            }
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   997
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
   998
            stateLock.unlock();
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
   999
        }
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1000
    }
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1001
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1002
    @SuppressWarnings("unchecked")
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1003
    protected <T> T getOption(SocketOption<T> opt) throws IOException {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1004
        if (!supportedOptions().contains(opt))
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1005
            throw new UnsupportedOperationException("'" + opt + "' not supported");
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1006
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1007
        try {
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1008
            ensureOpen();
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1009
            if (opt == StandardSocketOptions.IP_TOS) {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1010
                return (T) Integer.valueOf(trafficClass);
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1011
            } else if (opt == StandardSocketOptions.SO_REUSEADDR) {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1012
                if (Net.useExclusiveBind()) {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1013
                    return (T) Boolean.valueOf(isReuseAddress);
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1014
                } else {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1015
                    return (T) Net.getSocketOption(fd, opt);
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1016
                }
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1017
            } else {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1018
                // option does not need special handling
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1019
                return (T) Net.getSocketOption(fd, opt);
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1020
            }
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1021
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1022
            stateLock.unlock();
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1023
        }
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1024
    }
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1025
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1026
    private boolean booleanValue(Object value, String desc) throws SocketException {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1027
        if (!(value instanceof Boolean))
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1028
            throw new SocketException("Bad value for " + desc);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1029
        return (boolean) value;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1030
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1031
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1032
    private int intValue(Object value, String desc) throws SocketException {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1033
        if (!(value instanceof Integer))
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1034
            throw new SocketException("Bad value for " + desc);
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1035
        return (int) value;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1036
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1037
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1038
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1039
    public void setOption(int opt, Object value) throws SocketException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1040
        stateLock.lock(); 
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1041
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1042
            ensureOpen();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1043
            try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1044
                switch (opt) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1045
                case SO_LINGER: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1046
                    // the value is "false" to disable, or linger interval to enable
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1047
                    int i;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1048
                    if (value instanceof Boolean && ((boolean) value) == false) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1049
                        i = -1;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1050
                    } else {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1051
                        i = intValue(value, "SO_LINGER");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1052
                    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1053
                    Net.setSocketOption(fd, StandardSocketOptions.SO_LINGER, i);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1054
                    break;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1055
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1056
                case SO_TIMEOUT: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1057
                    int i = intValue(value, "SO_TIMEOUT");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1058
                    if (i < 0)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1059
                        throw new IllegalArgumentException("timeout < 0");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1060
                    timeout = i;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1061
                    break;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1062
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1063
                case IP_TOS: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1064
                    int i = intValue(value, "IP_TOS");
57188
1f2101ee432d Change Socket.getInputStream/getOutputStream to return the same stream each time
alanb
parents: 57187
diff changeset
  1065
                    Net.setSocketOption(fd, family(), StandardSocketOptions.IP_TOS, i);
57187
056911ad3ee7 Allow setTrafficClass on streams socket
alanb
parents: 57186
diff changeset
  1066
                    trafficClass = i;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1067
                    break;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1068
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1069
                case TCP_NODELAY: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1070
                    boolean b = booleanValue(value, "TCP_NODELAY");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1071
                    Net.setSocketOption(fd, StandardSocketOptions.TCP_NODELAY, b);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1072
                    break;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1073
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1074
                case SO_SNDBUF: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1075
                    int i = intValue(value, "SO_SNDBUF");
57187
056911ad3ee7 Allow setTrafficClass on streams socket
alanb
parents: 57186
diff changeset
  1076
                    if (i <= 0)
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1077
                        throw new SocketException("SO_SNDBUF <= 0");
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1078
                    Net.setSocketOption(fd, StandardSocketOptions.SO_SNDBUF, i);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1079
                    break;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1080
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1081
                case SO_RCVBUF: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1082
                    int i = intValue(value, "SO_RCVBUF");
57187
056911ad3ee7 Allow setTrafficClass on streams socket
alanb
parents: 57186
diff changeset
  1083
                    if (i <= 0)
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1084
                        throw new SocketException("SO_RCVBUF <= 0");
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1085
                    Net.setSocketOption(fd, StandardSocketOptions.SO_RCVBUF, i);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1086
                    break;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1087
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1088
                case SO_KEEPALIVE: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1089
                    boolean b = booleanValue(value, "SO_KEEPALIVE");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1090
                    Net.setSocketOption(fd, StandardSocketOptions.SO_KEEPALIVE, b);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1091
                    break;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1092
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1093
                case SO_OOBINLINE: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1094
                    boolean b = booleanValue(value, "SO_OOBINLINE");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1095
                    Net.setSocketOption(fd, ExtendedSocketOption.SO_OOBINLINE, b);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1096
                    break;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1097
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1098
                case SO_REUSEADDR: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1099
                    boolean b = booleanValue(value, "SO_REUSEADDR");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1100
                    if (Net.useExclusiveBind()) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1101
                        isReuseAddress = b;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1102
                    } else {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1103
                        Net.setSocketOption(fd, StandardSocketOptions.SO_REUSEADDR, b);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1104
                    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1105
                    break;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1106
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1107
                case SO_REUSEPORT: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1108
                    if (!Net.isReusePortAvailable())
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1109
                        throw new SocketException("SO_REUSEPORT not supported");
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1110
                    boolean b = booleanValue(value, "SO_REUSEPORT");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1111
                    Net.setSocketOption(fd, StandardSocketOptions.SO_REUSEPORT, b);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1112
                    break;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1113
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1114
                default:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1115
                    throw new SocketException("Unknown option " + opt);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1116
                }
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1117
            } catch (SocketException e) {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1118
                throw e;
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1119
            } catch (IllegalArgumentException | IOException e) {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1120
                throw new SocketException(e.getMessage());
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1121
            }
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1122
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1123
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1124
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1125
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1126
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1127
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1128
    public Object getOption(int opt) throws SocketException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1129
        stateLock.lock(); 
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1130
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1131
            ensureOpen();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1132
            try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1133
                switch (opt) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1134
                case SO_TIMEOUT:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1135
                    return timeout;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1136
                case TCP_NODELAY:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1137
                    return Net.getSocketOption(fd, StandardSocketOptions.TCP_NODELAY);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1138
                case SO_OOBINLINE:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1139
                    return Net.getSocketOption(fd, ExtendedSocketOption.SO_OOBINLINE);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1140
                case SO_LINGER: {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1141
                    // return "false" when disabled, linger interval when enabled
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1142
                    int i = (int) Net.getSocketOption(fd, StandardSocketOptions.SO_LINGER);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1143
                    if (i == -1) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1144
                        return Boolean.FALSE;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1145
                    } else {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1146
                        return i;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1147
                    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1148
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1149
                case SO_REUSEADDR:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1150
                    if (Net.useExclusiveBind()) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1151
                        return isReuseAddress;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1152
                    } else {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1153
                        return Net.getSocketOption(fd, StandardSocketOptions.SO_REUSEADDR);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1154
                    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1155
                case SO_BINDADDR:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1156
                    return Net.localAddress(fd).getAddress();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1157
                case SO_SNDBUF:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1158
                    return Net.getSocketOption(fd, StandardSocketOptions.SO_SNDBUF);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1159
                case SO_RCVBUF:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1160
                    return Net.getSocketOption(fd, StandardSocketOptions.SO_RCVBUF);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1161
                case IP_TOS:
57187
056911ad3ee7 Allow setTrafficClass on streams socket
alanb
parents: 57186
diff changeset
  1162
                    return trafficClass;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1163
                case SO_KEEPALIVE:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1164
                    return Net.getSocketOption(fd, StandardSocketOptions.SO_KEEPALIVE);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1165
                case SO_REUSEPORT:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1166
                    if (!Net.isReusePortAvailable())
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1167
                        throw new SocketException("SO_REUSEPORT not supported");
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1168
                    return Net.getSocketOption(fd, StandardSocketOptions.SO_REUSEPORT);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1169
                default:
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1170
                    throw new SocketException("Unknown option " + opt);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1171
                }
57189
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1172
            } catch (SocketException e) {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1173
                throw e;
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1174
            } catch (IllegalArgumentException | IOException e) {
c56554b46dec Clean up setOption/getOption implementations
alanb
parents: 57188
diff changeset
  1175
                throw new SocketException(e.getMessage());
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1176
            }
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1177
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1178
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1179
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1180
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1181
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1182
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1183
    protected void shutdownInput() throws IOException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1184
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1185
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1186
            ensureOpenAndConnected();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1187
            if (!isInputClosed) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1188
                Net.shutdown(fd, Net.SHUT_RD);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1189
                isInputClosed = true;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1190
            }
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1191
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1192
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1193
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1194
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1195
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1196
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1197
    protected void shutdownOutput() throws IOException {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1198
        stateLock.lock();
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1199
        try {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1200
            ensureOpenAndConnected();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1201
            if (!isOutputClosed) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1202
                Net.shutdown(fd, Net.SHUT_WR);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1203
                isOutputClosed = true;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1204
            }
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1205
        } finally {
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1206
            stateLock.unlock();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1207
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1208
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1209
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1210
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1211
    protected boolean supportsUrgentData() {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1212
        return true;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1213
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1214
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1215
    @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1216
    protected void sendUrgentData(int data) throws IOException {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1217
        writeLock.lock();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1218
        try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1219
            int n = 0;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1220
            FileDescriptor fd = beginWrite();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1221
            try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1222
                do {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1223
                    n = Net.sendOOB(fd, (byte) data);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1224
                } while (n == IOStatus.INTERRUPTED && isOpen());
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1225
                if (n == IOStatus.UNAVAILABLE) {
57207
30695f27d7ea More cleanup and tests
alanb
parents: 57189
diff changeset
  1226
                    throw new SocketException("No buffer space available");
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1227
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1228
            } finally {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1229
                endWrite(n > 0);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1230
            }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1231
        } finally {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1232
            writeLock.unlock();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1233
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1234
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1235
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1236
    /**
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1237
     * A task that closes a SocketImpl's file descriptor. The task runs when the
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1238
     * SocketImpl is explicitly closed and when the SocketImpl becomes phantom
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1239
     * reachable.
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1240
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1241
    private static class FileDescriptorCloser implements Runnable {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1242
        private static final VarHandle CLOSED;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1243
        static {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1244
            try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1245
                MethodHandles.Lookup l = MethodHandles.lookup();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1246
                CLOSED = l.findVarHandle(FileDescriptorCloser.class,
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1247
                                         "closed",
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1248
                                         boolean.class);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1249
            } catch (Exception e) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1250
                throw new InternalError(e);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1251
            }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1252
        }
57176
726630bc6a4c Cleanup
michaelm
parents: 57174
diff changeset
  1253
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1254
        private final FileDescriptor fd;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1255
        private final boolean stream;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1256
        private volatile boolean closed;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1257
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1258
        FileDescriptorCloser(FileDescriptor fd, boolean stream) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1259
            this.fd = fd;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1260
            this.stream = stream;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1261
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1262
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1263
        static FileDescriptorCloser create(NioSocketImpl impl) {
57321
eef9324f94cc stateLock and closeLock need to be ReentrantLock
alanb
parents: 57310
diff changeset
  1264
            assert impl.stateLock.isHeldByCurrentThread();
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1265
            var closer = new FileDescriptorCloser(impl.fd, impl.stream);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1266
            CleanerFactory.cleaner().register(impl, closer);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1267
            return closer;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1268
        }
57176
726630bc6a4c Cleanup
michaelm
parents: 57174
diff changeset
  1269
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1270
        @Override
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1271
        public void run() {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1272
            if (CLOSED.compareAndSet(this, false, true)) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1273
                try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1274
                    nd.close(fd);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1275
                } catch (IOException ioe) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1276
                    throw new RuntimeException(ioe);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1277
                } finally {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1278
                    if (!stream) {
57176
726630bc6a4c Cleanup
michaelm
parents: 57174
diff changeset
  1279
                        // decrement
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1280
                        ResourceManager.afterUdpClose();
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1281
                    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1282
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1283
            }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1284
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1285
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1286
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1287
    /**
57299
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1288
     * Attempts to acquire the given lock within the given waiting time.
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1289
     * @return the remaining time in nanoseconds when the lock is acquired, zero
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1290
     *         or less if the lock was not acquired before the timeout expired
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1291
     */
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1292
    private static long tryLock(ReentrantLock lock, long timeout, TimeUnit unit) {
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1293
        assert timeout > 0;
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1294
        boolean interrupted = false;
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1295
        long nanos = NANOSECONDS.convert(timeout, unit);
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1296
        long remainingNanos = nanos;
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1297
        long startNanos = System.nanoTime();
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1298
        boolean acquired = false;
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1299
        while (!acquired && (remainingNanos > 0)) {
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1300
            try {
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1301
                acquired = lock.tryLock(remainingNanos, NANOSECONDS);
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1302
            } catch (InterruptedException e) {
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1303
                interrupted = true;
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1304
            }
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1305
            remainingNanos = nanos - (System.nanoTime() - startNanos);
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1306
        }
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1307
        if (acquired && remainingNanos <= 0L)
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1308
            lock.unlock();  // release lock if timeout has expired
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1309
        if (interrupted)
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1310
            Thread.currentThread().interrupt();
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1311
        return remainingNanos;
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1312
    }
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1313
00d475a13e29 Adjust timeout when several threads are doing timed accepts at around the same time
alanb
parents: 57294
diff changeset
  1314
    /**
57225
debeede2898e niosocketimpl-branch: minor cleanup
chegar
parents: 57222
diff changeset
  1315
     * Returns the socket protocol family.
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1316
     */
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1317
    private static ProtocolFamily family() {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1318
        if (Net.isIPv6Available()) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1319
            return StandardProtocolFamily.INET6;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1320
        } else {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1321
            return StandardProtocolFamily.INET;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1322
        }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1323
    }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents:
diff changeset
  1324
}