/* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package io.netty.channel; import io.netty.buffer.ByteBuf; import io.netty.buffer.MessageBuf; import io.netty.channel.socket.DatagramChannel; import io.netty.channel.socket.ServerSocketChannel; import io.netty.channel.socket.SocketChannel; import io.netty.util.AttributeMap; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.channels.SelectionKey; /** * A nexus to a network socket or a component which is capable of I/O * operations such as read, write, connect, and bind. *

* A channel provides a user: *

* *

All I/O operations are asynchronous.

*

* All I/O operations in Netty are asynchronous. It means any I/O calls will * return immediately with no guarantee that the requested I/O operation has * been completed at the end of the call. Instead, you will be returned with * a {@link ChannelFuture} instance which will notify you when the requested I/O * operation has succeeded, failed, or canceled. * *

Channels are hierarchical

*

* A {@link Channel} can have a {@linkplain #getParent() parent} depending on * how it was created. For instance, a {@link SocketChannel}, that was accepted * by {@link ServerSocketChannel}, will return the {@link ServerSocketChannel} * as its parent on {@link #getParent()}. *

* The semantics of the hierarchical structure depends on the transport * implementation where the {@link Channel} belongs to. For example, you could * write a new {@link Channel} implementation that creates the sub-channels that * share one socket connection, as BEEP and * SSH do. * *

Downcast to access transport-specific operations

*

* Some transports exposes additional operations that is specific to the * transport. Down-cast the {@link Channel} to sub-type to invoke such * operations. For example, with the old I/O datagram transport, multicast * join / leave operations are provided by {@link DatagramChannel}. * *

InterestOps

*

* A {@link Channel} has a property called {@link #getInterestOps() interestOps} * which is similar to that of {@link SelectionKey#interestOps() NIO SelectionKey}. * It is represented as a bit * field which is composed of the two flags. *

*

* You can set or clear the {@link #OP_READ} flag to suspend and resume read * operation via {@link #setReadable(boolean)}. *

* Please note that you cannot suspend or resume write operation just like you * can set or clear {@link #OP_READ}. The {@link #OP_WRITE} flag is read only * and provided simply as a mean to tell you if the size of pending write * requests exceeded a certain threshold or not so that you don't issue too many * pending writes that lead to an {@link OutOfMemoryError}. For example, the * NIO socket transport uses the {@code writeBufferLowWaterMark} and * {@code writeBufferHighWaterMark} properties in {@link NioSocketChannelConfig} * to determine when to set or clear the {@link #OP_WRITE} flag. *

* @apiviz.landmark * @apiviz.composedOf io.netty.channel.ChannelConfig * @apiviz.composedOf io.netty.channel.ChannelPipeline * * @apiviz.exclude ^io\.netty\.channel\.([a-z]+\.)+[^\.]+Channel$ */ public interface Channel extends AttributeMap, ChannelOutboundInvoker, ChannelFutureFactory, Comparable { /** * Returns the unique integer ID of this channel. */ Integer id(); EventLoop eventLoop(); /** * Returns the parent of this channel. * * @return the parent channel. * {@code null} if this channel does not have a parent channel. */ Channel parent(); /** * Returns the configuration of this channel. */ ChannelConfig config(); /** * Returns the {@link ChannelPipeline} which handles {@link ChannelEvent}s * associated with this channel. */ ChannelPipeline pipeline(); boolean isOpen(); boolean isRegistered(); boolean isActive(); ChannelBufferType bufferType(); ByteBuf outboundByteBuffer(); MessageBuf outboundMessageBuffer(); /** * Returns the local address where this channel is bound to. The returned * {@link SocketAddress} is supposed to be down-cast into more concrete * type such as {@link InetSocketAddress} to retrieve the detailed * information. * * @return the local address of this channel. * {@code null} if this channel is not bound. */ SocketAddress localAddress(); /** * Returns the remote address where this channel is connected to. The * returned {@link SocketAddress} is supposed to be down-cast into more * concrete type such as {@link InetSocketAddress} to retrieve the detailed * information. * * @return the remote address of this channel. * {@code null} if this channel is not connected. * If this channel is not connected but it can receive messages * from arbitrary remote addresses (e.g. {@link DatagramChannel}, * use {@link MessageEvent#getRemoteAddress()} to determine * the origination of the received message as this method will * return {@code null}. */ SocketAddress remoteAddress(); /** * Returns the {@link ChannelFuture} which will be notified when this * channel is closed. This method always returns the same future instance. */ ChannelFuture closeFuture(); Unsafe unsafe(); public interface Unsafe { ChannelHandlerContext directOutboundContext(); ChannelFuture voidFuture(); SocketAddress localAddress(); SocketAddress remoteAddress(); void register(EventLoop eventLoop, ChannelFuture future); void bind(SocketAddress localAddress, ChannelFuture future); void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future); void disconnect(ChannelFuture future); void close(ChannelFuture future); void deregister(ChannelFuture future); void flush(ChannelFuture future); void flushNow(); } }