public class Nio2Session extends AbstractCloseable implements IoSession
AbstractCloseable.State
Modifier and Type | Field and Description |
---|---|
private java.net.SocketAddress |
acceptanceAddress |
private java.util.Map<java.lang.Object,java.lang.Object> |
attributes |
private java.util.concurrent.atomic.AtomicReference<Nio2DefaultIoWriteFuture> |
currentWrite |
static int |
DEFAULT_READBUF_SIZE |
private long |
id |
private IoHandler |
ioHandler |
private java.net.SocketAddress |
localAddress |
private FactoryManager |
manager |
private java.net.SocketAddress |
remoteAddress |
private Nio2Service |
service |
private static java.util.concurrent.atomic.AtomicLong |
SESSION_ID_GENERATOR |
private java.nio.channels.AsynchronousSocketChannel |
socketChannel |
private java.util.Queue<Nio2DefaultIoWriteFuture> |
writes |
closeFuture, lock, state
log
CLOSE_WAIT_TIMEOUT, DEFAULT_CLOSE_WAIT_TIMEOUT
Constructor and Description |
---|
Nio2Session(Nio2Service service,
FactoryManager manager,
IoHandler handler,
java.nio.channels.AsynchronousSocketChannel socket,
java.net.SocketAddress acceptanceAddress) |
Modifier and Type | Method and Description |
---|---|
protected Nio2CompletionHandler<java.lang.Integer,java.lang.Object> |
createReadCycleCompletionHandler(java.nio.ByteBuffer buffer,
Readable bufReader) |
protected Nio2CompletionHandler<java.lang.Integer,java.lang.Object> |
createWriteCycleCompletionHandler(Nio2DefaultIoWriteFuture future,
java.nio.channels.AsynchronousSocketChannel socket,
java.nio.ByteBuffer buffer) |
protected CloseFuture |
doCloseGracefully() |
protected void |
doCloseImmediately()
doCloseImmediately is called once and only once
with state == Immediate
|
protected void |
doReadCycle(java.nio.ByteBuffer buffer,
Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completion) |
protected void |
doReadCycle(java.nio.ByteBuffer buffer,
Readable bufReader) |
protected void |
doWriteCycle(java.nio.ByteBuffer buffer,
Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completion) |
protected void |
exceptionCaught(java.lang.Throwable exc) |
protected void |
finishWrite(Nio2DefaultIoWriteFuture future) |
java.net.SocketAddress |
getAcceptanceAddress() |
java.lang.Object |
getAttribute(java.lang.Object key)
Returns the value of the user-defined attribute of this session.
|
long |
getId() |
IoHandler |
getIoHandler() |
java.net.SocketAddress |
getLocalAddress() |
java.net.SocketAddress |
getRemoteAddress() |
Nio2Service |
getService() |
java.nio.channels.AsynchronousSocketChannel |
getSocket() |
protected void |
handleCompletedWriteCycle(Nio2DefaultIoWriteFuture future,
java.nio.channels.AsynchronousSocketChannel socket,
java.nio.ByteBuffer buffer,
int writeLen,
Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completionHandler,
java.lang.Integer result,
java.lang.Object attachment) |
protected void |
handleReadCycleCompletion(java.nio.ByteBuffer buffer,
Readable bufReader,
Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completionHandler,
java.lang.Integer result,
java.lang.Object attachment) |
protected void |
handleReadCycleFailure(java.nio.ByteBuffer buffer,
Readable bufReader,
java.lang.Throwable exc,
java.lang.Object attachment) |
protected void |
handleWriteCycleFailure(Nio2DefaultIoWriteFuture future,
java.nio.channels.AsynchronousSocketChannel socket,
java.nio.ByteBuffer buffer,
int writeLen,
java.lang.Throwable exc,
java.lang.Object attachment) |
java.lang.Object |
removeAttribute(java.lang.Object key)
Removes a user-defined attribute with the specified key.
|
java.lang.Object |
setAttribute(java.lang.Object key,
java.lang.Object value)
Sets a user-defined attribute.
|
java.lang.Object |
setAttributeIfAbsent(java.lang.Object key,
java.lang.Object value)
Sets a user defined attribute if the attribute with the specified key
is not set yet.
|
void |
startReading() |
void |
startReading(byte[] buf) |
void |
startReading(byte[] buf,
int offset,
int len) |
void |
startReading(java.nio.ByteBuffer buffer) |
void |
startReading(int bufSize) |
protected void |
startWriting() |
void |
suspend() |
java.lang.String |
toString() |
IoWriteFuture |
writePacket(Buffer buffer)
Encode and send the given buffer.
|
addCloseFutureListener, builder, close, isClosed, isClosing, preClose, removeCloseFutureListener
getSimplifiedLogger
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addCloseFutureListener, close, close, close, getMaxCloseWaitTime, isClosed, isClosing, isOpen, removeCloseFutureListener
public static final int DEFAULT_READBUF_SIZE
private static final java.util.concurrent.atomic.AtomicLong SESSION_ID_GENERATOR
private final long id
private final Nio2Service service
private final IoHandler ioHandler
private final java.nio.channels.AsynchronousSocketChannel socketChannel
private final java.util.Map<java.lang.Object,java.lang.Object> attributes
private final java.net.SocketAddress localAddress
private final java.net.SocketAddress remoteAddress
private final java.net.SocketAddress acceptanceAddress
private final FactoryManager manager
private final java.util.Queue<Nio2DefaultIoWriteFuture> writes
private final java.util.concurrent.atomic.AtomicReference<Nio2DefaultIoWriteFuture> currentWrite
public Nio2Session(Nio2Service service, FactoryManager manager, IoHandler handler, java.nio.channels.AsynchronousSocketChannel socket, java.net.SocketAddress acceptanceAddress) throws java.io.IOException
java.io.IOException
public long getId()
public java.lang.Object getAttribute(java.lang.Object key)
IoSession
getAttribute
in interface IoSession
key
- the key of the attributenull
if there is no attribute with the specified keypublic java.lang.Object setAttribute(java.lang.Object key, java.lang.Object value)
IoSession
setAttribute
in interface IoSession
key
- the key of the attributevalue
- the value of the attributenull
if it is new.public java.lang.Object setAttributeIfAbsent(java.lang.Object key, java.lang.Object value)
IoSession
if (containsAttribute(key)) {
return getAttribute(key);
} else {
return setAttribute(key, value);
}
setAttributeIfAbsent
in interface IoSession
key
- The key of the attribute we want to setvalue
- The value we want to setnull
if not found.public java.lang.Object removeAttribute(java.lang.Object key)
IoSession
removeAttribute
in interface IoSession
key
- The key of the attribute we want to removenull
if not found.public java.net.SocketAddress getRemoteAddress()
getRemoteAddress
in interface ConnectionEndpointsIndicator
public java.net.SocketAddress getLocalAddress()
getLocalAddress
in interface ConnectionEndpointsIndicator
public java.net.SocketAddress getAcceptanceAddress()
getAcceptanceAddress
in interface IoSession
null
if session was initiated by this peer instead of being acceptedpublic java.nio.channels.AsynchronousSocketChannel getSocket()
public IoHandler getIoHandler()
public void suspend()
public IoWriteFuture writePacket(Buffer buffer) throws java.io.IOException
PacketWriter
writePacket
in interface PacketWriter
buffer
- the buffer to encode and send. NOTE: the buffer must not be touched
until the returned write future is completed.IoWriteFuture
that can be used to check when the packet has actually been sentjava.io.IOException
- if an error occurred when encoding sending the packetprotected void exceptionCaught(java.lang.Throwable exc)
protected CloseFuture doCloseGracefully()
doCloseGracefully
in class AbstractCloseable
protected void doCloseImmediately()
AbstractCloseable
doCloseImmediately is called once and only once with state == Immediate
Overriding methods should always call the base implementation. It may be called concurrently while preClose() or doCloseGracefully is executing
doCloseImmediately
in class AbstractCloseable
public Nio2Service getService()
getService
in interface IoSession
IoService
that created this session.public void startReading()
public void startReading(int bufSize)
public void startReading(byte[] buf)
public void startReading(byte[] buf, int offset, int len)
public void startReading(java.nio.ByteBuffer buffer)
protected void doReadCycle(java.nio.ByteBuffer buffer, Readable bufReader)
protected Nio2CompletionHandler<java.lang.Integer,java.lang.Object> createReadCycleCompletionHandler(java.nio.ByteBuffer buffer, Readable bufReader)
protected void handleReadCycleCompletion(java.nio.ByteBuffer buffer, Readable bufReader, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completionHandler, java.lang.Integer result, java.lang.Object attachment)
protected void handleReadCycleFailure(java.nio.ByteBuffer buffer, Readable bufReader, java.lang.Throwable exc, java.lang.Object attachment)
protected void doReadCycle(java.nio.ByteBuffer buffer, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completion)
protected void startWriting()
protected void doWriteCycle(java.nio.ByteBuffer buffer, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completion)
protected Nio2CompletionHandler<java.lang.Integer,java.lang.Object> createWriteCycleCompletionHandler(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket, java.nio.ByteBuffer buffer)
protected void handleCompletedWriteCycle(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket, java.nio.ByteBuffer buffer, int writeLen, Nio2CompletionHandler<java.lang.Integer,java.lang.Object> completionHandler, java.lang.Integer result, java.lang.Object attachment)
protected void handleWriteCycleFailure(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket, java.nio.ByteBuffer buffer, int writeLen, java.lang.Throwable exc, java.lang.Object attachment)
protected void finishWrite(Nio2DefaultIoWriteFuture future)
public java.lang.String toString()
toString
in class java.lang.Object