/// <summary>
        /// Ad-hoc removal of an existing session
        /// </summary>
        /// <param name="sessionID">ID of session to be removed</param>
        /// <param name="terminateActiveSession">if true, force disconnection and removal of session even if it has an active connection</param>
        /// <returns>true if session removed or not already present; false if could not be removed due to an active connection</returns>
        public bool RemoveSession(SessionID sessionID, bool terminateActiveSession)
        {
            Session session = null;

            if (sessions_.TryGetValue(sessionID, out session))
            {
                if (session.IsLoggedOn && !terminateActiveSession)
                {
                    return(false);
                }
                session.Disconnect("Dynamic session removal");
                foreach (AcceptorSocketDescriptor descriptor in socketDescriptorForAddress_.Values)
                {
                    if (descriptor.RemoveSession(sessionID))
                    {
                        break;
                    }
                }
                sessions_.Remove(sessionID);
                session.Dispose();
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Ad-hoc removal of an existing session
        /// </summary>
        /// <param name="sessionID">ID of session to be removed</param>
        /// <param name="terminateActiveSession">if true, force disconnection and removal of session even if it has an active connection</param>
        /// <returns>true if session removed or not already present; false if could not be removed due to an active connection</returns>
        public bool RemoveSession(SessionID sessionID, bool terminateActiveSession)
        {
            Session session            = null;
            bool    disconnectRequired = false;

            lock (sync_)
            {
                if (sessionIDs_.Contains(sessionID))
                {
                    session = sessions_[sessionID];
                    if (session.IsLoggedOn && !terminateActiveSession)
                    {
                        return(false);
                    }
                    sessions_.Remove(sessionID);
                    disconnectRequired = IsConnected(sessionID) || IsPending(sessionID);
                    if (disconnectRequired)
                    {
                        SetDisconnected(sessionID);
                    }
                    disconnected_.Remove(sessionID);
                    sessionIDs_.Remove(sessionID);
                }
            }
            lock (_settings)
                _settings.Remove(sessionID);
            if (disconnectRequired)
            {
                session.Disconnect("Dynamic session removal");
            }
            OnRemove(sessionID); // ensure session's reader thread is gone before we dispose session
            if (session != null)
            {
                session.Dispose();
            }
            return(true);
        }