Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/channels/AsynchronousServerSocketChannel.java
38918 views
/*1* Copyright (c) 2007, 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*/2425package java.nio.channels;2627import java.nio.channels.spi.*;28import java.net.SocketOption;29import java.net.SocketAddress;30import java.util.concurrent.Future;31import java.io.IOException;3233/**34* An asynchronous channel for stream-oriented listening sockets.35*36* <p> An asynchronous server-socket channel is created by invoking the37* {@link #open open} method of this class.38* A newly-created asynchronous server-socket channel is open but not yet bound.39* It can be bound to a local address and configured to listen for connections40* by invoking the {@link #bind(SocketAddress,int) bind} method. Once bound,41* the {@link #accept(Object,CompletionHandler) accept} method42* is used to initiate the accepting of connections to the channel's socket.43* An attempt to invoke the <tt>accept</tt> method on an unbound channel will44* cause a {@link NotYetBoundException} to be thrown.45*46* <p> Channels of this type are safe for use by multiple concurrent threads47* though at most one accept operation can be outstanding at any time.48* If a thread initiates an accept operation before a previous accept operation49* has completed then an {@link AcceptPendingException} will be thrown.50*51* <p> Socket options are configured using the {@link #setOption(SocketOption,Object)52* setOption} method. Channels of this type support the following options:53* <blockquote>54* <table border summary="Socket options">55* <tr>56* <th>Option Name</th>57* <th>Description</th>58* </tr>59* <tr>60* <td> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </td>61* <td> The size of the socket receive buffer </td>62* </tr>63* <tr>64* <td> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </td>65* <td> Re-use address </td>66* </tr>67* </table>68* </blockquote>69* Additional (implementation specific) options may also be supported.70*71* <p> <b>Usage Example:</b>72* <pre>73* final AsynchronousServerSocketChannel listener =74* AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(5000));75*76* listener.accept(null, new CompletionHandler<AsynchronousSocketChannel,Void>() {77* public void completed(AsynchronousSocketChannel ch, Void att) {78* // accept the next connection79* listener.accept(null, this);80*81* // handle this connection82* handle(ch);83* }84* public void failed(Throwable exc, Void att) {85* ...86* }87* });88* </pre>89*90* @since 1.791*/9293public abstract class AsynchronousServerSocketChannel94implements AsynchronousChannel, NetworkChannel95{96private final AsynchronousChannelProvider provider;9798/**99* Initializes a new instance of this class.100*101* @param provider102* The provider that created this channel103*/104protected AsynchronousServerSocketChannel(AsynchronousChannelProvider provider) {105this.provider = provider;106}107108/**109* Returns the provider that created this channel.110*111* @return The provider that created this channel112*/113public final AsynchronousChannelProvider provider() {114return provider;115}116117/**118* Opens an asynchronous server-socket channel.119*120* <p> The new channel is created by invoking the {@link121* java.nio.channels.spi.AsynchronousChannelProvider#openAsynchronousServerSocketChannel122* openAsynchronousServerSocketChannel} method on the {@link123* java.nio.channels.spi.AsynchronousChannelProvider} object that created124* the given group. If the group parameter is <tt>null</tt> then the125* resulting channel is created by the system-wide default provider, and126* bound to the <em>default group</em>.127*128* @param group129* The group to which the newly constructed channel should be bound,130* or <tt>null</tt> for the default group131*132* @return A new asynchronous server socket channel133*134* @throws ShutdownChannelGroupException135* If the channel group is shutdown136* @throws IOException137* If an I/O error occurs138*/139public static AsynchronousServerSocketChannel open(AsynchronousChannelGroup group)140throws IOException141{142AsynchronousChannelProvider provider = (group == null) ?143AsynchronousChannelProvider.provider() : group.provider();144return provider.openAsynchronousServerSocketChannel(group);145}146147/**148* Opens an asynchronous server-socket channel.149*150* <p> This method returns an asynchronous server socket channel that is151* bound to the <em>default group</em>. This method is equivalent to evaluating152* the expression:153* <blockquote><pre>154* open((AsynchronousChannelGroup)null);155* </pre></blockquote>156*157* @return A new asynchronous server socket channel158*159* @throws IOException160* If an I/O error occurs161*/162public static AsynchronousServerSocketChannel open()163throws IOException164{165return open(null);166}167168/**169* Binds the channel's socket to a local address and configures the socket to170* listen for connections.171*172* <p> An invocation of this method is equivalent to the following:173* <blockquote><pre>174* bind(local, 0);175* </pre></blockquote>176*177* @param local178* The local address to bind the socket, or <tt>null</tt> to bind179* to an automatically assigned socket address180*181* @return This channel182*183* @throws AlreadyBoundException {@inheritDoc}184* @throws UnsupportedAddressTypeException {@inheritDoc}185* @throws SecurityException {@inheritDoc}186* @throws ClosedChannelException {@inheritDoc}187* @throws IOException {@inheritDoc}188*/189public final AsynchronousServerSocketChannel bind(SocketAddress local)190throws IOException191{192return bind(local, 0);193}194195/**196* Binds the channel's socket to a local address and configures the socket to197* listen for connections.198*199* <p> This method is used to establish an association between the socket and200* a local address. Once an association is established then the socket remains201* bound until the associated channel is closed.202*203* <p> The {@code backlog} parameter is the maximum number of pending204* connections on the socket. Its exact semantics are implementation specific.205* In particular, an implementation may impose a maximum length or may choose206* to ignore the parameter altogther. If the {@code backlog} parameter has207* the value {@code 0}, or a negative value, then an implementation specific208* default is used.209*210* @param local211* The local address to bind the socket, or {@code null} to bind212* to an automatically assigned socket address213* @param backlog214* The maximum number of pending connections215*216* @return This channel217*218* @throws AlreadyBoundException219* If the socket is already bound220* @throws UnsupportedAddressTypeException221* If the type of the given address is not supported222* @throws SecurityException223* If a security manager has been installed and its {@link224* SecurityManager#checkListen checkListen} method denies the operation225* @throws ClosedChannelException226* If the channel is closed227* @throws IOException228* If some other I/O error occurs229*/230public abstract AsynchronousServerSocketChannel bind(SocketAddress local, int backlog)231throws IOException;232233/**234* @throws IllegalArgumentException {@inheritDoc}235* @throws ClosedChannelException {@inheritDoc}236* @throws IOException {@inheritDoc}237*/238public abstract <T> AsynchronousServerSocketChannel setOption(SocketOption<T> name, T value)239throws IOException;240241/**242* Accepts a connection.243*244* <p> This method initiates an asynchronous operation to accept a245* connection made to this channel's socket. The {@code handler} parameter is246* a completion handler that is invoked when a connection is accepted (or247* the operation fails). The result passed to the completion handler is248* the {@link AsynchronousSocketChannel} to the new connection.249*250* <p> When a new connection is accepted then the resulting {@code251* AsynchronousSocketChannel} will be bound to the same {@link252* AsynchronousChannelGroup} as this channel. If the group is {@link253* AsynchronousChannelGroup#isShutdown shutdown} and a connection is accepted,254* then the connection is closed, and the operation completes with an {@code255* IOException} and cause {@link ShutdownChannelGroupException}.256*257* <p> To allow for concurrent handling of new connections, the completion258* handler is not invoked directly by the initiating thread when a new259* connection is accepted immediately (see <a260* href="AsynchronousChannelGroup.html#threading">Threading</a>).261*262* <p> If a security manager has been installed then it verifies that the263* address and port number of the connection's remote endpoint are permitted264* by the security manager's {@link SecurityManager#checkAccept checkAccept}265* method. The permission check is performed with privileges that are restricted266* by the calling context of this method. If the permission check fails then267* the connection is closed and the operation completes with a {@link268* SecurityException}.269*270* @param <A>271* The type of the attachment272* @param attachment273* The object to attach to the I/O operation; can be {@code null}274* @param handler275* The handler for consuming the result276*277* @throws AcceptPendingException278* If an accept operation is already in progress on this channel279* @throws NotYetBoundException280* If this channel's socket has not yet been bound281* @throws ShutdownChannelGroupException282* If the channel group has terminated283*/284public abstract <A> void accept(A attachment,285CompletionHandler<AsynchronousSocketChannel,? super A> handler);286287/**288* Accepts a connection.289*290* <p> This method initiates an asynchronous operation to accept a291* connection made to this channel's socket. The method behaves in exactly292* the same manner as the {@link #accept(Object, CompletionHandler)} method293* except that instead of specifying a completion handler, this method294* returns a {@code Future} representing the pending result. The {@code295* Future}'s {@link Future#get() get} method returns the {@link296* AsynchronousSocketChannel} to the new connection on successful completion.297*298* @return a {@code Future} object representing the pending result299*300* @throws AcceptPendingException301* If an accept operation is already in progress on this channel302* @throws NotYetBoundException303* If this channel's socket has not yet been bound304*/305public abstract Future<AsynchronousSocketChannel> accept();306307/**308* {@inheritDoc}309* <p>310* If there is a security manager set, its {@code checkConnect} method is311* called with the local address and {@code -1} as its arguments to see312* if the operation is allowed. If the operation is not allowed,313* a {@code SocketAddress} representing the314* {@link java.net.InetAddress#getLoopbackAddress loopback} address and the315* local port of the channel's socket is returned.316*317* @return The {@code SocketAddress} that the socket is bound to, or the318* {@code SocketAddress} representing the loopback address if319* denied by the security manager, or {@code null} if the320* channel's socket is not bound321*322* @throws ClosedChannelException {@inheritDoc}323* @throws IOException {@inheritDoc}324*/325@Override326public abstract SocketAddress getLocalAddress() throws IOException;327}328329330