9da052d59a74917ff1bee6123f689388e547931d.svn-base 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. using System;
  2. using System.Data;
  3. using System.Collections;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7. using Core.Mes.Common;
  8. using Core.Mes.IBaseInterface;
  9. namespace Core.Mes.ClientFrameWork
  10. {
  11. /// <summary>
  12. /// RemotingHelp 的摘要说明。
  13. /// </summary>
  14. public class RemotingHelp
  15. {
  16. static DateTime LastCallTime = System.DateTime.Now;
  17. static CallingMessage LastCallMsg = new CallingMessage();
  18. public RemotingHelp()
  19. {
  20. }
  21. public DataSet ServerUrlList
  22. {
  23. set
  24. {
  25. DataSet ds = value;
  26. if (ds != null || ds.Tables.Count > 0)
  27. {
  28. _htServerUrlList = new Hashtable();
  29. foreach (DataRow dr in ds.Tables[0].Rows)
  30. {
  31. _htServerUrlList.Add(dr["ServerName"].ToString(), dr["Url"].ToString() + "/" + dr["ServerName"]);
  32. }
  33. }
  34. ds.Dispose();
  35. }
  36. }
  37. private Hashtable _htServerUrlList = new Hashtable();
  38. private Hashtable _htRemoteServer = new Hashtable();
  39. private bool GetServer(string serverName)
  40. {
  41. try
  42. {
  43. if (!_htRemoteServer.Contains(serverName))
  44. _htRemoteServer.Add(serverName, (ICommon)Activator.GetObject(typeof(ICommon), _htServerUrlList[serverName].ToString()));
  45. }
  46. catch (Exception ex)
  47. {
  48. Console.WriteLine(ex.Message);
  49. return false;
  50. }
  51. return true;
  52. }
  53. /*
  54. public object ExecuteMethod(CallingMessage par, out string errorInfo)
  55. {
  56. SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
  57. object obj = ExecuteMethod(par, out outInfo);
  58. errorInfo = outInfo.ErrMessage;
  59. return obj;
  60. }
  61. public object ExecuteMethod(string serverName, string className, string methodName, object[] args, out string errorInfo)
  62. {
  63. //== 解析参数
  64. CallingMessage par = new CallingMessage();
  65. par.ServerName = serverName;
  66. par.ClassName = className;
  67. par.MethodName = methodName;
  68. par.args = args;
  69. par.ServerType = MesServerType.IComponentContainServer;
  70. par.TransType = NetWorkTransType.Remoting;
  71. par.visitType = VisitType.Method;
  72. SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
  73. object obj = ExecuteMethod(par, out outInfo);
  74. errorInfo = outInfo.ErrMessage;
  75. return obj;
  76. }
  77. public object ExecuteMethod(CallingMessage par, out SimpleReturnObject outInfo)
  78. {
  79. outInfo = new SimpleReturnObject();
  80. outInfo.ErrCode = 0;
  81. outInfo.ErrMessage = "";
  82. DateTime dtime = DateTime.Now;
  83. if (DateTime.Compare(dtime, LastCallTime) > 0)
  84. {
  85. LastCallTime = dtime;
  86. LastCallMsg = par;
  87. }
  88. if (!FindRemoteServer(par.ServerName))
  89. {
  90. outInfo.ErrCode = 1;
  91. outInfo.ErrMessage = "未找到请求的服务!";
  92. return null;
  93. }
  94. try
  95. {
  96. ReturnObject rtnObj = (_htRemoteServer[par.ServerName] as ICommon).MethodHandler(par, new ValidateInfo());
  97. outInfo.ErrCode = rtnObj.ErrCode;
  98. outInfo.ErrMessage = rtnObj.ErrMessage;
  99. if (rtnObj.RealDataSet != null && rtnObj.RealDataSet.Tables.Count > 0 &&
  100. rtnObj.RealDataSet.Tables.Contains("RETURN_RESULT"))
  101. {
  102. DataTable dt_rus = rtnObj.RealDataSet.Tables["RETURN_RESULT"];
  103. if (dt_rus.Rows.Count > 0)
  104. {
  105. int IsCompressed = (int)dt_rus.Rows[0]["IsCompressed"];
  106. int UnCompress_size = (int)dt_rus.Rows[0]["UnCompress_size"];
  107. int Compress_size = (int)dt_rus.Rows[0]["Compress_size"];
  108. if (IsCompressed == 1)
  109. {
  110. byte[] _rtn_comp = (byte[])rtnObj.RealObject;
  111. if (_rtn_comp.GetLength(0) != Compress_size)
  112. {
  113. outInfo.ErrCode = 1;
  114. outInfo.ErrMessage = "接受的数据大小和服务端发送的数据大小不一致,请重试! [可能是网络原因造成]";
  115. return null;
  116. }
  117. byte[] _rtn = Utility.Decompress(_rtn_comp);
  118. if (_rtn.GetLength(0) != UnCompress_size)
  119. {
  120. outInfo.ErrCode = 1;
  121. outInfo.ErrMessage = "接受的数据大小和服务端发送的数据大小不一致,请重试! [可能是网络原因造成]";
  122. return null;
  123. }
  124. rtnObj.RealDataSet.Tables.Remove("RETURN_RESULT");
  125. rtnObj.RealDataSet.AcceptChanges();
  126. rtnObj.RealObject = Utility.ReSerializable(_rtn);
  127. }
  128. }
  129. }
  130. return rtnObj.RealObject;
  131. }
  132. catch (Exception ex)
  133. {
  134. outInfo.ErrCode = 1;
  135. outInfo.ErrMessage = ex.Message;
  136. return null;
  137. }
  138. }
  139. public object ExecuteMethod(string serverName, string className, string methodName, object[] args, out SimpleReturnObject outInfo)
  140. {
  141. //== 解析参数
  142. CallingMessage par = new CallingMessage();
  143. par.ServerName = serverName;
  144. par.ClassName = className;
  145. par.MethodName = methodName;
  146. par.args = args;
  147. par.ServerType = MesServerType.IComponentContainServer;
  148. par.TransType = NetWorkTransType.Remoting;
  149. par.visitType = VisitType.Method;
  150. outInfo.ErrCode = 0;
  151. outInfo.ErrMessage = "";
  152. return ExecuteMethod(par, out outInfo);
  153. }
  154. */
  155. public object ExecuteMethod(CallingMessage par, out string errorInfo)
  156. {
  157. SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
  158. CallingMessageEx parEx = new CallingMessageEx();
  159. parEx.FromCallingMessage(par);
  160. parEx.RedirectLimit = 3;
  161. parEx.InnerServerRedirect = false;
  162. object obj = ExecuteMethod(parEx, out outInfo);
  163. errorInfo = outInfo.ErrMessage;
  164. return obj;
  165. }
  166. public object ExecuteMethod(string serverName, string className, string methodName, object[] args, out string errorInfo)
  167. {
  168. //== 解析参数
  169. CallingMessageEx parEx = new CallingMessageEx();
  170. parEx.ServerName = serverName;
  171. parEx.ClassName = className;
  172. parEx.MethodName = methodName;
  173. parEx.args = args;
  174. parEx.RedirectLimit = 3;
  175. parEx.InnerServerRedirect = false;
  176. parEx.ServerType = MesServerType.IComponentContainServer;
  177. parEx.TransType = NetWorkTransType.Remoting;
  178. parEx.visitType = VisitType.Method;
  179. SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
  180. object obj = ExecuteMethod(parEx, out outInfo);
  181. errorInfo = outInfo.ErrMessage;
  182. return obj;
  183. }
  184. public object ExecuteMethod(string serverName, string className, string methodName, object[] args, out SimpleReturnObject outInfo)
  185. {
  186. //== 解析参数
  187. CallingMessageEx parEx = new CallingMessageEx();
  188. parEx.ServerName = serverName;
  189. parEx.ClassName = className;
  190. parEx.MethodName = methodName;
  191. parEx.args = args;
  192. parEx.RedirectLimit = 3;
  193. parEx.InnerServerRedirect = false;
  194. parEx.ServerType = MesServerType.IComponentContainServer;
  195. parEx.TransType = NetWorkTransType.Remoting;
  196. parEx.visitType = VisitType.Method;
  197. outInfo.ErrCode = 0;
  198. outInfo.ErrMessage = "";
  199. return ExecuteMethod(parEx, out outInfo);
  200. }
  201. public object ExecuteMethod(CallingMessage par, out SimpleReturnObject outInfo)
  202. {
  203. CallingMessageEx parEx = new CallingMessageEx();
  204. parEx.FromCallingMessage(par);
  205. parEx.RedirectLimit = 3;
  206. parEx.InnerServerRedirect = false;
  207. return ExecuteMethod(parEx, out outInfo);
  208. }
  209. public object ExecuteMethod(CallingMessageEx parEx, out SimpleReturnObject outInfo)
  210. {
  211. outInfo = new SimpleReturnObject();
  212. outInfo.ErrCode = 0;
  213. outInfo.ErrMessage = "";
  214. DateTime dtime = DateTime.Now;
  215. if (DateTime.Compare(dtime, LastCallTime) > 0)
  216. {
  217. LastCallTime = dtime;
  218. LastCallMsg = parEx.ToCallingMessage();
  219. }
  220. if (!FindRemoteServer(parEx.ServerName))
  221. {
  222. outInfo.ErrCode = 1;
  223. outInfo.ErrMessage = "未找到请求的服务!";
  224. return null;
  225. }
  226. try
  227. {
  228. ICommon executor = (_htRemoteServer[parEx.ServerName] as ICommon);
  229. ReturnObjectEx rtnObjEx = new ReturnObjectEx();
  230. do
  231. {
  232. if (parEx.ServerName.ToUpper() != "SERVERCOMMON")
  233. {
  234. rtnObjEx = executor.MethodHandlerEx(parEx, new ValidateInfo());
  235. }
  236. else
  237. {
  238. ReturnObject rtnObj = executor.MethodHandler(parEx.ToCallingMessage(), new ValidateInfo());
  239. rtnObjEx.ErrCode = rtnObj.ErrCode;
  240. rtnObjEx.ErrMessage = rtnObj.ErrMessage;
  241. rtnObjEx.RealDataSet = rtnObj.RealDataSet;
  242. rtnObjEx.RealObject = rtnObj.RealObject;
  243. }
  244. if (!string.IsNullOrEmpty(rtnObjEx.ReDirectURL) && parEx.RedirectLimit <= 0)
  245. {
  246. outInfo.ErrCode = 1;
  247. outInfo.ErrMessage = "重定位次数过多!";
  248. return null;
  249. }
  250. else if (!string.IsNullOrEmpty(rtnObjEx.ReDirectURL))
  251. {
  252. parEx.RedirectLimit--;
  253. try
  254. {
  255. executor = (ICommon)Activator.GetObject(typeof(ICommon), rtnObjEx.ReDirectURL);
  256. }
  257. catch
  258. {
  259. executor = (_htRemoteServer[parEx.ServerName] as ICommon);
  260. parEx.RedirectLimit = 0;
  261. }
  262. }
  263. else
  264. {
  265. outInfo.ErrCode = rtnObjEx.ErrCode;
  266. outInfo.ErrMessage = rtnObjEx.ErrMessage;
  267. break;
  268. }
  269. } while (parEx.RedirectLimit >= 0);
  270. if (rtnObjEx.RealDataSet != null && rtnObjEx.RealDataSet.Tables.Count > 0 &&
  271. rtnObjEx.RealDataSet.Tables.Contains("RETURN_RESULT"))
  272. {
  273. DataTable dt_rus = rtnObjEx.RealDataSet.Tables["RETURN_RESULT"];
  274. if (dt_rus.Rows.Count > 0)
  275. {
  276. int IsCompressed = (int)dt_rus.Rows[0]["IsCompressed"];
  277. int UnCompress_size = (int)dt_rus.Rows[0]["UnCompress_size"];
  278. int Compress_size = (int)dt_rus.Rows[0]["Compress_size"];
  279. if (IsCompressed == 1)
  280. {
  281. byte[] _rtn_comp = (byte[])rtnObjEx.RealObject;
  282. if (_rtn_comp.GetLength(0) != Compress_size)
  283. {
  284. outInfo.ErrCode = 1;
  285. outInfo.ErrMessage = "接受的数据大小和服务端发送的数据大小不一致,请重试! [可能是网络原因造成]";
  286. return null;
  287. }
  288. byte[] _rtn = Utility.Decompress(_rtn_comp);
  289. if (_rtn.GetLength(0) != UnCompress_size)
  290. {
  291. outInfo.ErrCode = 1;
  292. outInfo.ErrMessage = "接受的数据大小和服务端发送的数据大小不一致,请重试! [可能是网络原因造成]";
  293. return null;
  294. }
  295. rtnObjEx.RealDataSet.Tables.Remove("RETURN_RESULT");
  296. rtnObjEx.RealDataSet.AcceptChanges();
  297. rtnObjEx.RealObject = Utility.ReSerializable(_rtn);
  298. }
  299. }
  300. }
  301. return rtnObjEx.RealObject;
  302. }
  303. catch (Exception ex)
  304. {
  305. outInfo.ErrCode = 1;
  306. outInfo.ErrMessage = ex.Message;
  307. return null;
  308. }
  309. }
  310. public DateTime GetLastExecute(out CallingMessage _lastCallMsg)
  311. {
  312. _lastCallMsg = LastCallMsg;
  313. return LastCallTime;
  314. }
  315. public DateTime GetLastExecute()
  316. {
  317. return LastCallTime;
  318. }
  319. private bool FindRemoteServer(string serverName)
  320. {
  321. if (!_htRemoteServer.Contains(serverName))
  322. return GetServerUrl(serverName);
  323. return true;
  324. }
  325. private bool GetServerUrl(string serverName)
  326. {
  327. if (!_htServerUrlList.Contains(serverName))
  328. return false;
  329. return GetServer(serverName);
  330. }
  331. public void InitServerUrlList(string serverName, string url)
  332. {
  333. if (_htServerUrlList.Count == 0)
  334. _htServerUrlList.Add(serverName, url);
  335. }
  336. public AsyncExecute AsyncExecute
  337. {
  338. get
  339. {
  340. return new AsyncExecute(this);
  341. }
  342. }
  343. }
  344. #region add maliang 2012-4-12
  345. public class AsyncExecute
  346. {
  347. private RemotingHelp _RemotingHelp = null;
  348. public AsyncExecute(RemotingHelp pRemotingHelp)
  349. {
  350. _RemotingHelp = pRemotingHelp;
  351. }
  352. private Form m_ParentBusy;
  353. /// <summary>
  354. /// 需要显示忙的窗体。
  355. /// </summary>
  356. public Form ParentBusy
  357. {
  358. get { return m_ParentBusy; }
  359. set { m_ParentBusy = value; }
  360. }
  361. private Point m_BusyLocation = new Point(0, 0);
  362. /// <summary>
  363. /// 需要显示忙的窗体。
  364. /// </summary>
  365. public Point BusyLocation
  366. {
  367. get { return m_BusyLocation; }
  368. set { m_BusyLocation = value; }
  369. }
  370. /// <summary>
  371. /// 请求完成的委托
  372. /// </summary>
  373. /// <param name="returnObj">返回的对象</param>
  374. /// <param name="returnMsg">返回的消息内容</param>
  375. public delegate void CompleteEventHandler(object returnObj, string returnMsg);
  376. /// <summary>
  377. /// 请求完成的事件,如果返回的对象需要处理,请订阅该事件
  378. /// </summary>
  379. public event CompleteEventHandler OnComplete;
  380. /// <summary>
  381. /// 参数
  382. /// </summary>
  383. private CallingMessage CallingMessage;
  384. /// <summary>
  385. /// 错误信息
  386. /// </summary>
  387. private string errorInfo = string.Empty;
  388. /// <summary>
  389. /// 返回的对象
  390. /// </summary>
  391. private object returnObj;
  392. public void AsyncExecuteMethod(CallingMessage par)
  393. {
  394. CallingMessage = par;
  395. BackgroundWorker bw = new BackgroundWorker();
  396. bw.DoWork += new DoWorkEventHandler(DoWork);
  397. bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
  398. bw.WorkerReportsProgress = true;
  399. bw.RunWorkerAsync();
  400. }
  401. /// <summary>
  402. /// 请求完成
  403. /// </summary>
  404. void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  405. {
  406. if (ParentBusy != null)
  407. {
  408. BusyCtl.Stop();
  409. ParentBusy.Controls.Remove(BusyCtl);
  410. }
  411. string msg = string.Empty;
  412. //如果订阅了该事件并且返回的结果没有问题才调用完成的事件
  413. if (OnComplete == null)
  414. {
  415. throw new Exception("调用接口请求时未订阅OnComplete事件,请在调用AsyncExecuteMethod请求对象是订阅OnComplete事件");
  416. }
  417. OnComplete.Invoke(returnObj, errorInfo);
  418. }
  419. private BusyControl BusyCtl = new BusyControl();
  420. /// <summary>
  421. /// 发送请求
  422. /// </summary>
  423. void DoWork(object sender, DoWorkEventArgs e)
  424. {
  425. if (ParentBusy != null)
  426. {
  427. ParentBusy.Invoke(new dlgAddControl(addCtl), ParentBusy, BusyCtl);
  428. }
  429. errorInfo = "";
  430. returnObj = _RemotingHelp.ExecuteMethod(CallingMessage, out errorInfo);
  431. }
  432. delegate void dlgAddControl(Control Pctl, BusyControl BusyCtl);
  433. private void addCtl(Control Pctl, BusyControl BusyCtl)
  434. {
  435. BusyCtl.Size = new System.Drawing.Size(32, 32);
  436. Pctl.Controls.Add(BusyCtl);
  437. BusyCtl.BringToFront();
  438. BusyCtl.Location = BusyLocation;
  439. BusyCtl.Start();
  440. }
  441. public void AsyncExecuteMethod(string serverName, string className, string methodName, object[] args)
  442. {
  443. //== 解析参数
  444. CallingMessage par = new CallingMessage();
  445. par.ServerName = serverName;
  446. par.ClassName = className;
  447. par.MethodName = methodName;
  448. par.args = args;
  449. par.ServerType = MesServerType.IComponentContainServer;
  450. par.TransType = NetWorkTransType.Remoting;
  451. par.visitType = VisitType.Method;
  452. AsyncExecuteMethod(par);
  453. }
  454. }
  455. #endregion
  456. }