public class NioSocketChannel extends AbstractNioByteChannel implements SocketChannel
SocketChannel
which uses NIO selector based implementation.AbstractNioChannel.AbstractNioUnsafe, AbstractNioChannel.NioUnsafe
AbstractChannel.AbstractUnsafe
Channel.Unsafe
readInterestOp
flushFutureNotifier
Constructor and Description |
---|
NioSocketChannel()
Create a new instance
|
NioSocketChannel(Channel parent,
Integer id,
SocketChannel socket)
Create a new instance
|
NioSocketChannel(SocketChannel socket)
Create a new instance using the given
SocketChannel . |
Modifier and Type | Method and Description |
---|---|
ByteBufAllocator |
alloc()
Return the assigned
ByteBufAllocator which will be used for allocate ByteBuf s. |
ChannelFuture |
bind(SocketAddress localAddress)
Request to bind to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
bind(SocketAddress localAddress,
ChannelPromise promise)
Request to bind to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
close()
Request to close this ChannelOutboundInvoker and notify the
ChannelFuture once the operation completes,
either because the operation was successful or because of
an error. |
ChannelFuture |
close(ChannelPromise promise)
Request to close this ChannelOutboundInvoker and notify the
ChannelFuture once the operation completes,
either because the operation was successful or because of
an error. |
SocketChannelConfig |
config()
Returns the configuration of this channel.
|
ChannelFuture |
connect(SocketAddress remoteAddress)
Request to connect to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
connect(SocketAddress remoteAddress,
ChannelPromise promise)
Request to connect to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
connect(SocketAddress remoteAddress,
SocketAddress localAddress)
Request to connect to the given
SocketAddress while bind to the localAddress and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
connect(SocketAddress remoteAddress,
SocketAddress localAddress,
ChannelPromise promise)
Request to connect to the given
SocketAddress while bind to the localAddress and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
deregister()
Request to deregister this ChannelOutboundInvoker from the previous assigned
EventExecutor and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
deregister(ChannelPromise promise)
Request to deregister this ChannelOutboundInvoker from the previous assigned
EventExecutor and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
disconnect()
Request to discconect from the remote peer and notify the
ChannelFuture once the operation completes,
either because the operation was successful or because of an error. |
ChannelFuture |
disconnect(ChannelPromise promise)
Request to discconect from the remote peer and notify the
ChannelFuture once the operation completes,
either because the operation was successful or because of an error. |
protected void |
doBind(SocketAddress localAddress)
Bind the
Channel to the SocketAddress |
protected void |
doClose()
Close the
Channel |
protected boolean |
doConnect(SocketAddress remoteAddress,
SocketAddress localAddress)
Conect to the remote peer
|
protected void |
doDisconnect()
Disconnect this
Channel from its remote peer |
protected void |
doFinishConnect()
Finish the connect
|
protected int |
doReadBytes(ByteBuf byteBuf)
Read bytes into the given
ByteBuf and return the amount. |
protected int |
doWriteBytes(ByteBuf buf,
boolean lastSpin)
|
ChannelFuture |
flush()
Request to flush all pending data which belongs to this ChannelOutboundInvoker and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
flush(ChannelPromise promise)
Request to flush all pending data which belongs to this ChannelOutboundInvoker and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
boolean |
isActive()
Return
true if the Channel is active and so connected. |
boolean |
isInputShutdown()
Return
true if the input of this Channel is shutdown |
boolean |
isOutputShutdown() |
protected SocketChannel |
javaChannel() |
InetSocketAddress |
localAddress()
Returns the local address where this channel is bound to.
|
protected SocketAddress |
localAddress0()
Returns the
SocketAddress which is bound locally. |
ChannelMetadata |
metadata()
|
ChannelFuture |
newFailedFuture(Throwable cause)
Create a new
Future which is marked as fakued already. |
ChannelPromise |
newPromise()
Return a new
Promise . |
ChannelFuture |
newSucceededFuture()
Create a new
Future which is marked as successes already. |
ServerSocketChannel |
parent()
Returns the parent of this channel.
|
ChannelPipeline |
pipeline()
Return the assigned
ChannelPipeline |
void |
read()
Request to Read data from the
Channel into the first inbound buffer, triggers an
inboundBufferUpdated event if data was
read, and triggers an
channelReadSuspended event so the
handler can decide to continue reading. |
InetSocketAddress |
remoteAddress()
Returns the remote address where this channel is connected to.
|
protected SocketAddress |
remoteAddress0()
Return the
SocketAddress which the Channel is connected to. |
ChannelFuture |
sendFile(FileRegion region)
Request to send a
FileRegion via this ChannelOutboundInvoker and notify the ChannelFuture
once the operation completes, either because the operation was successful or because of an error. |
ChannelFuture |
sendFile(FileRegion region,
ChannelPromise promise)
Request to send a
FileRegion via this ChannelOutboundInvoker and notify the ChannelFuture
once the operation completes, either because the operation was successful or because of an error. |
ChannelFuture |
shutdownOutput() |
ChannelFuture |
shutdownOutput(ChannelPromise promise) |
ChannelFuture |
write(Object message)
Request to write a message via this ChannelOutboundInvoker and notify the
ChannelFuture
once the operation completes, either because the operation was successful or because of an error. |
ChannelFuture |
write(Object message,
ChannelPromise promise)
Request to write a message via this ChannelOutboundInvoker and notify the
ChannelFuture
once the operation completes, either because the operation was successful or because of an error. |
doFlushByteBuffer, doFlushFileRegion, newUnsafe
doBeginRead, doDeregister, doRegister, eventLoop, isCompatible, isFlushPending, isOpen, selectionKey, unsafe
alloc, bind, bind, checkEOF, close, close, closeFuture, compareTo, connect, connect, connect, connect, deregister, deregister, disconnect, disconnect, doFlushMessageBuffer, doPreClose, equals, expandReadBuffer, flush, flush, hashCode, id, invalidateLocalAddress, invalidateRemoteAddress, isRegistered, newFailedFuture, newPromise, newSucceededFuture, outboundByteBuffer, outboundMessageBuffer, pipeline, read, sendFile, sendFile, toString, write, write
attr
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
closeFuture, eventLoop, id, isOpen, isRegistered, outboundByteBuffer, outboundMessageBuffer, unsafe
attr
compareTo
public NioSocketChannel()
public NioSocketChannel(SocketChannel socket)
SocketChannel
.public NioSocketChannel(Channel parent, Integer id, SocketChannel socket)
parent
- the Channel
which created this instance or null
if it was created by the userid
- the id to use for this instance or null
if a new one should be generatedsocket
- the SocketChannel
which will be usedpublic ServerSocketChannel parent()
Channel
parent
in interface Channel
parent
in interface SocketChannel
parent
in class AbstractChannel
null
if this channel does not have a parent channel.public ChannelMetadata metadata()
Channel
public SocketChannelConfig config()
Channel
config
in interface Channel
config
in interface SocketChannel
protected SocketChannel javaChannel()
javaChannel
in class AbstractNioChannel
public boolean isActive()
Channel
true
if the Channel
is active and so connected.public boolean isInputShutdown()
AbstractNioChannel
true
if the input of this Channel
is shutdownisInputShutdown
in interface SocketChannel
isInputShutdown
in class AbstractNioChannel
public InetSocketAddress localAddress()
Channel
SocketAddress
is supposed to be down-cast into more concrete
type such as InetSocketAddress
to retrieve the detailed
information.localAddress
in interface Channel
localAddress
in interface SocketChannel
localAddress
in class AbstractChannel
null
if this channel is not bound.public InetSocketAddress remoteAddress()
Channel
SocketAddress
is supposed to be down-cast into more
concrete type such as InetSocketAddress
to retrieve the detailed
information.remoteAddress
in interface Channel
remoteAddress
in interface SocketChannel
remoteAddress
in class AbstractChannel
null
if this channel is not connected.
If this channel is not connected but it can receive messages
from arbitrary remote addresses (e.g. DatagramChannel
,
use DatagramPacket.remoteAddress()
to determine
the origination of the received message as this method will
return null
.public boolean isOutputShutdown()
isOutputShutdown
in interface SocketChannel
Socket.isOutputShutdown()
public ChannelFuture shutdownOutput()
shutdownOutput
in interface SocketChannel
Socket.shutdownOutput()
public ChannelFuture shutdownOutput(ChannelPromise promise)
shutdownOutput
in interface SocketChannel
Will notify the given {@link ChannelPromise}
protected SocketAddress localAddress0()
AbstractChannel
SocketAddress
which is bound locally.localAddress0
in class AbstractChannel
protected SocketAddress remoteAddress0()
AbstractChannel
SocketAddress
which the Channel
is connected to.remoteAddress0
in class AbstractChannel
protected void doBind(SocketAddress localAddress) throws Exception
AbstractChannel
Channel
to the SocketAddress
doBind
in class AbstractChannel
Exception
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception
AbstractNioChannel
doConnect
in class AbstractNioChannel
Exception
protected void doFinishConnect() throws Exception
AbstractNioChannel
doFinishConnect
in class AbstractNioChannel
Exception
protected void doDisconnect() throws Exception
AbstractChannel
Channel
from its remote peerdoDisconnect
in class AbstractChannel
Exception
protected void doClose() throws Exception
AbstractChannel
Channel
doClose
in class AbstractChannel
Exception
protected int doReadBytes(ByteBuf byteBuf) throws Exception
AbstractNioByteChannel
ByteBuf
and return the amount.doReadBytes
in class AbstractNioByteChannel
Exception
protected int doWriteBytes(ByteBuf buf, boolean lastSpin) throws Exception
AbstractNioByteChannel
doWriteBytes
in class AbstractNioByteChannel
buf
- the ByteBuf
from which the bytes should be writtenlastSpin
- true
if this is the last write tryException
- thrown if an error accourpublic ChannelFuture bind(SocketAddress localAddress)
SocketAddress
and notify the ChannelFuture
once the operation
completes, either because the operation was successful or because of an error.
This will result in having the
ChannelOperationHandler.bind(ChannelHandlerContext, SocketAddress, ChannelPromise)
method
called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture connect(SocketAddress remoteAddress)
SocketAddress
and notify the ChannelFuture
once the operation
completes, either because the operation was successful or because of an error.
If the connection fails because of a connection timeout, the ChannelFuture
will get failed with
a ConnectTimeoutException
. If it fails because of connection refused a ConnectException
will be used.
This will result in having the
ChannelOperationHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress)
SocketAddress
while bind to the localAddress and notify the
ChannelFuture
once the operation completes, either because the operation was successful or because of
an error.
This will result in having the
ChannelOperationHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture disconnect()
ChannelFuture
once the operation completes,
either because the operation was successful or because of an error.
This will result in having the
ChannelOperationHandler.disconnect(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture close()
ChannelFuture
once the operation completes,
either because the operation was successful or because of
an error.
After it is closed it is not possible to reuse it again.
This will result in having the
ChannelOperationHandler.close(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture deregister()
EventExecutor
and notify the
ChannelFuture
once the operation completes, either because the operation was successful or because of
an error.
This will result in having the
ChannelOperationHandler.deregister(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture flush()
ChannelFuture
once the operation completes, either because the operation was successful or because of
an error.
Be aware that the flush could be only partially successful. In such cases the ChannelFuture
will be
failed with an IncompleteFlushException
. So if you are interested to know if it was partial successful
you need to check if the returned Future.cause()
returns an instance of
IncompleteFlushException
. In such cases you may want to call flush(ChannelPromise)
or
flush()
to flush the rest of the data or just close the connection via close(ChannelPromise)
or
close()
if it is not possible to recover.
This will result in having the
ChannelOperationHandler.flush(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture write(Object message)
ChannelFuture
once the operation completes, either because the operation was successful or because of an error.
If you want to write a FileRegion
use sendFile(FileRegion)
.
Be aware that the write could be only partially successful as the message may need to get encoded before write it
to the remote peer. In such cases the ChannelFuture
will be failed with a
IncompleteFlushException
. In such cases you may want to call flush(ChannelPromise)
or
flush()
to flush the rest of the data or just close the connection via close(ChannelPromise)
or close()
if it is not possible to recover.
This will result in having the message added to the outbound buffer of the next ChannelOutboundHandler
and the ChannelOperationHandler.flush(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture sendFile(FileRegion region)
FileRegion
via this ChannelOutboundInvoker and notify the ChannelFuture
once the operation completes, either because the operation was successful or because of an error.
This will result in having the
ChannelOperationHandler.sendFile(ChannelHandlerContext, FileRegion, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise)
SocketAddress
and notify the ChannelFuture
once the operation
completes, either because the operation was successful or because of an error.
The given ChannelPromise
will be notified.
This will result in having the
ChannelOperationHandler.bind(ChannelHandlerContext, SocketAddress, ChannelPromise)
method
called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise)
SocketAddress
and notify the ChannelFuture
once the operation
completes, either because the operation was successful or because of an error.
The given ChannelFuture
will be notified.
If the connection fails because of a connection timeout, the ChannelFuture
will get failed with
a ConnectTimeoutException
. If it fails because of connection refused a ConnectException
will be used.
This will result in having the
ChannelOperationHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise)
SocketAddress
while bind to the localAddress and notify the
ChannelFuture
once the operation completes, either because the operation was successful or because of
an error.
The given ChannelPromise
will be notified and also returned.
This will result in having the
ChannelOperationHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture disconnect(ChannelPromise promise)
ChannelFuture
once the operation completes,
either because the operation was successful or because of an error.
The given ChannelPromise
will be notified.
This will result in having the
ChannelOperationHandler.disconnect(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture close(ChannelPromise promise)
ChannelFuture
once the operation completes,
either because the operation was successful or because of
an error.
After it is closed it is not possible to reuse it again.
The given ChannelPromise
will be notified.
This will result in having the
ChannelOperationHandler.close(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture deregister(ChannelPromise promise)
EventExecutor
and notify the
ChannelFuture
once the operation completes, either because the operation was successful or because of
an error.
The given ChannelPromise
will be notified.
This will result in having the
ChannelOperationHandler.deregister(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public void read()
Channel
into the first inbound buffer, triggers an
inboundBufferUpdated
event if data was
read, and triggers an
channelReadSuspended
event so the
handler can decide to continue reading. If there's a pending read operation already, this method does nothing.
This will result in having the
ChannelOperationHandler.read(ChannelHandlerContext)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture flush(ChannelPromise promise)
ChannelFuture
once the operation completes, either because the operation was successful or because of
an error.
Be aware that the flush could be only partially successful. In such cases the ChannelFuture
will be
failed with an IncompleteFlushException
. So if you are interested to know if it was partial successful
you need to check if the returned Future.cause()
returns an instance of
IncompleteFlushException
. In such cases you may want to call flush(ChannelPromise)
or
flush()
to flush the rest of the data or just close the connection via close(ChannelPromise)
or
close()
if it is not possible to recover.
The given ChannelPromise
will be notified.
This will result in having the
ChannelOperationHandler.flush(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture write(Object message, ChannelPromise promise)
ChannelFuture
once the operation completes, either because the operation was successful or because of an error.
If you want to write a FileRegion
use sendFile(FileRegion)
.
Be aware that the write could be only partially successful as the message may need to get encoded before write it
to the remote peer. In such cases the ChannelFuture
will be failed with a
IncompleteFlushException
. In such cases you may want to call flush(ChannelPromise)
or
flush()
to flush the rest of the data or just close the connection via close(ChannelPromise)
or close()
if it is not possible to recover.
The given ChannelPromise
will be notified.
This will result in having the message added to the outbound buffer of the next ChannelOutboundHandler
and the ChannelOperationHandler.flush(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture sendFile(FileRegion region, ChannelPromise promise)
FileRegion
via this ChannelOutboundInvoker and notify the ChannelFuture
once the operation completes, either because the operation was successful or because of an error.
The given ChannelPromise
will be notified.
This will result in having the
ChannelOperationHandler.sendFile(ChannelHandlerContext, FileRegion, ChannelPromise)
method called of the next ChannelOperationHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelPipeline pipeline()
ChannelPipeline
public ByteBufAllocator alloc()
ByteBufAllocator
which will be used for allocate ByteBuf
s.public ChannelPromise newPromise()
FutureFactory
Promise
.newPromise
in interface FutureFactory
public ChannelFuture newSucceededFuture()
FutureFactory
Future
which is marked as successes already. So Future.isSuccess()
will return true
. All FutureListener
added to it will be notified directly. Also
every call of blocking methods will just return without blocking.newSucceededFuture
in interface FutureFactory
public ChannelFuture newFailedFuture(Throwable cause)
FutureFactory
Future
which is marked as fakued already. So Future.isSuccess()
will return false
. All FutureListener
added to it will be notified directly. Also
every call of blocking methods will just return without blocking.newFailedFuture
in interface FutureFactory
Copyright © 2008-2013 The Netty Project. All Rights Reserved.