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
SocketChannelwhich uses NIO selector based implementation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classNioSocketChannel.NioSocketChannelConfigprivate classNioSocketChannel.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 SocketChannelConfigconfigprivate static java.nio.channels.spi.SelectorProviderDEFAULT_SELECTOR_PROVIDERprivate static InternalLoggerlogger-
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 voidadjustMaxBytesPerGatheringWrite(int attempted, int written, int oldMaxBytesPerGatheringWrite)SocketChannelConfigconfig()Returns the configuration of this channel.protected voiddoBind(java.net.SocketAddress localAddress)Bind theChannelto theSocketAddressprivate voiddoBind0(java.net.SocketAddress localAddress)protected voiddoClose()Close theChannelprotected booleandoConnect(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress)Connect to the remote peerprotected voiddoDisconnect()Disconnect thisChannelfrom its remote peerprotected voiddoFinishConnect()Finish the connectprotected intdoReadBytes(ByteBuf byteBuf)Read bytes into the givenByteBufand return the amount.protected voiddoShutdownOutput()Called when conditions justify shutting down the output portion of the channel.protected voiddoWrite(ChannelOutboundBuffer in)Flush the content of the given buffer to the remote peer.protected intdoWriteBytes(ByteBuf buf)Write bytes form the givenByteBufto the underlyingChannel.protected longdoWriteFileRegion(FileRegion region)Write aFileRegionbooleanisActive()Returntrueif theChannelis active and so connected.booleanisInputShutdown()Returnstrueif and only if the remote peer shut down its output so that no more data is received from this channel.protected booleanisInputShutdown0()booleanisOutputShutdown()booleanisShutdown()Determine if both the input and output of this channel have been shutdown.protected java.nio.channels.SocketChanneljavaChannel()java.net.InetSocketAddresslocalAddress()Returns the local address where this channel is bound to.protected java.net.SocketAddresslocalAddress0()Returns theSocketAddresswhich is bound locally.private static java.nio.channels.SocketChannelnewSocket(java.nio.channels.spi.SelectorProvider provider)protected AbstractNioChannel.AbstractNioUnsafenewUnsafe()Create a newAbstractChannel.AbstractUnsafeinstance which will be used for the life-time of theChannelServerSocketChannelparent()Returns the parent of this channel.java.net.InetSocketAddressremoteAddress()Returns the remote address where this channel is connected to.protected java.net.SocketAddressremoteAddress0()Return theSocketAddresswhich theChannelis connected to.ChannelFutureshutdown()Will shutdown the input and output sides of this channel.ChannelFutureshutdown(ChannelPromise promise)Will shutdown the input and output sides of this channel.private static voidshutdownDone(ChannelFuture shutdownOutputFuture, ChannelFuture shutdownInputFuture, ChannelPromise promise)ChannelFutureshutdownInput()Shutdown the input side of the channel.ChannelFutureshutdownInput(ChannelPromise promise)Will shutdown the input and notifyChannelPromise.private voidshutdownInput0()private voidshutdownInput0(ChannelPromise promise)ChannelFutureshutdownOutput()ChannelFutureshutdownOutput(ChannelPromise promise)Will shutdown the output and notifyChannelPromise.private voidshutdownOutputDone(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:ChannelReturns the parent of this channel.- Specified by:
parentin interfaceChannel- Specified by:
parentin interfaceSocketChannel- Overrides:
parentin classAbstractChannel- Returns:
- the parent channel.
nullif this channel does not have a parent channel.
-
config
public SocketChannelConfig config()
Description copied from interface:ChannelReturns the configuration of this channel.- Specified by:
configin interfaceChannel- Specified by:
configin interfaceSocketChannel
-
javaChannel
protected java.nio.channels.SocketChannel javaChannel()
- Overrides:
javaChannelin classAbstractNioChannel
-
isActive
public boolean isActive()
Description copied from interface:ChannelReturntrueif theChannelis active and so connected.
-
isOutputShutdown
public boolean isOutputShutdown()
- Specified by:
isOutputShutdownin interfaceDuplexChannel- See Also:
Socket.isOutputShutdown()
-
isInputShutdown
public boolean isInputShutdown()
Description copied from interface:DuplexChannelReturnstrueif 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:
isInputShutdownin interfaceDuplexChannel
-
isShutdown
public boolean isShutdown()
Description copied from interface:DuplexChannelDetermine if both the input and output of this channel have been shutdown.- Specified by:
isShutdownin interfaceDuplexChannel
-
localAddress
public java.net.InetSocketAddress localAddress()
Description copied from interface:ChannelReturns 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.- Specified by:
localAddressin interfaceChannel- Specified by:
localAddressin interfaceSocketChannel- Overrides:
localAddressin classAbstractChannel- Returns:
- the local address of this channel.
nullif this channel is not bound.
-
remoteAddress
public java.net.InetSocketAddress remoteAddress()
Description copied from interface:ChannelReturns 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.- Specified by:
remoteAddressin interfaceChannel- Specified by:
remoteAddressin interfaceSocketChannel- Overrides:
remoteAddressin classAbstractChannel- 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, 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:AbstractChannelCalled when conditions justify shutting down the output portion of the channel. This may happen if a write operation throws an exception.- Overrides:
doShutdownOutputin classAbstractChannel- Throws:
java.lang.Exception
-
shutdownOutput
public ChannelFuture shutdownOutput()
- Specified by:
shutdownOutputin interfaceDuplexChannel- See Also:
Socket.shutdownOutput()
-
shutdownOutput
public ChannelFuture shutdownOutput(ChannelPromise promise)
Description copied from interface:DuplexChannelWill shutdown the output and notifyChannelPromise.- Specified by:
shutdownOutputin interfaceDuplexChannel- See Also:
Socket.shutdownOutput()
-
shutdownInput
public ChannelFuture shutdownInput()
Description copied from class:AbstractNioByteChannelShutdown the input side of the channel.- Specified by:
shutdownInputin interfaceDuplexChannel- Specified by:
shutdownInputin classAbstractNioByteChannel- See Also:
Socket.shutdownInput()
-
isInputShutdown0
protected boolean isInputShutdown0()
- Overrides:
isInputShutdown0in classAbstractNioByteChannel
-
shutdownInput
public ChannelFuture shutdownInput(ChannelPromise promise)
Description copied from interface:DuplexChannelWill shutdown the input and notifyChannelPromise.- Specified by:
shutdownInputin interfaceDuplexChannel- See Also:
Socket.shutdownInput()
-
shutdown
public ChannelFuture shutdown()
Description copied from interface:DuplexChannelWill shutdown the input and output sides of this channel.- Specified by:
shutdownin interfaceDuplexChannel- Returns:
- will be completed when both shutdown operations complete.
-
shutdown
public ChannelFuture shutdown(ChannelPromise promise)
Description copied from interface:DuplexChannelWill shutdown the input and output sides of this channel.- Specified by:
shutdownin 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:AbstractChannelReturns theSocketAddresswhich is bound locally.- Specified by:
localAddress0in classAbstractChannel
-
remoteAddress0
protected java.net.SocketAddress remoteAddress0()
Description copied from class:AbstractChannelReturn theSocketAddresswhich theChannelis connected to.- Specified by:
remoteAddress0in classAbstractChannel
-
doBind
protected void doBind(java.net.SocketAddress localAddress) throws java.lang.ExceptionDescription copied from class:AbstractChannelBind theChannelto theSocketAddress- Specified by:
doBindin 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.ExceptionDescription copied from class:AbstractNioChannelConnect to the remote peer- Specified by:
doConnectin classAbstractNioChannel- Throws:
java.lang.Exception
-
doFinishConnect
protected void doFinishConnect() throws java.lang.ExceptionDescription copied from class:AbstractNioChannelFinish the connect- Specified by:
doFinishConnectin classAbstractNioChannel- Throws:
java.lang.Exception
-
doDisconnect
protected void doDisconnect() throws java.lang.ExceptionDescription copied from class:AbstractChannelDisconnect thisChannelfrom its remote peer- Specified by:
doDisconnectin classAbstractChannel- Throws:
java.lang.Exception
-
doClose
protected void doClose() throws java.lang.ExceptionDescription copied from class:AbstractChannelClose theChannel- Overrides:
doClosein classAbstractNioChannel- Throws:
java.lang.Exception
-
doReadBytes
protected int doReadBytes(ByteBuf byteBuf) throws java.lang.Exception
Description copied from class:AbstractNioByteChannelRead bytes into the givenByteBufand return the amount.- Specified by:
doReadBytesin classAbstractNioByteChannel- Throws:
java.lang.Exception
-
doWriteBytes
protected int doWriteBytes(ByteBuf buf) throws java.lang.Exception
Description copied from class:AbstractNioByteChannelWrite bytes form the givenByteBufto the underlyingChannel.- Specified by:
doWriteBytesin classAbstractNioByteChannel- Parameters:
buf- theByteBuffrom 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:AbstractNioByteChannelWrite aFileRegion- Specified by:
doWriteFileRegionin classAbstractNioByteChannel- Parameters:
region- theFileRegionfrom 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:AbstractChannelFlush the content of the given buffer to the remote peer.- Overrides:
doWritein classAbstractNioByteChannel- Throws:
java.lang.Exception
-
newUnsafe
protected AbstractNioChannel.AbstractNioUnsafe newUnsafe()
Description copied from class:AbstractChannelCreate a newAbstractChannel.AbstractUnsafeinstance which will be used for the life-time of theChannel- Overrides:
newUnsafein classAbstractNioByteChannel
-
-