Exemplo n.º 1
0
        public bool TestIdle(int opType, int idleTime)
        {
            if (m_State <= 0 || m_IsGoingToClose)
            {
                return(false);
            }
            if (opType < 0 || opType > 2 || idleTime <= 0)
            {
                return(false);
            }

            DateTime currentTime = DateTime.Now;

            double readIdleTime  = (currentTime - m_LastReadTime).TotalSeconds;
            double writeIdleTime = (currentTime - m_LastWriteTime).TotalSeconds;

            bool isIdle = false;

            if (opType == IO_RECV_SEND)
            {
                isIdle = readIdleTime > idleTime && writeIdleTime > idleTime;
            }
            else if (opType == IO_RECV)
            {
                isIdle = readIdleTime > idleTime;
            }
            else if (opType == IO_SEND)
            {
                isIdle = writeIdleTime > idleTime;
            }

            if (isIdle)
            {
                if (m_IoHandler != null)
                {
                    try
                    {
                        m_IoHandler.OnIdle(this, opType);
                    }
                    catch { }
                }
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        public int TestIdle(int opType, int idleTime)
        {
            if (m_State <= 0 || m_IsGoingToClose)
            {
                return(0);
            }
            if (opType < 0 || idleTime <= 0)
            {
                return(0);
            }

            DateTime currentTime = DateTime.Now;

            double readIdleTime  = (currentTime - m_LastReadTime).TotalSeconds;
            double writeIdleTime = (currentTime - m_LastWriteTime).TotalSeconds;

            bool isIdle   = false;
            int  idleFlag = 0;

            if (opType == IO_ANY)
            {
                if (readIdleTime > idleTime)
                {
                    idleFlag |= IO_RECEIVE;
                }
                if (writeIdleTime > idleTime)
                {
                    idleFlag |= IO_SEND;
                }
                isIdle = idleFlag > 0;
            }
            else if (opType == IO_BOTH)
            {
                isIdle = readIdleTime > idleTime && writeIdleTime > idleTime;
                if (isIdle)
                {
                    idleFlag = IO_BOTH;
                }
            }
            else if (opType == IO_RECEIVE)
            {
                isIdle = readIdleTime > idleTime;
                if (isIdle)
                {
                    idleFlag = IO_RECEIVE;
                }
            }
            else if (opType == IO_SEND)
            {
                isIdle = writeIdleTime > idleTime;
                if (isIdle)
                {
                    idleFlag = IO_SEND;
                }
            }

            if (isIdle)
            {
                if (m_IoHandler != null)
                {
                    try
                    {
                        // isIdle is true, so idleFlag > 0 (IO_RECEIVE/IO_SEND/IO_BOTH)
                        m_IoHandler.OnIdle(this, idleFlag);
                    }
                    catch { }
                }
                return(idleFlag);
            }

            return(0);
        }