- java.lang.Object
-
- io.netty5.util.DefaultAttributeMap
-
- io.netty5.channel.AbstractChannel<P,L,R>
-
- io.netty5.channel.nio.AbstractNioChannel<P,L,R>
-
- io.netty5.channel.nio.AbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
-
- io.netty5.channel.socket.nio.NioSocketChannel
-
- All Implemented Interfaces:
Channel
,ChannelOutboundInvoker
,IoHandle
,SocketChannel
,AttributeMap
,FuturePromiseFactory
,Comparable<Channel>
public class NioSocketChannel extends AbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress> implements SocketChannel
SocketChannel
which uses NIO selector based implementation.Available options
In addition to the options provided bySocketChannel
,NioSocketChannel
allows the following options in the option map:ChannelOption
INET
INET6
{@code UNIX {@link NioChannelOption} X X X
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.netty5.channel.AbstractChannel
AbstractChannel.DefaultAbstractChannelPipeline
-
-
Field Summary
-
Fields inherited from class io.netty5.channel.nio.AbstractNioChannel
readInterestOp
-
-
Constructor Summary
Constructors Constructor Description NioSocketChannel(EventLoop eventLoop)
Create a new instanceNioSocketChannel(EventLoop eventLoop, SocketChannel socket)
Create a new instance using the givenSocketChannel
.NioSocketChannel(EventLoop eventLoop, SelectorProvider provider)
Create a new instance using the givenSelectorProvider
.NioSocketChannel(EventLoop eventLoop, SelectorProvider provider, ProtocolFamily family)
Create a new instance using the givenSelectorProvider
and protocol family (supported only since JDK 15).NioSocketChannel(NioServerSocketChannel parent, EventLoop eventLoop, SocketChannel socket)
Create a new instanceNioSocketChannel(NioServerSocketChannel parent, EventLoop eventLoop, SocketChannel socket, ProtocolFamily family)
Create a new instance
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
autoReadCleared()
Is called onceAbstractChannel.setAutoRead(boolean)
is called withfalse
andAbstractChannel.isAutoRead()
wastrue
before.protected void
doBind(SocketAddress localAddress)
Bind theChannel
to theSocketAddress
protected boolean
doConnect(SocketAddress remoteAddress, SocketAddress localAddress)
Connect to remote peer.protected void
doDisconnect()
Disconnect thisChannel
from its remote peerprotected boolean
doFinishConnect(SocketAddress requestedRemoteAddress)
Finish a connect request.protected int
doReadBytes(Buffer buffer)
Read bytes into the givenBuffer
and return the amount.protected void
doShutdown(ChannelShutdownDirection direction)
Shutdown one direction of theChannel
.protected void
doWrite(ChannelOutboundBuffer in)
Flush the content of the given buffer to the remote peer.protected int
doWriteBytes(Buffer buf)
protected long
doWriteFileRegion(FileRegion region)
Write aFileRegion
protected <T> T
getExtendedOption(ChannelOption<T> option)
Override to add support for moreChannelOption
s.boolean
isActive()
Returntrue
if theChannel
is active and so connected.protected boolean
isExtendedOptionSupported(ChannelOption<?> option)
Override to add support for moreChannelOption
s.boolean
isShutdown(ChannelShutdownDirection direction)
protected SocketChannel
javaChannel()
protected SocketAddress
localAddress0()
Returns theSocketAddress
which is bound locally.protected Future<Executor>
prepareToClose()
Prepares to close theChannel
.protected SocketAddress
remoteAddress0()
Return theSocketAddress
which theChannel
is connected to.protected <T> void
setExtendedOption(ChannelOption<T> option, T value)
Override to add support for moreChannelOption
s.-
Methods inherited from class io.netty5.channel.nio.AbstractNioByteChannel
clearOpWrite, doWrite0, filterOutboundMessage, incompleteWrite, readNow, setOpWrite
-
Methods inherited from class io.netty5.channel.nio.AbstractNioChannel
clearReadPending, doBeginRead, doClose, isOpen, isReadPending, newDirectBuffer, newDirectBuffer, removeReadOp, selectionKey, setReadPending, writeFlushed
-
Methods inherited from class io.netty5.channel.AbstractChannel
annotateConnectException, assertEventLoop, bufferAllocator, cacheAddresses, closeForciblyTransport, closeFuture, closeIfClosed, closeTransport, compareTo, deregisterTransport, ensureOpen, equals, executor, finishConnect, fireChannelActiveIfNotActiveBefore, getMaxMessagesPerWrite, getOption, getWriteSpinCount, handleWriteError, hashCode, id, isAllowHalfClosure, isAutoRead, isConnectPending, isOptionSupported, isRegistered, localAddress, metadata, newChannelPipeline, newSupportedIdentityOptionsSet, outboundBuffer, parent, pipeline, readIfIsAutoRead, recvBufAllocHandle, remoteAddress, runAfterTransportAction, safeSetFailure, safeSetSuccess, setBufferAllocator, setOption, shutdownTransport, toString, validate, validateEventLoopGroup, validateFileRegion, writableBytes
-
Methods inherited from class io.netty5.util.DefaultAttributeMap
attr, hasAttr
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.netty5.util.AttributeMap
attr, hasAttr
-
Methods inherited from interface io.netty5.channel.Channel
bind, bufferAllocator, close, closeFuture, connect, connect, deregister, disconnect, executor, flush, getOption, id, isOpen, isOptionSupported, isWritable, localAddress, metadata, pipeline, read, register, remoteAddress, sendOutboundEvent, setOption, shutdown, writableBytes, write, writeAndFlush
-
Methods inherited from interface io.netty5.channel.ChannelOutboundInvoker
newFailedFuture, newPromise, newSucceededFuture, newSucceededFuture
-
Methods inherited from interface java.lang.Comparable
compareTo
-
Methods inherited from interface io.netty5.channel.IoHandle
isRegistered
-
Methods inherited from interface io.netty5.channel.socket.SocketChannel
parent
-
-
-
-
Constructor Detail
-
NioSocketChannel
public NioSocketChannel(EventLoop eventLoop)
Create a new instance
-
NioSocketChannel
public NioSocketChannel(EventLoop eventLoop, SelectorProvider provider)
Create a new instance using the givenSelectorProvider
.
-
NioSocketChannel
public NioSocketChannel(EventLoop eventLoop, SelectorProvider provider, ProtocolFamily family)
Create a new instance using the givenSelectorProvider
and protocol family (supported only since JDK 15).
-
NioSocketChannel
public NioSocketChannel(EventLoop eventLoop, SocketChannel socket)
Create a new instance using the givenSocketChannel
.
-
NioSocketChannel
public NioSocketChannel(NioServerSocketChannel parent, EventLoop eventLoop, SocketChannel socket)
Create a new instance- Parameters:
parent
- theChannel
which created this instance ornull
if it was created by the usereventLoop
- theEventLoop
to use for IO.socket
- theSocketChannel
which will be used
-
NioSocketChannel
public NioSocketChannel(NioServerSocketChannel parent, EventLoop eventLoop, SocketChannel socket, ProtocolFamily family)
Create a new instance- Parameters:
parent
- theChannel
which created this instance ornull
if it was created by the usereventLoop
- theEventLoop
to use for IO.socket
- theSocketChannel
which will be usedfamily
- theProtocolFamily
that was used to create thSocketChannel
-
-
Method Detail
-
javaChannel
protected SocketChannel javaChannel()
- Overrides:
javaChannel
in classAbstractNioChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
-
isActive
public boolean isActive()
Description copied from interface:Channel
Returntrue
if theChannel
is active and so connected.
-
isShutdown
public boolean isShutdown(ChannelShutdownDirection direction)
Description copied from interface:Channel
- Specified by:
isShutdown
in interfaceChannel
-
doShutdown
protected void doShutdown(ChannelShutdownDirection direction) throws Exception
Description copied from class:AbstractChannel
Shutdown one direction of theChannel
.- Specified by:
doShutdown
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Parameters:
direction
- the direction to shutdown.- Throws:
Exception
- thrown on error.
-
localAddress0
protected SocketAddress localAddress0()
Description copied from class:AbstractChannel
Returns theSocketAddress
which is bound locally.- Specified by:
localAddress0
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
-
remoteAddress0
protected SocketAddress remoteAddress0()
Description copied from class:AbstractChannel
Return theSocketAddress
which theChannel
is connected to.- Specified by:
remoteAddress0
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
-
doBind
protected void doBind(SocketAddress localAddress) throws Exception
Description copied from class:AbstractChannel
Bind theChannel
to theSocketAddress
- Specified by:
doBind
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Throws:
Exception
-
doConnect
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception
Description copied from class:AbstractChannel
Connect to remote peer.- Specified by:
doConnect
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Parameters:
remoteAddress
- the address of the remote peer.localAddress
- the local address of this channel.- Returns:
true
if the connect was completed,false
ifAbstractChannel.finishConnect()
will be called later again to try finishing the connect.- Throws:
Exception
- thrown on error.
-
doFinishConnect
protected boolean doFinishConnect(SocketAddress requestedRemoteAddress) throws Exception
Description copied from class:AbstractChannel
Finish a connect request.- Specified by:
doFinishConnect
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Parameters:
requestedRemoteAddress
- the remote address of the peer.- Returns:
true
if the connect was completed,false
ifAbstractChannel.finishConnect()
will be called later again to try finishing the connect.- Throws:
Exception
- thrown on error.
-
doDisconnect
protected void doDisconnect() throws Exception
Description copied from class:AbstractChannel
Disconnect thisChannel
from its remote peer- Specified by:
doDisconnect
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Throws:
Exception
-
doReadBytes
protected int doReadBytes(Buffer buffer) throws Exception
Description copied from class:AbstractNioByteChannel
Read bytes into the givenBuffer
and return the amount.- Specified by:
doReadBytes
in classAbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Throws:
Exception
-
doWriteBytes
protected int doWriteBytes(Buffer buf) throws Exception
Description copied from class:AbstractNioByteChannel
- Specified by:
doWriteBytes
in classAbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Parameters:
buf
- theBuffer
from which the bytes should be written- Returns:
- amount the amount of written bytes
- Throws:
Exception
-
doWriteFileRegion
protected long doWriteFileRegion(FileRegion region) throws Exception
Description copied from class:AbstractNioByteChannel
Write aFileRegion
- Specified by:
doWriteFileRegion
in classAbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Parameters:
region
- theFileRegion
from which the bytes should be written- Returns:
- amount the amount of written bytes
- Throws:
Exception
-
doWrite
protected void doWrite(ChannelOutboundBuffer in) throws Exception
Description copied from class:AbstractChannel
Flush the content of the given buffer to the remote peer.- Overrides:
doWrite
in classAbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Throws:
Exception
-
prepareToClose
protected Future<Executor> prepareToClose()
Description copied from class:AbstractChannel
Prepares to close theChannel
. If this method returns anExecutor
, the caller must call theExecutor.execute(Runnable)
method with a task that callsAbstractChannel.doClose()
on the returnedExecutor
. If this method returnsnull
,AbstractChannel.doClose()
must be called from the caller thread. (i.e.EventLoop
)- Overrides:
prepareToClose
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
-
getExtendedOption
protected <T> T getExtendedOption(ChannelOption<T> option)
Description copied from class:AbstractChannel
Override to add support for moreChannelOption
s. You need to also call {@link super} after handling the extra options.- Overrides:
getExtendedOption
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Type Parameters:
T
- the value type.- Parameters:
option
- theChannelOption
.- Returns:
- the value for the option
-
setExtendedOption
protected <T> void setExtendedOption(ChannelOption<T> option, T value)
Description copied from class:AbstractChannel
Override to add support for moreChannelOption
s. You need to also call {@link super} after handling the extra options.- Overrides:
setExtendedOption
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Type Parameters:
T
- the value type.- Parameters:
option
- theChannelOption
.
-
isExtendedOptionSupported
protected boolean isExtendedOptionSupported(ChannelOption<?> option)
Description copied from class:AbstractChannel
Override to add support for moreChannelOption
s. You need to also call {@link super} after handling the extra options.- Overrides:
isExtendedOptionSupported
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
- Parameters:
option
- theChannelOption
.- Returns:
true
if supported,false
otherwise.
-
autoReadCleared
protected void autoReadCleared()
Description copied from class:AbstractChannel
Is called onceAbstractChannel.setAutoRead(boolean)
is called withfalse
andAbstractChannel.isAutoRead()
wastrue
before.- Overrides:
autoReadCleared
in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
-
-