Class SocketChannelProvider
java.lang.Object
org.omegazero.net.nio.socket.provider.SocketChannelProvider
- All Implemented Interfaces:
ChannelProvider
A
ChannelProvider
for SocketChannel
s.- API Note:
- Before version 2.1.0, this class was in package
org.omegazero.net.socket.provider
.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the underlyingjava.nio
channel.boolean
connect
(SocketAddress remote, int timeout) Connects the underlyingjava.nio
channel to the given remote address.Returns theSelectionKey
of thisChannelProvider
.void
init
(ChannelConnection connection, SelectionKey key) Initializes thisChannelProvider
.boolean
Returnstrue
if the underlyingjava.nio
channel is connected.int
read
(ByteBuffer buf) Reads data from the underlyingjava.nio
channel into the given buffer.void
setReadBlock
(boolean block) If giventrue
, the underlying selection key should stop listening for read ops.int
write
(ByteBuffer buf) Writes data from the given buffer to the underlyingjava.nio
channel.void
Called by theChannelConnection
to indicate write backlog has ended.void
Called by theChannelConnection
to indicate write backlog has started because the outgoing socket buffer is full.
-
Constructor Details
-
SocketChannelProvider
public SocketChannelProvider()
-
-
Method Details
-
init
Description copied from interface:ChannelProvider
Initializes thisChannelProvider
.- Specified by:
init
in interfaceChannelProvider
- Parameters:
connection
- TheChannelConnection
thisChannelProvider
is forkey
- The selection key of the connection
-
connect
Description copied from interface:ChannelProvider
Connects the underlyingjava.nio
channel to the given remote address.- Specified by:
connect
in interfaceChannelProvider
- Parameters:
remote
- The remote addresstimeout
- If applicable, the connection attempt will cancel after this time in milliseconds- Returns:
true
if the channel connected immediately- Throws:
IOException
- If an IO error occurs
-
close
Description copied from interface:ChannelProvider
Closes the underlyingjava.nio
channel.- Specified by:
close
in interfaceChannelProvider
- Throws:
IOException
- If an IO error occurs
-
read
Description copied from interface:ChannelProvider
Reads data from the underlyingjava.nio
channel into the given buffer.- Specified by:
read
in interfaceChannelProvider
- Parameters:
buf
- The buffer to write data to- Returns:
- The number of bytes read
- Throws:
IOException
- If an IO error occurs
-
write
Description copied from interface:ChannelProvider
Writes data from the given buffer to the underlyingjava.nio
channel.- Specified by:
write
in interfaceChannelProvider
- Parameters:
buf
- The buffer to read from- Returns:
- The number of bytes written
- Throws:
IOException
- If an IO error occurs
-
writeBacklogStarted
public void writeBacklogStarted()Description copied from interface:ChannelProvider
Called by theChannelConnection
to indicate write backlog has started because the outgoing socket buffer is full.- Specified by:
writeBacklogStarted
in interfaceChannelProvider
-
writeBacklogEnded
public void writeBacklogEnded()Description copied from interface:ChannelProvider
Called by theChannelConnection
to indicate write backlog has ended.- Specified by:
writeBacklogEnded
in interfaceChannelProvider
-
setReadBlock
public void setReadBlock(boolean block) Description copied from interface:ChannelProvider
If giventrue
, the underlying selection key should stop listening for read ops.- Specified by:
setReadBlock
in interfaceChannelProvider
- Parameters:
block
- Whether to block read events
-
isAvailable
public boolean isAvailable()Description copied from interface:ChannelProvider
Returnstrue
if the underlyingjava.nio
channel is connected.- Specified by:
isAvailable
in interfaceChannelProvider
- Returns:
true
if the channel is connected
-
getSelectionKey
Description copied from interface:ChannelProvider
Returns theSelectionKey
of thisChannelProvider
.- Specified by:
getSelectionKey
in interfaceChannelProvider
- Returns:
- The
SelectionKey
-