Example #1
0
        private Object syncObj = new Object();                          //定义一个静态对象用于线程部份代码块的锁定,用于lock操作
        public NormalIPCManager(IPCType _ipctype, Func <string, Dictionary <string, string>, string> _funcExecCmd, Action <string> _actionReceiveData)
        {
            IPCType           = _ipctype;
            funcExecCmd       = _funcExecCmd;
            actionReceiveData = _actionReceiveData;
            ipcw = new IPCWriteHelper();
            ipcr = new IPCReceiveHelper();

            Action <string> action = ((string text) =>
            {
                if (CmdObject.AnalysisCmdText(text).typestr == "cmd")
                {
                    ExecuteCmd(text);
                }
                else if (CmdObject.AnalysisCmdText(text).typestr == "data")
                {
                    ReceiveData(text);
                }
                else
                {
                    return;//无效的文本
                }
            });

            ipcr.Init(action, _ipctype, _ipctype);
        }
Example #2
0
        /// <summary>
        /// 收到数据
        /// </summary>
        /// <param name="data"></param>
        void ReceiveData(string data)
        {
            try
            {
                string eprocess = CmdObject.AnalysisCmdText(data).pathstr_end;//目标执行命令的进程名
                switch (eprocess)
                {
                case "efwplusbase":
                case "efwpluswebapi":
                case "efwplusroute":
                    if (actionReceiveData != null)
                    {
                        actionReceiveData(CmdObject.AnalysisCmdText(data).retdata);
                    }
                    if (actionReturnData != null)
                    {
                        actionReturnData(CmdObject.AnalysisCmdText(data));
                    }
                    break;

                default:
                    return;
                }
            }
            catch (Exception e)
            {
                throw new Exception("命令格式错误:" + e.Message);
            }
        }
Example #3
0
        public string CallCmd(string eprocess, string method, string arg)
        {
            lock (syncObj)
            {
                string cmdtext     = "";
                bool   IsCompleted = false;//是否完成
                string retData     = "";
                actionReturnData = ((CmdObject cobj) =>
                {
                    if (cobj.uniqueid == CmdObject.AnalysisCmdText(cmdtext).uniqueid)
                    {
                        retData = cobj.retdata;
                        IsCompleted = true;
                    }
                });
                cmdtext = CmdObject.BuildCmdText("efwplusserver", eprocess, Guid.NewGuid().ToString(), method, arg);


                if (eprocess == "efwplusbase")
                {
                    ipcw_base.WriteData(cmdtext, IPCType.efwplusBase, IPCType.efwplusBase);
                }
                else if (eprocess == "efwpluswebapi")
                {
                    ipcw_api.WriteData(cmdtext, IPCType.efwplusWebAPI, IPCType.efwplusWebAPI);
                }
                else if (eprocess == "efwplusroute")
                {
                    ipcw_route.WriteData(cmdtext, IPCType.efwplusRoute, IPCType.efwplusRoute);
                }
                //超时计时器
                Stopwatch sw = new Stopwatch();
                sw.Start();
                //是否超时
                bool isouttime = false;
                while (!IsCompleted)
                {
                    if (IsCompleted)
                    {
                        break;
                    }
                    //如果还未获取连接判断是否超时5秒,如果超时抛异常
                    if (sw.Elapsed >= new TimeSpan(5 * 1000 * 10000))
                    {
                        isouttime = true;
                        break;
                    }
                    else
                    {
                        Thread.Sleep(100);
                    }
                }
                if (isouttime)
                {
                    throw new Exception("命令执行超时");
                }

                return(retData);
            }
        }
Example #4
0
        /// <summary>
        /// 执行命令
        /// cmd#efwpluswebapi-efwplusbase#setmnodestate@name=111&state=1
        /// data#efwplusbase-efwpluswebapi#msgtext
        /// </summary>
        /// <param name="cmd"></param>
        void ExecuteCmd(string cmd)
        {
            try
            {
                string bprocess = CmdObject.AnalysisCmdText(cmd).pathstr_begin; //开始发送执行命令的进程名
                string eprocess = CmdObject.AnalysisCmdText(cmd).pathstr_end;   //目标执行命令的进程名

                if (eprocess == "efwplusserver")
                {
                    //执行efwplusserver进程的命令
                    if (funcExecCmd != null)
                    {
                        string retval = funcExecCmd(CmdObject.AnalysisCmdText(cmd).methodstr, CmdObject.AnalysisCmdText(cmd).argdic);//执行命令 arg1方法名 arg2参数

                        string datatext = CmdObject.BuildDataText(CmdObject.AnalysisCmdText(cmd).pathstr_end, CmdObject.AnalysisCmdText(cmd).pathstr_begin, CmdObject.AnalysisCmdText(cmd).uniqueid, retval);
                        //返回结果写回调用命令的进程
                        if (bprocess == "efwplusbase")
                        {
                            ipcw_base.WriteData(datatext, IPCType.efwplusBase, IPCType.efwplusBase);
                        }
                        else if (bprocess == "efwpluswebapi")
                        {
                            ipcw_api.WriteData(datatext, IPCType.efwplusWebAPI, IPCType.efwplusWebAPI);
                        }
                        else if (bprocess == "efwplusroute")
                        {
                            ipcw_route.WriteData(datatext, IPCType.efwplusRoute, IPCType.efwplusRoute);
                        }
                    }
                }
                else if (eprocess == "efwplusbase")
                {
                    ipcw_base.WriteData(cmd, IPCType.efwplusBase, IPCType.efwplusBase);
                }
                else if (eprocess == "efwpluswebapi")
                {
                    ipcw_api.WriteData(cmd, IPCType.efwplusWebAPI, IPCType.efwplusWebAPI);
                }
                else if (eprocess == "efwplusroute")
                {
                    ipcw_route.WriteData(cmd, IPCType.efwplusRoute, IPCType.efwplusRoute);
                }
                else
                {
                    return;
                }
            }
            catch (Exception e)
            {
                throw new Exception("命令格式错误:" + e.Message);
            }
        }
Example #5
0
        /// <summary>
        /// 解析命令
        /// </summary>
        /// <param name="cmdtext"></param>
        /// <returns></returns>
        public static CmdObject AnalysisCmdText(string cmdtext)
        {
            cmdtext = cmdtext.ToLower();
            CmdObject co  = new CmdObject();
            int       _c1 = cmdtext.IndexOf('#') + 1;
            int       _c2 = cmdtext.IndexOf('#', _c1) + 1;
            int       _c3 = cmdtext.IndexOf('#', _c2) + 1;

            co.typestr = cmdtext.Substring(0, _c1 - 1);
            string pathstr = cmdtext.Substring(_c1, _c2 - _c1 - 1);

            co.pathstr_begin = pathstr.Split('-')[0];
            co.pathstr_end   = pathstr.Split('-')[1];
            co.uniqueid      = cmdtext.Substring(_c2, _c3 - _c2 - 1);
            if (co.typestr == "cmd")
            {
                string cmdstr = cmdtext.Substring(_c3, cmdtext.Length - (_c3));

                int _a1 = cmdstr.IndexOf('@') + 1;
                co.methodstr = cmdstr.Substring(0, _a1 - 1);
                co.argstr    = cmdstr.Substring(_a1, cmdstr.Length - _a1);
                if (co.argstr.Trim() != "")
                {
                    co.argdic = new Dictionary <string, string>();
                    string[] args = co.argstr.Split('&');
                    foreach (string a in args)
                    {
                        co.argdic.Add(a.Split('=')[0], a.Split('=')[1]);
                    }
                }
            }
            else if (co.typestr == "data")
            {
                co.retdata = cmdtext.Substring(_c3, cmdtext.Length - (_c3));
            }

            return(co);
        }
Example #6
0
 /// <summary>
 /// 收到数据
 /// </summary>
 /// <param name="data"></param>
 void ReceiveData(string data)
 {
     try
     {
         string eprocess = CmdObject.AnalysisCmdText(data).pathstr_end; //目标执行命令的进程名
         if (eprocess == "efwplusserver")                               //显示数据
         {
             if (actionReceiveData != null)
             {
                 actionReceiveData(CmdObject.AnalysisCmdText(data).retdata);
             }
             if (actionReturnData != null)
             {
                 actionReturnData(CmdObject.AnalysisCmdText(data));
             }
         }
         else if (eprocess == "efwplusbase")
         {
             ipcw_base.WriteData(data, IPCType.efwplusBase, IPCType.efwplusBase);
         }
         else if (eprocess == "efwpluswebapi")
         {
             ipcw_api.WriteData(data, IPCType.efwplusWebAPI, IPCType.efwplusWebAPI);
         }
         else if (eprocess == "efwplusroute")
         {
             ipcw_route.WriteData(data, IPCType.efwplusRoute, IPCType.efwplusRoute);
         }
         else
         {
             return;
         }
     }
     catch (Exception e)
     {
         throw new Exception("命令格式错误:" + e.Message);
     }
 }
Example #7
0
        /// <summary>
        /// 显示信息
        /// </summary>
        /// <param name="msg"></param>
        public void ShowMsg(string msg)
        {
            if (actionReceiveData != null)
            {
                actionReceiveData(msg);
            }
            string datatext = "";

            if (IPCType == IPCType.efwplusBase)
            {
                datatext = CmdObject.BuildDataText("efwplusbase", "efwplusserver", Guid.NewGuid().ToString(), msg);
                ipcw.WriteData(datatext, IPCType.efwplusServer, IPCType);
            }
            else if (IPCType == IPCType.efwplusRoute)
            {
                datatext = CmdObject.BuildDataText("efwplusroute", "efwplusserver", Guid.NewGuid().ToString(), msg);
                ipcw.WriteData(datatext, IPCType.efwplusServer, IPCType);
            }
            else if (IPCType == IPCType.efwplusWebAPI)
            {
                datatext = CmdObject.BuildDataText("efwpluswebapi", "efwplusserver", Guid.NewGuid().ToString(), msg);
                ipcw.WriteData(datatext, IPCType.efwplusServer, IPCType);
            }
        }
Example #8
0
        /// <summary>
        /// 执行命令
        /// cmd#efwpluswebapi-efwplusbase#setmnodestate@name=111&state=1
        /// data#efwplusbase-efwpluswebapi#msgtext
        /// </summary>
        /// <param name="cmd"></param>
        void ExecuteCmd(string cmd)
        {
            try
            {
                //string bprocess = CmdObject.AnalysisCmdText(cmd).pathstr_begin;//开始发送执行命令的进程名
                string eprocess = CmdObject.AnalysisCmdText(cmd).pathstr_end;//目标执行命令的进程名
                string retval, datatext;
                switch (eprocess)
                {
                case "efwplusbase":
                    retval   = funcExecCmd(CmdObject.AnalysisCmdText(cmd).methodstr, CmdObject.AnalysisCmdText(cmd).argdic);  //执行命令 arg1方法名 arg2参数
                    datatext = CmdObject.BuildDataText(CmdObject.AnalysisCmdText(cmd).pathstr_end, CmdObject.AnalysisCmdText(cmd).pathstr_begin, CmdObject.AnalysisCmdText(cmd).uniqueid, retval);
                    ipcw.WriteData(datatext, IPCType.efwplusServer, IPCType);
                    break;

                case "efwpluswebapi":
                    retval   = funcExecCmd(CmdObject.AnalysisCmdText(cmd).methodstr, CmdObject.AnalysisCmdText(cmd).argdic);  //执行命令 arg1方法名 arg2参数
                    datatext = CmdObject.BuildDataText(CmdObject.AnalysisCmdText(cmd).pathstr_end, CmdObject.AnalysisCmdText(cmd).pathstr_begin, CmdObject.AnalysisCmdText(cmd).uniqueid, retval);
                    ipcw.WriteData(datatext, IPCType.efwplusServer, IPCType);
                    break;

                case "efwplusroute":
                    retval   = funcExecCmd(CmdObject.AnalysisCmdText(cmd).methodstr, CmdObject.AnalysisCmdText(cmd).argdic);  //执行命令 arg1方法名 arg2参数
                    datatext = CmdObject.BuildDataText(CmdObject.AnalysisCmdText(cmd).pathstr_end, CmdObject.AnalysisCmdText(cmd).pathstr_begin, CmdObject.AnalysisCmdText(cmd).uniqueid, retval);
                    ipcw.WriteData(datatext, IPCType.efwplusServer, IPCType);
                    break;

                default:
                    return;
                }
            }
            catch (Exception e)
            {
                throw new Exception("命令格式错误:" + e.Message);
            }
        }
Example #9
0
        private Object syncObj = new Object();                          //定义一个静态对象用于线程部份代码块的锁定,用于lock操作

        public efwplusServerIPCManager(Func <string, Dictionary <string, string>, string> _funcExecCmd, Action <string> _actionReceiveData)
        {
            funcExecCmd       = _funcExecCmd;
            actionReceiveData = _actionReceiveData;
            Action <string> action = ((string text) =>
            {
                if (CmdObject.AnalysisCmdText(text).typestr == "cmd")
                {
                    ExecuteCmd(text);
                }
                else if (CmdObject.AnalysisCmdText(text).typestr == "data")
                {
                    ReceiveData(text);
                }
                else
                {
                    return;//无效的文本
                }
            });


            ipcw_base  = new IPCWriteHelper();
            ipcw_api   = new IPCWriteHelper();
            ipcw_route = new IPCWriteHelper();
            IPCReceiveHelper ipcr_base = new IPCReceiveHelper();

            ipcr_base.Init(action, IPCType.efwplusServer, IPCType.efwplusBase);

            IPCReceiveHelper ipcr_api = new IPCReceiveHelper();

            ipcr_api.Init(action, IPCType.efwplusServer, IPCType.efwplusWebAPI);

            IPCReceiveHelper ipcr_route = new IPCReceiveHelper();

            ipcr_route.Init(action, IPCType.efwplusServer, IPCType.efwplusRoute);
        }
Example #10
0
        public string CallCmd(string eprocess, string method, string arg)
        {
            if (IPCName.GetProcessName(IPCType) == eprocess)
            {
                string cmdtext = CmdObject.BuildCmdText(IPCName.GetProcessName(IPCType), eprocess, Guid.NewGuid().ToString(), method, arg);
                string retval  = funcExecCmd(CmdObject.AnalysisCmdText(cmdtext).methodstr, CmdObject.AnalysisCmdText(cmdtext).argdic);//执行命令 arg1方法名 arg2参数
                //string datatext = CmdObject.BuildDataText(CmdObject.AnalysisCmdText(cmdtext).pathstr_end, CmdObject.AnalysisCmdText(cmdtext).pathstr_begin, CmdObject.AnalysisCmdText(cmdtext).uniqueid, retval);
                return(retval);
            }

            lock (syncObj)
            {
                string cmdtext     = "";
                bool   IsCompleted = false;//是否完成
                string retData     = "";
                actionReturnData = ((CmdObject cobj) =>
                {
                    if (cobj.uniqueid == CmdObject.AnalysisCmdText(cmdtext).uniqueid)
                    {
                        retData = cobj.retdata;
                        IsCompleted = true;
                    }
                });


                if (IPCType == IPCType.efwplusBase)
                {
                    cmdtext = CmdObject.BuildCmdText("efwplusbase", eprocess, Guid.NewGuid().ToString(), method, arg);
                    ipcw.WriteData(cmdtext, IPCType.efwplusServer, IPCType);
                }
                else if (IPCType == IPCType.efwplusRoute)
                {
                    cmdtext = CmdObject.BuildCmdText("efwplusroute", eprocess, Guid.NewGuid().ToString(), method, arg);
                    ipcw.WriteData(cmdtext, IPCType.efwplusServer, IPCType);
                }
                else if (IPCType == IPCType.efwplusWebAPI)
                {
                    cmdtext = CmdObject.BuildCmdText("efwpluswebapi", eprocess, Guid.NewGuid().ToString(), method, arg);
                    ipcw.WriteData(cmdtext, IPCType.efwplusServer, IPCType);
                }

                //超时计时器
                Stopwatch sw = new Stopwatch();
                sw.Start();
                //是否超时
                bool isouttime = false;
                while (!IsCompleted)
                {
                    if (IsCompleted)
                    {
                        break;
                    }
                    //如果还未获取连接判断是否超时5秒,如果超时抛异常
                    if (sw.Elapsed >= new TimeSpan(5 * 1000 * 10000))
                    {
                        isouttime = true;
                        break;
                    }
                    else
                    {
                        Thread.Sleep(100);
                    }
                }
                if (isouttime)
                {
                    throw new Exception("命令执行超时");
                }
                return(retData);
            }
        }