- 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,io.netty5.util.AttributeMap,io.netty5.util.concurrent.FuturePromiseFactory,Comparable<Channel>
public class NioSocketChannel extends AbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress> implements SocketChannel
SocketChannelwhich uses NIO selector based implementation.Available options
In addition to the options provided bySocketChannel,NioSocketChannelallows the following options in the option map:ChannelOptionINETINET6{@code UNIX {@link NioChannelOption} X X X
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.netty5.channel.AbstractChannel
AbstractChannel.DefaultAbstractChannelPipeline, AbstractChannel.ReadSink, AbstractChannel.WriteSink
-
-
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 givenSelectorProviderand 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 voiddoBind(SocketAddress localAddress)Bind theChannelto theSocketAddressprotected booleandoConnect(SocketAddress remoteAddress, SocketAddress localAddress, io.netty5.buffer.Buffer data)Connect to remote peer.protected voiddoDisconnect()Disconnect thisChannelfrom its remote peerprotected booleandoFinishConnect(SocketAddress requestedRemoteAddress)Finish a connect request.protected intdoReadBytes(io.netty5.buffer.Buffer buffer)Read bytes into the givenBufferand return the amount.protected voiddoShutdown(ChannelShutdownDirection direction)Shutdown one direction of theChannel.protected intdoWriteBytes(io.netty5.buffer.Buffer buf)Write bytes form the givenBufferto the underlyingChannel.protected longdoWriteFileRegion(FileRegion region)Write aFileRegionprotected voiddoWriteNow(AbstractChannel.WriteSink writeSink)Called in a loop when writes should be performed until this method returnsfalseor there are no more messages to write.protected <T> TgetExtendedOption(ChannelOption<T> option)Override to add support for moreChannelOptions.booleanisActive()Returntrueif theChannelis active and so connected.protected booleanisExtendedOptionSupported(ChannelOption<?> option)Override to add support for moreChannelOptions.booleanisShutdown(ChannelShutdownDirection direction)protected SocketChanneljavaChannel()protected SocketAddresslocalAddress0()Returns theSocketAddresswhich is bound locally.protected io.netty5.util.concurrent.Future<Executor>prepareToClose()Prepares to close theChannel.protected SocketAddressremoteAddress0()Return theSocketAddresswhich theChannelis connected to.protected <T> voidsetExtendedOption(ChannelOption<T> option, T value)Override to add support for moreChannelOptions.-
Methods inherited from class io.netty5.channel.nio.AbstractNioByteChannel
doReadNow, filterOutboundMessage
-
Methods inherited from class io.netty5.channel.nio.AbstractNioChannel
doClearScheduledRead, doClose, doRead, isOpen, isWriteFlushedScheduled, newDirectBuffer, newDirectBuffer, selectionKey, writeLoopComplete
-
Methods inherited from class io.netty5.channel.AbstractChannel
bufferAllocator, cacheAddresses, closeFuture, closeTransport, compareTo, equals, executor, finishConnect, getOption, hashCode, id, isConnectPending, isOptionSupported, isReadPending, isRegistered, isSupportingDisconnect, localAddress, newChannelPipeline, newSupportedIdentityOptionsSet, parent, pipeline, readBufferAllocator, readHandle, readIfIsAutoRead, readLoopComplete, readNow, remoteAddress, setOption, shutdownReadSide, toString, validateEventLoopGroup, validateFileRegion, writableBytes, writeFlushed, writeFlushedNow, writeHandle
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.Channel
bind, bufferAllocator, close, closeFuture, connect, connect, deregister, disconnect, executor, flush, getOption, id, isOpen, isOptionSupported, isWritable, localAddress, pipeline, read, 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 givenSelectorProviderand 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- theChannelwhich created this instance ornullif it was created by the usereventLoop- theEventLoopto use for IO.socket- theSocketChannelwhich will be used
-
NioSocketChannel
public NioSocketChannel(NioServerSocketChannel parent, EventLoop eventLoop, SocketChannel socket, ProtocolFamily family)
Create a new instance- Parameters:
parent- theChannelwhich created this instance ornullif it was created by the usereventLoop- theEventLoopto use for IO.socket- theSocketChannelwhich will be usedfamily- theProtocolFamilythat was used to create thSocketChannel
-
-
Method Detail
-
javaChannel
protected SocketChannel javaChannel()
- Overrides:
javaChannelin classAbstractNioChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
-
isActive
public boolean isActive()
Description copied from interface:ChannelReturntrueif theChannelis active and so connected.
-
isShutdown
public boolean isShutdown(ChannelShutdownDirection direction)
Description copied from interface:Channel- Specified by:
isShutdownin interfaceChannel
-
doShutdown
protected void doShutdown(ChannelShutdownDirection direction) throws Exception
Description copied from class:AbstractChannelShutdown one direction of theChannel.- Specified by:
doShutdownin classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Parameters:
direction- the direction to shut down.- Throws:
Exception- thrown on error.
-
localAddress0
protected SocketAddress localAddress0()
Description copied from class:AbstractChannelReturns theSocketAddresswhich is bound locally.- Specified by:
localAddress0in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Returns:
- the local address if any,
nullotherwise.
-
remoteAddress0
protected SocketAddress remoteAddress0()
Description copied from class:AbstractChannelReturn theSocketAddresswhich theChannelis connected to.- Specified by:
remoteAddress0in classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Returns:
- the remote address if any,
nullotherwise.
-
doBind
protected void doBind(SocketAddress localAddress) throws Exception
Description copied from class:AbstractChannelBind theChannelto theSocketAddress- Specified by:
doBindin classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Parameters:
localAddress- theSocketAddressto bound to.- Throws:
Exception- when an error happens.
-
doConnect
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress, io.netty5.buffer.Buffer data) throws Exception
Description copied from class:AbstractChannelConnect to remote peer. This method should never be directly called.- Specified by:
doConnectin classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Parameters:
remoteAddress- the address of the remote peer.localAddress- the local address of this channel.data- the initial data that is written during connect (ifChannelOption.TCP_FASTOPEN_CONNECTis supported and configured). If data is written care must be taken to update thereader offset.- Returns:
trueif the connect operation was completed,falseifAbstractChannel.finishConnect()will be called later again to try finish connecting.- Throws:
Exception- thrown on error.
-
doFinishConnect
protected boolean doFinishConnect(SocketAddress requestedRemoteAddress) throws Exception
Description copied from class:AbstractChannelFinish a connect request. This method should never be directly called, useAbstractChannel.finishConnect()instead.- Specified by:
doFinishConnectin classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Parameters:
requestedRemoteAddress- the remote address of the peer.- Returns:
trueif the connect operations was completed,falseifAbstractChannel.finishConnect()will be called later again to try finishing the connect operation.- Throws:
Exception- thrown on error.
-
doDisconnect
protected void doDisconnect() throws ExceptionDescription copied from class:AbstractChannelDisconnect thisChannelfrom its remote peer- Specified by:
doDisconnectin classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Throws:
Exception- thrown on error.
-
doReadBytes
protected int doReadBytes(io.netty5.buffer.Buffer buffer) throws ExceptionDescription copied from class:AbstractNioByteChannelRead bytes into the givenBufferand return the amount.- Specified by:
doReadBytesin classAbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Throws:
Exception
-
doWriteBytes
protected int doWriteBytes(io.netty5.buffer.Buffer buf) throws ExceptionDescription copied from class:AbstractNioByteChannelWrite bytes form the givenBufferto the underlyingChannel.- Specified by:
doWriteBytesin classAbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Parameters:
buf- theBufferfrom 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:AbstractNioByteChannelWrite aFileRegion- Specified by:
doWriteFileRegionin classAbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Parameters:
region- theFileRegionfrom which the bytes should be written- Returns:
- amount the amount of written bytes
- Throws:
Exception
-
doWriteNow
protected void doWriteNow(AbstractChannel.WriteSink writeSink) throws Exception
Description copied from class:AbstractChannelCalled in a loop when writes should be performed until this method returnsfalseor there are no more messages to write. Implementations are responsible for handling partial writes, which for example means that ifBuffers are written partial implementations need to ensure thereaderOffsetis updated accordingly.- Overrides:
doWriteNowin classAbstractNioByteChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Parameters:
writeSink- theAbstractChannel.WriteSinkthat must be completed with the write progress.AbstractChannel.WriteSink.complete(long, long, int, boolean)orAbstractChannel.WriteSink.complete(long, Throwable, boolean)must be called exactly once before this method returns non-exceptional.- Throws:
Exception- if an error happened during writing. This will also terminate the write loop.
-
prepareToClose
protected io.netty5.util.concurrent.Future<Executor> prepareToClose()
Description copied from class:AbstractChannelPrepares 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:
prepareToClosein classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>
-
getExtendedOption
protected <T> T getExtendedOption(ChannelOption<T> option)
Description copied from class:AbstractChannelOverride to add support for moreChannelOptions. You need to also call {@link super} after handling the extra options.- Overrides:
getExtendedOptionin 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:AbstractChannelOverride to add support for moreChannelOptions. You need to also callsuperafter handling the extra options.- Overrides:
setExtendedOptionin classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Type Parameters:
T- the value type.- Parameters:
option- theChannelOption.
-
isExtendedOptionSupported
protected boolean isExtendedOptionSupported(ChannelOption<?> option)
Description copied from class:AbstractChannelOverride to add support for moreChannelOptions. You need to also callsuperafter handling the extra options.- Overrides:
isExtendedOptionSupportedin classAbstractChannel<NioServerSocketChannel,SocketAddress,SocketAddress>- Parameters:
option- theChannelOption.- Returns:
trueif supported,falseotherwise.
-
-