Class Nio2Session

    • Field Detail

      • SESSION_ID_GENERATOR

        private static final java.util.concurrent.atomic.AtomicLong SESSION_ID_GENERATOR
      • id

        private final long id
      • ioHandler

        private final IoHandler ioHandler
      • socketChannel

        private final java.nio.channels.AsynchronousSocketChannel socketChannel
      • attributes

        private final java.util.Map<java.lang.Object,​java.lang.Object> attributes
      • localAddress

        private final java.net.SocketAddress localAddress
      • remoteAddress

        private final java.net.SocketAddress remoteAddress
      • acceptanceAddress

        private final java.net.SocketAddress acceptanceAddress
      • readCyclesCounter

        private final java.util.concurrent.atomic.AtomicLong readCyclesCounter
      • lastReadCycleStart

        private final java.util.concurrent.atomic.AtomicLong lastReadCycleStart
      • writeCyclesCounter

        private final java.util.concurrent.atomic.AtomicLong writeCyclesCounter
      • lastWriteCycleStart

        private final java.util.concurrent.atomic.AtomicLong lastWriteCycleStart
      • outputShutDown

        private final java.util.concurrent.atomic.AtomicBoolean outputShutDown
      • suspendLock

        private final java.lang.Object suspendLock
      • suspend

        private volatile boolean suspend
      • readRunnable

        private volatile java.lang.Runnable readRunnable
      • readerThread

        private java.lang.Thread readerThread
    • Constructor Detail

      • Nio2Session

        public Nio2Session​(Nio2Service service,
                           PropertyResolver propertyResolver,
                           IoHandler handler,
                           java.nio.channels.AsynchronousSocketChannel socket,
                           java.net.SocketAddress acceptanceAddress)
                    throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • getId

        public long getId()
        Specified by:
        getId in interface IoSession
        Returns:
        a unique identifier for this session. Every session has its own ID which is different from any other.
      • getAttribute

        public java.lang.Object getAttribute​(java.lang.Object key)
        Description copied from interface: IoSession
        Returns the value of the user-defined attribute of this session.
        Specified by:
        getAttribute in interface IoSession
        Parameters:
        key - the key of the attribute
        Returns:
        null if there is no attribute with the specified key
      • setAttribute

        public java.lang.Object setAttribute​(java.lang.Object key,
                                             java.lang.Object value)
        Description copied from interface: IoSession
        Sets a user-defined attribute.
        Specified by:
        setAttribute in interface IoSession
        Parameters:
        key - the key of the attribute
        value - the value of the attribute
        Returns:
        The old value of the attribute - null if it is new.
      • setAttributeIfAbsent

        public java.lang.Object setAttributeIfAbsent​(java.lang.Object key,
                                                     java.lang.Object value)
        Description copied from interface: IoSession
        Sets a user defined attribute if the attribute with the specified key is not set yet. This method is same with the following code except that the operation is performed atomically.
         
         if (containsAttribute(key)) {
             return getAttribute(key);
         } else {
             return setAttribute(key, value);
         }
         
         
        Specified by:
        setAttributeIfAbsent in interface IoSession
        Parameters:
        key - The key of the attribute we want to set
        value - The value we want to set
        Returns:
        The old value of the attribute - null if not found.
      • removeAttribute

        public java.lang.Object removeAttribute​(java.lang.Object key)
        Description copied from interface: IoSession
        Removes a user-defined attribute with the specified key.
        Specified by:
        removeAttribute in interface IoSession
        Parameters:
        key - The key of the attribute we want to remove
        Returns:
        The old value of the attribute - null if not found.
      • getLocalAddress

        public java.net.SocketAddress getLocalAddress()
        Specified by:
        getLocalAddress in interface ConnectionEndpointsIndicator
        Returns:
        the socket address of local machine which is associated with this session.
      • getAcceptanceAddress

        public java.net.SocketAddress getAcceptanceAddress()
        Specified by:
        getAcceptanceAddress in interface IoSession
        Returns:
        The service address through which this session was accepted - null if session was initiated by this peer instead of being accepted
      • getSocket

        public java.nio.channels.AsynchronousSocketChannel getSocket()
      • getIoHandler

        public IoHandler getIoHandler()
      • suspend

        public void suspend()
        Intended for tests simulating a sudden connection drop only! Do not call otherwise.
      • writeBuffer

        public IoWriteFuture writeBuffer​(Buffer buffer)
                                  throws java.io.IOException
        Description copied from interface: IoSession
        Write a packet on the socket. Multiple writes can be issued concurrently and will be queued.
        Specified by:
        writeBuffer in interface IoSession
        Parameters:
        buffer - the buffer send. NOTE: the buffer must not be touched until the returned write future is completed.
        Returns:
        An IoWriteFuture that can be used to check when the packet has actually been sent
        Throws:
        java.io.IOException - if an error occurred when sending the packet
      • exceptionCaught

        protected void exceptionCaught​(java.lang.Throwable exc)
      • doCloseImmediately

        protected void doCloseImmediately()
        Description copied from class: 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

        Overrides:
        doCloseImmediately in class AbstractCloseable
      • shutdownOutputStream

        public void shutdownOutputStream()
                                  throws java.io.IOException
        Description copied from interface: IoSession
        Handle received EOF.
        Specified by:
        shutdownOutputStream in interface IoSession
        Throws:
        java.io.IOException - If failed to shutdown the stream
      • doShutdownOutputStream

        protected void doShutdownOutputStream​(Nio2DefaultIoWriteFuture future,
                                              java.nio.channels.AsynchronousSocketChannel socket)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • startReading

        public void startReading()
      • startReading

        public void startReading​(int bufSize)
      • startReading

        public void startReading​(byte[] buf)
      • startReading

        public void startReading​(byte[] buf,
                                 int offset,
                                 int len)
      • startReading

        public void startReading​(java.nio.ByteBuffer buffer)
      • doReadCycle

        protected void doReadCycle​(java.nio.ByteBuffer buffer,
                                   Readable bufReader)
      • createReadCycleCompletionHandler

        protected Nio2CompletionHandler<java.lang.Integer,​java.lang.Object> createReadCycleCompletionHandler​(java.nio.ByteBuffer buffer,
                                                                                                                   Readable bufReader)
      • handleReadCycleCompletion

        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)
      • handleReadCycleFailure

        protected void handleReadCycleFailure​(java.nio.ByteBuffer buffer,
                                              Readable bufReader,
                                              java.lang.Throwable exc,
                                              java.lang.Object attachment)
      • suspendRead

        public void suspendRead()
        Description copied from interface: IoSession
        Suspend read operations on this session. If the session usage includes a graceful shutdown with messages being exchanged, the caller needs to take care of resuming reading the input in order to actually be able to carry on the conversation with the peer.
        Specified by:
        suspendRead in interface IoSession
      • resumeRead

        public void resumeRead()
        Description copied from interface: IoSession
        Resume read operations on this session.
        Specified by:
        resumeRead in interface IoSession
      • doReadCycle

        protected void doReadCycle​(java.nio.ByteBuffer buffer,
                                   Nio2CompletionHandler<java.lang.Integer,​java.lang.Object> completion)
      • startWriting

        protected void startWriting()
      • doWriteCycle

        protected void doWriteCycle​(java.nio.ByteBuffer buffer,
                                    Nio2CompletionHandler<java.lang.Integer,​java.lang.Object> completion)
      • createWriteCycleCompletionHandler

        protected Nio2CompletionHandler<java.lang.Integer,​java.lang.Object> createWriteCycleCompletionHandler​(Nio2DefaultIoWriteFuture future,
                                                                                                                    java.nio.channels.AsynchronousSocketChannel socket,
                                                                                                                    java.nio.ByteBuffer buffer)
      • handleCompletedWriteCycle

        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)
      • handleWriteCycleFailure

        protected void handleWriteCycleFailure​(Nio2DefaultIoWriteFuture future,
                                               java.nio.channels.AsynchronousSocketChannel socket,
                                               java.nio.ByteBuffer buffer,
                                               int writeLen,
                                               java.lang.Throwable exc,
                                               java.lang.Object attachment)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object