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