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