public class EndPoint
extends java.lang.Object
SessionTask. It serves as the bridge between the SessionTask and the NioSelectorScheduler, using
a pair of mailboxes for exchanging socket registration and socket ready events.
The other purpose of this class is to provide convenience methods that read from a socket into a bytebuffer, or write
from a bytebuffer to the socket. If the socket is not ready for business, the endpoint (and hence the task) simply
yields, without registering with the NioSelectorScheduler. The idea is to give the other runnable
tasks a chance to run before retrying the operation (on resumption); this avoids waking up the selector -- an
expensive operation -- as much as possible, and introduces a delay between retries. If, after a fixed number of times
(YIELD_COUNT), the operation still hasn't succeeded, the endpoint registers itself with the
NioSelectorScheduler, and waits for a socket-ready event from the selector.
This scheme is adaptive to load, in that the delay between retries is proportional to the number of runnable tasks.
Busy sockets tend to get serviced more often as the socket is always ready.
| Modifier and Type | Field and Description |
|---|---|
java.nio.channels.spi.AbstractSelectableChannel |
sockch
The socket channel wrapped by the EndPoint.
|
| Constructor and Description |
|---|
EndPoint(NioSelectorScheduler nio,
java.nio.channels.spi.AbstractSelectableChannel ch) |
| Modifier and Type | Method and Description |
|---|---|
java.io.IOException |
close()
Deprecated.
in a future version, this method will no longer print the stack trace
|
java.nio.channels.SocketChannel |
dataChannel() |
java.nio.ByteBuffer |
fill(java.nio.ByteBuffer buf,
int atleastN)
Read
atleastN bytes more into the buffer if there's space. |
java.nio.ByteBuffer |
fillMessage(java.nio.ByteBuffer bb,
int lengthLength,
boolean lengthIncludesItself)
Reads a length-prefixed message in its entirety.
|
void |
pauseUntilAcceptable() |
void |
pauseUntilReadable() |
void |
pauseUntilWritable() |
void |
write(java.nio.ByteBuffer buf)
Write buf.remaining() bytes to dataChannel().
|
void |
write(java.nio.channels.FileChannel fc,
long start,
long length)
Write a file to the endpoint using
FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel) |
public java.nio.channels.spi.AbstractSelectableChannel sockch
public EndPoint(NioSelectorScheduler nio, java.nio.channels.spi.AbstractSelectableChannel ch)
public java.nio.channels.SocketChannel dataChannel()
public void write(java.nio.ByteBuffer buf)
throws java.io.IOException,
Pausable
java.io.IOExceptionPausablepublic java.nio.ByteBuffer fill(java.nio.ByteBuffer buf,
int atleastN)
throws java.io.IOException,
Pausable
atleastN bytes more into the buffer if there's space. Otherwise, allocate a bigger
buffer that'll accomodate the earlier contents and atleastN more bytes.buf - ByteBuffer to be filledatleastN - At least this many bytes to be read.java.io.IOExceptionPausablepublic java.nio.ByteBuffer fillMessage(java.nio.ByteBuffer bb,
int lengthLength,
boolean lengthIncludesItself)
throws java.io.IOException,
Pausable
bb - The bytebuffer to fill, assuming there is sufficient space (including the bytes for the length). Otherwise, the
contents are copied into a sufficiently big buffer, and the new buffer is returned.lengthLength - The number of bytes occupied by the length. Must be 1, 2 or 4, assumed to be in big-endian order.lengthIncludesItself - true if the packet length includes lengthLengthjava.io.IOExceptionPausablepublic void pauseUntilReadable()
throws Pausable,
java.io.IOException
Pausablejava.io.IOExceptionpublic void pauseUntilWritable()
throws Pausable,
java.io.IOException
Pausablejava.io.IOExceptionpublic void pauseUntilAcceptable()
throws Pausable,
java.io.IOException
Pausablejava.io.IOExceptionpublic void write(java.nio.channels.FileChannel fc,
long start,
long length)
throws java.io.IOException,
Pausable
FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel)fc - FileChannel to copy to endpointstart - Start offsetlength - Number of bytes to be writtenjava.io.IOExceptionPausable@Deprecated public java.io.IOException close()