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