| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- using System;
- using System.Data;
- using System.Collections;
- using System.Windows.Forms;
- using System.Drawing;
- using System.ComponentModel;
- using Core.Mes.Common;
- using Core.Mes.IBaseInterface;
- namespace Core.Mes.ClientFrameWork
- {
- /// <summary>
- /// RemotingHelp 的摘要说明。
- /// </summary>
- public class RemotingHelp
- {
- static DateTime LastCallTime = System.DateTime.Now;
- static CallingMessage LastCallMsg = new CallingMessage();
- public RemotingHelp()
- {
- }
- public DataSet ServerUrlList
- {
- set
- {
- DataSet ds = value;
- if (ds != null || ds.Tables.Count > 0)
- {
- _htServerUrlList = new Hashtable();
- foreach (DataRow dr in ds.Tables[0].Rows)
- {
- _htServerUrlList.Add(dr["ServerName"].ToString(), dr["Url"].ToString() + "/" + dr["ServerName"]);
- }
- }
- ds.Dispose();
- }
- }
- private Hashtable _htServerUrlList = new Hashtable();
- private Hashtable _htRemoteServer = new Hashtable();
- private bool GetServer(string serverName)
- {
- try
- {
- if (!_htRemoteServer.Contains(serverName))
- _htRemoteServer.Add(serverName, (ICommon)Activator.GetObject(typeof(ICommon), _htServerUrlList[serverName].ToString()));
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- return false;
- }
- return true;
- }
- /*
- public object ExecuteMethod(CallingMessage par, out string errorInfo)
- {
- SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
- object obj = ExecuteMethod(par, out outInfo);
- errorInfo = outInfo.ErrMessage;
- return obj;
- }
- public object ExecuteMethod(string serverName, string className, string methodName, object[] args, out string errorInfo)
- {
- //== 解析参数
- CallingMessage par = new CallingMessage();
- par.ServerName = serverName;
- par.ClassName = className;
- par.MethodName = methodName;
- par.args = args;
- par.ServerType = MesServerType.IComponentContainServer;
- par.TransType = NetWorkTransType.Remoting;
- par.visitType = VisitType.Method;
- SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
- object obj = ExecuteMethod(par, out outInfo);
- errorInfo = outInfo.ErrMessage;
- return obj;
- }
- public object ExecuteMethod(CallingMessage par, out SimpleReturnObject outInfo)
- {
- outInfo = new SimpleReturnObject();
- outInfo.ErrCode = 0;
- outInfo.ErrMessage = "";
- DateTime dtime = DateTime.Now;
- if (DateTime.Compare(dtime, LastCallTime) > 0)
- {
- LastCallTime = dtime;
- LastCallMsg = par;
- }
- if (!FindRemoteServer(par.ServerName))
- {
- outInfo.ErrCode = 1;
- outInfo.ErrMessage = "未找到请求的服务!";
- return null;
- }
- try
- {
- ReturnObject rtnObj = (_htRemoteServer[par.ServerName] as ICommon).MethodHandler(par, new ValidateInfo());
- outInfo.ErrCode = rtnObj.ErrCode;
- outInfo.ErrMessage = rtnObj.ErrMessage;
- if (rtnObj.RealDataSet != null && rtnObj.RealDataSet.Tables.Count > 0 &&
- rtnObj.RealDataSet.Tables.Contains("RETURN_RESULT"))
- {
- DataTable dt_rus = rtnObj.RealDataSet.Tables["RETURN_RESULT"];
- if (dt_rus.Rows.Count > 0)
- {
- int IsCompressed = (int)dt_rus.Rows[0]["IsCompressed"];
- int UnCompress_size = (int)dt_rus.Rows[0]["UnCompress_size"];
- int Compress_size = (int)dt_rus.Rows[0]["Compress_size"];
- if (IsCompressed == 1)
- {
- byte[] _rtn_comp = (byte[])rtnObj.RealObject;
- if (_rtn_comp.GetLength(0) != Compress_size)
- {
- outInfo.ErrCode = 1;
- outInfo.ErrMessage = "接受的数据大小和服务端发送的数据大小不一致,请重试! [可能是网络原因造成]";
- return null;
- }
- byte[] _rtn = Utility.Decompress(_rtn_comp);
- if (_rtn.GetLength(0) != UnCompress_size)
- {
- outInfo.ErrCode = 1;
- outInfo.ErrMessage = "接受的数据大小和服务端发送的数据大小不一致,请重试! [可能是网络原因造成]";
- return null;
- }
- rtnObj.RealDataSet.Tables.Remove("RETURN_RESULT");
- rtnObj.RealDataSet.AcceptChanges();
- rtnObj.RealObject = Utility.ReSerializable(_rtn);
- }
- }
- }
- return rtnObj.RealObject;
- }
- catch (Exception ex)
- {
- outInfo.ErrCode = 1;
- outInfo.ErrMessage = ex.Message;
- return null;
- }
- }
- public object ExecuteMethod(string serverName, string className, string methodName, object[] args, out SimpleReturnObject outInfo)
- {
- //== 解析参数
- CallingMessage par = new CallingMessage();
- par.ServerName = serverName;
- par.ClassName = className;
- par.MethodName = methodName;
- par.args = args;
- par.ServerType = MesServerType.IComponentContainServer;
- par.TransType = NetWorkTransType.Remoting;
- par.visitType = VisitType.Method;
- outInfo.ErrCode = 0;
- outInfo.ErrMessage = "";
- return ExecuteMethod(par, out outInfo);
- }
- */
- public object ExecuteMethod(CallingMessage par, out string errorInfo)
- {
- SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
- CallingMessageEx parEx = new CallingMessageEx();
- parEx.FromCallingMessage(par);
- parEx.RedirectLimit = 3;
- parEx.InnerServerRedirect = false;
- object obj = ExecuteMethod(parEx, out outInfo);
- errorInfo = outInfo.ErrMessage;
- return obj;
- }
- public object ExecuteMethod(string serverName, string className, string methodName, object[] args, out string errorInfo)
- {
- //== 解析参数
- CallingMessageEx parEx = new CallingMessageEx();
- parEx.ServerName = serverName;
- parEx.ClassName = className;
- parEx.MethodName = methodName;
- parEx.args = args;
- parEx.RedirectLimit = 3;
- parEx.InnerServerRedirect = false;
- parEx.ServerType = MesServerType.IComponentContainServer;
- parEx.TransType = NetWorkTransType.Remoting;
- parEx.visitType = VisitType.Method;
- SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
- object obj = ExecuteMethod(parEx, out outInfo);
- errorInfo = outInfo.ErrMessage;
- return obj;
- }
- public object ExecuteMethod(string serverName, string className, string methodName, object[] args, out SimpleReturnObject outInfo)
- {
- //== 解析参数
- CallingMessageEx parEx = new CallingMessageEx();
- parEx.ServerName = serverName;
- parEx.ClassName = className;
- parEx.MethodName = methodName;
- parEx.args = args;
- parEx.RedirectLimit = 3;
- parEx.InnerServerRedirect = false;
- parEx.ServerType = MesServerType.IComponentContainServer;
- parEx.TransType = NetWorkTransType.Remoting;
- parEx.visitType = VisitType.Method;
- outInfo.ErrCode = 0;
- outInfo.ErrMessage = "";
- return ExecuteMethod(parEx, out outInfo);
- }
- public object ExecuteMethod(CallingMessage par, out SimpleReturnObject outInfo)
- {
- CallingMessageEx parEx = new CallingMessageEx();
- parEx.FromCallingMessage(par);
- parEx.RedirectLimit = 3;
- parEx.InnerServerRedirect = false;
- return ExecuteMethod(parEx, out outInfo);
- }
- public object ExecuteMethod(CallingMessageEx parEx, out SimpleReturnObject outInfo)
- {
- outInfo = new SimpleReturnObject();
- outInfo.ErrCode = 0;
- outInfo.ErrMessage = "";
- DateTime dtime = DateTime.Now;
- if (DateTime.Compare(dtime, LastCallTime) > 0)
- {
- LastCallTime = dtime;
- LastCallMsg = parEx.ToCallingMessage();
- }
- if (!FindRemoteServer(parEx.ServerName))
- {
- outInfo.ErrCode = 1;
- outInfo.ErrMessage = "未找到请求的服务!";
- return null;
- }
- try
- {
- ICommon executor = (_htRemoteServer[parEx.ServerName] as ICommon);
- ReturnObjectEx rtnObjEx = new ReturnObjectEx();
- do
- {
- if (parEx.ServerName.ToUpper() != "SERVERCOMMON")
- {
- rtnObjEx = executor.MethodHandlerEx(parEx, new ValidateInfo());
- }
- else
- {
- ReturnObject rtnObj = executor.MethodHandler(parEx.ToCallingMessage(), new ValidateInfo());
- rtnObjEx.ErrCode = rtnObj.ErrCode;
- rtnObjEx.ErrMessage = rtnObj.ErrMessage;
- rtnObjEx.RealDataSet = rtnObj.RealDataSet;
- rtnObjEx.RealObject = rtnObj.RealObject;
- }
- if (!string.IsNullOrEmpty(rtnObjEx.ReDirectURL) && parEx.RedirectLimit <= 0)
- {
- outInfo.ErrCode = 1;
- outInfo.ErrMessage = "重定位次数过多!";
- return null;
- }
- else if (!string.IsNullOrEmpty(rtnObjEx.ReDirectURL))
- {
- parEx.RedirectLimit--;
- try
- {
- executor = (ICommon)Activator.GetObject(typeof(ICommon), rtnObjEx.ReDirectURL);
- }
- catch
- {
- executor = (_htRemoteServer[parEx.ServerName] as ICommon);
- parEx.RedirectLimit = 0;
- }
- }
- else
- {
- outInfo.ErrCode = rtnObjEx.ErrCode;
- outInfo.ErrMessage = rtnObjEx.ErrMessage;
- break;
- }
- } while (parEx.RedirectLimit >= 0);
- if (rtnObjEx.RealDataSet != null && rtnObjEx.RealDataSet.Tables.Count > 0 &&
- rtnObjEx.RealDataSet.Tables.Contains("RETURN_RESULT"))
- {
- DataTable dt_rus = rtnObjEx.RealDataSet.Tables["RETURN_RESULT"];
- if (dt_rus.Rows.Count > 0)
- {
- int IsCompressed = (int)dt_rus.Rows[0]["IsCompressed"];
- int UnCompress_size = (int)dt_rus.Rows[0]["UnCompress_size"];
- int Compress_size = (int)dt_rus.Rows[0]["Compress_size"];
- if (IsCompressed == 1)
- {
- byte[] _rtn_comp = (byte[])rtnObjEx.RealObject;
- if (_rtn_comp.GetLength(0) != Compress_size)
- {
- outInfo.ErrCode = 1;
- outInfo.ErrMessage = "接受的数据大小和服务端发送的数据大小不一致,请重试! [可能是网络原因造成]";
- return null;
- }
- byte[] _rtn = Utility.Decompress(_rtn_comp);
- if (_rtn.GetLength(0) != UnCompress_size)
- {
- outInfo.ErrCode = 1;
- outInfo.ErrMessage = "接受的数据大小和服务端发送的数据大小不一致,请重试! [可能是网络原因造成]";
- return null;
- }
- rtnObjEx.RealDataSet.Tables.Remove("RETURN_RESULT");
- rtnObjEx.RealDataSet.AcceptChanges();
- rtnObjEx.RealObject = Utility.ReSerializable(_rtn);
- }
- }
- }
- return rtnObjEx.RealObject;
- }
- catch (Exception ex)
- {
- outInfo.ErrCode = 1;
- outInfo.ErrMessage = ex.Message;
- return null;
- }
- }
- public DateTime GetLastExecute(out CallingMessage _lastCallMsg)
- {
- _lastCallMsg = LastCallMsg;
- return LastCallTime;
- }
- public DateTime GetLastExecute()
- {
- return LastCallTime;
- }
- private bool FindRemoteServer(string serverName)
- {
- if (!_htRemoteServer.Contains(serverName))
- return GetServerUrl(serverName);
- return true;
- }
- private bool GetServerUrl(string serverName)
- {
- if (!_htServerUrlList.Contains(serverName))
- return false;
- return GetServer(serverName);
- }
- public void InitServerUrlList(string serverName, string url)
- {
- if (_htServerUrlList.Count == 0)
- _htServerUrlList.Add(serverName, url);
- }
- public AsyncExecute AsyncExecute
- {
- get
- {
- return new AsyncExecute(this);
- }
- }
- }
- #region add maliang 2012-4-12
- public class AsyncExecute
- {
- private RemotingHelp _RemotingHelp = null;
- public AsyncExecute(RemotingHelp pRemotingHelp)
- {
- _RemotingHelp = pRemotingHelp;
- }
- private Form m_ParentBusy;
- /// <summary>
- /// 需要显示忙的窗体。
- /// </summary>
- public Form ParentBusy
- {
- get { return m_ParentBusy; }
- set { m_ParentBusy = value; }
- }
- private Point m_BusyLocation = new Point(0, 0);
- /// <summary>
- /// 需要显示忙的窗体。
- /// </summary>
- public Point BusyLocation
- {
- get { return m_BusyLocation; }
- set { m_BusyLocation = value; }
- }
- /// <summary>
- /// 请求完成的委托
- /// </summary>
- /// <param name="returnObj">返回的对象</param>
- /// <param name="returnMsg">返回的消息内容</param>
- public delegate void CompleteEventHandler(object returnObj, string returnMsg);
- /// <summary>
- /// 请求完成的事件,如果返回的对象需要处理,请订阅该事件
- /// </summary>
- public event CompleteEventHandler OnComplete;
- /// <summary>
- /// 参数
- /// </summary>
- private CallingMessage CallingMessage;
- /// <summary>
- /// 错误信息
- /// </summary>
- private string errorInfo = string.Empty;
- /// <summary>
- /// 返回的对象
- /// </summary>
- private object returnObj;
- public void AsyncExecuteMethod(CallingMessage par)
- {
- CallingMessage = par;
- BackgroundWorker bw = new BackgroundWorker();
- bw.DoWork += new DoWorkEventHandler(DoWork);
- bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
- bw.WorkerReportsProgress = true;
- bw.RunWorkerAsync();
- }
- /// <summary>
- /// 请求完成
- /// </summary>
- void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- if (ParentBusy != null)
- {
- BusyCtl.Stop();
- ParentBusy.Controls.Remove(BusyCtl);
- }
- string msg = string.Empty;
- //如果订阅了该事件并且返回的结果没有问题才调用完成的事件
- if (OnComplete == null)
- {
- throw new Exception("调用接口请求时未订阅OnComplete事件,请在调用AsyncExecuteMethod请求对象是订阅OnComplete事件");
- }
- OnComplete.Invoke(returnObj, errorInfo);
- }
- private BusyControl BusyCtl = new BusyControl();
- /// <summary>
- /// 发送请求
- /// </summary>
- void DoWork(object sender, DoWorkEventArgs e)
- {
- if (ParentBusy != null)
- {
- ParentBusy.Invoke(new dlgAddControl(addCtl), ParentBusy, BusyCtl);
- }
- errorInfo = "";
- returnObj = _RemotingHelp.ExecuteMethod(CallingMessage, out errorInfo);
- }
- delegate void dlgAddControl(Control Pctl, BusyControl BusyCtl);
- private void addCtl(Control Pctl, BusyControl BusyCtl)
- {
- BusyCtl.Size = new System.Drawing.Size(32, 32);
- Pctl.Controls.Add(BusyCtl);
- BusyCtl.BringToFront();
- BusyCtl.Location = BusyLocation;
- BusyCtl.Start();
- }
- public void AsyncExecuteMethod(string serverName, string className, string methodName, object[] args)
- {
- //== 解析参数
- CallingMessage par = new CallingMessage();
- par.ServerName = serverName;
- par.ClassName = className;
- par.MethodName = methodName;
- par.args = args;
- par.ServerType = MesServerType.IComponentContainServer;
- par.TransType = NetWorkTransType.Remoting;
- par.visitType = VisitType.Method;
- AsyncExecuteMethod(par);
- }
- }
- #endregion
- }
|