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.util.concurrent.atomic.AtomicLong |
lastReadCycleStart |
private java.util.concurrent.atomic.AtomicLong |
lastWriteCycleStart |
private java.net.SocketAddress |
localAddress |
private java.util.concurrent.atomic.AtomicBoolean |
outputShutDown |
private PropertyResolver |
propertyResolver |
private java.util.concurrent.atomic.AtomicLong |
readCyclesCounter |
private java.lang.Thread |
readerThread |
private java.lang.Runnable |
readRunnable |
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 boolean |
suspend |
private java.lang.Object |
suspendLock |
private java.util.concurrent.atomic.AtomicLong |
writeCyclesCounter |
private java.util.Queue<Nio2DefaultIoWriteFuture> |
writes |
closeFuture, futureLock, statelog| Constructor and Description |
|---|
Nio2Session(Nio2Service service,
PropertyResolver propertyResolver,
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 |
doShutdownOutputStream(Nio2DefaultIoWriteFuture future,
java.nio.channels.AsynchronousSocketChannel socket) |
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.
|
void |
resumeRead()
Resume read operations on this session.
|
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 |
shutdownOutputStream()
Handle received EOF.
|
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()
Intended for tests simulating a sudden connection drop only! Do not call otherwise.
|
void |
suspendRead()
Suspend read operations on this session.
|
java.lang.String |
toString() |
IoWriteFuture |
writeBuffer(Buffer buffer)
Write a packet on the socket.
|
addCloseFutureListener, builder, close, getFutureLock, isClosed, isClosing, preClose, removeCloseFutureListenerdebug, debug, debug, debug, debug, error, error, error, error, error, getSimplifiedLogger, info, info, warn, warn, warn, warn, warn, warn, warn, warnclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitaddCloseFutureListener, close, close, getMaxCloseWaitTime, isClosed, isClosing, isOpen, removeCloseFutureListenerpublic 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 PropertyResolver propertyResolver
private final java.util.Queue<Nio2DefaultIoWriteFuture> writes
private final java.util.concurrent.atomic.AtomicReference<Nio2DefaultIoWriteFuture> currentWrite
private final java.util.concurrent.atomic.AtomicLong readCyclesCounter
private final java.util.concurrent.atomic.AtomicLong lastReadCycleStart
private final java.util.concurrent.atomic.AtomicLong writeCyclesCounter
private final java.util.concurrent.atomic.AtomicLong lastWriteCycleStart
private final java.util.concurrent.atomic.AtomicBoolean outputShutDown
private final java.lang.Object suspendLock
private volatile boolean suspend
private volatile java.lang.Runnable readRunnable
private java.lang.Thread readerThread
public Nio2Session(Nio2Service service, PropertyResolver propertyResolver, IoHandler handler, java.nio.channels.AsynchronousSocketChannel socket, java.net.SocketAddress acceptanceAddress) throws java.io.IOException
java.io.IOExceptionpublic long getId()
public java.lang.Object getAttribute(java.lang.Object key)
IoSessiongetAttribute in interface IoSessionkey - 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)
IoSessionsetAttribute in interface IoSessionkey - 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 IoSessionkey - 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)
IoSessionremoveAttribute in interface IoSessionkey - The key of the attribute we want to removenull if not found.public java.net.SocketAddress getRemoteAddress()
getRemoteAddress in interface ConnectionEndpointsIndicatorpublic java.net.SocketAddress getLocalAddress()
getLocalAddress in interface ConnectionEndpointsIndicatorpublic java.net.SocketAddress getAcceptanceAddress()
getAcceptanceAddress in interface IoSessionnull if session was initiated by
this peer instead of being acceptedpublic java.nio.channels.AsynchronousSocketChannel getSocket()
public IoHandler getIoHandler()
public void suspend()
public IoWriteFuture writeBuffer(Buffer buffer) throws java.io.IOException
IoSessionwriteBuffer in interface IoSessionbuffer - the buffer 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 sending the packetprotected void exceptionCaught(java.lang.Throwable exc)
protected CloseFuture doCloseGracefully()
doCloseGracefully in class AbstractCloseableprotected void doCloseImmediately()
AbstractCloseabledoCloseImmediately 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 AbstractCloseablepublic Nio2Service getService()
getService in interface IoSessionIoService that created this session.public void shutdownOutputStream()
throws java.io.IOException
IoSessionshutdownOutputStream in interface IoSessionjava.io.IOException - If failed to shutdown the streamprotected void doShutdownOutputStream(Nio2DefaultIoWriteFuture future, java.nio.channels.AsynchronousSocketChannel socket) throws java.io.IOException
java.io.IOExceptionpublic 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)
public void suspendRead()
IoSessionsuspendRead in interface IoSessionpublic void resumeRead()
IoSessionresumeRead in interface IoSessionprotected 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