Package io.netty.channel.socket.nio
Class NioSocketChannel
java.lang.Object
io.netty.util.DefaultAttributeMap
io.netty.channel.AbstractChannel
io.netty.channel.nio.AbstractNioChannel
io.netty.channel.nio.AbstractNioByteChannel
io.netty.channel.socket.nio.NioSocketChannel
- All Implemented Interfaces:
Channel
,ChannelOutboundInvoker
,DuplexChannel
,SocketChannel
,AttributeMap
,Comparable<Channel>
SocketChannel
which uses NIO selector based implementation.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final class
private final class
Nested classes/interfaces inherited from class io.netty.channel.nio.AbstractNioByteChannel
AbstractNioByteChannel.NioByteUnsafe
Nested classes/interfaces inherited from class io.netty.channel.nio.AbstractNioChannel
AbstractNioChannel.AbstractNioUnsafe, AbstractNioChannel.NioUnsafe
Nested classes/interfaces inherited from class io.netty.channel.AbstractChannel
AbstractChannel.AbstractUnsafe
Nested classes/interfaces inherited from interface io.netty.channel.Channel
Channel.Unsafe
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final SocketChannelConfig
private static final SelectorProvider
private static final InternalLogger
private static final Method
Fields inherited from class io.netty.channel.nio.AbstractNioChannel
readInterestOp
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new instanceNioSocketChannel
(Channel parent, SocketChannel socket) Create a new instanceNioSocketChannel
(SocketChannel socket) Create a new instance using the givenSocketChannel
.NioSocketChannel
(SelectorProvider provider) Create a new instance using the givenSelectorProvider
.NioSocketChannel
(SelectorProvider provider, InternetProtocolFamily family) Create a new instance using the givenSelectorProvider
and protocol family (supported only since JDK 15). -
Method Summary
Modifier and TypeMethodDescriptionprivate void
adjustMaxBytesPerGatheringWrite
(int attempted, int written, int oldMaxBytesPerGatheringWrite) config()
Returns the configuration of this channel.protected void
doBind
(SocketAddress localAddress) Bind theChannel
to theSocketAddress
private void
doBind0
(SocketAddress localAddress) protected void
doClose()
Close theChannel
protected boolean
doConnect
(SocketAddress remoteAddress, SocketAddress localAddress) Connect to the remote peerprotected void
Disconnect thisChannel
from its remote peerprotected void
Finish the connectprotected int
doReadBytes
(ByteBuf byteBuf) Read bytes into the givenByteBuf
and return the amount.protected final void
Called when conditions justify shutting down the output portion of the channel.protected void
Flush the content of the given buffer to the remote peer.protected int
doWriteBytes
(ByteBuf buf) protected long
doWriteFileRegion
(FileRegion region) Write aFileRegion
boolean
isActive()
Returntrue
if theChannel
is active and so connected.boolean
Returnstrue
if and only if the remote peer shut down its output so that no more data is received from this channel.protected boolean
boolean
boolean
Determine if both the input and output of this channel have been shutdown.protected SocketChannel
Returns the local address where this channel is bound to.protected SocketAddress
Returns theSocketAddress
which is bound locally.private static SocketChannel
newChannel
(SelectorProvider provider, InternetProtocolFamily family) protected AbstractNioChannel.AbstractNioUnsafe
Create a newAbstractChannel.AbstractUnsafe
instance which will be used for the life-time of theChannel
parent()
Returns the parent of this channel.Returns the remote address where this channel is connected to.protected SocketAddress
Return theSocketAddress
which theChannel
is connected to.shutdown()
Will shutdown the input and output sides of this channel.shutdown
(ChannelPromise promise) Will shutdown the input and output sides of this channel.private static void
shutdownDone
(ChannelFuture shutdownOutputFuture, ChannelFuture shutdownInputFuture, ChannelPromise promise) Shutdown the input side of the channel.shutdownInput
(ChannelPromise promise) Will shutdown the input and notifyChannelPromise
.private void
private void
shutdownInput0
(ChannelPromise promise) shutdownOutput
(ChannelPromise promise) Will shutdown the output and notifyChannelPromise
.private void
shutdownOutputDone
(ChannelFuture shutdownOutputFuture, ChannelPromise promise) Methods inherited from class io.netty.channel.nio.AbstractNioByteChannel
clearOpWrite, doWrite0, filterOutboundMessage, incompleteWrite, metadata, setOpWrite
Methods inherited from class io.netty.channel.nio.AbstractNioChannel
clearReadPending, doBeginRead, doDeregister, doRegister, eventLoop, isCompatible, isOpen, isReadPending, newDirectBuffer, newDirectBuffer, selectionKey, setReadPending, unsafe
Methods inherited from class io.netty.channel.AbstractChannel
alloc, bind, bind, bytesBeforeUnwritable, bytesBeforeWritable, close, close, closeFuture, compareTo, connect, connect, connect, connect, deregister, deregister, disconnect, disconnect, equals, flush, hashCode, id, invalidateLocalAddress, invalidateRemoteAddress, isRegistered, isWritable, maxMessagesPerWrite, newChannelPipeline, newFailedFuture, newId, newProgressivePromise, newPromise, newSucceededFuture, pipeline, read, toString, validateFileRegion, voidPromise, write, write, writeAndFlush, writeAndFlush
Methods inherited from class io.netty.util.DefaultAttributeMap
attr, hasAttr
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface io.netty.util.AttributeMap
attr, hasAttr
Methods inherited from interface io.netty.channel.Channel
alloc, bytesBeforeUnwritable, bytesBeforeWritable, closeFuture, eventLoop, flush, id, isOpen, isRegistered, isWritable, metadata, pipeline, read, unsafe
Methods inherited from interface io.netty.channel.ChannelOutboundInvoker
bind, bind, close, close, connect, connect, connect, connect, deregister, deregister, disconnect, disconnect, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, voidPromise, write, write, writeAndFlush, writeAndFlush
Methods inherited from interface java.lang.Comparable
compareTo
-
Field Details
-
logger
-
DEFAULT_SELECTOR_PROVIDER
-
OPEN_SOCKET_CHANNEL_WITH_FAMILY
-
config
-
-
Constructor Details
-
NioSocketChannel
public NioSocketChannel()Create a new instance -
NioSocketChannel
Create a new instance using the givenSelectorProvider
. -
NioSocketChannel
Create a new instance using the givenSelectorProvider
and protocol family (supported only since JDK 15). -
NioSocketChannel
Create a new instance using the givenSocketChannel
. -
NioSocketChannel
Create a new instance- Parameters:
parent
- theChannel
which created this instance ornull
if it was created by the usersocket
- theSocketChannel
which will be used
-
-
Method Details
-
newChannel
-
parent
Description copied from interface:Channel
Returns the parent of this channel.- Specified by:
parent
in interfaceChannel
- Specified by:
parent
in interfaceSocketChannel
- Overrides:
parent
in classAbstractChannel
- Returns:
- the parent channel.
null
if this channel does not have a parent channel.
-
config
Description copied from interface:Channel
Returns the configuration of this channel.- Specified by:
config
in interfaceChannel
- Specified by:
config
in interfaceSocketChannel
-
javaChannel
- Overrides:
javaChannel
in classAbstractNioChannel
-
isActive
public boolean isActive()Description copied from interface:Channel
Returntrue
if theChannel
is active and so connected. -
isOutputShutdown
public boolean isOutputShutdown()- Specified by:
isOutputShutdown
in interfaceDuplexChannel
- See Also:
-
isInputShutdown
public boolean isInputShutdown()Description copied from interface:DuplexChannel
Returnstrue
if and only if the remote peer shut down its output so that no more data is received from this channel. Note that the semantic of this method is different from that ofSocket.shutdownInput()
andSocket.isInputShutdown()
.- Specified by:
isInputShutdown
in interfaceDuplexChannel
-
isShutdown
public boolean isShutdown()Description copied from interface:DuplexChannel
Determine if both the input and output of this channel have been shutdown.- Specified by:
isShutdown
in interfaceDuplexChannel
-
localAddress
Description copied from interface:Channel
Returns the local address where this channel is bound to. The returnedSocketAddress
is supposed to be down-cast into more concrete type such asInetSocketAddress
to retrieve the detailed information.- Specified by:
localAddress
in interfaceChannel
- Specified by:
localAddress
in interfaceSocketChannel
- Overrides:
localAddress
in classAbstractChannel
- Returns:
- the local address of this channel.
null
if this channel is not bound.
-
remoteAddress
Description copied from interface:Channel
Returns the remote address where this channel is connected to. The returnedSocketAddress
is supposed to be down-cast into more concrete type such asInetSocketAddress
to retrieve the detailed information.- Specified by:
remoteAddress
in interfaceChannel
- Specified by:
remoteAddress
in interfaceSocketChannel
- Overrides:
remoteAddress
in classAbstractChannel
- Returns:
- the remote address of this channel.
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
, useDefaultAddressedEnvelope.recipient()
to determine the origination of the received message as this method will returnnull
.
-
doShutdownOutput
Description copied from class:AbstractChannel
Called when conditions justify shutting down the output portion of the channel. This may happen if a write operation throws an exception.- Overrides:
doShutdownOutput
in classAbstractChannel
- Throws:
Exception
-
shutdownOutput
- Specified by:
shutdownOutput
in interfaceDuplexChannel
- See Also:
-
shutdownOutput
Description copied from interface:DuplexChannel
Will shutdown the output and notifyChannelPromise
.- Specified by:
shutdownOutput
in interfaceDuplexChannel
- See Also:
-
shutdownInput
Description copied from class:AbstractNioByteChannel
Shutdown the input side of the channel.- Specified by:
shutdownInput
in interfaceDuplexChannel
- Specified by:
shutdownInput
in classAbstractNioByteChannel
- See Also:
-
isInputShutdown0
protected boolean isInputShutdown0()- Overrides:
isInputShutdown0
in classAbstractNioByteChannel
-
shutdownInput
Description copied from interface:DuplexChannel
Will shutdown the input and notifyChannelPromise
.- Specified by:
shutdownInput
in interfaceDuplexChannel
- See Also:
-
shutdown
Description copied from interface:DuplexChannel
Will shutdown the input and output sides of this channel.- Specified by:
shutdown
in interfaceDuplexChannel
- Returns:
- will be completed when both shutdown operations complete.
-
shutdown
Description copied from interface:DuplexChannel
Will shutdown the input and output sides of this channel.- Specified by:
shutdown
in interfaceDuplexChannel
- Parameters:
promise
- will be completed when both shutdown operations complete.- Returns:
- will be completed when both shutdown operations complete.
-
shutdownOutputDone
-
shutdownDone
private static void shutdownDone(ChannelFuture shutdownOutputFuture, ChannelFuture shutdownInputFuture, ChannelPromise promise) -
shutdownInput0
-
shutdownInput0
- Throws:
Exception
-
localAddress0
Description copied from class:AbstractChannel
Returns theSocketAddress
which is bound locally.- Specified by:
localAddress0
in classAbstractChannel
-
remoteAddress0
Description copied from class:AbstractChannel
Return theSocketAddress
which theChannel
is connected to.- Specified by:
remoteAddress0
in classAbstractChannel
-
doBind
Description copied from class:AbstractChannel
Bind theChannel
to theSocketAddress
- Specified by:
doBind
in classAbstractChannel
- Throws:
Exception
-
doBind0
- Throws:
Exception
-
doConnect
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception Description copied from class:AbstractNioChannel
Connect to the remote peer- Specified by:
doConnect
in classAbstractNioChannel
- Throws:
Exception
-
doFinishConnect
Description copied from class:AbstractNioChannel
Finish the connect- Specified by:
doFinishConnect
in classAbstractNioChannel
- Throws:
Exception
-
doDisconnect
Description copied from class:AbstractChannel
Disconnect thisChannel
from its remote peer- Specified by:
doDisconnect
in classAbstractChannel
- Throws:
Exception
-
doClose
Description copied from class:AbstractChannel
Close theChannel
- Overrides:
doClose
in classAbstractNioChannel
- Throws:
Exception
-
doReadBytes
Description copied from class:AbstractNioByteChannel
Read bytes into the givenByteBuf
and return the amount.- Specified by:
doReadBytes
in classAbstractNioByteChannel
- Throws:
Exception
-
doWriteBytes
Description copied from class:AbstractNioByteChannel
- Specified by:
doWriteBytes
in classAbstractNioByteChannel
- Parameters:
buf
- theByteBuf
from which the bytes should be written- Returns:
- amount the amount of written bytes
- Throws:
Exception
-
doWriteFileRegion
Description copied from class:AbstractNioByteChannel
Write aFileRegion
- Specified by:
doWriteFileRegion
in classAbstractNioByteChannel
- Parameters:
region
- theFileRegion
from which the bytes should be written- Returns:
- amount the amount of written bytes
- Throws:
Exception
-
adjustMaxBytesPerGatheringWrite
private void adjustMaxBytesPerGatheringWrite(int attempted, int written, int oldMaxBytesPerGatheringWrite) -
doWrite
Description copied from class:AbstractChannel
Flush the content of the given buffer to the remote peer.- Overrides:
doWrite
in classAbstractNioByteChannel
- Throws:
Exception
-
newUnsafe
Description copied from class:AbstractChannel
Create a newAbstractChannel.AbstractUnsafe
instance which will be used for the life-time of theChannel
- Overrides:
newUnsafe
in classAbstractNioByteChannel
-