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