jdk/src/share/classes/java/net/AbstractPlainSocketImpl.java
author michaelm
Thu, 16 Sep 2010 08:08:06 -0700
changeset 6875 81d6ec3397e5
parent 5506 202f599c92aa
child 7027 d3404b1406d5
permissions -rw-r--r--
6981426: limit use of TRACE method in HttpURLConnection Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
     2
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InterruptedIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.net.ConnectionResetException;
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
    36
import sun.net.NetHooks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Default Socket Implementation. This implementation does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * not implement any security checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Note this class should <b>NOT</b> be public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  Steven B. Byrne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
abstract class AbstractPlainSocketImpl extends SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /* instance variable for SO_TIMEOUT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    int timeout;   // timeout in millisec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // traffic class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private int trafficClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private boolean shut_rd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private boolean shut_wr = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private SocketInputStream socketInputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* number of threads using the FileDescriptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected int fdUseCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /* lock when increment/decrementing fdUseCount */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected Object fdLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* indicates a close is pending on the file descriptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected boolean closePending = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /* indicates connection reset state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int CONNECTION_NOT_RESET = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int CONNECTION_RESET_PENDING = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private int CONNECTION_RESET = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int resetState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private Object resetLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Load net library into runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                  new sun.security.action.LoadLibraryAction("net"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Creates a socket with a boolean that specifies whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * is a stream socket (true) or an unconnected UDP socket (false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected synchronized void create(boolean stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        socketCreate(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (socket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            socket.setCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (serverSocket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            serverSocket.setCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Creates a socket and connects it to the specified port on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * the specified host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param host the specified host
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param port the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected void connect(String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        throws UnknownHostException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        IOException pending = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            InetAddress address = InetAddress.getByName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                connectToAddress(address, port, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                pending = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            pending = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // everything failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        throw pending;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Creates a socket and connects it to the specified address on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * the specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param address the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param port the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected void connect(InetAddress address, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            connectToAddress(address, port, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // everything failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Creates a socket and connects it to the specified address on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * the specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param address the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param timeout the timeout value in milliseconds, or zero for no timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @throws IOException if connection fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @throws  IllegalArgumentException if address is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    protected void connect(SocketAddress address, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (address == null || !(address instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new IllegalArgumentException("unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        InetSocketAddress addr = (InetSocketAddress) address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (addr.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new UnknownHostException(addr.getHostName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.port = addr.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.address = addr.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            connectToAddress(this.address, port, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // everything failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private void connectToAddress(InetAddress address, int port, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (address.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            doConnect(InetAddress.getLocalHost(), port, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            doConnect(address, port, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void setOption(int opt, Object val) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        boolean on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            /* check type safety b4 going native.  These should never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
             * fail, since only java.Socket* has access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
             * PlainSocketImpl.setOption().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        case SO_LINGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (val == null || (!(val instanceof Integer) && !(val instanceof Boolean)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                throw new SocketException("Bad parameter for option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (val instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                /* true only if disabling - enabling should be Integer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                on = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        case SO_TIMEOUT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (val == null || (!(val instanceof Integer)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                throw new SocketException("Bad parameter for SO_TIMEOUT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            int tmp = ((Integer) val).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (tmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                throw new IllegalArgumentException("timeout < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            timeout = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        case IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
             if (val == null || !(val instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                 throw new SocketException("bad argument for IP_TOS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
             trafficClass = ((Integer)val).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        case SO_BINDADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new SocketException("Cannot re-bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        case TCP_NODELAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (val == null || !(val instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                throw new SocketException("bad parameter for TCP_NODELAY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            on = ((Boolean)val).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        case SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        case SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (val == null || !(val instanceof Integer) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                !(((Integer)val).intValue() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                throw new SocketException("bad parameter for SO_SNDBUF " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                          "or SO_RCVBUF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        case SO_KEEPALIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (val == null || !(val instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                throw new SocketException("bad parameter for SO_KEEPALIVE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            on = ((Boolean)val).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        case SO_OOBINLINE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (val == null || !(val instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                throw new SocketException("bad parameter for SO_OOBINLINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            on = ((Boolean)val).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        case SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (val == null || !(val instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                throw new SocketException("bad parameter for SO_REUSEADDR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            on = ((Boolean)val).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new SocketException("unrecognized TCP option: " + opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        socketSetOption(opt, on, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public Object getOption(int opt) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (opt == SO_TIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return new Integer(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         * The native socketGetOption() knows about 3 options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         * The 32 bit value it returns will be interpreted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * to what we're asking.  A return of -1 means it understands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * the option but its turned off.  It will raise a SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * if "opt" isn't one it understands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        case TCP_NODELAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return Boolean.valueOf(ret != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        case SO_OOBINLINE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return Boolean.valueOf(ret != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        case SO_LINGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return (ret == -1) ? Boolean.FALSE: (Object)(new Integer(ret));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        case SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return Boolean.valueOf(ret != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        case SO_BINDADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            InetAddressContainer in = new InetAddressContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            ret = socketGetOption(opt, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return in.addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        case SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        case SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return new Integer(ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        case IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (ret == -1) { // ipv6 tos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                return new Integer(trafficClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                return new Integer(ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        case SO_KEEPALIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return Boolean.valueOf(ret != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // should never get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * The workhorse of the connection operation.  Tries several times to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * establish a connection to the given <host, port>.  If unsuccessful,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * throws an IOException indicating what went wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   308
        synchronized (fdLock) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   309
            if (!closePending && (socket == null || !socket.isBound())) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   310
                NetHooks.beforeTcpConnect(fd, address, port);
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   311
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   312
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            FileDescriptor fd = acquireFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                socketConnect(address, port, timeout);
1335
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   317
                /* socket may have been closed during poll/select */
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   318
                synchronized (fdLock) {
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   319
                    if (closePending) {
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   320
                        throw new SocketException ("Socket closed");
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   321
                    }
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   322
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                // If we have a ref. to the Socket, then sets the flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                // created, bound & connected to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                // This is normally done in Socket.connect() but some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                // subclasses of Socket may call impl.connect() directly!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    socket.setBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    socket.setConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                releaseFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Binds the socket to the specified address of the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param address the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param port the port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    protected synchronized void bind(InetAddress address, int lport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    {
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   348
       synchronized (fdLock) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   349
            if (!closePending && (socket == null || !socket.isBound())) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   350
                NetHooks.beforeTcpBind(fd, address, lport);
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   351
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   352
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        socketBind(address, lport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (socket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            socket.setBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (serverSocket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            serverSocket.setBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Listens, for a specified amount of time, for connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @param count the amount of time to listen for connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    protected synchronized void listen(int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        socketListen(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Accepts connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param s the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    protected void accept(SocketImpl s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        FileDescriptor fd = acquireFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            socketAccept(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            releaseFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Gets an InputStream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    protected synchronized InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            throw new IOException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (shut_rd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            throw new IOException("Socket input is shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (socketInputStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            socketInputStream = new SocketInputStream(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return socketInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    void setInputStream(SocketInputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        socketInputStream = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Gets an OutputStream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    protected synchronized OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            throw new IOException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (shut_wr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throw new IOException("Socket output is shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return new SocketOutputStream(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    void setFileDescriptor(FileDescriptor fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    void setAddress(InetAddress address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    void setPort(int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    void setLocalPort(int localport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        this.localport = localport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Returns the number of bytes that can be read without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    protected synchronized int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throw new IOException("Stream closed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         * If connection has been reset then return 0 to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * there are no buffered bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (isConnectionReset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * If no bytes available and we were previously notified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         * of a connection reset then we move to the reset state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         * If are notified of a connection reset then check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * again if there are bytes buffered on the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            n = socketAvailable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            if (n == 0 && isConnectionResetPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                setConnectionReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        } catch (ConnectionResetException exc1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            setConnectionResetPending();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                n = socketAvailable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    setConnectionReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            } catch (ConnectionResetException exc2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Closes the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    protected void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        synchronized(fdLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                if (fdUseCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    if (closePending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    closePending = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                     * We close the FileDescriptor in two-steps - first the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                     * "pre-close" which closes the socket but doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                     * release the underlying file descriptor. This operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                     * may be lengthy due to untransmitted data and a long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                     * linger interval. Once the pre-close is done we do the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                     * actual socket to release the fd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        socketPreClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        socketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                     * If a thread has acquired the fd and a close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                     * isn't pending then use a deferred close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                     * Also decrement fdUseCount to signal the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                     * thread that releases the fd to close it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    if (!closePending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        closePending = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        fdUseCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        socketPreClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            socketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Shutdown read-half of the socket connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    protected void shutdownInput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
      if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
          socketShutdown(SHUT_RD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
          if (socketInputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
              socketInputStream.setEOF(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
          shut_rd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Shutdown write-half of the socket connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    protected void shutdownOutput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
      if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
          socketShutdown(SHUT_WR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
          shut_wr = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    protected boolean supportsUrgentData () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    protected void sendUrgentData (int data) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if (fd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            throw new IOException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        socketSendUrgentData (data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Cleans up if the user forgets to close it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * "Acquires" and returns the FileDescriptor for this impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * A corresponding releaseFD is required to "release" the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * FileDescriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    FileDescriptor acquireFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        synchronized (fdLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            fdUseCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * "Release" the FileDescriptor for this impl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * If the use count goes to -1 then the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    void releaseFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        synchronized (fdLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            fdUseCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            if (fdUseCount == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        socketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    public boolean isConnectionReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        synchronized (resetLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            return (resetState == CONNECTION_RESET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    public boolean isConnectionResetPending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        synchronized (resetLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            return (resetState == CONNECTION_RESET_PENDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public void setConnectionReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        synchronized (resetLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            resetState = CONNECTION_RESET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    public void setConnectionResetPending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        synchronized (resetLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (resetState == CONNECTION_NOT_RESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                resetState = CONNECTION_RESET_PENDING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Return true if already closed or close is pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    public boolean isClosedOrPending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * Lock on fdLock to ensure that we wait if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         * close is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        synchronized (fdLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            if (closePending || (fd == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Return the current value of SO_TIMEOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    public int getTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * "Pre-close" a socket by dup'ing the file descriptor - this enables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * the socket to be closed without releasing the file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    private void socketPreClose() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        socketClose0(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * Close the socket (and release the file descriptor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    protected void socketClose() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        socketClose0(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    abstract void socketCreate(boolean isServer) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    abstract void socketConnect(InetAddress address, int port, int timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    abstract void socketBind(InetAddress address, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    abstract void socketListen(int count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    abstract void socketAccept(SocketImpl s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    abstract int socketAvailable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    abstract void socketClose0(boolean useDeferredClose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    abstract void socketShutdown(int howto)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    abstract void socketSetOption(int cmd, boolean on, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    abstract int socketGetOption(int opt, Object iaContainerObj) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    abstract void socketSendUrgentData(int data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public final static int SHUT_RD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    public final static int SHUT_WR = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
class InetAddressContainer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    InetAddress addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
}