src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
author alanb
Sun, 27 Oct 2019 12:13:51 +0000
changeset 58808 9261ad32cba9
parent 55102 59567035d279
permissions -rw-r--r--
8212132: (dc) Remove DatagramChannelImpl finalize method Reviewed-by: bpb, chegar, dfuchs, martin
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;
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
    33
import java.lang.ref.Cleaner.Cleanable;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    34
import java.net.InetAddress;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    35
import java.net.InetSocketAddress;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    36
import java.net.ProtocolFamily;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    37
import java.net.SocketAddress;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    38
import java.net.SocketException;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    39
import java.net.SocketImpl;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    40
import java.net.SocketOption;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    41
import java.net.SocketTimeoutException;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    42
import java.net.StandardProtocolFamily;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    43
import java.net.StandardSocketOptions;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    44
import java.net.UnknownHostException;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    45
import java.nio.ByteBuffer;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    46
import java.util.Collections;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    47
import java.util.HashSet;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    48
import java.util.Objects;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    49
import java.util.Set;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    50
import java.util.concurrent.TimeUnit;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    51
import java.util.concurrent.locks.ReentrantLock;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    52
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    53
import jdk.internal.ref.CleanerFactory;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    54
import sun.net.ConnectionResetException;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    55
import sun.net.NetHooks;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    56
import sun.net.PlatformSocketImpl;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    57
import sun.net.ResourceManager;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    58
import sun.net.ext.ExtendedSocketOptions;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    59
import sun.net.util.SocketExceptions;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    60
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    61
import static java.util.concurrent.TimeUnit.MILLISECONDS;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    62
import static java.util.concurrent.TimeUnit.NANOSECONDS;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    63
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    64
/**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    65
 * NIO based SocketImpl.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    66
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    67
 * This implementation attempts to be compatible with legacy PlainSocketImpl,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    68
 * including behavior and exceptions that are not specified by SocketImpl.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    69
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    70
 * The underlying socket used by this SocketImpl is initially configured
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    71
 * blocking. If the connect method is used to establish a connection with a
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    72
 * timeout then the socket is configured non-blocking for the connect attempt,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    73
 * and then restored to blocking mode when the connection is established.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    74
 * 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
    75
 * configured non-blocking and is never restored. When in non-blocking mode,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    76
 * operations that don't complete immediately will poll the socket and preserve
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    77
 * the semantics of blocking operations.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    78
 */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    79
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    80
public final class NioSocketImpl extends SocketImpl implements PlatformSocketImpl {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    81
    private static final NativeDispatcher nd = new SocketDispatcher();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    82
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    83
    // The maximum number of bytes to read/write per syscall to avoid needing
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    84
    // a huge buffer from the temporary buffer cache
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    85
    private static final int MAX_BUFFER_SIZE = 128 * 1024;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    86
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    87
    // true if this is a SocketImpl for a ServerSocket
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    88
    private final boolean server;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    89
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    90
    // Lock held when reading (also used when accepting or connecting)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    91
    private final ReentrantLock readLock = new ReentrantLock();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    92
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    93
    // Lock held when writing
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    94
    private final ReentrantLock writeLock = new ReentrantLock();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    95
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    96
    // The stateLock for read/changing state
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    97
    private final Object stateLock = new Object();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    98
    private static final int ST_NEW = 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    99
    private static final int ST_UNCONNECTED = 1;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   100
    private static final int ST_CONNECTING = 2;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   101
    private static final int ST_CONNECTED = 3;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   102
    private static final int ST_CLOSING = 4;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   103
    private static final int ST_CLOSED = 5;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   104
    private volatile int state;  // need stateLock to change
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   105
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   106
    // set by SocketImpl.create, protected by stateLock
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   107
    private boolean stream;
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
   108
    private Cleanable cleaner;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   109
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   110
    // set to true when the socket is in non-blocking mode
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   111
    private volatile boolean nonBlocking;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   112
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   113
    // used by connect/read/write/accept, protected by stateLock
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   114
    private long readerThread;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   115
    private long writerThread;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   116
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   117
    // used when SO_REUSEADDR is emulated, protected by stateLock
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   118
    private boolean isReuseAddress;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   119
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   120
    // read or accept timeout in millis
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   121
    private volatile int timeout;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   122
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   123
    // flags to indicate if the connection is shutdown for input and output
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   124
    private volatile boolean isInputClosed;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   125
    private volatile boolean isOutputClosed;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   126
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   127
    // used by read to emulate legacy behavior, protected by readLock
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   128
    private boolean readEOF;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   129
    private boolean connectionReset;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   130
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   131
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   132
     * Creates an instance of this SocketImpl.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   133
     * @param server true if this is a SocketImpl for a ServerSocket
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   134
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   135
    public NioSocketImpl(boolean server) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   136
        this.server = server;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   137
    }
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
     * Returns true if the socket is open.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   141
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   142
    private boolean isOpen() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   143
        return state < ST_CLOSING;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   144
    }
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
     * Throws SocketException if the socket is not open.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   148
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   149
    private void ensureOpen() throws SocketException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   150
        int state = this.state;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   151
        if (state == ST_NEW)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   152
            throw new SocketException("Socket not created");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   153
        if (state >= ST_CLOSING)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   154
            throw new SocketException("Socket closed");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   155
    }
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
     * Throws SocketException if the socket is not open and connected.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   159
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   160
    private void ensureOpenAndConnected() throws SocketException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   161
        int state = this.state;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   162
        if (state < ST_CONNECTED)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   163
            throw new SocketException("Not connected");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   164
        if (state > ST_CONNECTED)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   165
            throw new SocketException("Socket closed");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   166
    }
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
     * Disables the current thread for scheduling purposes until the socket is
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   170
     * ready for I/O, or is asynchronously closed, for up to the specified
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   171
     * waiting time.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   172
     * @throws IOException if an I/O error occurs
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   173
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   174
    private void park(FileDescriptor fd, int event, long nanos) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   175
        long millis;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   176
        if (nanos == 0) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   177
            millis = -1;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   178
        } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   179
            millis = NANOSECONDS.toMillis(nanos);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   180
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   181
        Net.poll(fd, event, millis);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   182
    }
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
     * Disables the current thread for scheduling purposes until the socket is
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   186
     * ready for I/O or is asynchronously closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   187
     * @throws IOException if an I/O error occurs
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   188
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   189
    private void park(FileDescriptor fd, int event) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   190
        park(fd, event, 0);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   191
    }
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
     * 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
   195
     * socket is already in blocking mode.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   196
     * @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
   197
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   198
    private void configureBlocking(FileDescriptor fd) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   199
        assert readLock.isHeldByCurrentThread();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   200
        if (nonBlocking) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   201
            synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   202
                ensureOpen();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   203
                IOUtil.configureBlocking(fd, true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   204
                nonBlocking = false;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   205
            }
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
     * 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
   211
     * socket is already in non-blocking mode.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   212
     * @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
   213
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   214
    private void configureNonBlocking(FileDescriptor fd) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   215
        assert readLock.isHeldByCurrentThread();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   216
        if (!nonBlocking) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   217
            synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   218
                ensureOpen();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   219
                IOUtil.configureBlocking(fd, false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   220
                nonBlocking = true;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   221
            }
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
     * Marks the beginning of a read operation that might block.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   227
     * @throws SocketException if the socket is closed or not connected
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   228
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   229
    private FileDescriptor beginRead() throws SocketException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   230
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   231
            ensureOpenAndConnected();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   232
            readerThread = NativeThread.current();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   233
            return fd;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   234
        }
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
     * Marks the end of a read operation that may have blocked.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   239
     * @throws SocketException is the socket is closed
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   240
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   241
    private void endRead(boolean completed) throws SocketException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   242
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   243
            readerThread = 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   244
            int state = this.state;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   245
            if (state == ST_CLOSING)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   246
                tryFinishClose();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   247
            if (!completed && state >= ST_CLOSING)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   248
                throw new SocketException("Socket closed");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   249
        }
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
     * Attempts to read bytes from the socket into the given byte array.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   254
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   255
    private int tryRead(FileDescriptor fd, byte[] b, int off, int len)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   256
        throws IOException
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   257
    {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   258
        ByteBuffer dst = Util.getTemporaryDirectBuffer(len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   259
        assert dst.position() == 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   260
        try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   261
            int n = nd.read(fd, ((DirectBuffer)dst).address(), len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   262
            if (n > 0) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   263
                dst.get(b, off, n);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   264
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   265
            return n;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   266
        } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   267
            Util.offerFirstTemporaryDirectBuffer(dst);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   268
        }
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
     * Reads bytes from the socket into the given byte array with a timeout.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   273
     * @throws SocketTimeoutException if the read timeout elapses
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   274
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   275
    private int timedRead(FileDescriptor fd, byte[] b, int off, int len, long nanos)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   276
        throws IOException
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   277
    {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   278
        long startNanos = System.nanoTime();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   279
        int n = tryRead(fd, b, off, len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   280
        while (n == IOStatus.UNAVAILABLE && isOpen()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   281
            long remainingNanos = nanos - (System.nanoTime() - startNanos);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   282
            if (remainingNanos <= 0) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   283
                throw new SocketTimeoutException("Read timed out");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   284
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   285
            park(fd, Net.POLLIN, remainingNanos);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   286
            n = tryRead(fd, b, off, len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   287
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   288
        return n;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   289
    }
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
     * Reads bytes from the socket into the given byte array.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   293
     * @return the number of bytes read or -1 at EOF
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   294
     * @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
   295
     * @throws SocketTimeoutException if the read timeout elapses
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   296
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   297
    private int implRead(byte[] b, int off, int len) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   298
        int n = 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   299
        FileDescriptor fd = beginRead();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   300
        try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   301
            if (connectionReset)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   302
                throw new SocketException("Connection reset");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   303
            if (isInputClosed)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   304
                return -1;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   305
            int timeout = this.timeout;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   306
            if (timeout > 0) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   307
                // read with timeout
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   308
                configureNonBlocking(fd);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   309
                n = timedRead(fd, b, off, len, MILLISECONDS.toNanos(timeout));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   310
            } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   311
                // read, no timeout
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   312
                n = tryRead(fd, b, off, len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   313
                while (IOStatus.okayToRetry(n) && isOpen()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   314
                    park(fd, Net.POLLIN);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   315
                    n = tryRead(fd, b, off, len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   316
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   317
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   318
            return n;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   319
        } catch (SocketTimeoutException e) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   320
            throw e;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   321
        } catch (ConnectionResetException e) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   322
            connectionReset = true;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   323
            throw new SocketException("Connection reset");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   324
        } catch (IOException ioe) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   325
            throw new SocketException(ioe.getMessage());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   326
        } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   327
            endRead(n > 0);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   328
        }
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
     * Reads bytes from the socket into the given byte array.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   333
     * @return the number of bytes read or -1 at EOF
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   334
     * @throws IndexOutOfBoundsException if the bound checks fail
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   335
     * @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
   336
     * @throws SocketTimeoutException if the read timeout elapses
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   337
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   338
    private int read(byte[] b, int off, int len) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   339
        Objects.checkFromIndexSize(off, len, b.length);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   340
        if (len == 0) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   341
            return 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   342
        } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   343
            readLock.lock();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   344
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   345
                // emulate legacy behavior to return -1, even if socket is closed
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   346
                if (readEOF)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   347
                    return -1;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   348
                // read up to MAX_BUFFER_SIZE bytes
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   349
                int size = Math.min(len, MAX_BUFFER_SIZE);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   350
                int n = implRead(b, off, size);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   351
                if (n == -1)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   352
                    readEOF = true;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   353
                return n;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   354
            } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   355
                readLock.unlock();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   356
            }
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
     * Marks the beginning of a write operation that might block.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   362
     * @throws SocketException if the socket is closed or not connected
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   363
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   364
    private FileDescriptor beginWrite() throws SocketException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   365
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   366
            ensureOpenAndConnected();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   367
            writerThread = NativeThread.current();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   368
            return fd;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   369
        }
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
     * Marks the end of a write operation that may have blocked.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   374
     * @throws SocketException is the socket is closed
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   375
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   376
    private void endWrite(boolean completed) throws SocketException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   377
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   378
            writerThread = 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   379
            int state = this.state;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   380
            if (state == ST_CLOSING)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   381
                tryFinishClose();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   382
            if (!completed && state >= ST_CLOSING)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   383
                throw new SocketException("Socket closed");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   384
        }
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
     * Attempts to write a sequence of bytes to the socket from the given
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   389
     * byte array.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   390
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   391
    private int tryWrite(FileDescriptor fd, byte[] b, int off, int len)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   392
        throws IOException
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   393
    {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   394
        ByteBuffer src = Util.getTemporaryDirectBuffer(len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   395
        assert src.position() == 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   396
        try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   397
            src.put(b, off, len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   398
            return nd.write(fd, ((DirectBuffer)src).address(), len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   399
        } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   400
            Util.offerFirstTemporaryDirectBuffer(src);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   401
        }
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
     * Writes a sequence of bytes to the socket from the given byte array.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   406
     * @return the number of bytes written
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   407
     * @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
   408
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   409
    private int implWrite(byte[] b, int off, int len) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   410
        int n = 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   411
        FileDescriptor fd = beginWrite();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   412
        try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   413
            n = tryWrite(fd, b, off, len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   414
            while (IOStatus.okayToRetry(n) && isOpen()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   415
                park(fd, Net.POLLOUT);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   416
                n = tryWrite(fd, b, off, len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   417
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   418
            return n;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   419
        } catch (IOException ioe) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   420
            throw new SocketException(ioe.getMessage());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   421
        } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   422
            endWrite(n > 0);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   423
        }
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
     * Writes a sequence of bytes to the socket from the given byte array.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   428
     * @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
   429
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   430
    private void write(byte[] b, int off, int len) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   431
        Objects.checkFromIndexSize(off, len, b.length);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   432
        if (len > 0) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   433
            writeLock.lock();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   434
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   435
                int pos = off;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   436
                int end = off + len;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   437
                while (pos < end) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   438
                    // write up to MAX_BUFFER_SIZE bytes
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   439
                    int size = Math.min((end - pos), MAX_BUFFER_SIZE);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   440
                    int n = implWrite(b, pos, size);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   441
                    pos += n;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   442
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   443
            } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   444
                writeLock.unlock();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   445
            }
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
     * Creates the socket.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   451
     * @param stream {@code true} for a streams socket
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   452
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   453
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   454
    protected void create(boolean stream) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   455
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   456
            if (state != ST_NEW)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   457
                throw new IOException("Already created");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   458
            if (!stream)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   459
                ResourceManager.beforeUdpCreate();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   460
            FileDescriptor fd;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   461
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   462
                if (server) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   463
                    assert stream;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   464
                    fd = Net.serverSocket(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   465
                } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   466
                    fd = Net.socket(stream);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   467
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   468
            } catch (IOException ioe) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   469
                if (!stream)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   470
                    ResourceManager.afterUdpClose();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   471
                throw ioe;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   472
            }
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
   473
            Runnable closer = closerFor(fd, stream);
55102
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;
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
   476
            this.cleaner = CleanerFactory.cleaner().register(this, closer);
55102
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
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
   780
        Runnable closer = closerFor(newfd, true);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   781
        synchronized (nsi.stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   782
            nsi.fd = newfd;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   783
            nsi.stream = true;
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
   784
            nsi.cleaner = CleanerFactory.cleaner().register(nsi, closer);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   785
            nsi.localport = localAddress.getPort();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   786
            nsi.address = isaa[0].getAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   787
            nsi.port = isaa[0].getPort();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   788
            nsi.state = ST_CONNECTED;
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   792
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   793
    protected InputStream getInputStream() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   794
        return new InputStream() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   795
            @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   796
            public int read() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   797
                byte[] a = new byte[1];
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   798
                int n = read(a, 0, 1);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   799
                return (n > 0) ? (a[0] & 0xff) : -1;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   800
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   801
            @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   802
            public int read(byte[] b, int off, int len) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   803
                return NioSocketImpl.this.read(b, off, len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   804
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   805
            @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   806
            public int available() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   807
                return NioSocketImpl.this.available();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   808
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   809
            @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   810
            public void close() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   811
                NioSocketImpl.this.close();
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   816
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   817
    protected OutputStream getOutputStream() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   818
        return new OutputStream() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   819
            @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   820
            public void write(int b) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   821
                byte[] a = new byte[]{(byte) b};
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   822
                write(a, 0, 1);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   823
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   824
            @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   825
            public void write(byte[] b, int off, int len) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   826
                NioSocketImpl.this.write(b, off, len);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   827
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   828
            @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   829
            public void close() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   830
                NioSocketImpl.this.close();
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   835
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   836
    protected int available() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   837
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   838
            ensureOpenAndConnected();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   839
            if (isInputClosed) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   840
                return 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   841
            } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   842
                return Net.available(fd);
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
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   848
     * Closes the socket if there are no I/O operations in progress.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   849
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   850
    private boolean tryClose() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   851
        assert Thread.holdsLock(stateLock) && state == ST_CLOSING;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   852
        if (readerThread == 0 && writerThread == 0) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   853
            try {
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
   854
                cleaner.clean();
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   855
            } catch (UncheckedIOException ioe) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   856
                throw ioe.getCause();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   857
            } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   858
                state = ST_CLOSED;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   859
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   860
            return true;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   861
        } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   862
            return false;
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
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   867
     * Invokes tryClose to attempt to close the socket.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   868
     *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   869
     * This method is used for deferred closing by I/O operations.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   870
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   871
    private void tryFinishClose() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   872
        try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   873
            tryClose();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   874
        } catch (IOException ignore) { }
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
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   878
     * Closes the socket. If there are I/O operations in progress then the
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   879
     * socket is pre-closed and the threads are signalled. The socket will be
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   880
     * closed when the last I/O operation aborts.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   881
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   882
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   883
    protected void close() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   884
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   885
            int state = this.state;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   886
            if (state >= ST_CLOSING)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   887
                return;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   888
            if (state == ST_NEW) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   889
                // stillborn
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   890
                this.state = ST_CLOSED;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   891
                return;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   892
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   893
            this.state = ST_CLOSING;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   894
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   895
            // shutdown output when linger interval not set to 0
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   896
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   897
                var SO_LINGER = StandardSocketOptions.SO_LINGER;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   898
                if ((int) Net.getSocketOption(fd, SO_LINGER) != 0) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   899
                    Net.shutdown(fd, Net.SHUT_WR);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   900
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   901
            } catch (IOException ignore) { }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   902
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   903
            // attempt to close the socket. If there are I/O operations in progress
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   904
            // then the socket is pre-closed and the thread(s) signalled. The
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   905
            // last thread will close the file descriptor.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   906
            if (!tryClose()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   907
                nd.preClose(fd);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   908
                long reader = readerThread;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   909
                if (reader != 0)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   910
                    NativeThread.signal(reader);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   911
                long writer = writerThread;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   912
                if (writer != 0)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   913
                    NativeThread.signal(writer);
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   918
    // the socket options supported by client and server sockets
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   919
    private static volatile Set<SocketOption<?>> clientSocketOptions;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   920
    private static volatile Set<SocketOption<?>> serverSocketOptions;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   921
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   922
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   923
    protected Set<SocketOption<?>> supportedOptions() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   924
        Set<SocketOption<?>> options = (server) ? serverSocketOptions : clientSocketOptions;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   925
        if (options == null) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   926
            options = new HashSet<>();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   927
            options.add(StandardSocketOptions.SO_RCVBUF);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   928
            options.add(StandardSocketOptions.SO_REUSEADDR);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   929
            if (server) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   930
                // IP_TOS added for server socket to maintain compatibility
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   931
                options.add(StandardSocketOptions.IP_TOS);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   932
                options.addAll(ExtendedSocketOptions.serverSocketOptions());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   933
            } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   934
                options.add(StandardSocketOptions.IP_TOS);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   935
                options.add(StandardSocketOptions.SO_KEEPALIVE);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   936
                options.add(StandardSocketOptions.SO_SNDBUF);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   937
                options.add(StandardSocketOptions.SO_LINGER);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   938
                options.add(StandardSocketOptions.TCP_NODELAY);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   939
                options.addAll(ExtendedSocketOptions.clientSocketOptions());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   940
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   941
            if (Net.isReusePortAvailable())
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   942
                options.add(StandardSocketOptions.SO_REUSEPORT);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   943
            options = Collections.unmodifiableSet(options);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   944
            if (server) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   945
                serverSocketOptions = options;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   946
            } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   947
                clientSocketOptions = options;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   948
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   949
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   950
        return options;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   951
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   952
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   953
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   954
    protected <T> void setOption(SocketOption<T> opt, T value) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   955
        if (!supportedOptions().contains(opt))
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   956
            throw new UnsupportedOperationException("'" + opt + "' not supported");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   957
        if (!opt.type().isInstance(value))
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   958
            throw new IllegalArgumentException("Invalid value '" + value + "'");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   959
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   960
            ensureOpen();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   961
            if (opt == StandardSocketOptions.IP_TOS) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   962
                // maps to IP_TOS or IPV6_TCLASS
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   963
                Net.setSocketOption(fd, family(), opt, value);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   964
            } else if (opt == StandardSocketOptions.SO_REUSEADDR) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   965
                boolean b = (boolean) value;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   966
                if (Net.useExclusiveBind()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   967
                    isReuseAddress = b;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   968
                } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   969
                    Net.setSocketOption(fd, opt, b);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   970
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   971
            } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   972
                // option does not need special handling
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   973
                Net.setSocketOption(fd, opt, value);
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   978
    @SuppressWarnings("unchecked")
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   979
    protected <T> T getOption(SocketOption<T> opt) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   980
        if (!supportedOptions().contains(opt))
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   981
            throw new UnsupportedOperationException("'" + opt + "' not supported");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   982
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   983
            ensureOpen();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   984
            if (opt == StandardSocketOptions.IP_TOS) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   985
                return (T) Net.getSocketOption(fd, family(), opt);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   986
            } else if (opt == StandardSocketOptions.SO_REUSEADDR) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   987
                if (Net.useExclusiveBind()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   988
                    return (T) Boolean.valueOf(isReuseAddress);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   989
                } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   990
                    return (T) Net.getSocketOption(fd, opt);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   991
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   992
            } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   993
                // option does not need special handling
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   994
                return (T) Net.getSocketOption(fd, opt);
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   999
    private boolean booleanValue(Object value, String desc) throws SocketException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1000
        if (!(value instanceof Boolean))
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1001
            throw new SocketException("Bad value for " + desc);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1002
        return (boolean) value;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1003
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1004
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1005
    private int intValue(Object value, String desc) throws SocketException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1006
        if (!(value instanceof Integer))
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1007
            throw new SocketException("Bad value for " + desc);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1008
        return (int) value;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1009
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1010
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1011
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1012
    public void setOption(int opt, Object value) throws SocketException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1013
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1014
            ensureOpen();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1015
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1016
                switch (opt) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1017
                case SO_LINGER: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1018
                    // the value is "false" to disable, or linger interval to enable
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1019
                    int i;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1020
                    if (value instanceof Boolean && ((boolean) value) == false) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1021
                        i = -1;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1022
                    } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1023
                        i = intValue(value, "SO_LINGER");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1024
                    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1025
                    Net.setSocketOption(fd, StandardSocketOptions.SO_LINGER, i);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1026
                    break;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1027
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1028
                case SO_TIMEOUT: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1029
                    int i = intValue(value, "SO_TIMEOUT");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1030
                    if (i < 0)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1031
                        throw new IllegalArgumentException("timeout < 0");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1032
                    timeout = i;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1033
                    break;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1034
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1035
                case IP_TOS: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1036
                    int i = intValue(value, "IP_TOS");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1037
                    Net.setSocketOption(fd, family(), StandardSocketOptions.IP_TOS, i);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1038
                    break;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1039
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1040
                case TCP_NODELAY: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1041
                    boolean b = booleanValue(value, "TCP_NODELAY");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1042
                    Net.setSocketOption(fd, StandardSocketOptions.TCP_NODELAY, b);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1043
                    break;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1044
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1045
                case SO_SNDBUF: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1046
                    int i = intValue(value, "SO_SNDBUF");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1047
                    if (i <= 0)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1048
                        throw new SocketException("SO_SNDBUF <= 0");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1049
                    Net.setSocketOption(fd, StandardSocketOptions.SO_SNDBUF, i);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1050
                    break;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1051
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1052
                case SO_RCVBUF: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1053
                    int i = intValue(value, "SO_RCVBUF");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1054
                    if (i <= 0)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1055
                        throw new SocketException("SO_RCVBUF <= 0");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1056
                    Net.setSocketOption(fd, StandardSocketOptions.SO_RCVBUF, i);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1057
                    break;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1058
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1059
                case SO_KEEPALIVE: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1060
                    boolean b = booleanValue(value, "SO_KEEPALIVE");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1061
                    Net.setSocketOption(fd, StandardSocketOptions.SO_KEEPALIVE, b);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1062
                    break;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1063
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1064
                case SO_OOBINLINE: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1065
                    boolean b = booleanValue(value, "SO_OOBINLINE");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1066
                    Net.setSocketOption(fd, ExtendedSocketOption.SO_OOBINLINE, b);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1067
                    break;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1068
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1069
                case SO_REUSEADDR: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1070
                    boolean b = booleanValue(value, "SO_REUSEADDR");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1071
                    if (Net.useExclusiveBind()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1072
                        isReuseAddress = b;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1073
                    } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1074
                        Net.setSocketOption(fd, StandardSocketOptions.SO_REUSEADDR, b);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1075
                    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1076
                    break;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1077
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1078
                case SO_REUSEPORT: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1079
                    if (!Net.isReusePortAvailable())
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1080
                        throw new SocketException("SO_REUSEPORT not supported");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1081
                    boolean b = booleanValue(value, "SO_REUSEPORT");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1082
                    Net.setSocketOption(fd, StandardSocketOptions.SO_REUSEPORT, b);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1083
                    break;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1084
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1085
                default:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1086
                    throw new SocketException("Unknown option " + opt);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1087
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1088
            } catch (SocketException e) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1089
                throw e;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1090
            } catch (IllegalArgumentException | IOException e) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1091
                throw new SocketException(e.getMessage());
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1096
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1097
    public Object getOption(int opt) throws SocketException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1098
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1099
            ensureOpen();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1100
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1101
                switch (opt) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1102
                case SO_TIMEOUT:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1103
                    return timeout;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1104
                case TCP_NODELAY:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1105
                    return Net.getSocketOption(fd, StandardSocketOptions.TCP_NODELAY);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1106
                case SO_OOBINLINE:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1107
                    return Net.getSocketOption(fd, ExtendedSocketOption.SO_OOBINLINE);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1108
                case SO_LINGER: {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1109
                    // return "false" when disabled, linger interval when enabled
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1110
                    int i = (int) Net.getSocketOption(fd, StandardSocketOptions.SO_LINGER);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1111
                    if (i == -1) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1112
                        return Boolean.FALSE;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1113
                    } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1114
                        return i;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1115
                    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1116
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1117
                case SO_REUSEADDR:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1118
                    if (Net.useExclusiveBind()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1119
                        return isReuseAddress;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1120
                    } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1121
                        return Net.getSocketOption(fd, StandardSocketOptions.SO_REUSEADDR);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1122
                    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1123
                case SO_BINDADDR:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1124
                    return Net.localAddress(fd).getAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1125
                case SO_SNDBUF:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1126
                    return Net.getSocketOption(fd, StandardSocketOptions.SO_SNDBUF);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1127
                case SO_RCVBUF:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1128
                    return Net.getSocketOption(fd, StandardSocketOptions.SO_RCVBUF);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1129
                case IP_TOS:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1130
                    return Net.getSocketOption(fd, family(), StandardSocketOptions.IP_TOS);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1131
                case SO_KEEPALIVE:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1132
                    return Net.getSocketOption(fd, StandardSocketOptions.SO_KEEPALIVE);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1133
                case SO_REUSEPORT:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1134
                    if (!Net.isReusePortAvailable())
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1135
                        throw new SocketException("SO_REUSEPORT not supported");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1136
                    return Net.getSocketOption(fd, StandardSocketOptions.SO_REUSEPORT);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1137
                default:
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1138
                    throw new SocketException("Unknown option " + opt);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1139
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1140
            } catch (SocketException e) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1141
                throw e;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1142
            } catch (IllegalArgumentException | IOException e) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1143
                throw new SocketException(e.getMessage());
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1148
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1149
    protected void shutdownInput() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1150
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1151
            ensureOpenAndConnected();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1152
            if (!isInputClosed) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1153
                Net.shutdown(fd, Net.SHUT_RD);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1154
                isInputClosed = true;
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1159
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1160
    protected void shutdownOutput() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1161
        synchronized (stateLock) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1162
            ensureOpenAndConnected();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1163
            if (!isOutputClosed) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1164
                Net.shutdown(fd, Net.SHUT_WR);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1165
                isOutputClosed = true;
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1170
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1171
    protected boolean supportsUrgentData() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1172
        return true;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1173
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1174
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1175
    @Override
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1176
    protected void sendUrgentData(int data) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1177
        writeLock.lock();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1178
        try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1179
            int n = 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1180
            FileDescriptor fd = beginWrite();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1181
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1182
                do {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1183
                    n = Net.sendOOB(fd, (byte) data);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1184
                } while (n == IOStatus.INTERRUPTED && isOpen());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1185
                if (n == IOStatus.UNAVAILABLE) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1186
                    throw new SocketException("No buffer space available");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1187
                }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1188
            } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1189
                endWrite(n > 0);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1190
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1191
        } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1192
            writeLock.unlock();
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
    /**
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1197
     * Returns an action to close the given file descriptor.
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1198
     */
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1199
    private static Runnable closerFor(FileDescriptor fd, boolean stream) {
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1200
        if (stream) {
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1201
            return () -> {
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1202
                try {
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1203
                    nd.close(fd);
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1204
                } catch (IOException ioe) {
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1205
                    throw new UncheckedIOException(ioe);
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1206
                }
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1207
            };
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1208
        } else {
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1209
            return () -> {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1210
                try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1211
                    nd.close(fd);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1212
                } catch (IOException ioe) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1213
                    throw new UncheckedIOException(ioe);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1214
                } finally {
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1215
                    // decrement
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1216
                    ResourceManager.afterUdpClose();
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1217
                }
58808
9261ad32cba9 8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents: 55102
diff changeset
  1218
            };
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1219
        }
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
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1223
     * Attempts to acquire the given lock within the given waiting time.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1224
     * @return the remaining time in nanoseconds when the lock is acquired, zero
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1225
     *         or less if the lock was not acquired before the timeout expired
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1226
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1227
    private static long tryLock(ReentrantLock lock, long timeout, TimeUnit unit) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1228
        assert timeout > 0;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1229
        boolean interrupted = false;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1230
        long nanos = NANOSECONDS.convert(timeout, unit);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1231
        long remainingNanos = nanos;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1232
        long startNanos = System.nanoTime();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1233
        boolean acquired = false;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1234
        while (!acquired && (remainingNanos > 0)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1235
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1236
                acquired = lock.tryLock(remainingNanos, NANOSECONDS);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1237
            } catch (InterruptedException e) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1238
                interrupted = true;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1239
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1240
            remainingNanos = nanos - (System.nanoTime() - startNanos);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1241
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1242
        if (acquired && remainingNanos <= 0L)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1243
            lock.unlock();  // release lock if timeout has expired
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1244
        if (interrupted)
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1245
            Thread.currentThread().interrupt();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1246
        return remainingNanos;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1247
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1248
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1249
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1250
     * Returns the socket protocol family.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1251
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1252
    private static ProtocolFamily family() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1253
        if (Net.isIPv6Available()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1254
            return StandardProtocolFamily.INET6;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1255
        } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1256
            return StandardProtocolFamily.INET;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1257
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1258
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
  1259
}