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
{
///
/// RemotingHelp 的摘要说明。
///
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;
///
/// 需要显示忙的窗体。
///
public Form ParentBusy
{
get { return m_ParentBusy; }
set { m_ParentBusy = value; }
}
private Point m_BusyLocation = new Point(0, 0);
///
/// 需要显示忙的窗体。
///
public Point BusyLocation
{
get { return m_BusyLocation; }
set { m_BusyLocation = value; }
}
///
/// 请求完成的委托
///
/// 返回的对象
/// 返回的消息内容
public delegate void CompleteEventHandler(object returnObj, string returnMsg);
///
/// 请求完成的事件,如果返回的对象需要处理,请订阅该事件
///
public event CompleteEventHandler OnComplete;
///
/// 参数
///
private CallingMessage CallingMessage;
///
/// 错误信息
///
private string errorInfo = string.Empty;
///
/// 返回的对象
///
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();
}
///
/// 请求完成
///
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();
///
/// 发送请求
///
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
}