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
,java.lang.Comparable<Channel>
public class NioSocketChannel extends AbstractNioByteChannel implements SocketChannel
SocketChannel
which uses NIO selector based implementation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
NioSocketChannel.NioSocketChannelConfig
private class
NioSocketChannel.NioSocketChannelUnsafe
-
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
Fields Modifier and Type Field Description private SocketChannelConfig
config
private static java.nio.channels.spi.SelectorProvider
DEFAULT_SELECTOR_PROVIDER
private static InternalLogger
logger
-
Fields inherited from class io.netty.channel.nio.AbstractNioChannel
readInterestOp
-
-
Constructor Summary
Constructors Constructor Description NioSocketChannel()
Create a new instanceNioSocketChannel(Channel parent, java.nio.channels.SocketChannel socket)
Create a new instanceNioSocketChannel(java.nio.channels.SocketChannel socket)
Create a new instance using the givenSocketChannel
.NioSocketChannel(java.nio.channels.spi.SelectorProvider provider)
Create a new instance using the givenSelectorProvider
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
adjustMaxBytesPerGatheringWrite(int attempted, int written, int oldMaxBytesPerGatheringWrite)
SocketChannelConfig
config()
Returns the configuration of this channel.protected void
doBind(java.net.SocketAddress localAddress)
Bind theChannel
to theSocketAddress
private void
doBind0(java.net.SocketAddress localAddress)
protected void
doClose()
Close theChannel
protected boolean
doConnect(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress)
Connect to the remote peerprotected void
doDisconnect()
Disconnect thisChannel
from its remote peerprotected void
doFinishConnect()
Finish the connectprotected int
doReadBytes(ByteBuf byteBuf)
Read bytes into the givenByteBuf
and return the amount.protected void
doShutdownOutput()
Called when conditions justify shutting down the output portion of the channel.protected void
doWrite(ChannelOutboundBuffer in)
Flush the content of the given buffer to the remote peer.protected int
doWriteBytes(ByteBuf buf)
Write bytes form the givenByteBuf
to the underlyingChannel
.protected long
doWriteFileRegion(FileRegion region)
Write aFileRegion
boolean
isActive()
Returntrue
if theChannel
is active and so connected.boolean
isInputShutdown()
Returnstrue
if and only if the remote peer shut down its output so that no more data is received from this channel.protected boolean
isInputShutdown0()
boolean
isOutputShutdown()
boolean
isShutdown()
Determine if both the input and output of this channel have been shutdown.protected java.nio.channels.SocketChannel
javaChannel()
java.net.InetSocketAddress
localAddress()
Returns the local address where this channel is bound to.protected java.net.SocketAddress
localAddress0()
Returns theSocketAddress
which is bound locally.private static java.nio.channels.SocketChannel
newSocket(java.nio.channels.spi.SelectorProvider provider)
protected AbstractNioChannel.AbstractNioUnsafe
newUnsafe()
Create a newAbstractChannel.AbstractUnsafe
instance which will be used for the life-time of theChannel
ServerSocketChannel
parent()
Returns the parent of this channel.java.net.InetSocketAddress
remoteAddress()
Returns the remote address where this channel is connected to.protected java.net.SocketAddress
remoteAddress0()
Return theSocketAddress
which theChannel
is connected to.ChannelFuture
shutdown()
Will shutdown the input and output sides of this channel.ChannelFuture
shutdown(ChannelPromise promise)
Will shutdown the input and output sides of this channel.private static void
shutdownDone(ChannelFuture shutdownOutputFuture, ChannelFuture shutdownInputFuture, ChannelPromise promise)
ChannelFuture
shutdownInput()
Shutdown the input side of the channel.ChannelFuture
shutdownInput(ChannelPromise promise)
Will shutdown the input and notifyChannelPromise
.private void
shutdownInput0()
private void
shutdownInput0(ChannelPromise promise)
ChannelFuture
shutdownOutput()
ChannelFuture
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, 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
-
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
DEFAULT_SELECTOR_PROVIDER
private static final java.nio.channels.spi.SelectorProvider DEFAULT_SELECTOR_PROVIDER
-
config
private final SocketChannelConfig config
-
-
Constructor Detail
-
NioSocketChannel
public NioSocketChannel()
Create a new instance
-
NioSocketChannel
public NioSocketChannel(java.nio.channels.spi.SelectorProvider provider)
Create a new instance using the givenSelectorProvider
.
-
NioSocketChannel
public NioSocketChannel(java.nio.channels.SocketChannel socket)
Create a new instance using the givenSocketChannel
.
-
-
Method Detail
-
newSocket
private static java.nio.channels.SocketChannel newSocket(java.nio.channels.spi.SelectorProvider provider)
-
parent
public ServerSocketChannel 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
public SocketChannelConfig config()
Description copied from interface:Channel
Returns the configuration of this channel.- Specified by:
config
in interfaceChannel
- Specified by:
config
in interfaceSocketChannel
-
javaChannel
protected java.nio.channels.SocketChannel 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:
Socket.isOutputShutdown()
-
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
public java.net.InetSocketAddress 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
public java.net.InetSocketAddress 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
@UnstableApi protected final void doShutdownOutput() throws java.lang.Exception
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:
java.lang.Exception
-
shutdownOutput
public ChannelFuture shutdownOutput()
- Specified by:
shutdownOutput
in interfaceDuplexChannel
- See Also:
Socket.shutdownOutput()
-
shutdownOutput
public ChannelFuture shutdownOutput(ChannelPromise promise)
Description copied from interface:DuplexChannel
Will shutdown the output and notifyChannelPromise
.- Specified by:
shutdownOutput
in interfaceDuplexChannel
- See Also:
Socket.shutdownOutput()
-
shutdownInput
public ChannelFuture 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:
Socket.shutdownInput()
-
isInputShutdown0
protected boolean isInputShutdown0()
- Overrides:
isInputShutdown0
in classAbstractNioByteChannel
-
shutdownInput
public ChannelFuture shutdownInput(ChannelPromise promise)
Description copied from interface:DuplexChannel
Will shutdown the input and notifyChannelPromise
.- Specified by:
shutdownInput
in interfaceDuplexChannel
- See Also:
Socket.shutdownInput()
-
shutdown
public ChannelFuture 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
public ChannelFuture shutdown(ChannelPromise promise)
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
private void shutdownOutputDone(ChannelFuture shutdownOutputFuture, ChannelPromise promise)
-
shutdownDone
private static void shutdownDone(ChannelFuture shutdownOutputFuture, ChannelFuture shutdownInputFuture, ChannelPromise promise)
-
shutdownInput0
private void shutdownInput0(ChannelPromise promise)
-
shutdownInput0
private void shutdownInput0() throws java.lang.Exception
- Throws:
java.lang.Exception
-
localAddress0
protected java.net.SocketAddress localAddress0()
Description copied from class:AbstractChannel
Returns theSocketAddress
which is bound locally.- Specified by:
localAddress0
in classAbstractChannel
-
remoteAddress0
protected java.net.SocketAddress remoteAddress0()
Description copied from class:AbstractChannel
Return theSocketAddress
which theChannel
is connected to.- Specified by:
remoteAddress0
in classAbstractChannel
-
doBind
protected void doBind(java.net.SocketAddress localAddress) throws java.lang.Exception
Description copied from class:AbstractChannel
Bind theChannel
to theSocketAddress
- Specified by:
doBind
in classAbstractChannel
- Throws:
java.lang.Exception
-
doBind0
private void doBind0(java.net.SocketAddress localAddress) throws java.lang.Exception
- Throws:
java.lang.Exception
-
doConnect
protected boolean doConnect(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress) throws java.lang.Exception
Description copied from class:AbstractNioChannel
Connect to the remote peer- Specified by:
doConnect
in classAbstractNioChannel
- Throws:
java.lang.Exception
-
doFinishConnect
protected void doFinishConnect() throws java.lang.Exception
Description copied from class:AbstractNioChannel
Finish the connect- Specified by:
doFinishConnect
in classAbstractNioChannel
- Throws:
java.lang.Exception
-
doDisconnect
protected void doDisconnect() throws java.lang.Exception
Description copied from class:AbstractChannel
Disconnect thisChannel
from its remote peer- Specified by:
doDisconnect
in classAbstractChannel
- Throws:
java.lang.Exception
-
doClose
protected void doClose() throws java.lang.Exception
Description copied from class:AbstractChannel
Close theChannel
- Overrides:
doClose
in classAbstractNioChannel
- Throws:
java.lang.Exception
-
doReadBytes
protected int doReadBytes(ByteBuf byteBuf) throws java.lang.Exception
Description copied from class:AbstractNioByteChannel
Read bytes into the givenByteBuf
and return the amount.- Specified by:
doReadBytes
in classAbstractNioByteChannel
- Throws:
java.lang.Exception
-
doWriteBytes
protected int doWriteBytes(ByteBuf buf) throws java.lang.Exception
Description copied from class:AbstractNioByteChannel
Write bytes form the givenByteBuf
to the underlyingChannel
.- Specified by:
doWriteBytes
in classAbstractNioByteChannel
- Parameters:
buf
- theByteBuf
from which the bytes should be written- Returns:
- amount the amount of written bytes
- Throws:
java.lang.Exception
-
doWriteFileRegion
protected long doWriteFileRegion(FileRegion region) throws java.lang.Exception
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:
java.lang.Exception
-
adjustMaxBytesPerGatheringWrite
private void adjustMaxBytesPerGatheringWrite(int attempted, int written, int oldMaxBytesPerGatheringWrite)
-
doWrite
protected void doWrite(ChannelOutboundBuffer in) throws java.lang.Exception
Description copied from class:AbstractChannel
Flush the content of the given buffer to the remote peer.- Overrides:
doWrite
in classAbstractNioByteChannel
- Throws:
java.lang.Exception
-
newUnsafe
protected AbstractNioChannel.AbstractNioUnsafe 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
-
-