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