jdk/src/solaris/classes/sun/nio/ch/SctpChannelImpl.java
author alanb
Mon, 09 May 2011 18:45:33 +0100
changeset 9679 d98ae8bc45fc
parent 7668 d4a77089c587
child 10137 d92637d3d673
permissions -rw-r--r--
7042979: Rename StandardSocketOption and StandardWatchEventKind Reviewed-by: forax, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     4
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d859108aea12 4927640: Implementation of the sctp protocol
chegar
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: 4677
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4677
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    10
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    15
 * accompanied this code).
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    16
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4677
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4677
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4677
diff changeset
    23
 * questions.
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    24
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    25
package sun.nio.ch;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    26
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    27
import java.net.InetAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    28
import java.net.SocketAddress;
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
    29
import java.net.SocketException;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    30
import java.net.InetSocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    31
import java.io.FileDescriptor;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    32
import java.io.IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    33
import java.util.Collections;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    34
import java.util.Set;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
import java.util.HashSet;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
import java.nio.ByteBuffer;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import java.nio.channels.SelectionKey;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import java.nio.channels.ClosedChannelException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
import java.nio.channels.ConnectionPendingException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
import java.nio.channels.NoConnectionPendingException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
import java.nio.channels.AlreadyConnectedException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
import java.nio.channels.NotYetBoundException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
import java.nio.channels.NotYetConnectedException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
import java.nio.channels.spi.SelectorProvider;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
import com.sun.nio.sctp.AbstractNotificationHandler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
import com.sun.nio.sctp.Association;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
import com.sun.nio.sctp.AssociationChangeNotification;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
import com.sun.nio.sctp.HandlerResult;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
import com.sun.nio.sctp.IllegalReceiveException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
import com.sun.nio.sctp.InvalidStreamException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
import com.sun.nio.sctp.IllegalUnbindException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
import com.sun.nio.sctp.MessageInfo;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
import com.sun.nio.sctp.NotificationHandler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
import com.sun.nio.sctp.SctpSocketOption;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
import sun.nio.ch.PollArrayWrapper;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
import sun.nio.ch.SelChImpl;
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
    58
import static com.sun.nio.sctp.SctpStandardSocketOptions.*;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
import static sun.nio.ch.SctpResultContainer.SEND_FAILED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
import static sun.nio.ch.SctpResultContainer.ASSOCIATION_CHANGED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
import static sun.nio.ch.SctpResultContainer.PEER_ADDRESS_CHANGED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
import static sun.nio.ch.SctpResultContainer.SHUTDOWN;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
/**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
 * An implementation of an SctpChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
public class SctpChannelImpl extends SctpChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
    implements SelChImpl
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
{
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
    private final FileDescriptor fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
    private final int fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
    /* IDs of native threads doing send and receivess, for signalling */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
    private volatile long receiverThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
    private volatile long senderThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
    /* Lock held by current receiving or connecting thread */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
    private final Object receiveLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
    /* Lock held by current sending or connecting thread */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
    private final Object sendLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
    private final ThreadLocal<Boolean> receiveInvoked =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
        new ThreadLocal<Boolean>() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
             @Override protected Boolean initialValue() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
                 return Boolean.FALSE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
    };
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
    /* Lock held by any thread that modifies the state fields declared below
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
       DO NOT invoke a blocking I/O operation while holding this lock! */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
    private final Object stateLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
    private enum ChannelState {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
        UNINITIALIZED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
        UNCONNECTED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
        PENDING,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
        CONNECTED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
        KILLPENDING,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
        KILLED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
    /* -- The following fields are protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
    private ChannelState state = ChannelState.UNINITIALIZED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
    /* Binding; Once bound the port will remain constant. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
    int port = -1;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
    private HashSet<InetSocketAddress> localAddresses = new HashSet<InetSocketAddress>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
    /* Has the channel been bound to the wildcard address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
    private boolean wildcard; /* false */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
    //private InetSocketAddress remoteAddress = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
    /* Input/Output open */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
    private boolean readyToConnect;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
    /* Shutdown */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
    private boolean isShutdown;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
    private Association association;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   121
    private Set<SocketAddress> remoteAddresses = Collections.EMPTY_SET;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   122
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
    /* -- End of fields protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
     * Constructor for normal connecting sockets
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
    public SctpChannelImpl(SelectorProvider provider) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
        //TODO: update provider remove public modifier
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
        super(provider);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
        this.fd = SctpNet.socket(true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
        this.fdVal = IOUtil.fdVal(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
        this.state = ChannelState.UNCONNECTED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
     * Constructor for sockets obtained from server sockets
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
    public SctpChannelImpl(SelectorProvider provider, FileDescriptor fd)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
         throws IOException {
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   141
        this(provider, fd, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   142
    }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   143
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   144
    /**
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   145
     * Constructor for sockets obtained from branching
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   146
     */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   147
    public SctpChannelImpl(SelectorProvider provider,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   148
                           FileDescriptor fd,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   149
                           Association association)
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   150
            throws IOException {
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
        super(provider);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
        this.fd = fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   153
        this.fdVal = IOUtil.fdVal(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
        this.state = ChannelState.CONNECTED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
        port = (Net.localAddress(fd)).getPort();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   157
        if (association != null) { /* branched */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   158
            this.association = association;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   159
        } else { /* obtained from server channel */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   160
            /* Receive COMM_UP */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   161
            ByteBuffer buf = Util.getTemporaryDirectBuffer(50);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   162
            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   163
                receive(buf, null, null, true);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   164
            } finally {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   165
                Util.releaseTemporaryDirectBuffer(buf);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   166
            }
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   167
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   170
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   171
     * Binds the channel's socket to a local address.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   172
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
    public SctpChannel bind(SocketAddress local) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
        synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   176
            synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   177
                synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   178
                    ensureOpenAndUnconnected();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
                    if (isBound())
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3320
diff changeset
   180
                        SctpNet.throwAlreadyBoundException();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   181
                    InetSocketAddress isa = (local == null) ?
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   182
                        new InetSocketAddress(0) : Net.checkAddress(local);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   183
                    Net.bind(fd, isa.getAddress(), isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
                    InetSocketAddress boundIsa = Net.localAddress(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   185
                    port = boundIsa.getPort();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   186
                    localAddresses.add(isa);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
                    if (isa.getAddress().isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
                        wildcard = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   192
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   193
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   194
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   195
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   196
    public SctpChannel bindAddress(InetAddress address)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   197
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
        bindUnbindAddress(address, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   199
        localAddresses.add(new InetSocketAddress(address, port));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   200
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   201
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   202
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   204
    public SctpChannel unbindAddress(InetAddress address)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   205
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   206
        bindUnbindAddress(address, false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   207
        localAddresses.remove(new InetSocketAddress(address, port));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   208
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   209
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
    private SctpChannel bindUnbindAddress(InetAddress address, boolean add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   212
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   213
        if (address == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   214
            throw new IllegalArgumentException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   216
        synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   217
            synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
                synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
                    if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
                        throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
                    if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
                        throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
                    if (wildcard)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
                        throw new IllegalStateException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
                                "Cannot add or remove addresses from a channel that is bound to the wildcard address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
                    if (address.isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
                        throw new IllegalArgumentException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
                                "Cannot add or remove the wildcard address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
                    if (add) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
                        for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
                            if (addr.getAddress().equals(address)) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3320
diff changeset
   232
                                SctpNet.throwAlreadyBoundException();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   233
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   234
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
                    } else { /*removing */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
                        /* Verify that there is more than one address
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
                         * and that address is already bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
                        if (localAddresses.size() <= 1)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
                            throw new IllegalUnbindException("Cannot remove address from a channel with only one address bound");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
                        boolean foundAddress = false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
                        for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
                            if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
                                foundAddress = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
                                break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
                        if (!foundAddress )
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
                            throw new IllegalUnbindException("Cannot remove address from a channel that is not bound to that address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
                    SctpNet.bindx(fdVal, new InetAddress[]{address}, port, add);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   253
                    /* Update our internal Set to reflect the addition/removal */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   254
                    if (add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   255
                        localAddresses.add(new InetSocketAddress(address, port));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
                    else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
                        for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   258
                            if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   259
                                localAddresses.remove(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   260
                                break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   261
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   262
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   263
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   268
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   269
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   270
    private boolean isBound() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   272
            return port == -1 ? false : true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   273
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   274
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   275
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   276
    private boolean isConnected() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   277
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   278
            return (state == ChannelState.CONNECTED);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   279
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   280
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   281
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   282
    private void ensureOpenAndUnconnected() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   283
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   284
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   285
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   286
            if (isConnected())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   287
                throw new AlreadyConnectedException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   288
            if (state == ChannelState.PENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   289
                throw new ConnectionPendingException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   290
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   291
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   292
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   293
    private boolean ensureReceiveOpen() throws ClosedChannelException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   294
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   295
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   296
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   297
            if (!isConnected())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   298
                throw new NotYetConnectedException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   299
            else
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   300
                return true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   301
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   302
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   303
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   304
    private void ensureSendOpen() throws ClosedChannelException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   305
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   306
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   307
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   308
            if (isShutdown)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   309
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   310
            if (!isConnected())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   311
                throw new NotYetConnectedException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   312
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   313
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   314
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   315
    private void receiverCleanup() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   316
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   317
            receiverThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   318
            if (state == ChannelState.KILLPENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   319
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   320
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   321
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   322
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   323
    private void senderCleanup() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   324
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   325
            senderThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   326
            if (state == ChannelState.KILLPENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   327
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   328
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   329
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   330
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   331
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   332
    public Association association() throws ClosedChannelException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   333
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   334
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   335
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   336
            if (!isConnected())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   337
                return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   338
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   339
            return association;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   340
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   341
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   342
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   343
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   344
    public boolean connect(SocketAddress endpoint) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   345
        synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   346
            synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   347
                ensureOpenAndUnconnected();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   348
                InetSocketAddress isa = Net.checkAddress(endpoint);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   349
                SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   350
                if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   351
                    sm.checkConnect(isa.getAddress().getHostAddress(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   352
                                    isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   353
                synchronized (blockingLock()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   354
                    int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   355
                    try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   356
                        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   357
                            begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   358
                            synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   359
                                if (!isOpen()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   360
                                    return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   361
                                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   362
                                receiverThread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   363
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   364
                            for (;;) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   365
                                InetAddress ia = isa.getAddress();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   366
                                if (ia.isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   367
                                    ia = InetAddress.getLocalHost();
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3320
diff changeset
   368
                                n = SctpNet.connect(fdVal, ia, isa.getPort());
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   369
                                if (  (n == IOStatus.INTERRUPTED)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   370
                                      && isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   371
                                    continue;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   372
                                break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   373
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   374
                        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   375
                            receiverCleanup();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   376
                            end((n > 0) || (n == IOStatus.UNAVAILABLE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   377
                            assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   378
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   379
                    } catch (IOException x) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   380
                        /* If an exception was thrown, close the channel after
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   381
                         * invoking end() so as to avoid bogus
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   382
                         * AsynchronousCloseExceptions */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   383
                        close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   384
                        throw x;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   385
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   386
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   387
                    if (n > 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   388
                        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   389
                            /* Connection succeeded */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   390
                            state = ChannelState.CONNECTED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   391
                            if (!isBound()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   392
                                InetSocketAddress boundIsa =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   393
                                        Net.localAddress(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   394
                                port = boundIsa.getPort();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   395
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   396
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   397
                            /* Receive COMM_UP */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   398
                            ByteBuffer buf = Util.getTemporaryDirectBuffer(50);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   399
                            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   400
                                receive(buf, null, null, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   401
                            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   402
                                Util.releaseTemporaryDirectBuffer(buf);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   403
                            }
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   404
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   405
                            /* cache remote addresses */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   406
                            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   407
                                remoteAddresses = getRemoteAddresses();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   408
                            } catch (IOException unused) { /* swallow exception */ }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   409
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   410
                            return true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   411
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   412
                    } else  {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   413
                        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   414
                            /* If nonblocking and no exception then connection
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   415
                             * pending; disallow another invocation */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   416
                            if (!isBlocking())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   417
                                state = ChannelState.PENDING;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   418
                            else
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   419
                                assert false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   420
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   421
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   422
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   423
                return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   424
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   425
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   426
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   427
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   428
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   429
    public boolean connect(SocketAddress endpoint,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   430
                           int maxOutStreams,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   431
                           int maxInStreams)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   432
            throws IOException {
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   433
        ensureOpenAndUnconnected();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   434
        return setOption(SCTP_INIT_MAXSTREAMS, InitMaxStreams.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   435
                create(maxInStreams, maxOutStreams)).connect(endpoint);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   436
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   437
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   438
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   439
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   440
    public boolean isConnectionPending() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   441
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   442
            return (state == ChannelState.PENDING);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   443
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   444
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   445
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   446
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   447
    public boolean finishConnect() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   448
        synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   449
            synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   450
                synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   451
                    if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   452
                        throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   453
                    if (isConnected())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   454
                        return true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   455
                    if (state != ChannelState.PENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   456
                        throw new NoConnectionPendingException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   457
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   458
                int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   459
                try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   460
                    try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   461
                        begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   462
                        synchronized (blockingLock()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   463
                            synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   464
                                if (!isOpen()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   465
                                    return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   466
                                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   467
                                receiverThread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   468
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   469
                            if (!isBlocking()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   470
                                for (;;) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   471
                                    n = checkConnect(fd, false, readyToConnect);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   472
                                    if (  (n == IOStatus.INTERRUPTED)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   473
                                          && isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   474
                                        continue;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   475
                                    break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   476
                                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   477
                            } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   478
                                for (;;) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   479
                                    n = checkConnect(fd, true, readyToConnect);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   480
                                    if (n == 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   481
                                        // Loop in case of
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   482
                                        // spurious notifications
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   483
                                        continue;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   484
                                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   485
                                    if (  (n == IOStatus.INTERRUPTED)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   486
                                          && isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   487
                                        continue;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   488
                                    break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   489
                                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   490
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   491
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   492
                    } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   493
                        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   494
                            receiverThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   495
                            if (state == ChannelState.KILLPENDING) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   496
                                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   497
                                /* poll()/getsockopt() does not report
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   498
                                 * error (throws exception, with n = 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   499
                                 * on Linux platform after dup2 and
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   500
                                 * signal-wakeup. Force n to 0 so the
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   501
                                 * end() can throw appropriate exception */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   502
                                n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   503
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   504
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   505
                        end((n > 0) || (n == IOStatus.UNAVAILABLE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   506
                        assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   507
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   508
                } catch (IOException x) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   509
                    /* If an exception was thrown, close the channel after
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   510
                     * invoking end() so as to avoid bogus
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   511
                     * AsynchronousCloseExceptions */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   512
                    close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   513
                    throw x;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   514
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   515
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   516
                if (n > 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   517
                    synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   518
                        state = ChannelState.CONNECTED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   519
                        if (!isBound()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   520
                            InetSocketAddress boundIsa =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   521
                                    Net.localAddress(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   522
                            port = boundIsa.getPort();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   523
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   524
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   525
                        /* Receive COMM_UP */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   526
                        ByteBuffer buf = Util.getTemporaryDirectBuffer(50);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   527
                        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   528
                            receive(buf, null, null, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   529
                        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   530
                            Util.releaseTemporaryDirectBuffer(buf);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   531
                        }
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   532
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   533
                        /* cache remote addresses */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   534
                        try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   535
                            remoteAddresses = getRemoteAddresses();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   536
                        } catch (IOException unused) { /* swallow exception */ }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   537
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   538
                        return true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   539
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   540
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   541
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   542
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   543
        return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   544
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   545
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   546
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   547
    protected void implConfigureBlocking(boolean block) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   548
        IOUtil.configureBlocking(fd, block);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   549
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   550
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   551
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   552
    public void implCloseSelectableChannel() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   553
        synchronized (stateLock) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3320
diff changeset
   554
            SctpNet.preClose(fdVal);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   555
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   556
            if (receiverThread != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   557
                NativeThread.signal(receiverThread);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   558
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   559
            if (senderThread != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   560
                NativeThread.signal(senderThread);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   561
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   562
            if (!isRegistered())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   563
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   564
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   565
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   566
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   567
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   568
    public FileDescriptor getFD() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   569
        return fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   570
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   571
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   572
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   573
    public int getFDVal() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   574
        return fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   575
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   576
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   577
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   578
     * Translates native poll revent ops into a ready operation ops
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   579
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   580
    private boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   581
        int intOps = sk.nioInterestOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   582
        int oldOps = sk.nioReadyOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   583
        int newOps = initialOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   584
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   585
        if ((ops & PollArrayWrapper.POLLNVAL) != 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   586
            /* This should only happen if this channel is pre-closed while a
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   587
             * selection operation is in progress
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   588
             * ## Throw an error if this channel has not been pre-closed */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   589
            return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   590
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   591
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   592
        if ((ops & (PollArrayWrapper.POLLERR
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   593
                    | PollArrayWrapper.POLLHUP)) != 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   594
            newOps = intOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   595
            sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   596
            /* No need to poll again in checkConnect,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   597
             * the error will be detected there */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   598
            readyToConnect = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   599
            return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   600
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   601
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   602
        if (((ops & PollArrayWrapper.POLLIN) != 0) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   603
            ((intOps & SelectionKey.OP_READ) != 0) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   604
            isConnected())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   605
            newOps |= SelectionKey.OP_READ;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   606
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   607
        if (((ops & PollArrayWrapper.POLLCONN) != 0) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   608
            ((intOps & SelectionKey.OP_CONNECT) != 0) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   609
            ((state == ChannelState.UNCONNECTED) || (state == ChannelState.PENDING))) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   610
            newOps |= SelectionKey.OP_CONNECT;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   611
            readyToConnect = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   612
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   613
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   614
        if (((ops & PollArrayWrapper.POLLOUT) != 0) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   615
            ((intOps & SelectionKey.OP_WRITE) != 0) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   616
            isConnected())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   617
            newOps |= SelectionKey.OP_WRITE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   618
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   619
        sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   620
        return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   621
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   622
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   623
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   624
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   625
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   626
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   627
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   628
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   629
    @SuppressWarnings("all")
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   630
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   631
        return translateReadyOps(ops, 0, sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   632
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   633
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   634
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   635
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   636
        int newOps = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   637
        if ((ops & SelectionKey.OP_READ) != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   638
            newOps |= PollArrayWrapper.POLLIN;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   639
        if ((ops & SelectionKey.OP_WRITE) != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   640
            newOps |= PollArrayWrapper.POLLOUT;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   641
        if ((ops & SelectionKey.OP_CONNECT) != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   642
            newOps |= PollArrayWrapper.POLLCONN;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   643
        sk.selector.putEventOps(sk, newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   644
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   645
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   646
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   647
    public void kill() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   648
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   649
            if (state == ChannelState.KILLED)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   650
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   651
            if (state == ChannelState.UNINITIALIZED) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   652
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   653
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   654
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   655
            assert !isOpen() && !isRegistered();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   656
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   657
            /* Postpone the kill if there is a waiting reader
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   658
             * or writer thread. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   659
            if (receiverThread == 0 && senderThread == 0) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3320
diff changeset
   660
                SctpNet.close(fdVal);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   661
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   662
            } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   663
                state = ChannelState.KILLPENDING;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   664
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   665
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   666
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   667
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   668
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   669
    public <T> SctpChannel setOption(SctpSocketOption<T> name, T value)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   670
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   671
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   672
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   673
        if (!supportedOptions().contains(name))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   674
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   675
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   676
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   677
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   678
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   679
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   680
            SctpNet.setSocketOption(fdVal, name, value, 0 /*oneToOne*/);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   681
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   682
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   683
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   684
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   685
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   686
    @SuppressWarnings("unchecked")
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   687
    public <T> T getOption(SctpSocketOption<T> name) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   688
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   689
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   690
        if (!supportedOptions().contains(name))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   691
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   692
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   693
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   694
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   695
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   696
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   697
            return (T)SctpNet.getSocketOption(fdVal, name, 0 /*oneToOne*/);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   698
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   699
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   700
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   701
    private static class DefaultOptionsHolder {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   702
        static final Set<SctpSocketOption<?>> defaultOptions = defaultOptions();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   703
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   704
        private static Set<SctpSocketOption<?>> defaultOptions() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   705
            HashSet<SctpSocketOption<?>> set = new HashSet<SctpSocketOption<?>>(10);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   706
            set.add(SCTP_DISABLE_FRAGMENTS);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   707
            set.add(SCTP_EXPLICIT_COMPLETE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   708
            set.add(SCTP_FRAGMENT_INTERLEAVE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   709
            set.add(SCTP_INIT_MAXSTREAMS);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   710
            set.add(SCTP_NODELAY);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   711
            set.add(SCTP_PRIMARY_ADDR);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   712
            set.add(SCTP_SET_PEER_PRIMARY_ADDR);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   713
            set.add(SO_SNDBUF);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   714
            set.add(SO_RCVBUF);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   715
            set.add(SO_LINGER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   716
            return Collections.unmodifiableSet(set);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   717
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   718
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   719
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   720
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   721
    public final Set<SctpSocketOption<?>> supportedOptions() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   722
        return DefaultOptionsHolder.defaultOptions;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   723
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   724
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   725
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   726
    public <T> MessageInfo receive(ByteBuffer buffer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   727
                                   T attachment,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   728
                                   NotificationHandler<T> handler)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   729
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   730
        return receive(buffer, attachment, handler, false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   731
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   732
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   733
    private <T> MessageInfo receive(ByteBuffer buffer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   734
                                    T attachment,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   735
                                    NotificationHandler<T> handler,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   736
                                    boolean fromConnect)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   737
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   738
        if (buffer == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   739
            throw new IllegalArgumentException("buffer cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   740
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   741
        if (buffer.isReadOnly())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   742
            throw new IllegalArgumentException("Read-only buffer");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   743
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   744
        if (receiveInvoked.get())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   745
            throw new IllegalReceiveException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   746
                    "cannot invoke receive from handler");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   747
        receiveInvoked.set(Boolean.TRUE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   748
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   749
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   750
            SctpResultContainer resultContainer = new SctpResultContainer();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   751
            do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   752
                resultContainer.clear();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   753
                synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   754
                    if (!ensureReceiveOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   755
                        return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   756
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   757
                    int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   758
                    try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   759
                        begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   760
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   761
                        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   762
                            if(!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   763
                                return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   764
                            receiverThread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   765
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   766
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   767
                        do {
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   768
                            n = receive(fdVal, buffer, resultContainer, fromConnect);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   769
                        } while ((n == IOStatus.INTERRUPTED) && isOpen());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   770
                    } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   771
                        receiverCleanup();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   772
                        end((n > 0) || (n == IOStatus.UNAVAILABLE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   773
                        assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   774
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   775
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   776
                    if (!resultContainer.isNotification()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   777
                        /* message or nothing */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   778
                        if (resultContainer.hasSomething()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   779
                            /* Set the association before returning */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   780
                            SctpMessageInfoImpl info =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   781
                                    resultContainer.getMessageInfo();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   782
                            synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   783
                                assert association != null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   784
                                info.setAssociation(association);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   785
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   786
                            return info;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   787
                        } else
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   788
                            /* Non-blocking may return null if nothing available*/
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   789
                            return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   790
                    } else { /* notification */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   791
                        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   792
                            handleNotificationInternal(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   793
                                    resultContainer);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   794
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   795
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   796
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   797
                    if (fromConnect)  {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   798
                        /* If we reach here, then it was connect that invoked
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   799
                         * receive and received the COMM_UP. We have already
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   800
                         * handled the COMM_UP with the internal notification
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   801
                         * handler. Simply return. */
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   802
                        return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   803
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   804
                }  /* receiveLock */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   805
            } while (handler == null ? true :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   806
                (invokeNotificationHandler(resultContainer, handler, attachment)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   807
                 == HandlerResult.CONTINUE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   808
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   809
            return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   810
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   811
            receiveInvoked.set(Boolean.FALSE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   812
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   813
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   814
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   815
    private int receive(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   816
                        ByteBuffer dst,
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   817
                        SctpResultContainer resultContainer,
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   818
                        boolean peek)
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   819
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   820
        int pos = dst.position();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   821
        int lim = dst.limit();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   822
        assert (pos <= lim);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   823
        int rem = (pos <= lim ? lim - pos : 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   824
        if (dst instanceof DirectBuffer && rem > 0)
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   825
            return receiveIntoNativeBuffer(fd, resultContainer, dst, rem, pos, peek);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   826
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   827
        /* Substitute a native buffer */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   828
        int newSize = Math.max(rem, 1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   829
        ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   830
        try {
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   831
            int n = receiveIntoNativeBuffer(fd, resultContainer, bb, newSize, 0, peek);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   832
            bb.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   833
            if (n > 0 && rem > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   834
                dst.put(bb);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   835
            return n;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   836
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   837
            Util.releaseTemporaryDirectBuffer(bb);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   838
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   839
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   840
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   841
    private int receiveIntoNativeBuffer(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   842
                                        SctpResultContainer resultContainer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   843
                                        ByteBuffer bb,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   844
                                        int rem,
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   845
                                        int pos,
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   846
                                        boolean peek)
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   847
        throws IOException
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   848
    {
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   849
        int n = receive0(fd, resultContainer, ((DirectBuffer)bb).address() + pos, rem, peek);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   850
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   851
        if (n > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   852
            bb.position(pos + n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   853
        return n;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   854
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   855
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   856
    private InternalNotificationHandler<?> internalNotificationHandler =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   857
            new InternalNotificationHandler();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   858
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   859
    private void handleNotificationInternal(SctpResultContainer resultContainer)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   860
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   861
        invokeNotificationHandler(resultContainer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   862
                internalNotificationHandler, null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   863
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   864
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   865
    private class InternalNotificationHandler<T>
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   866
            extends AbstractNotificationHandler<T>
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   867
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   868
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   869
        public HandlerResult handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   870
                AssociationChangeNotification not, T unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   871
            if (not.event().equals(
4677
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   872
                    AssociationChangeNotification.AssocChangeEvent.COMM_UP) &&
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   873
                    association == null) {
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   874
                SctpAssocChange sac = (SctpAssocChange) not;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   875
                association = new SctpAssociationImpl
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   876
                       (sac.assocId(), sac.maxInStreams(), sac.maxOutStreams());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   877
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   878
            return HandlerResult.CONTINUE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   879
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   880
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   881
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   882
    private <T> HandlerResult invokeNotificationHandler
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   883
                                 (SctpResultContainer resultContainer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   884
                                  NotificationHandler<T> handler,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   885
                                  T attachment) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   886
        SctpNotification notification = resultContainer.notification();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   887
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   888
            notification.setAssociation(association);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   889
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   890
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   891
        if (!(handler instanceof AbstractNotificationHandler)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   892
            return handler.handleNotification(notification, attachment);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   893
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   894
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   895
        /* AbstractNotificationHandler */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   896
        AbstractNotificationHandler absHandler =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   897
                (AbstractNotificationHandler)handler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   898
        switch(resultContainer.type()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   899
            case ASSOCIATION_CHANGED :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   900
                return absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   901
                        resultContainer.getAssociationChanged(), attachment);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   902
            case PEER_ADDRESS_CHANGED :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   903
                return absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   904
                        resultContainer.getPeerAddressChanged(), attachment);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   905
            case SEND_FAILED :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   906
                return absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   907
                        resultContainer.getSendFailed(), attachment);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   908
            case SHUTDOWN :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   909
                return absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   910
                        resultContainer.getShutdown(), attachment);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   911
            default :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   912
                /* implementation specific handlers */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   913
                return absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   914
                        resultContainer.notification(), attachment);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   915
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   916
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   917
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   918
    private void checkAssociation(Association sendAssociation) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   919
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   920
            if (sendAssociation != null && !sendAssociation.equals(association)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   921
                throw new IllegalArgumentException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   922
                        "Cannot send to another association");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   923
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   924
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   925
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   926
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   927
    private void checkStreamNumber(int streamNumber) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   928
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   929
            if (association != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   930
                if (streamNumber < 0 ||
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   931
                      streamNumber >= association.maxOutboundStreams())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   932
                    throw new InvalidStreamException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   933
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   934
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   935
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   936
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   937
    /* TODO: Add support for ttl and isComplete to both 121 12M
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   938
     *       SCTP_EOR not yet supported on reference platforms
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   939
     *       TTL support limited...
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   940
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   941
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   942
    public int send(ByteBuffer buffer, MessageInfo messageInfo)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   943
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   944
        if (buffer == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   945
            throw new IllegalArgumentException("buffer cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   946
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   947
        if (messageInfo == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   948
            throw new IllegalArgumentException("messageInfo cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   949
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   950
        checkAssociation(messageInfo.association());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   951
        checkStreamNumber(messageInfo.streamNumber());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   952
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   953
        synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   954
            ensureSendOpen();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   955
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   956
            int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   957
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   958
                begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   959
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   960
                synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   961
                    if(!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   962
                        return 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   963
                    senderThread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   964
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   965
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   966
                do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   967
                    n = send(fdVal, buffer, messageInfo);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   968
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   969
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   970
                return IOStatus.normalize(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   971
            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   972
                senderCleanup();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   973
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   974
                assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   975
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   976
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   977
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   978
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   979
    private int send(int fd, ByteBuffer src, MessageInfo messageInfo)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   980
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   981
        int streamNumber = messageInfo.streamNumber();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   982
        SocketAddress target = messageInfo.address();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   983
        boolean unordered = messageInfo.isUnordered();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   984
        int ppid = messageInfo.payloadProtocolID();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   985
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   986
        if (src instanceof DirectBuffer)
4677
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   987
            return sendFromNativeBuffer(fd, src, target, streamNumber,
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   988
                    unordered, ppid);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   989
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   990
        /* Substitute a native buffer */
4677
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   991
        int pos = src.position();
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   992
        int lim = src.limit();
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   993
        assert (pos <= lim && streamNumber >= 0);
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   994
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   995
        int rem = (pos <= lim ? lim - pos : 0);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   996
        ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   997
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   998
            bb.put(src);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   999
            bb.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1000
            /* Do not update src until we see how many bytes were written */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1001
            src.position(pos);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1002
4677
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
  1003
            int n = sendFromNativeBuffer(fd, bb, target, streamNumber,
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1004
                    unordered, ppid);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1005
            if (n > 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1006
                /* now update src */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1007
                src.position(pos + n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1008
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1009
            return n;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1010
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1011
            Util.releaseTemporaryDirectBuffer(bb);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1012
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1013
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1014
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1015
    private int sendFromNativeBuffer(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1016
                                     ByteBuffer bb,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1017
                                     SocketAddress target,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1018
                                     int streamNumber,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1019
                                     boolean unordered,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1020
                                     int ppid)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1021
            throws IOException {
4677
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
  1022
        int pos = bb.position();
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
  1023
        int lim = bb.limit();
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
  1024
        assert (pos <= lim);
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
  1025
        int rem = (pos <= lim ? lim - pos : 0);
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
  1026
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1027
        int written = send0(fd, ((DirectBuffer)bb).address() + pos,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1028
                            rem, target, -1 /*121*/, streamNumber, unordered, ppid);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1029
        if (written > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1030
            bb.position(pos + written);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1031
        return written;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1032
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1033
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1034
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1035
    public SctpChannel shutdown() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1036
        synchronized(stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1037
            if (isShutdown)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1038
                return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1039
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1040
            ensureSendOpen();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1041
            SctpNet.shutdown(fdVal, -1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1042
            if (senderThread != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1043
                NativeThread.signal(senderThread);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1044
            isShutdown = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1045
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1046
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1047
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1048
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1049
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1050
    public Set<SocketAddress> getAllLocalAddresses()
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1051
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1052
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1053
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1054
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1055
            if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1056
                return Collections.EMPTY_SET;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1057
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1058
            return SctpNet.getLocalAddresses(fdVal);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1059
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1060
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1061
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1062
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1063
    public Set<SocketAddress> getRemoteAddresses()
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1064
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1065
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1066
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1067
                throw new ClosedChannelException();
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
  1068
            if (!isConnected() || isShutdown)
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1069
                return Collections.EMPTY_SET;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1070
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
  1071
            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
  1072
                return SctpNet.getRemoteAddresses(fdVal, 0/*unused*/);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
  1073
            } catch (SocketException unused) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
  1074
                /* an open connected channel should always have remote addresses */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
  1075
                return remoteAddresses;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
  1076
            }
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1077
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1078
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1079
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1080
    /* Native */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1081
    private static native void initIDs();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1082
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1083
    static native int receive0(int fd, SctpResultContainer resultContainer,
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
  1084
            long address, int length, boolean peek) throws IOException;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1085
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1086
    static native int send0(int fd, long address, int length,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1087
            SocketAddress target, int assocId, int streamNumber,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1088
            boolean unordered, int ppid) throws IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1089
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1090
    private static native int checkConnect(FileDescriptor fd, boolean block,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1091
            boolean ready) throws IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1092
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1093
    static {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1094
        Util.load();   /* loads nio & net native libraries */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1095
        java.security.AccessController.doPrivileged(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1096
                new sun.security.action.LoadLibraryAction("sctp"));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1097
        initIDs();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1098
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1099
}