Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/nio/sctp/SctpMultiChannel.java
38923 views
/*1* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.nio.sctp;2526import java.net.SocketAddress;27import java.net.InetAddress;28import java.io.IOException;29import java.util.Set;30import java.nio.ByteBuffer;31import java.nio.channels.spi.AbstractSelectableChannel;32import java.nio.channels.spi.SelectorProvider;33import java.nio.channels.ClosedChannelException;34import java.nio.channels.NotYetBoundException;35import java.nio.channels.SelectionKey;3637/**38* A selectable channel for message-oriented SCTP sockets.39*40* <P> An SCTP multi channel supports many associations on a single socket.41* An {@code SctpMultiChannel} is created by invoking the42* {@link #open open} method of this class. A newly-created channel is open but43* not yet bound. An attempt to invoke the {@link #receive receive} method of an44* unbound channel will cause the {@link NotYetBoundException}45* to be thrown. An attempt to invoke the {@link #send send} method of an46* unbound channel will cause it to first invoke the {@link #bind bind} method.47* The address(es) that the channel's socket is bound to can be retrieved by48* calling {@link #getAllLocalAddresses getAllLocalAddresses}.49*50* <P> Messages may be sent and received without explicitly setting up an51* association with the remote peer. The channel will implicitly setup52* a new association whenever it sends or receives a message from a remote53* peer if there is not already an association with that peer. Upon successful54* association setup, an {@link AssociationChangeNotification55* association changed} notification will be put to the SCTP stack with its56* {@code event} parameter set to {@link57* AssociationChangeNotification.AssocChangeEvent#COMM_UP58* COMM_UP}. This notification can be received by invoking {@link #receive59* receive}.60*61* <P> Socket options are configured using the62* {@link #setOption(SctpSocketOption,Object,Association) setOption} method. An63* {@code SctpMultiChannel} supports the following options:64* <blockquote>65* <table border summary="Socket options">66* <tr>67* <th>Option Name</th>68* <th>Description</th>69* </tr>70* <tr>71* <td> {@link SctpStandardSocketOptions#SCTP_DISABLE_FRAGMENTS72* SCTP_DISABLE_FRAGMENTS} </td>73* <td> Enables or disables message fragmentation </td>74* </tr>75* <tr>76* <td> {@link SctpStandardSocketOptions#SCTP_EXPLICIT_COMPLETE77* SCTP_EXPLICIT_COMPLETE} </td>78* <td> Enables or disables explicit message completion </td>79* </tr>80* <tr>81* <td> {@link SctpStandardSocketOptions#SCTP_FRAGMENT_INTERLEAVE82* SCTP_FRAGMENT_INTERLEAVE} </td>83* <td> Controls how the presentation of messages occur for the message84* receiver </td>85* </tr>86* <tr>87* <td> {@link SctpStandardSocketOptions#SCTP_INIT_MAXSTREAMS88* SCTP_INIT_MAXSTREAMS} </td>89* <td> The maximum number of streams requested by the local endpoint during90* association initialization </td>91* </tr>92* <tr>93* <td> {@link SctpStandardSocketOptions#SCTP_NODELAY SCTP_NODELAY} </td>94* <td> Enables or disable a Nagle-like algorithm </td>95* </tr>96* <tr>97* <td> {@link SctpStandardSocketOptions#SCTP_PRIMARY_ADDR98* SCTP_PRIMARY_ADDR} </td>99* <td> Requests that the local SCTP stack use the given peer address as the100* association primary </td>101* </tr>102* <tr>103* <td> {@link SctpStandardSocketOptions#SCTP_SET_PEER_PRIMARY_ADDR104* SCTP_SET_PEER_PRIMARY_ADDR} </td>105* <td> Requests that the peer mark the enclosed address as the association106* primary </td>107* </tr>108* <tr>109* <td> {@link SctpStandardSocketOptions#SO_SNDBUF110* SO_SNDBUF} </td>111* <td> The size of the socket send buffer </td>112* </tr>113* <tr>114* <td> {@link SctpStandardSocketOptions#SO_RCVBUF115* SO_RCVBUF} </td>116* <td> The size of the socket receive buffer </td>117* </tr>118* <tr>119* <td> {@link SctpStandardSocketOptions#SO_LINGER120* SO_LINGER} </td>121* <td> Linger on close if data is present (when configured in blocking mode122* only) </td>123* </tr>124* </table>125* </blockquote>126* Additional (implementation specific) options may also be supported. The list127* of options supported is obtained by invoking the {@link #supportedOptions()128* supportedOptions} method.129*130* <p> SCTP multi channels are safe for use by multiple concurrent threads.131* They support concurrent sending and receiving, though at most one thread may be132* sending and at most one thread may be receiving at any given time.133*134* @since 1.7135*/136@jdk.Exported137public abstract class SctpMultiChannel138extends AbstractSelectableChannel139{140/**141* Initializes a new instance of this class.142*143* @param provider144* The selector provider for this channel145*/146protected SctpMultiChannel(SelectorProvider provider) {147super(provider);148}149150/**151* Opens an SCTP multi channel.152*153* <P> The new channel is unbound.154*155* @return A new SCTP multi channel156*157* @throws UnsupportedOperationException158* If the SCTP protocol is not supported159*160* @throws IOException161* If an I/O error occurs162*/163public static SctpMultiChannel open() throws164IOException {165return new sun.nio.ch.sctp.SctpMultiChannelImpl((SelectorProvider)null);166}167168/**169* Returns the open associations on this channel's socket.170*171* <P> Only associations whose {@link AssociationChangeNotification.AssocChangeEvent#COMM_UP172* COMM_UP} association change event has been received are included173* in the returned set of associations. Associations for which a174* {@link AssociationChangeNotification.AssocChangeEvent#COMM_LOST COMM_LOST} or {@link175* AssociationChangeNotification.AssocChangeEvent#SHUTDOWN SHUTDOWN} association change176* event have been receive are removed from the set of associations.177*178* <P> The returned set of associations is a snapshot of the open179* associations at the time that this method is invoked.180*181* @return A {@code Set} containing the open associations, or an empty182* {@code Set} if there are none.183*184* @throws ClosedChannelException185* If this channel is closed186*187* @throws IOException188* If some other I/O error occurs189*/190public abstract Set<Association> associations()191throws IOException;192193/**194* Binds the channel's socket to a local address and configures the socket195* to listen for connections.196*197* <P> This method is used to establish a relationship between the socket198* and the local address. Once a relationship is established then199* the socket remains bound until the channel is closed. This relationship200* may not necesssarily be with the address {@code local} as it may be removed201* by {@link #unbindAddress unbindAddress}, but there will always be at least one local202* address bound to the channel's socket once an invocation of this method203* successfully completes.204*205* <P> Once the channel's socket has been successfully bound to a specific206* address, that is not automatically assigned, more addresses207* may be bound to it using {@link #bindAddress bindAddress}, or removed208* using {@link #unbindAddress unbindAddress}.209*210* <P> The backlog parameter is the maximum number of pending connections on211* the socket. Its exact semantics are implementation specific. An implementation212* may impose an implementation specific maximum length or may choose to ignore213* the parameter. If the backlog parameter has the value {@code 0}, or a negative214* value, then an implementation specific default is used.215*216* @param local217* The local address to bind the socket, or {@code null} to218* bind the socket to an automatically assigned socket address219*220* @param backlog221* The maximum number number of pending connections222*223* @return This channel224*225* @throws ClosedChannelException226* If this channel is closed227*228* @throws java.nio.channels.AlreadyBoundException229* If this channel is already bound230*231* @throws java.nio.channels.UnsupportedAddressTypeException232* If the type of the given address is not supported233*234* @throws SecurityException235* If a security manager has been installed and its {@link236* java.lang.SecurityManager#checkListen(int) checkListen} method237* denies the operation238*239* @throws IOException240* If some other I/O error occurs241*/242public abstract SctpMultiChannel bind(SocketAddress local,243int backlog)244throws IOException;245246/**247* Binds the channel's socket to a local address and configures the socket248* to listen for connections.249*250* <P> This method works as if invoking it were equivalent to evaluating the251* expression:252* <blockquote><pre>253* bind(local, 0);254* </pre></blockquote>255*256* @param local257* The local address to bind the socket, or {@code null} to258* bind the socket to an automatically assigned socket address259*260* @return This channel261*262* @throws ClosedChannelException263* If this channel is closed264*265* @throws java.nio.channels.AlreadyBoundException266* If this channel is already bound267*268* @throws java.nio.channels.UnsupportedAddressTypeException269* If the type of the given address is not supported270*271* @throws SecurityException272* If a security manager has been installed and its {@link273* java.lang.SecurityManager#checkListen(int) checkListen} method274* denies the operation275*276* @throws IOException277* If some other I/O error occurs278*/279public final SctpMultiChannel bind(SocketAddress local)280throws IOException {281return bind(local, 0);282}283284/**285* Adds the given address to the bound addresses for the channel's286* socket.287*288* <P> The given address must not be the {@link289* java.net.InetAddress#isAnyLocalAddress wildcard} address.290* The channel must be first bound using {@link #bind bind} before291* invoking this method, otherwise {@link NotYetBoundException} is thrown.292* The {@link #bind bind} method takes a {@code SocketAddress} as its293* argument which typically contains a port number as well as an address.294* Addresses subquently bound using this method are simply addresses as the295* SCTP port number remains the same for the lifetime of the channel.296*297* <P> New associations setup after this method successfully completes298* will be associated with the given address. Adding addresses to existing299* associations is optional functionality. If the endpoint supports300* dynamic address reconfiguration then it may send the appropriate message301* to the peer to change the peers address lists.302*303* @param address304* The address to add to the bound addresses for the socket305*306* @return This channel307*308* @throws ClosedChannelException309* If this channel is closed310*311* @throws NotYetBoundException312* If this channel is not yet bound313*314* @throws java.nio.channels.AlreadyBoundException315* If this channel is already bound to the given address316*317* @throws IllegalArgumentException318* If address is {@code null} or the {@link319* java.net.InetAddress#isAnyLocalAddress wildcard} address320*321* @throws IOException322* If some other I/O error occurs323*/324public abstract SctpMultiChannel bindAddress(InetAddress address)325throws IOException;326327/**328* Removes the given address from the bound addresses for the channel's329* socket.330*331* <P> The given address must not be the {@link332* java.net.InetAddress#isAnyLocalAddress wildcard} address.333* The channel must be first bound using {@link #bind bind} before334* invoking this method, otherwise {@link NotYetBoundException} is thrown.335*336* <P> If this method is invoked on a channel that does337* not have {@code address} as one of its bound addresses, or that has only338* one local address bound to it, then this method throws339* {@link IllegalUnbindException}.340*341* <P> The initial address that the channel's socket is bound to using342* {@link #bind bind} may be removed from the bound addresses for the343* channel's socket.344*345* <P> New associations setup after this method successfully completes346* will not be associated with the given address. Removing addresses from347* existing associations is optional functionality. If the endpoint supports348* dynamic address reconfiguration then it may send the appropriate message349* to the peer to change the peers address lists.350*351* @param address352* The address to remove from the bound addresses for the socket353*354* @return This channel355*356* @throws ClosedChannelException357* If this channel is closed358*359* @throws NotYetBoundException360* If this channel is not yet bound361*362* @throws IllegalUnbindException363* {@code address} is not bound to the channel's socket, or the364* channel has only one address bound to it365*366* @throws IllegalArgumentException367* If address is {@code null} or the {@link368* java.net.InetAddress#isAnyLocalAddress wildcard} address369*370* @throws IOException371* If some other I/O error occurs372*/373public abstract SctpMultiChannel unbindAddress(InetAddress address)374throws IOException;375376/**377* Returns all of the socket addresses to which this channel's socket is378* bound.379*380* @return All the socket addresses that this channel's socket is381* bound to, or an empty {@code Set} if the channel's socket is not382* bound383*384* @throws ClosedChannelException385* If the channel is closed386*387* @throws IOException388* If an I/O error occurs389*/390public abstract Set<SocketAddress> getAllLocalAddresses()391throws IOException;392393/**394* Returns all of the remote addresses to which the given association on395* this channel's socket is connected.396*397* @param association398* The association399*400* @return All of the remote addresses for the given association, or401* an empty {@code Set} if the association has been shutdown402*403* @throws ClosedChannelException404* If the channel is closed405*406* @throws IOException407* If an I/O error occurs408*/409public abstract Set<SocketAddress> getRemoteAddresses(Association association)410throws IOException;411412/**413* Shutdown an association without closing the channel.414*415* @param association416* The association to shutdown417*418* @return This channel419*420* @throws ClosedChannelException421* If this channel is closed422*423* @throws IOException424* If some other I/O error occurs425*/426public abstract SctpMultiChannel shutdown(Association association)427throws IOException;428429/**430* Returns the value of a socket option.431*432* <P> Note that some options are retrieved on the channel's socket,433* therefore the {@code association} parameter is not applicable and will be434* ignored if given. However, if the option is association specific then the435* association must be given.436*437* @param <T>438* The type of the socket option value439*440* @param name441* The socket option442*443* @param association444* The association whose option should be retrieved, or {@code null}445* if this option should be retrieved at the channel's socket level.446*447* @return The value of the socket option. A value of {@code null} may be448* a valid value for some socket options.449*450* @throws UnsupportedOperationException451* If the socket option is not supported by this channel452*453* @throws ClosedChannelException454* If this channel is closed455*456* @throws IOException457* If an I/O error occurs458*459* @see SctpStandardSocketOptions460*/461public abstract <T> T getOption(SctpSocketOption<T> name,462Association association)463throws IOException;464465/**466* Sets the value of a socket option.467*468* <P> Note that some options are retrieved on the channel's socket,469* therefore the {@code association} parameter is not applicable and will be470* ignored if given. However, if the option is association specific then the471* association must be given.472*473* @param <T>474* The type of the socket option value475*476* @param name477* The socket option478*479* @param association480* The association whose option should be set, or {@code null}481* if this option should be set at the channel's socket level.482*483* @param value484* The value of the socket option. A value of {@code null} may be485* a valid value for some socket options.486*487* @return This channel488*489* @throws UnsupportedOperationException490* If the socket option is not supported by this channel491*492* @throws IllegalArgumentException493* If the value is not a valid value for this socket option494*495* @throws ClosedChannelException496* If this channel is closed497*498* @throws IOException499* If an I/O error occurs500*501* @see SctpStandardSocketOptions502*/503public abstract <T> SctpMultiChannel setOption(SctpSocketOption<T> name,504T value,505Association association)506throws IOException;507508/**509* Returns a set of the socket options supported by this channel.510*511* <P> This method will continue to return the set of options even after the512* channel has been closed.513*514* @return A set of the socket options supported by this channel515*/516public abstract Set<SctpSocketOption<?>> supportedOptions();517518/**519* Returns an operation set identifying this channel's supported operations.520*521* <P> SCTP multi channels support reading, and writing, so this522* method returns523* {@code (}{@link SelectionKey#OP_READ} {@code |} {@link524* SelectionKey#OP_WRITE}{@code )}. </p>525*526* @return The valid-operation set527*/528@Override529public final int validOps() {530return (SelectionKey.OP_READ |531SelectionKey.OP_WRITE );532}533534/**535* Receives a message and/or handles a notification via this channel.536*537* <P> If a message or notification is immediately available, or if this538* channel is in blocking mode and one eventually becomes available, then539* the message or notification is returned or handled, respectively. If this540* channel is in non-blocking mode and a message or notification is not541* immediately available then this method immediately returns {@code null}.542*543* <P> If this method receives a message it is copied into the given byte544* buffer and an {@link MessageInfo} is returned.545* The message is transferred into the given byte buffer starting at its546* current position and the buffers position is incremented by the number of547* bytes read. If there are fewer bytes remaining in the buffer than are548* required to hold the message, or the underlying input buffer does not549* contain the complete message, then an invocation of {@link550* MessageInfo#isComplete isComplete} on the returned {@code551* MessageInfo} will return {@code false}, and more invocations of this552* method will be necessary to completely consume the messgae. Only553* one message at a time will be partially delivered in any stream. The554* socket option {@link SctpStandardSocketOptions#SCTP_FRAGMENT_INTERLEAVE555* SCTP_FRAGMENT_INTERLEAVE} controls various aspects of what interlacing of556* messages occurs.557*558* <P> If this method receives a notification then the appropriate method of559* the given handler, if there is one, is invoked. If the handler returns {@link560* HandlerResult#CONTINUE CONTINUE} then this method will try to receive another561* message/notification, otherwise, if {@link HandlerResult#RETURN RETURN} is returned562* this method will return {@code null}. If an uncaught exception is thrown by the563* handler it will be propagated up the stack through this method.564*565* <P> If a security manager has been installed then for each new association566* setup this method verifies that the associations source address and port567* number are permitted by the security manager's {@link568* java.lang.SecurityManager#checkAccept(String,int) checkAccept} method.569*570* <P> This method may be invoked at any time. If another thread has571* already initiated a receive operation upon this channel, then an572* invocation of this method will block until the first operation is573* complete. The given handler is invoked without holding any locks used574* to enforce the above synchronization policy, that way handlers575* will not stall other threads from receiving. A handler should not invoke576* the {@code receive} method of this channel, if it does an577* {@link IllegalReceiveException} will be thrown.578*579* @param <T>580* The type of the attachment581*582* @param buffer583* The buffer into which bytes are to be transferred584*585* @param attachment586* The object to attach to the receive operation; can be587* {@code null}588*589* @param handler590* A handler to handle notifications from the SCTP stack, or591* {@code null} to ignore any notifications.592*593* @return The {@code MessageInfo}, {@code null} if this channel is in594* non-blocking mode and no messages are immediately available or595* the notification handler returns {@code RETURN} after handling596* a notification597*598* @throws java.nio.channels.ClosedChannelException599* If this channel is closed600*601* @throws java.nio.channels.AsynchronousCloseException602* If another thread closes this channel603* while the read operation is in progress604*605* @throws java.nio.channels.ClosedByInterruptException606* If another thread interrupts the current thread607* while the read operation is in progress, thereby608* closing the channel and setting the current thread's609* interrupt status610*611* @throws NotYetBoundException612* If this channel is not yet bound613*614* @throws IllegalReceiveException615* If the given handler invokes the {@code receive} method of this616* channel617*618* @throws SecurityException619* If a security manager has been installed and it does not permit620* new associations to be accepted from the message's sender621*622* @throws IOException623* If some other I/O error occurs624*/625public abstract <T> MessageInfo receive(ByteBuffer buffer,626T attachment,627NotificationHandler<T> handler)628throws IOException;629630/**631* Sends a message via this channel.632*633* <P> If this channel is unbound then this method will invoke {@link634* #bind(SocketAddress, int) bind(null, 0)} before sending any data.635*636* <P> If there is no association existing between this channel's socket637* and the intended receiver, identified by the address in the given messageInfo, then one638* will be automatically setup to the intended receiver. This is considered639* to be Implicit Association Setup. Upon successful association setup, an640* {@link AssociationChangeNotification association changed}641* notification will be put to the SCTP stack with its {@code event} parameter set642* to {@link AssociationChangeNotification.AssocChangeEvent#COMM_UP COMM_UP}643* . This notification can be received by invoking {@link #receive644* receive}.645*646* <P> If this channel is in blocking mode, there is sufficient room in the647* underlying output buffer, then the remaining bytes in the given byte648* buffer are transmitted as a single message. Sending a message649* is atomic unless explicit message completion {@link650* SctpStandardSocketOptions#SCTP_EXPLICIT_COMPLETE SCTP_EXPLICIT_COMPLETE}651* socket option is enabled on this channel's socket.652*653* <P> If this channel is in non-blocking mode, there is sufficient room654* in the underlying output buffer, and an implicit association setup is655* required, then the remaining bytes in the given byte buffer are656* transmitted as a single message, subject to {@link657* SctpStandardSocketOptions#SCTP_EXPLICIT_COMPLETE SCTP_EXPLICIT_COMPLETE}.658* If for any reason the message cannot659* be delivered an {@link AssociationChangeNotification association660* changed} notification is put on the SCTP stack with its {@code event} parameter set661* to {@link AssociationChangeNotification.AssocChangeEvent#CANT_START CANT_START}.662*663* <P> The message is transferred from the byte buffer as if by a regular664* {@link java.nio.channels.WritableByteChannel#write(java.nio.ByteBuffer)665* write} operation.666*667* <P> If a security manager has been installed then for each new association668* setup this method verifies that the given remote peers address and port669* number are permitted by the security manager's {@link670* java.lang.SecurityManager#checkConnect(String,int) checkConnect} method.671*672* <P> This method may be invoked at any time. If another thread has already673* initiated a send operation upon this channel, then an invocation of674* this method will block until the first operation is complete.675*676* @param buffer677* The buffer containing the message to be sent678*679* @param messageInfo680* Ancillary data about the message to be sent681*682* @return The number of bytes sent, which will be either the number of683* bytes that were remaining in the messages buffer when this method684* was invoked or, if this channel is non-blocking, may be zero if685* there was insufficient room for the message in the underlying686* output buffer687*688* @throws InvalidStreamException689* If {@code streamNumber} is negative, or if an association already690* exists and {@code streamNumber} is greater than the maximum number691* of outgoing streams692*693* @throws java.nio.channels.ClosedChannelException694* If this channel is closed695*696* @throws java.nio.channels.AsynchronousCloseException697* If another thread closes this channel698* while the read operation is in progress699*700* @throws java.nio.channels.ClosedByInterruptException701* If another thread interrupts the current thread702* while the read operation is in progress, thereby703* closing the channel and setting the current thread's704* interrupt status705*706* @throws SecurityException707* If a security manager has been installed and it does not permit708* new associations to be setup with the the messages's address709*710* @throws IOException711* If some other I/O error occurs712*/713public abstract int send(ByteBuffer buffer, MessageInfo messageInfo)714throws IOException;715716/**717* Branches off an association.718*719* <P> An application can invoke this method to branch off an association720* into a separate channel. The new bound and connected {@link SctpChannel}721* will be created for the association. The branched off association will no722* longer be part of this channel.723*724* <P> This is particularly useful when, for instance, the application725* wishes to have a number of sporadic message senders/receivers remain726* under the original SCTP multi channel but branch off those727* associations carrying high volume data traffic into their own728* separate SCTP channels.729*730* @param association731* The association to branch off732*733* @return The {@code SctpChannel}734*735* @throws java.nio.channels.ClosedChannelException736* If this channel is closed737*738* @throws IOException739* If some other I/O error occurs740*/741public abstract SctpChannel branch(Association association)742throws IOException;743}744745746