/// <summary> /// 根据sessionId获取session /// </summary> /// <param name="sessionId">sessionId</param> /// <param name="sessions">session</param> /// <returns></returns> public SmcErr GetSessionsBySessionId(string sessionId, out CgwSession sessions) { SmcErr err = new SmcErr(); sessions = null; if (rwl.TryEnterReadLock(CgwConst.ENTER_LOCK_WAIT_TIME)) { try { foreach (CgwSession cgwSession in sessionDict.Values) { if (cgwSession.SessionId == sessionId) { sessions = cgwSession; break; } } } finally { rwl.ExitReadLock(); } } return(err); }
/// <summary> /// 连接融合网关 /// </summary> /// <param name="cgwPwd">连接字符串</param> /// <returns></returns> public SmcErr Connect(string cgwPwd) { SmcErr err = new CgwError(); NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log); if (isLoadSuccess == false) { err.SetErrorNo(CgwError.ERR_CGW_IS_LOADING); return(err); } //if (false == string.IsNullOrEmpty(cgwPwd)) //{ ////进行SHA256加密 //string newPassword = CommonFunction.SHA256Encrypt(strPwd); //保存当前会话 //if (newPassword == cgwPwd) //{ //获取SessionManage中Session个数 if (SessionManage.Instance().GetSessionsCount() == 0) { Guid sessionGuid = Guid.NewGuid(); //保存至CGW的session管理中 CgwSession CgwSession = new CgwSession(sessionGuid, OperationContext.Current.SessionId); isChangeLic = false; MessageProperties properties = OperationContext.Current.IncomingMessageProperties; //获取传进的消息属性 RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; //获取消息发送的远程终结点IP和端口 if (endpoint != null) { CgwConst.SmcIp = endpoint.Address; // 获取发送消息的客户端的 IP 地址。 logEx.Info("SMC Connect CgwService Successful,Current.SMC IP is : {0}", CgwConst.SmcIp); } else { logEx.Info("SMC Connect CgwService Successful,Current.SMC IP is null."); } } else { err.SetErrorNo(CgwError.ERR_CGW_BUSINESS_SESSION_COUNT_MAX); logEx.Warn("Session Count is Top Of Max number !"); } //} //else //{ // err.SetErrorNo(CgwError.ERR_CGW_BUSINESS_PASSWORD_INCORRECT); // logEx.Error("Connect Password is Incorrect !"); //} //} //else //{ // err.SetErrorNo(CgwError.ERR_CGW_BUSINESS_PASSWORD_NULL); // logEx.Error("Connect Password is null !"); //} return(err); }
/// <summary> /// 添加会话 /// </summary> /// <param name="sessionGuid">会话GuidID</param> /// <param name="smcSession">会话数据</param> /// <returns></returns> public SmcErr AddSession(Guid sessionGuid, CgwSession smcSession) { SmcErr err = new SmcErr(); if (rwl.TryEnterWriteLock(CgwConst.ENTER_LOCK_WAIT_TIME)) { try { if (sessionDict.ContainsKey(sessionGuid)) { sessionDict.Remove(sessionGuid); } sessionDict.Add(sessionGuid, smcSession); } finally { rwl.ExitWriteLock(); } } return(err); }
/// <summary> /// 检查Session /// </summary> /// <returns></returns> private SmcErr CheckSession() { NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log); SmcErr err = new CgwError(); CgwSession CgwSession = null; if (OperationContext.Current != null) { string strSessionId = OperationContext.Current.SessionId; err = SessionManage.Instance().GetSessionsBySessionId(strSessionId, out CgwSession); if (err.IsSuccess() == false || CgwSession == null) { err.SetErrorNo(CgwError.ERR_CGW_BUSINESS_SESSION_NULL); } else { if (CgwSession != null) { //更新Session最后访问时间 CgwSession.LastVisitedTime = DateTime.Now; //logEx.Trace("Current Id is:{0}, LastVisitedTime is :{1}", strSessionId, CgwSession.LastVisitedTime.ToLongTimeString()); logEx.Trace("Current LastVisitedTime is :{0}", CgwSession.LastVisitedTime.ToLongTimeString()); if (isLoadSuccess == false) { err.SetErrorNo(CgwError.ERR_CGW_IS_LOADING); } } } } else { err.SetErrorNo(CgwError.ERR_CGW_BUSINESS_CURRENTSESSION_NULL); } return(err); }
/// <summary> /// 添加会话 /// </summary> /// <param name="sessionGuid">会话GuidID</param> /// <param name="smcSession">会话数据</param> /// <returns></returns> public SmcErr AddSession(Guid sessionGuid, CgwSession smcSession) { SmcErr err = new SmcErr(); if (rwl.TryEnterWriteLock(CgwConst.ENTER_LOCK_WAIT_TIME)) { try { if (sessionDict.ContainsKey(sessionGuid)) { sessionDict.Remove(sessionGuid); } sessionDict.Add(sessionGuid, smcSession); } finally { rwl.ExitWriteLock(); } } return err; }
/// <summary> /// 根据sessionId获取session /// </summary> /// <param name="sessionId">sessionId</param> /// <param name="sessions">session</param> /// <returns></returns> public SmcErr GetSessionsBySessionId(string sessionId, out CgwSession sessions) { SmcErr err = new SmcErr(); sessions = null; if (rwl.TryEnterReadLock(CgwConst.ENTER_LOCK_WAIT_TIME)) { try { foreach (CgwSession cgwSession in sessionDict.Values) { if (cgwSession.SessionId == sessionId) { sessions = cgwSession; break; } } } finally { rwl.ExitReadLock(); } } return err; }
/// <summary> /// 连接融合网关 /// </summary> /// <param name="cgwPwd">连接字符串</param> /// <returns></returns> public SmcErr Connect(string cgwPwd) { SmcErr err = new CgwError(); NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log); if (isLoadSuccess == false) { err.SetErrorNo(CgwError.ERR_CGW_IS_LOADING); return err; } //if (false == string.IsNullOrEmpty(cgwPwd)) //{ ////进行SHA256加密 //string newPassword = CommonFunction.SHA256Encrypt(strPwd); //保存当前会话 //if (newPassword == cgwPwd) //{ //获取SessionManage中Session个数 if (SessionManage.Instance().GetSessionsCount() == 0) { Guid sessionGuid = Guid.NewGuid(); //保存至CGW的session管理中 CgwSession CgwSession = new CgwSession(sessionGuid, OperationContext.Current.SessionId); isChangeLic = false; MessageProperties properties = OperationContext.Current.IncomingMessageProperties; //获取传进的消息属性 RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; //获取消息发送的远程终结点IP和端口 if (endpoint != null) { CgwConst.SmcIp = endpoint.Address; // 获取发送消息的客户端的 IP 地址。 logEx.Info("SMC Connect CgwService Successful,Current.SMC IP is : {0}", CgwConst.SmcIp); } else { logEx.Info("SMC Connect CgwService Successful,Current.SMC IP is null."); } } else { err.SetErrorNo(CgwError.ERR_CGW_BUSINESS_SESSION_COUNT_MAX); logEx.Warn("Session Count is Top Of Max number !"); } //} //else //{ // err.SetErrorNo(CgwError.ERR_CGW_BUSINESS_PASSWORD_INCORRECT); // logEx.Error("Connect Password is Incorrect !"); //} //} //else //{ // err.SetErrorNo(CgwError.ERR_CGW_BUSINESS_PASSWORD_NULL); // logEx.Error("Connect Password is null !"); //} return err; }