| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.Timers;
- namespace SysDataLibs
- {
- public class LoginGUID : IDisposable
- {
- Timer _Timer = null;
- WSLoginSessionList _LoginSessonList = null;
- int _SessionExpiredInterval = AppEnv.SysSetObj.GetInt("SessionExpiredInterval");
- public LoginGUID()
- {
- _LoginSessonList = new WSLoginSessionList();
- _SessionExpiredInterval = AppEnv.SysSetObj.GetInt("SessionExpiredInterval");
- if (_SessionExpiredInterval <= 0)
- _SessionExpiredInterval = 20;
- _Timer = new Timer();
- _Timer.AutoReset = true;
- _Timer.Enabled = true;
- _Timer.Interval = 5000;
- _Timer.Elapsed += new ElapsedEventHandler(_Timer_Elapsed);
- _Timer.Start();
- }
- void _Timer_Elapsed(object sender, ElapsedEventArgs e)
- {
- lock (_LoginSessonList)
- {
- WSLoginSessionList loList = _LoginSessonList.GetExpiredLogin(_SessionExpiredInterval);
- if (loList.Count > 0)
- {
- foreach (WSLoginSession loSession in loList)
- {
- _LoginSessonList.Remove(loSession);
- }
- }
- }
- }
- public void AddOne(string guid, UserSession session)
- {
- _LoginSessonList.Add(guid, session);
- }
- public UserSession GetOne(string guid)
- {
- UserSession loRetVal = null;
- if (_LoginSessonList.ContainsGUID(guid))
- {
- _LoginSessonList[guid].RefreshLastAccessTime();
- loRetVal = _LoginSessonList[guid].UserSession;
- }
- return loRetVal;
- }
- public bool RemoveGUID(string guid)
- {
- bool lbRetVal = false;
- if (_LoginSessonList.ContainsGUID(guid))
- {
- lbRetVal = _LoginSessonList.Remove(_LoginSessonList[guid]);
-
- }
- return lbRetVal;
- }
- // 根据用户名称删除
- public void RemoveUser(string pcUserCode)
- {
- lock (_LoginSessonList)
- {
- WSLoginSessionList loList = _LoginSessonList.GetByUser(pcUserCode);
- if (loList.Count > 0)
- {
- foreach (WSLoginSession loSession in loList)
- {
- _LoginSessonList.Remove(loSession);
- }
- }
- }
- }
- #region IDisposable 成员
- public void Dispose()
- {
- if (_Timer != null)
- {
- _Timer.Close();
- _Timer.Dispose();
- }
- }
- #endregion
- // 单键模式
- private static LoginGUID _Instance = null;
- public static LoginGUID Instance
- {
- get
- {
- if (_Instance == null)
- {
- _Instance = new LoginGUID();
- }
- return _Instance;
- }
- }
- }
- public class WSLoginSessionList : List<WSLoginSession>
- {
- public void Add(string pcGUID, UserSession poUserSession)
- {
- if (!this.ContainsGUID(pcGUID))
- {
- this.Add(new WSLoginSession(pcGUID, poUserSession));
- }
- }
- public new void Add(WSLoginSession item)
- {
- if (!this.ContainsGUID(item.GUID))
- base.Add(item);
- }
- public WSLoginSession this[string pcGUID]
- {
- get
- {
- foreach (WSLoginSession loSession in this)
- {
- if (loSession.GUID == pcGUID)
- {
- return loSession;
- }
- }
- return null;
- }
- }
- public bool ContainsGUID(string pcGUID)
- {
- foreach (WSLoginSession loSession in this)
- {
- if (loSession.GUID == pcGUID)
- {
- return true;
- }
- }
- return false;
- }
-
- public WSLoginSessionList GetExpiredLogin(int pdExpiredTime)
- {
- WSLoginSessionList loRetVal = new WSLoginSessionList();
- foreach (WSLoginSession loLoginSession in this)
- {
- if (loLoginSession.IsExpired(pdExpiredTime))
- {
- loRetVal.Add(loLoginSession);
- }
- }
- return loRetVal;
- }
- public WSLoginSessionList GetByUser(string pcUserCode)
- {
- WSLoginSessionList loRetVal = new WSLoginSessionList();
- foreach (WSLoginSession loLoginSession in this)
- {
- if ((loLoginSession.UserSession != null) &&
- (loLoginSession.UserSession.UserInfo != null) &&
- (loLoginSession.UserSession.UserInfo.UserCode == pcUserCode))
- {
- loRetVal.Add(loLoginSession);
- }
- }
- return loRetVal;
- }
- }
- public class WSLoginSession
- {
- public WSLoginSession(string pcGUID, UserSession poUserSession)
- {
- _GUID = pcGUID;
- _UserSession = poUserSession;
- }
- string _GUID = "";
- public string GUID
- {
- get { return _GUID; }
- }
- DateTime _CreateTime = DateTime.Now;
- /// <summary>
- /// 创建时间
- /// </summary>
- public DateTime CreateTime
- {
- get { return _CreateTime; }
- }
- DateTime _LastTime = DateTime.Now;
- /// <summary>
- /// 最近一次访问时间
- /// </summary>
- public DateTime LastTime
- {
- get { return _LastTime; }
- }
- UserSession _UserSession = null;
- public UserSession UserSession
- {
- get { return _UserSession; }
- }
- public bool IsExpired(int pdExpiredTime)
- {
- TimeSpan loSpan = DateTime.Now - LastTime;
- return loSpan.Minutes > pdExpiredTime;
- }
- public void RefreshLastAccessTime()
- {
- _LastTime = DateTime.Now;
- }
- }
- }
|