Interface Channel
-
- All Superinterfaces:
java.lang.Comparable<Channel>
- All Known Subinterfaces:
DatagramChannel,LocalChannel,LocalServerChannel,ServerChannel,ServerSocketChannel,SocketChannel
- All Known Implementing Classes:
AbstractChannel,AbstractNioChannel,AbstractOioChannel,AbstractServerChannel,DefaultLocalChannel,DefaultLocalServerChannel,EmbeddedChannel,HttpTunnelingClientSocketChannel,NioAcceptedSocketChannel,NioClientSocketChannel,NioDatagramChannel,NioServerSocketChannel,NioSocketChannel,OioAcceptedSocketChannel,OioClientSocketChannel,OioDatagramChannel,OioServerSocketChannel,OioSocketChannel
public interface Channel extends java.lang.Comparable<Channel>
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:
- the current state of the channel (e.g. is it open? is it connected?),
- the configuration parameters of the channel (e.g. receive buffer size),
- the I/O operations that the channel supports (e.g. read, write, connect, and bind), and
- the
ChannelPipelinewhich handles all I/O events and requests associated with the channel.
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
ChannelFutureinstance which will notify you when the requested I/O operation has succeeded, failed, or canceled.Channels are hierarchical
A
Channelcan have a parent depending on how it was created. For instance, aSocketChannel, that was accepted byServerSocketChannel, will return theServerSocketChannelas its parent ongetParent().The semantics of the hierarchical structure depends on the transport implementation where the
Channelbelongs to. For example, you could write a newChannelimplementation 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
Channelto sub-type to invoke such operations. For example, with the old I/O datagram transport, multicast join / leave operations are provided byDatagramChannel.InterestOps
A
Channelhas a property calledinterestOpswhich is similar to that ofNIO SelectionKey. It is represented as a bit field which is composed of the two flags.OP_READ- If set, a message sent by a remote peer will be read immediately. If unset, the message from the remote peer will not be read until theOP_READflag is set again (i.e. read suspension).OP_WRITE- If set, a write request will not be sent to a remote peer until theOP_WRITEflag is cleared and the write request will be pending in a queue. If unset, the write request will be flushed out as soon as possible from the queue.OP_READ_WRITE- This is a combination ofOP_READandOP_WRITE, which means only write requests are suspended.OP_NONE- This is a combination of (NOTOP_READ) and (NOTOP_WRITE), which means only read operation is suspended.
You can set or clear the
OP_READflag to suspend and resume read operation viasetReadable(boolean).Please note that you cannot suspend or resume write operation just like you can set or clear
OP_READ. TheOP_WRITEflag 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 anOutOfMemoryError. For example, the NIO socket transport uses thewriteBufferLowWaterMarkandwriteBufferHighWaterMarkproperties inNioSocketChannelConfigto determine when to set or clear theOP_WRITEflag.
-
-
Field Summary
Fields Modifier and Type Field Description static intOP_NONETheinterestOpsvalue which tells that only read operation has been suspended.static intOP_READTheinterestOpsvalue which tells that neither read nor write operation has been suspended.static intOP_READ_WRITETheinterestOpsvalue which tells that only write operation has been suspended.static intOP_WRITETheinterestOpsvalue which tells that both read and write operation has been suspended.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ChannelFuturebind(java.net.SocketAddress localAddress)Binds this channel to the specified local address asynchronously.ChannelFutureclose()Closes this channel asynchronously.ChannelFutureconnect(java.net.SocketAddress remoteAddress)Connects this channel to the specified remote address asynchronously.ChannelFuturedisconnect()Disconnects this channel from the current remote address asynchronously.java.lang.ObjectgetAttachment()ChannelFuturegetCloseFuture()Returns theChannelFuturewhich will be notified when this channel is closed.ChannelConfiggetConfig()Returns the configuration of this channel.ChannelFactorygetFactory()Returns theChannelFactorywhich created this channel.java.lang.IntegergetId()Returns the unique integer ID of this channel.intgetInterestOps()Returns the currentinterestOpsof this channel.java.net.SocketAddressgetLocalAddress()Returns the local address where this channel is bound to.ChannelgetParent()Returns the parent of this channel.ChannelPipelinegetPipeline()Returns theChannelPipelinewhich handlesChannelEvents associated with this channel.java.net.SocketAddressgetRemoteAddress()Returns the remote address where this channel is connected to.booleangetUserDefinedWritability(int index)Returnstrueif and only if the user-defined writability flag at the specified index is set totrue.booleanisBound()Returnstrueif and only if this channel is bound to a local address.booleanisConnected()Returnstrueif and only if this channel is connected to a remote address.booleanisOpen()Returnstrueif and only if this channel is open.booleanisReadable()Returnstrueif and only if the I/O thread will read a message from this channel.booleanisWritable()Returnstrueif and only if the I/O thread will perform the requested write operation immediately.voidsetAttachment(java.lang.Object attachment)Attaches an object to thisChannelto store a stateful informationChannelFuturesetInterestOps(int interestOps)Changes theinterestOpsof this channel asynchronously.ChannelFuturesetReadable(boolean readable)Suspends or resumes the read operation of the I/O thread asynchronously.voidsetUserDefinedWritability(int index, boolean isWritable)Sets a user-defined writability flag at the specified index.ChannelFutureunbind()Unbinds this channel from the current local address asynchronously.ChannelFuturewrite(java.lang.Object message)Sends a message to this channel asynchronously.ChannelFuturewrite(java.lang.Object message, java.net.SocketAddress remoteAddress)Sends a message to this channel asynchronously.
-
-
-
Field Detail
-
OP_NONE
static final int OP_NONE
TheinterestOpsvalue which tells that only read operation has been suspended.- See Also:
- Constant Field Values
-
OP_READ
static final int OP_READ
TheinterestOpsvalue which tells that neither read nor write operation has been suspended.- See Also:
- Constant Field Values
-
OP_WRITE
static final int OP_WRITE
TheinterestOpsvalue which tells that both read and write operation has been suspended.- See Also:
- Constant Field Values
-
OP_READ_WRITE
static final int OP_READ_WRITE
TheinterestOpsvalue which tells that only write operation has been suspended.- See Also:
- Constant Field Values
-
-
Method Detail
-
getId
java.lang.Integer getId()
Returns the unique integer ID of this channel.
-
getFactory
ChannelFactory getFactory()
Returns theChannelFactorywhich created this channel.
-
getParent
Channel getParent()
Returns the parent of this channel.- Returns:
- the parent channel.
nullif this channel does not have a parent channel.
-
getConfig
ChannelConfig getConfig()
Returns the configuration of this channel.
-
getPipeline
ChannelPipeline getPipeline()
Returns theChannelPipelinewhich handlesChannelEvents associated with this channel.
-
isOpen
boolean isOpen()
Returnstrueif and only if this channel is open.
-
isBound
boolean isBound()
Returnstrueif and only if this channel is bound to a local address.
-
isConnected
boolean isConnected()
Returnstrueif and only if this channel is connected to a remote address.
-
getLocalAddress
java.net.SocketAddress getLocalAddress()
Returns the local address where this channel is bound to. The returnedSocketAddressis supposed to be down-cast into more concrete type such asInetSocketAddressto retrieve the detailed information.- Returns:
- the local address of this channel.
nullif this channel is not bound.
-
getRemoteAddress
java.net.SocketAddress getRemoteAddress()
Returns the remote address where this channel is connected to. The returnedSocketAddressis supposed to be down-cast into more concrete type such asInetSocketAddressto retrieve the detailed information.- Returns:
- the remote address of this channel.
nullif this channel is not connected. If this channel is not connected but it can receive messages from arbitrary remote addresses (e.g.DatagramChannel, useMessageEvent.getRemoteAddress()to determine the origination of the received message as this method will returnnull.
-
write
ChannelFuture write(java.lang.Object message)
Sends a message to this channel asynchronously. If this channel was created by a connectionless transport (e.g.DatagramChannel) and is not connected yet, you have to callwrite(Object, SocketAddress)instead. Otherwise, the write request will fail withNotYetConnectedExceptionand an'exceptionCaught'event will be triggered.- Parameters:
message- the message to write- Returns:
- the
ChannelFuturewhich will be notified when the write request succeeds or fails - Throws:
java.lang.NullPointerException- if the specified message isnull
-
write
ChannelFuture write(java.lang.Object message, java.net.SocketAddress remoteAddress)
Sends a message to this channel asynchronously. It has an additional parameter that allows a user to specify where to send the specified message instead of this channel's current remote address. If this channel was created by a connectionless transport (e.g.DatagramChannel) and is not connected yet, you must specify non-null address. Otherwise, the write request will fail withNotYetConnectedExceptionand an'exceptionCaught'event will be triggered.- Parameters:
message- the message to writeremoteAddress- where to send the specified message. This method is identical towrite(Object)ifnullis specified here.- Returns:
- the
ChannelFuturewhich will be notified when the write request succeeds or fails - Throws:
java.lang.NullPointerException- if the specified message isnull
-
bind
ChannelFuture bind(java.net.SocketAddress localAddress)
Binds this channel to the specified local address asynchronously.- Parameters:
localAddress- where to bind- Returns:
- the
ChannelFuturewhich will be notified when the bind request succeeds or fails - Throws:
java.lang.NullPointerException- if the specified address isnull
-
connect
ChannelFuture connect(java.net.SocketAddress remoteAddress)
Connects this channel to the specified remote address asynchronously.- Parameters:
remoteAddress- where to connect- Returns:
- the
ChannelFuturewhich will be notified when the connection request succeeds or fails - Throws:
java.lang.NullPointerException- if the specified address isnull
-
disconnect
ChannelFuture disconnect()
Disconnects this channel from the current remote address asynchronously.- Returns:
- the
ChannelFuturewhich will be notified when the disconnection request succeeds or fails
-
unbind
ChannelFuture unbind()
Unbinds this channel from the current local address asynchronously.- Returns:
- the
ChannelFuturewhich will be notified when the unbind request succeeds or fails
-
close
ChannelFuture close()
Closes this channel asynchronously. If this channel is bound or connected, it will be disconnected and unbound first. Once a channel is closed, it can not be open again. Calling this method on a closed channel has no effect. Please note that this method always returns the same future instance.- Returns:
- the
ChannelFuturewhich will be notified when the close request succeeds or fails
-
getCloseFuture
ChannelFuture getCloseFuture()
Returns theChannelFuturewhich will be notified when this channel is closed. This method always returns the same future instance.
-
getInterestOps
int getInterestOps()
Returns the currentinterestOpsof this channel.- Returns:
OP_NONE,OP_READ,OP_WRITE, orOP_READ_WRITE
-
isReadable
boolean isReadable()
Returnstrueif and only if the I/O thread will read a message from this channel. This method is a shortcut to the following code:return (getInterestOps() & OP_READ) != 0;
-
isWritable
boolean isWritable()
Returnstrueif and only if the I/O thread will perform the requested write operation immediately. Any write requests made when this method returnsfalseare queued until the I/O thread is ready to process the queued write requests. This method is a shortcut to the following code:return (getInterestOps() & OP_WRITE) == 0;
-
setInterestOps
ChannelFuture setInterestOps(int interestOps)
Changes theinterestOpsof this channel asynchronously.- Parameters:
interestOps- the newinterestOps- Returns:
- the
ChannelFuturewhich will be notified when theinterestOpschange request succeeds or fails
-
setReadable
ChannelFuture setReadable(boolean readable)
Suspends or resumes the read operation of the I/O thread asynchronously. This method is a shortcut to the following code:int interestOps = getInterestOps(); if (readable) { setInterestOps(interestOps | OP_READ); } else { setInterestOps(interestOps & ~OP_READ); }- Parameters:
readable-trueto resume the read operation andfalseto suspend the read operation- Returns:
- the
ChannelFuturewhich will be notified when theinterestOpschange request succeeds or fails
-
getUserDefinedWritability
boolean getUserDefinedWritability(int index)
Returnstrueif and only if the user-defined writability flag at the specified index is set totrue.
-
setUserDefinedWritability
void setUserDefinedWritability(int index, boolean isWritable)Sets a user-defined writability flag at the specified index.
-
getAttachment
java.lang.Object getAttachment()
- Returns:
nullif no object was attached ornullwas attached
-
setAttachment
void setAttachment(java.lang.Object attachment)
Attaches an object to thisChannelto store a stateful information
-
-