/// <summary> /// 检查模块许可 /// </summary> /// <param name="ClientHostKey"></param> /// <param name="AppName"></param> /// <param name="ModuleName"></param> /// <param name="ModuleVersion"></param> /// <returns></returns> public string CheckModuleLicense(string LoginID, string ClientHost, string AppName, string ModuleName, string ModuleVersion) { string ret = "Success"; if (AppName == "PCOCCenter" && ModuleName == "PCOCCenter.Manager") { return(ret); } if (ModuleName == AppName && ModuleName != "") { return(ret); // 表示引导程序APP启动,功能Module启动时才检测许可 } // 通过ModuleName反查许可中的AppName if (AppName == null || AppName == "") { AppName = GetAppNameFromLicenseAppModuleHT(ModuleName); } if (AppName == null || AppName == "") { ret = Utils.Utils.Translate("此功能未授权!"); return(ret); } // 检查Client是否已经连接此app[module,目前只检查到app一级],成功直接返回 ClientAppModuleInfo clientAppModuleInfo = CheckClientAppModuleExist(ClientHost, AppName, ModuleName, ModuleVersion); if (clientAppModuleInfo != null) { if (Monitor.TryEnter(ClientAppModuleInfos, 1000)) { try { clientAppModuleInfo.nAppRequestCount++; clientAppModuleInfo.nKeepAlive++; Console.WriteLine(string.Format("[{0}]{1}{2}{3}.", AppName, Utils.Utils.Translate("模块多开"), clientAppModuleInfo.nAppRequestCount, Utils.Utils.Translate("次"))); } catch (System.Exception ex) { } finally { Monitor.Exit(ClientAppModuleInfos); } } else { Monitor.Exit(ClientAppModuleInfos); } return(ret + @"_exist"); } // Client申请许可证,成功返回“Success”,失败返回原因。 return(RequestClientAppModuleLicense(LoginID, ClientHost, AppName, ModuleName, ModuleVersion)); }
////////////////////////////////////////////////////////////////////////// #region ClientLicense /// <summary> /// 客户端心跳包 /// </summary> /// <param name="ClientHost"></param> /// <param name="AppName"></param> /// <param name="ModuleName"></param> /// <param name="ModuleVersion"></param> public void ClientKeepAlive(string LoginID, string ClientHost, string AppName, List <string> ModuleNameList, string ModuleVersion) { foreach (string ModuleName in ModuleNameList) { if (ModuleName == null || ModuleName == "") { continue; } // 通过ModuleName反查许可中的AppName if (AppName == null || AppName == "") { AppName = GetAppNameFromLicenseAppModuleHT(ModuleName); } if (AppName == null || AppName == "") { continue; } ClientAppModuleInfo clientAppModuleInfo = CheckClientAppModuleExist(ClientHost, AppName, ModuleName, ModuleVersion); if (Monitor.TryEnter(ClientAppModuleInfos, 1000)) { if (clientAppModuleInfo == null) { RequestClientAppModuleLicense(LoginID, ClientHost, AppName, ModuleName, ModuleVersion); } try { clientAppModuleInfo.nKeepAlive++; if (clientAppModuleInfo.nKeepAlive > 10) { clientAppModuleInfo.nKeepAlive = 10; } } catch (System.Exception ex) { } finally { Monitor.Exit(ClientAppModuleInfos); } } else { Monitor.Exit(ClientAppModuleInfos); } } }
/// <summary> /// 校验客户端心跳信息,判断客户端是否异常退出 /// </summary> /// <param name="source"></param> /// <param name="e"></param> public void handlerVerifyClientsKeepAlive(object source, System.Timers.ElapsedEventArgs e) { if (bInit) { return; } for (int i = 0; i < ClientAppModuleInfos.Count; i++) { ClientAppModuleInfo clientAppModuleInfo = ClientAppModuleInfos[i]; if (clientAppModuleInfo.nKeepAlive <= 0) { clientAppModuleInfo.nAppRequestCount = 0; // 归还许可 GoBackLicense(clientAppModuleInfo.ClientHost, clientAppModuleInfo.AppName, clientAppModuleInfo.ModuleName, clientAppModuleInfo.ModuleVersion); // 更新登录状态 string sql = string.Format("update Login set LogoutTime = '{0}' , status = '{1}' where ID = '{2}'", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Utils.Utils.Translate("异常登出"), clientAppModuleInfo.LoginID); try { CenterService.DB.ExecuteNonQuery(sql); } catch (System.Exception ex) { } finally { } } else { clientAppModuleInfo.nKeepAlive--; } if (ClientAppModuleInfos.Count <= 0) { break; } } }
/// <summary> /// Client申请许可证,成功返回“Success”,失败返回原因。 /// </summary> /// <returns></returns> string RequestClientAppModuleLicense(string LoginID, string ClientHost, string AppName, string ModuleName, string ModuleVersion) { bool bRet = false; string failInfo = string.Empty; DateTime dateToday = DateTime.Today; if (Monitor.TryEnter(LicenseInfos, 1000)) { try { if (LicenseInfos.Count == 0) { failInfo = Utils.Utils.Translate("未发现许可文件,请和管理员联系!"); } else { foreach (LicenseInfo licenseInfo in LicenseInfos) { if (bRet) { break; } if (licenseInfo.IsUsed.ToLower() != "true") { continue; } if (string.IsNullOrEmpty(licenseInfo.ErrorInfo) == false) { continue; } failInfo = string.Format("[{0}]{1}", AppName, Utils.Utils.Translate("模块的许可信息未找到,请和管理员联系!")); foreach (ModuleInfo moduleInfo in licenseInfo.ModuleInfos) { if (moduleInfo.AppName == AppName) { DateTime dateExpiry = DateTime.Parse(moduleInfo.ExpiryDate); if (dateExpiry >= dateToday) { // 免费类型许可 if (moduleInfo.LicenseType == "free") { failInfo = string.Empty; bRet = true; } else { // 限制类型许可 if (moduleInfo.LicenseUsed < moduleInfo.LicenseCount) { moduleInfo.LicenseUsed++; // 成功申请到许可 failInfo = string.Empty; bRet = true; Console.WriteLine(string.Format("[{0}]{1}{2}.", AppName, Utils.Utils.Translate("模块被占用许可"), moduleInfo.LicenseUsed)); } else { failInfo = Utils.Utils.Translate("许可已经用完,请和管理员联系!"); Console.WriteLine(string.Format("[{0}]{1}{2}.", AppName, Utils.Utils.Translate("模块"), failInfo)); } } } else { failInfo = Utils.Utils.Translate("许可已经过期,请和管理员联系!"); Console.WriteLine(failInfo); } } } } } } finally { Monitor.Exit(LicenseInfos); } } else { // 超时后的处理代码 bRet = false; } if (bRet) { ClientAppModuleInfo clientAppModuleInfo = new ClientAppModuleInfo(); clientAppModuleInfo.LoginID = LoginID; clientAppModuleInfo.ClientHost = ClientHost; clientAppModuleInfo.AppName = AppName; clientAppModuleInfo.ModuleName = ModuleName; clientAppModuleInfo.ModuleVersion = ModuleVersion; clientAppModuleInfo.nAppRequestCount = 1; clientAppModuleInfo.nKeepAlive = 5; if (Monitor.TryEnter(ClientAppModuleInfos, 1000)) { try { ClientAppModuleInfos.Add(clientAppModuleInfo); } catch (System.Exception ex) { failInfo = ex.Message; } finally { Monitor.Exit(ClientAppModuleInfos); } } else { Monitor.Exit(ClientAppModuleInfos); } return("Success"); } else { return(failInfo); } }
/// <summary> /// Client登出,返还许可证。 /// </summary> /// <returns></returns> public void GoBackLicense(string ClientHost, string AppName, string ModuleName, string ModuleVersion) { if (AppName == "PEOfficeCenter" && ModuleName == "PEOfficeCenter.Manager") { return; } if (ModuleName == AppName) { return; // 表示引导程序APP退出 } if (ModuleName == "") { return; // 表示引导程序APP退出 } // 通过ModuleName反查许可中的AppName if (AppName == null || AppName == "") { AppName = GetAppNameFromLicenseAppModuleHT(ModuleName); } if (AppName == null || AppName == "") { return; } Console.WriteLine(string.Format("[{0}]{1}.", ModuleName, Utils.Utils.Translate("功能释放许可"))); // 检查Client是否已经连接此app[module,目前只检查到app一级],成功直接返回 ClientAppModuleInfo clientAppModuleInfo = CheckClientAppModuleExist(ClientHost, AppName, ModuleName, ModuleVersion); if (clientAppModuleInfo != null) { bool bRet = false; string failInfo = string.Empty; DateTime dateToday = DateTime.Today; if (Monitor.TryEnter(LicenseInfos, 1000)) { try { foreach (LicenseInfo licenseInfo in LicenseInfos) { if (bRet) { break; } if (licenseInfo.IsUsed.ToLower() != "true") { continue; } if (string.IsNullOrEmpty(licenseInfo.ErrorInfo) == false) { continue; } bool bHaveMoreAppRequestCount = false; foreach (ModuleInfo moduleInfo in licenseInfo.ModuleInfos) { if (moduleInfo.AppName == AppName) { DateTime dateExpiry = DateTime.Parse(moduleInfo.ExpiryDate); if (dateExpiry >= dateToday) { // 免费类型许可 if (moduleInfo.LicenseType == "free") { failInfo = string.Empty; bRet = true; } else { // 限制类型许可 if (bHaveMoreAppRequestCount == false && clientAppModuleInfo.nAppRequestCount > 0) { // 客户端多开处理 clientAppModuleInfo.nAppRequestCount--; bHaveMoreAppRequestCount = true; Console.WriteLine(string.Format("[{0}]{1}{2}{3}.", AppName, Utils.Utils.Translate("模块多开还有"), clientAppModuleInfo.nAppRequestCount, Utils.Utils.Translate("次"))); } if (clientAppModuleInfo.nAppRequestCount <= 0) { // App不再被占用时,返回License if (moduleInfo.LicenseUsed > 0) { moduleInfo.LicenseUsed--; // 成功返还许可 failInfo = string.Empty; bRet = true; Console.WriteLine(string.Format("[{0}]{1}.", AppName, Utils.Utils.Translate("模块释放许可成功"))); } } } } else { failInfo = Utils.Utils.Translate("许可已经过期,请和管理员联系!"); } } else { if (bRet == false) { failInfo = string.Format(Utils.Utils.Translate("未找到模块[{0}]的许可信息,请和管理员联系!"), AppName); } } } } } finally { Monitor.Exit(LicenseInfos); } } else { // 超时后的处理代码 bRet = false; } if (bRet) { if (Monitor.TryEnter(ClientAppModuleInfos, 1000)) { try { ClientAppModuleInfos.Remove(clientAppModuleInfo); } catch (System.Exception ex) { } finally { Monitor.Exit(ClientAppModuleInfos); } } else { Monitor.Exit(ClientAppModuleInfos); } } else { } } }