Class AbstractSession

  • All Implemented Interfaces:
    Session
    Direct Known Subclasses:
    AbstractNioSession

    public abstract class AbstractSession
    extends java.lang.Object
    implements Session
    Base connection
    • Field Detail

      • readBuffer

        protected IoBuffer readBuffer
      • log

        protected static final org.slf4j.Logger log
      • attributes

        protected final java.util.concurrent.ConcurrentHashMap<java.lang.String,​java.lang.Object> attributes
      • writeQueue

        protected java.util.Queue<WriteMessage> writeQueue
      • sessionIdleTimeout

        protected long sessionIdleTimeout
      • sessionTimeout

        protected long sessionTimeout
      • closed

        protected volatile boolean closed
      • handler

        protected Handler handler
      • loopback

        protected boolean loopback
      • lastOperationTimeStamp

        public java.util.concurrent.atomic.AtomicLong lastOperationTimeStamp
      • scheduleWritenBytes

        protected java.util.concurrent.atomic.AtomicLong scheduleWritenBytes
      • dispatchMessageDispatcher

        protected final Dispatcher dispatchMessageDispatcher
      • useBlockingWrite

        protected boolean useBlockingWrite
      • useBlockingRead

        protected boolean useBlockingRead
      • handleReadWriteConcurrently

        protected boolean handleReadWriteConcurrently
      • writeLock

        protected java.util.concurrent.locks.ReentrantLock writeLock
      • currentMessage

        protected java.util.concurrent.atomic.AtomicReference<WriteMessage> currentMessage
    • Constructor Detail

      • AbstractSession

        public AbstractSession​(SessionConfig sessionConfig)
    • Method Detail

      • setSessionIdleTimeout

        public void setSessionIdleTimeout​(long sessionIdleTimeout)
        Specified by:
        setSessionIdleTimeout in interface Session
      • setSessionTimeout

        public void setSessionTimeout​(long sessionTimeout)
        Specified by:
        setSessionTimeout in interface Session
      • getWriteQueue

        public java.util.Queue<WriteMessage> getWriteQueue()
      • getStatistics

        public Statistics getStatistics()
      • getDispatchMessageDispatcher

        public Dispatcher getDispatchMessageDispatcher()
      • getWriteLock

        public java.util.concurrent.locks.ReentrantLock getWriteLock()
      • decode

        public abstract void decode()
      • updateTimeStamp

        public void updateTimeStamp()
      • getLastOperationTimeStamp

        public long getLastOperationTimeStamp()
        Description copied from interface: Session
        Return last operation timestamp,operation include read,write,idle
        Specified by:
        getLastOperationTimeStamp in interface Session
        Returns:
      • isHandleReadWriteConcurrently

        public final boolean isHandleReadWriteConcurrently()
        Description copied from interface: Session
        Return true if allow handling read and write concurrently,default is true.
        Specified by:
        isHandleReadWriteConcurrently in interface Session
        Returns:
      • setHandleReadWriteConcurrently

        public final void setHandleReadWriteConcurrently​(boolean handleReadWriteConcurrently)
        Specified by:
        setHandleReadWriteConcurrently in interface Session
      • getScheduleWritenBytes

        public long getScheduleWritenBytes()
        Description copied from interface: Session
        Return the bytes in write queue,there bytes is in memory.Use this method to controll writing speed.
        Specified by:
        getScheduleWritenBytes in interface Session
        Returns:
      • getReadBuffer

        public IoBuffer getReadBuffer()
      • setReadBuffer

        public void setReadBuffer​(IoBuffer readBuffer)
      • getReadBufferByteOrder

        public final java.nio.ByteOrder getReadBufferByteOrder()
        Description copied from interface: Session
        Return the session read buffer's byte order,big end or little end.
        Specified by:
        getReadBufferByteOrder in interface Session
        Returns:
      • setReadBufferByteOrder

        public final void setReadBufferByteOrder​(java.nio.ByteOrder readBufferByteOrder)
        Specified by:
        setReadBufferByteOrder in interface Session
      • onIdle

        protected void onIdle()
      • onConnected

        protected void onConnected()
      • onExpired

        public void onExpired()
      • wrapMessage

        protected abstract WriteMessage wrapMessage​(java.lang.Object msg,
                                                    java.util.concurrent.Future<java.lang.Boolean> writeFuture)
      • preprocessWriteMessage

        protected WriteMessage preprocessWriteMessage​(WriteMessage writeMessage)
        Pre-Process WriteMessage before writing to channel
        Parameters:
        writeMessage -
        Returns:
      • dispatchReceivedMessage

        protected void dispatchReceivedMessage​(java.lang.Object message)
      • onMessage

        private void onMessage​(java.lang.Object message,
                               Session session)
      • isClosed

        public final boolean isClosed()
        Description copied from interface: Session
        Check if session is closed
        Specified by:
        isClosed in interface Session
        Returns:
      • setClosed

        public final void setClosed​(boolean closed)
      • close

        public void close()
        Description copied from interface: Session
        Close session
        Specified by:
        close in interface Session
      • closeChannel

        protected abstract void closeChannel()
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • onException

        public void onException​(java.lang.Throwable e)
      • onClosed

        protected void onClosed()
      • setAttribute

        public void setAttribute​(java.lang.String key,
                                 java.lang.Object value)
        Description copied from interface: Session
        Set a attribute attched with this session
        Specified by:
        setAttribute in interface Session
      • setAttributeIfAbsent

        public java.lang.Object setAttributeIfAbsent​(java.lang.String key,
                                                     java.lang.Object value)
        Specified by:
        setAttributeIfAbsent in interface Session
      • removeAttribute

        public void removeAttribute​(java.lang.String key)
        Description copied from interface: Session
        Remove attribute
        Specified by:
        removeAttribute in interface Session
      • getAttribute

        public java.lang.Object getAttribute​(java.lang.String key)
        Description copied from interface: Session
        Return attribute associated with key
        Specified by:
        getAttribute in interface Session
        Returns:
      • clearAttributes

        public void clearAttributes()
        Description copied from interface: Session
        Clear attributes
        Specified by:
        clearAttributes in interface Session
      • start

        public void start()
        Description copied from interface: Session
        Start session
        Specified by:
        start in interface Session
      • start0

        protected abstract void start0()
      • onStarted

        protected void onStarted()
      • write

        public void write​(java.lang.Object packet)
        Description copied from interface: Session
        Write a message,if you don't care when the message is written
        Specified by:
        write in interface Session
      • writeFromUserCode

        public abstract void writeFromUserCode​(WriteMessage message)
      • isLoopbackConnection

        public final boolean isLoopbackConnection()
        Description copied from interface: Session
        return true if it is a loopback connection
        Specified by:
        isLoopbackConnection in interface Session
        Returns:
      • isUseBlockingWrite

        public boolean isUseBlockingWrite()
        Description copied from interface: Session
        Return true if using blocking write
        Specified by:
        isUseBlockingWrite in interface Session
        Returns:
      • setUseBlockingWrite

        public void setUseBlockingWrite​(boolean useBlockingWrite)
        Description copied from interface: Session
        Set if using blocking write
        Specified by:
        setUseBlockingWrite in interface Session
      • isUseBlockingRead

        public boolean isUseBlockingRead()
        Description copied from interface: Session
        Return true if using blocking read
        Specified by:
        isUseBlockingRead in interface Session
        Returns:
      • setUseBlockingRead

        public void setUseBlockingRead​(boolean useBlockingRead)
        Specified by:
        setUseBlockingRead in interface Session
      • clearWriteQueue

        public void clearWriteQueue()
      • isExpired

        public boolean isExpired()
        Description copied from interface: Session
        Return true if session is expired,session is expired beacause you set the sessionTimeout,if since session's last operation form now is over this vlaue,isExpired return true,and Handler.onExpired() will be invoked.
        Specified by:
        isExpired in interface Session
        Returns:
      • isIdle

        public boolean isIdle()
        Description copied from interface: Session
        Check if session is idle
        Specified by:
        isIdle in interface Session
        Returns:
      • transferTo

        public long transferTo​(long position,
                               long count,
                               java.nio.channels.FileChannel target)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • transferFrom

        public long transferFrom​(long position,
                                 long count,
                                 java.nio.channels.FileChannel source)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • onCreated

        protected void onCreated()