Пример #1
0
        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);
                }
            }
        }
Пример #2
0
        //处理请求
        public static string ProcessRequest(string clientId, 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 + ")");
                }

                if (para.NodePath == null)
                {
                    // 首次请求验证
                    FirstVerification(clientId, plugin, controller, method, jsondata, para);

                    if (string.IsNullOrEmpty(para.endidentify))//计算远程节点路径
                    {
                        //验证本地执行还是远程执行服务
                        MNodePlugin localPlugin = RemotePluginManage.GetLocalPlugin();
                        if (localPlugin.LocalPlugin.ToList().FindIndex(x => x == plugin) != -1)//本地插件
                        {
                            //执行本地数据请求
                            retJson = LocalDataRequest(plugin, controller, method, jsondata, para);
                        }
                        else if (localPlugin.RemotePlugin.FindIndex(x => x.PluginName == plugin) != -1)//远程插件
                        {
                            para.NodePath = FirstGetNodePath(plugin, localPlugin);
                            retJson       = PathNextRequest(plugin, controller, method, jsondata, para);
                        }
                        else
                        {
                            throw new Exception("本中间件节点中没有配置此插件:" + plugin);
                        }
                    }
                    else//计算指定节点的路径
                    {
                        MNodeTree mtree = new MNodeTree();
                        mtree.LoadCache();
                        para.NodePath = mtree.CalculateMNodePath(WcfGlobal.Identify, para.endidentify);
                        retJson       = PathNextRequest(plugin, controller, method, jsondata, para);
                    }
                }
                else//由远程执行服务发送请求
                {
                    retJson = PathNextRequest(plugin, controller, method, jsondata, para);
                }

                //显示调试信息
                if (WcfGlobal.IsDebug == true)
                {
                    ShowHostMsg(Color.Green, DateTime.Now, "服务" + controller + "." + method + "执行完成:" + retJson);
                }

                //更新客户端信息
                ClientManage.UpdateRequestClient(clientId, jsondata == null ? 0 : jsondata.Length, retJson == null ? 0 : retJson.Length);


                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, "服务" + controller + "." + method + "执行失败:" + err.Message);
                    return(retJson);
                }
                else
                {
                    retJson = "{\"flag\":1,\"msg\":" + "\"" + err.InnerException.Message + "\"" + "}";
                    if (para.iscompressjson)
                    {
                        retJson = ZipComporessor.Compress(retJson);
                    }
                    ShowHostMsg(Color.Red, DateTime.Now, "服务" + controller + "." + method + "执行失败:" + err.InnerException.Message);
                    return(retJson);
                }
            }
        }