private static bool GetGlobalSessionToLocal(string token) { try { GlobalSessionService.SessionModel gsm = GlobalSession.GetSession(token, WebIpFlag); if (gsm != null) { SessionModel sm = new SessionModel(); SessionModelCopy(gsm, out sm); lock (userhash.SyncRoot) { userhash.Remove(token); userhash.Add(token, sm); } //LogHelper.WriteInfo("全局会话服务用户数据到本地:" + token); return(true); } else { //LogHelper.WriteInfo("全局会话无返回数据:" + token); return(false); } } catch (Exception ex) { LogHelper.WriteError("GetGlobalSessionToLocal执行异常", ex); return(false); } }
/// <summary> /// 创建用户会话接口 /// </summary> /// <param name="uid"></param> /// <param name="rights"></param> /// <returns></returns> public static string CreateSession(string sysflag, string username, int uid, int[] rid, int group, int[] rights, DataTable cars, string userIP, bool onecaruser = false) { string token = Guid.NewGuid().ToString(); SessionModel sm = new SessionModel(); sm.uid = uid; sm.username = username; sm.rid = rid; sm.group = group; sm.token = token; sm.aids = rights; sm.cars = cars; sm.cars.TableName = "cars"; sm.logintime = DateTime.Now; sm.lastoper = DateTime.Now; sm.sysflag = sysflag; sm.webserverflag = WebIpFlag; sm.onecaruser = onecaruser; sm.userIP = userIP; //添加到本地hash表 lock (userhash.SyncRoot) { userhash.Add(token, sm); } try { //构建全局会话管理数据模型 GlobalSessionService.SessionModel gsm = new GlobalSessionService.SessionModel(); SessionModelCopy(sm, out gsm); //添加数据到全局会话管理 if (GSRunFlag) { GlobalSession.SyncSession((GlobalSessionService.SessionModel)gsm); //LogHelper.WriteInfo("新增用户数据至全局会话服务:" + token); } } catch (Exception ex) { LogHelper.WriteError("CreateSession执行异常", ex); } return(token); }
/// <summary> /// 拷贝对象,将源对象中与目标对象中名称相同的属性拷贝过去 /// </summary> /// <param name="origin">源对象</param> /// <param name="target">目标对象</param> /// <param name="targetType">目标对象类型</param> protected static void SessionModelCopy(GlobalSessionService.SessionModel origin, out SessionModel target) { if (origin == null) { target = null; return; } target = new SessionModel(); target.username = origin.username; target.aids = origin.aids; target.cars = origin.cars; target.group = origin.group; target.lastoper = origin.lastoper; target.lastopersid = origin.lastopersid; target.logintime = origin.logintime; target.onecaruser = origin.onecaruser; target.rid = origin.rid; target.sysflag = origin.sysflag; target.token = origin.token; target.uid = origin.uid; target.webserverflag = origin.webserverflag; target.userIP = origin.userIP; }
/// <summary> /// 用户会话状态维护线程 /// 同时对全局用户状态和实时信息服务状态进行检查 /// </summary> private static void TimeoutManager() { LogHelper.WriteInfo("启动会话维护TimeoutManager服务。"); while (true) { string to = System.Configuration.ConfigurationManager.AppSettings["Timeout"].ToString(); int.TryParse(to, out Timeout); //判断全局会话服务是否正常 try { string value = GlobalSession.GetData("1"); if (value == "1") { GSRunFlag = true; } else { GSRunFlag = false; } } catch (Exception ex) { GSRunFlag = false; LogHelper.WriteError("全局会话服务调用异常", ex); } try { //超时会话集合 List <string> timeouttokens = new List <string>(); //正常会话集合 List <string> tokens = new List <string>(); foreach (string token in userhash.Keys) { SessionModel sm = (SessionModel)userhash[token]; if ((DateTime.Now - sm.lastoper).TotalSeconds < Timeout) { tokens.Add(token); } else { timeouttokens.Add(token); } } // if (tokens.Count > 0) { string tks = ""; List <GlobalSessionService.SessionModel> gsms = new List <GlobalSessionService.SessionModel>(); foreach (string token in tokens) { tks += token + ","; //构建全局会话管理数据模型 GlobalSessionService.SessionModel gsm = new GlobalSessionService.SessionModel(); SessionModelCopy((SessionModel)userhash[token], out gsm); gsms.Add((GlobalSessionService.SessionModel)gsm); } //判断全局会话服务是否需要同步数据 if (GSRunFlag && GlobalSession.IsActive(tks, WebIpFlag) == "false") { //同步所有数据至全局会话服务 GlobalSession.SyncSessions(gsms.ToArray()); //LogHelper.WriteInfo("同步所有数据至全局会话服务:" + tks); } } if (timeouttokens.Count > 0) { string timeouttks = ""; lock (userhash.SyncRoot) { foreach (string token in timeouttokens) { timeouttks += token + ","; logoutForDb((SessionModel)userhash[token]); userhash.Remove(token); } } if (GSRunFlag) { //清除全局会话 GlobalSession.CloseSession(timeouttks, WebIpFlag); //LogHelper.WriteInfo("清除全局会话服务:" + timeouttks); } } } catch (Exception ex) { LogHelper.WriteError("TimeoutManager执行异常", ex); } Thread.Sleep(10 * 1000); } }
/// <remarks/> public void SyncSessionAsync(SessionModel sm) { this.SyncSessionAsync(sm, null); }
public void SyncSession(SessionModel sm) { this.Invoke("SyncSession", new object[] { sm }); }