//在订阅中调用 public static void RegisterRemotePlugin(IDataReply callback, string ServerIdentify, string[] plugin) { //RemotePluginDic = new List<RemotePlugin>(); //自己没必要注册自己 if (ServerIdentify == WcfGlobal.Identify) { return; } bool isChanged = false; RemotePlugin rp = null; if (RemotePluginDic.FindIndex(x => x.ServerIdentify == ServerIdentify) > -1) { rp = RemotePluginDic.Find(x => x.ServerIdentify == ServerIdentify); //rp.clientService = callback; List <string> newplugin = rp.plugin.ToList(); foreach (var p in plugin) { //新注册的插件在原来插件中找不到,则新增 if (newplugin.ToList().FindIndex(x => x == p) == -1) { newplugin.Add(p); isChanged = true; } } foreach (var p in newplugin) { //如果注册的插件在新注册插件中找不到,则移除 if (plugin.ToList().FindIndex(x => x == p) == -1) { newplugin.Remove(p); isChanged = true; } } rp.plugin = newplugin.ToArray(); } else { rp = new RemotePlugin(); rp.ServerIdentify = ServerIdentify; rp.callback = callback; rp.plugin = plugin; RemotePluginDic.Add(rp); isChanged = true; } if (isChanged == true) { //重新注册远程插件 //? } }
public static string ProcessRequest(string clientId, string plugin, string controller, string method, string jsondata, HeaderParameter para) { string retJson = null; try { if (plugin == null || controller == null) { throw new Exception("插件名称或控制器名称不能为空!"); } if (ClientManage.ClientDic.ContainsKey(clientId) == false) { throw new Exception("客户端不存在,正在创建新的连接!"); } if (ClientManage.IsToken == true)//非调试模式下才验证 { //验证身份,创建连接的时候验证,请求不验证 IsAuth(plugin, controller, method, para.token); } //显示调试信息 if (WcfGlobal.IsDebug == true) { ShowHostMsg(Color.Black, DateTime.Now, "客户端[" + clientId + "]正在执行:" + controller + "." + method + "(" + jsondata + ")"); } begintime(); #region 执行插件控制器的核心算法 object[] paramValue = null;//jsondata? ServiceResponseData retObj = null; LocalPlugin localPlugin = RemotePluginManage.GetLocalPlugin(); if (string.IsNullOrEmpty(para.replyidentify) || localPlugin.ServerIdentify == para.replyidentify) { if (localPlugin.PluginDic.ContainsKey(plugin) == true) { //先解密再解压 string _jsondata = jsondata; //解密参数 if (para.isencryptionjson) { DESEncryptor des = new DESEncryptor(); des.InputString = _jsondata; des.DesDecrypt(); _jsondata = des.OutString; } //解压参数 if (para.iscompressjson) { _jsondata = ZipComporessor.Decompress(_jsondata); } ClientRequestData requestData = new ClientRequestData(para.iscompressjson, para.isencryptionjson, para.serializetype); requestData.SetJsonData(_jsondata); requestData.LoginRight = para.LoginRight; EFWCoreLib.CoreFrame.Plugin.ModulePlugin moduleplugin = localPlugin.PluginDic[plugin]; retObj = (ServiceResponseData)moduleplugin.WcfServerExecuteMethod(controller, method, paramValue, requestData); if (retObj != null) { retJson = retObj.GetJsonData(); } else { retObj = new ServiceResponseData(); retObj.Iscompressjson = para.iscompressjson; retObj.Isencryptionjson = para.isencryptionjson; retObj.Serializetype = para.serializetype; retJson = retObj.GetJsonData(); } retJson = "{\"flag\":0,\"msg\":" + "\"\"" + ",\"data\":" + retJson + "}"; //先压缩再加密 //压缩结果 if (para.iscompressjson) { retJson = ZipComporessor.Compress(retJson); } //加密结果 if (para.isencryptionjson) { DESEncryptor des = new DESEncryptor(); des.InputString = retJson; des.DesEncrypt(); retJson = des.OutString; } } else { throw new Exception("本地插件找不到指定的插件"); } } else//本地插件找不到,就执行远程插件 { if (RemotePluginManage.GetRemotePlugin().FindIndex(x => x.ServerIdentify == para.replyidentify) > -1) { RemotePlugin rp = RemotePluginManage.GetRemotePlugin().Find(x => x.ServerIdentify == para.replyidentify); string[] ps = rp.plugin; if (ps.ToList().FindIndex(x => x == plugin) > -1) { retJson = rp.callback.ReplyProcessRequest(para, plugin, controller, method, jsondata); } else { throw new Exception("远程插件找不到指定的插件"); } } else { throw new Exception("远程插件找不到指定的回调中间件"); } } #endregion double outtime = endtime(); //记录超时的方法 if (ClientManage.IsOverTime == true) { if (outtime > Convert.ToDouble(ClientManage.OverTime * 1000)) { WriterOverTimeLog(outtime, controller + "." + method + "(" + jsondata + ")"); } } //显示调试信息 if (WcfGlobal.IsDebug == true) { ShowHostMsg(Color.Green, DateTime.Now, "客户端[" + clientId + "]收到结果(耗时[" + outtime + "]):" + retJson); } //更新客户端信息 ClientManage.UpdateRequestClient(clientId, jsondata == null ? 0 : jsondata.Length, retJson == null ? 0 : retJson.Length); if (retJson == null) { throw new Exception("插件执行未返回有效数据"); } return(retJson); } catch (Exception err) { //记录错误日志 if (err.InnerException == null) { retJson = "{\"flag\":1,\"msg\":" + "\"" + err.Message + "\"" + "}"; if (para.iscompressjson) { retJson = ZipComporessor.Compress(retJson); } ShowHostMsg(Color.Red, DateTime.Now, "客户端[" + clientId + "]执行失败:" + err.Message); return(retJson); } else { retJson = "{\"flag\":1,\"msg\":" + "\"" + err.InnerException.Message + "\"" + "}"; if (para.iscompressjson) { retJson = ZipComporessor.Compress(retJson); } ShowHostMsg(Color.Red, DateTime.Now, "客户端[" + clientId + "]执行失败:" + err.InnerException.Message); return(retJson); } } }
public static string ReplyProcessRequest(string plugin, string controller, string method, string jsondata, HeaderParameter para) { string retJson = null; try { //显示调试信息 if (WcfGlobal.IsDebug == true) { ShowHostMsg(Color.Black, DateTime.Now, "客户端[本地回调]正在执行:" + controller + "." + method + "(" + jsondata + ")"); } begintime(); #region 执行插件控制器的核心算法 object[] paramValue = null;//jsondata? ServiceResponseData retObj = null; LocalPlugin localPlugin = RemotePluginManage.GetLocalPlugin(); if (string.IsNullOrEmpty(para.replyidentify) || localPlugin.ServerIdentify == para.replyidentify) { if (localPlugin.PluginDic.ContainsKey(plugin) == true) { //先解密再解压 string _jsondata = jsondata; //解密参数 if (para.isencryptionjson) { DESEncryptor des = new DESEncryptor(); des.InputString = _jsondata; des.DesDecrypt(); _jsondata = des.OutString; } //解压参数 if (para.iscompressjson) { _jsondata = ZipComporessor.Decompress(_jsondata); } ClientRequestData requestData = new ClientRequestData(para.iscompressjson, para.isencryptionjson, para.serializetype); requestData.SetJsonData(_jsondata); requestData.LoginRight = para.LoginRight; EFWCoreLib.CoreFrame.Plugin.ModulePlugin moduleplugin = localPlugin.PluginDic[plugin]; retObj = (ServiceResponseData)moduleplugin.WcfServerExecuteMethod(controller, method, paramValue, requestData); if (retObj != null) { retJson = retObj.GetJsonData(); } else { retObj = new ServiceResponseData(); retObj.Iscompressjson = para.iscompressjson; retObj.Isencryptionjson = para.isencryptionjson; retObj.Serializetype = para.serializetype; retJson = retObj.GetJsonData(); } retJson = "{\"flag\":0,\"msg\":" + "\"\"" + ",\"data\":" + retJson + "}"; //先压缩再加密 //压缩结果 if (para.iscompressjson) { retJson = ZipComporessor.Compress(retJson); } //加密结果 if (para.isencryptionjson) { DESEncryptor des = new DESEncryptor(); des.InputString = retJson; des.DesEncrypt(); retJson = des.OutString; } } else { throw new Exception("本地插件找不到指定的插件"); } } else//本地插件找不到,就执行远程插件 { if (RemotePluginManage.GetRemotePlugin().FindIndex(x => x.ServerIdentify == para.replyidentify) > -1) { RemotePlugin rp = RemotePluginManage.GetRemotePlugin().Find(x => x.ServerIdentify == para.replyidentify); string[] ps = rp.plugin; if (ps.ToList().FindIndex(x => x == plugin) > -1) { retJson = rp.callback.ReplyProcessRequest(para, plugin, controller, method, jsondata); } else { throw new Exception("远程插件找不到指定的插件"); } } else { throw new Exception("远程插件找不到指定的回调中间件"); } } #endregion //System.Threading.Thread.Sleep(20000);//测试并发问题,此处也没有问题 double outtime = endtime(); //记录超时的方法 if (ClientManage.IsOverTime == true) { if (outtime > Convert.ToDouble(ClientManage.OverTime * 1000)) { WriterOverTimeLog(outtime, controller + "." + method + "(" + jsondata + ")"); } } //显示调试信息 if (WcfGlobal.IsDebug == true) { ShowHostMsg(Color.Green, DateTime.Now, "客户端[本地回调]收到结果(耗时[" + outtime + "]):" + retJson); } if (retJson == null) { throw new Exception("请求的插件未获取到有效数据"); } return(retJson); } catch (Exception err) { //记录错误日志 //EFWCoreLib.CoreFrame.EntLib.ZhyContainer.CreateException().HandleException(err, "HISPolicy"); if (err.InnerException == null) { retJson = "{\"flag\":1,\"msg\":" + "\"" + err.Message + "\"" + "}"; if (para.iscompressjson) { retJson = ZipComporessor.Compress(retJson); } ShowHostMsg(Color.Red, DateTime.Now, "客户端[本地回调]执行失败:" + err.Message); return(retJson); } else { retJson = "{\"flag\":1,\"msg\":" + "\"" + err.InnerException.Message + "\"" + "}"; if (para.iscompressjson) { retJson = ZipComporessor.Compress(retJson); } ShowHostMsg(Color.Red, DateTime.Now, "客户端[本地回调]执行失败:" + err.InnerException.Message); return(retJson); } } }
/// <summary> /// 监视中间件节点配置的远程插件 /// </summary> static void MonitorRemotePlugin() { if (WcfGlobal.IsRootMNode) { #region 加载mpList //从Mongodb加载节点服务配置 List <MNodePlugin> mpList = new List <MNodePlugin>(); foreach (var ps in npList) { MNodePlugin mp = new MNodePlugin(); mp.ServerIdentify = ps.identify; mp.PathStrategy = ps.pathstrategy; mp.LocalPlugin = ps.localplugin; mp.RemotePlugin = new List <RemotePlugin>(); foreach (var r in ps.remoteplugin) { RemotePlugin rp = new RemotePlugin(); rp.PluginName = r.pluginname; rp.MNodeIdentify = r.mnodeidentify; mp.RemotePlugin.Add(rp); } mp.LocalPinfoList = new List <MNPluginInfo>(); foreach (string p in mp.LocalPlugin) { PluginService pservice = psList.Find(x => x.pluginname == p); if (pservice != null) { MNPluginInfo pinfo = new MNPluginInfo(); pinfo.pluginname = pservice.pluginname; pinfo.title = pservice.title; pinfo.versions = pservice.versions; pinfo.author = pservice.author; pinfo.introduce = pservice.introduce; pinfo.updatenotes = pservice.updatenotes; pinfo.delflag = pservice.delflag; mp.LocalPinfoList.Add(pinfo); } } mpList.Add(mp); } #endregion //CoreFrame.Common.MiddlewareLogHelper.WriterLog(JsonConvert.SerializeObject(mpList)); #region 步mpList到分布式缓存 Dictionary <string, string> sync_adddata = new Dictionary <string, string>(); //需要同步的数据 Dictionary <string, string> sync_updatedata = new Dictionary <string, string>(); //需要同步的数据 List <string> sync_deldata = new List <string>(); //需要同步的数据 string cacheName = "mnodeplugin"; CacheObject cobj = DistributedCacheManage.GetLocalCache(cacheName); if (cobj != null) { List <CacheData> cdatalist = cobj.cacheValue; //新增的 foreach (var n in mpList) { if (cdatalist.FindIndex(x => x.key == n.ServerIdentify) == -1) { sync_adddata.Add(n.ServerIdentify, JsonConvert.SerializeObject(n)); } } //删除的 foreach (var o in cdatalist) { if (mpList.FindIndex(x => x.ServerIdentify == o.key) == -1) { sync_deldata.Add(o.key); } } //更新的 foreach (var o in cdatalist) { MNodePlugin mp = mpList.Find(x => x.ServerIdentify == o.key); if (mp != null && JsonConvert.SerializeObject(mp) != o.value) { sync_updatedata.Add(o.key, JsonConvert.SerializeObject(mp)); } } } else { //新增的 foreach (var n in mpList) { sync_adddata.Add(n.ServerIdentify, JsonConvert.SerializeObject(n)); } } //调试 //foreach(var i in sync_updatedata) //{ // CoreFrame.Common.MiddlewareLogHelper.WriterLog(i.Key); // CoreFrame.Common.MiddlewareLogHelper.WriterLog(i.Value); //} DistributedCacheManage.SetCache(cacheName, sync_adddata, sync_updatedata, sync_deldata); #endregion } }