public interface TcpChannel
This interface is used by TcpServer
and TcpClient
to abstract a TCP channel.
You may want to use TcpConnection
or SslConnection
instead that wraps over a TcpChannel.
This class is thread-safe, all methods can be called on any thread at any time.
However it's best to have only one flow, using the channel executor
.
Abstract Methods | |
---|---|
String |
getPeerHost()
Get the host name of the peer; or null if unknown.
|
InetAddress |
getPeerIp()
Get the IP address of the peer.
|
int |
getPeerPort()
Get the TCP port of the peer.
|
int |
read(ByteBuffer bb)
Read some bytes into the ByteBuffer.
|
Async<Void> |
awaitReadable(boolean accepting)
Wait till this channel becomes readable.
|
long |
write(ByteBuffer... srcs)
Write bytes to this channel.
|
Async<Void> |
awaitWritable()
Wait till this channel becomes writable.
|
void |
shutdownOutput()
Shutdown the output direction of this channel.
|
void |
close()
Close this channel.
|
Executor |
getExecutor()
Get an executor associated with this channel.
|
String getPeerHost()
If the host name is unknown, this method does not lookup the host name from the peer IP.
For example, method TcpClient.connect(peerHost, ip, port)
provides peerHost for the resulting TcpChannel.
InetAddress getPeerIp()
int getPeerPort()
int read(ByteBuffer bb) throws Exception
The ByteBuffer should have remaining()
>0.
This method returns the number of bytes read; or -1 if EOF is reached (i.e. a TCP FIN is received from the peer).
If this method returns 0, caller usually should call awaitReadable(boolean)
to wait for more bytes from the peer.
Exception
Async<Void> awaitReadable(boolean accepting)
This method is typically called after a previous read() returns 0.
There can be only one pending awaitReadable action at any time.
How will this action complete:
`accepting==true`
, and the server is/becomes in the state
of acceptingPaused/Stopped
,
this action fails with a message that the server is not accepting new connections.
This is useful, for example, for an HTTP server to await for a new http request on a persistent connection;
if pause/stopAccepting() is called, the pending awaitReadable actions fail, achieving the effect
of not only pause/stop accepting new connections, but new requests as well.
The parameter `accepting`
has no meaning if this is a client-side channel,
for example, this channel is the result of TcpClient.connect(String, java.net.InetAddress, int)
.
In that case, the false
value is recommended for `accepting`.
long write(ByteBuffer... srcs) throws Exception
Return number of bytes actually written. Note that not all bytes in `srcs` are guaranteed to be written, because the send buffer may be full.
If not all bytes are written, caller should usually call awaitWritable()
.
Exception
Async<Void> awaitWritable()
This method is typically called after a previous write() didn't write all bytes. This action will wait for more rooms in the send buffer.
There can be only one pending awaitWritable action at any time.
This action succeeds when this channel become writable (next write() should be able to write more bytes), or fails due to cancellation, channel being closed, server shutdown etc.
void shutdownOutput() throws Exception
A TCP FIN will be sent to the peer.
Exception
void close()
Executor getExecutor()
The executor can be used to execute non-blocking tasks related to this channel. Tasks will be executed sequentially in the order they are submitted.