9072507adeb74bef8b3b5c9a9bfd92c056d9a55a.svn-base 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using System;
  2. using System.Data;
  3. using System.Collections;
  4. using System.Windows.Forms;
  5. using System.ComponentModel;
  6. using Core.Mes.Common;
  7. using Core.Mes.IBaseInterface;
  8. using System.Threading;
  9. using System.Diagnostics;
  10. namespace Core.Mes.ServerFrameWork
  11. {
  12. /// <summary>
  13. /// RemotingHelp 的摘要说明。
  14. /// </summary>
  15. public class RemotingServer
  16. {
  17. private double _priorityFactor = 1;
  18. public double PriorityFactor
  19. {
  20. get { return _priorityFactor; }
  21. }
  22. private string _externServerName = "";
  23. private ExtSrvDaemon.ShowExtSrvEvent ServiceStatusChanged = null;
  24. public string ExternServerName
  25. {
  26. get { return _externServerName; }
  27. }
  28. public RemotingServer(string _ServerName, Hashtable _MainServiceKeys)
  29. {
  30. _externServerName = _ServerName;
  31. MainServiceKeys = _MainServiceKeys;
  32. }
  33. public bool Enable = true;
  34. public RemoteServerStatus Valid = RemoteServerStatus.Normal;
  35. private Hashtable _htServiceUrlList = new Hashtable();
  36. private Hashtable _htServices = new Hashtable();
  37. public Hashtable HTServices
  38. {
  39. get { return _htServices; }
  40. }
  41. private Hashtable MainServiceKeys;
  42. public void CreateServiceInfo(string ServerName, string URL, string MD5_KEY, string SHA256_KEY)
  43. {
  44. if (!_htServiceUrlList.ContainsKey(ServerName))
  45. {
  46. _htServiceUrlList.Add(ServerName, URL);
  47. }
  48. ServiceObject so = null;
  49. if (!_htServices.ContainsKey(ServerName))
  50. {
  51. so = new ServiceObject();
  52. _htServices.Add(ServerName, so);
  53. }
  54. else
  55. {
  56. so = (ServiceObject)_htServices[ServerName];
  57. }
  58. so.ServerName = ServerName;
  59. so.MD5_KEY = MD5_KEY;
  60. so.SHA256_KEY = SHA256_KEY;
  61. if (so.instance == null)
  62. {
  63. so.instance = (ICommon)Activator.GetObject(typeof(ICommon), URL);
  64. }
  65. else if (so.URL != URL)
  66. {
  67. so.instance = (ICommon)Activator.GetObject(typeof(ICommon), URL);
  68. }
  69. so.URL = URL;
  70. }
  71. public void CompareMainService(string ServerName, string Main_MD5_KEY, string Main_SHA256_KEY)
  72. {
  73. ServiceObject so = null;
  74. if (_htServices.ContainsKey(ServerName))
  75. {
  76. so = (ServiceObject)_htServices[ServerName];
  77. if (Main_MD5_KEY == so.MD5_KEY && Main_SHA256_KEY == so.SHA256_KEY)
  78. {
  79. so.Valid = ServiceObjectStatus.Normal;
  80. }
  81. else
  82. {
  83. so.Valid = ServiceObjectStatus.UnMatch;
  84. }
  85. }
  86. }
  87. public void DeleteServiceInfo(string ServerName)
  88. {
  89. if (_htServiceUrlList.ContainsKey(ServerName)) _htServiceUrlList.Remove(ServerName);
  90. if (_htServices.ContainsKey(ServerName)) _htServices.Remove(ServerName);
  91. }
  92. public void SetChangeEvent(Core.Mes.ServerFrameWork.ExtSrvDaemon.ShowExtSrvEvent ssc_event)
  93. {
  94. ServiceStatusChanged += new ExtSrvDaemon.ShowExtSrvEvent(ssc_event);
  95. }
  96. public void SetEnableFlag(string ServerName, bool enable)
  97. {
  98. if (_htServices.ContainsKey(ServerName))
  99. {
  100. ServiceObject so = ((ServiceObject)_htServices[ServerName]);
  101. so.Enable = enable;
  102. }
  103. }
  104. public void CheckHeartBeat()
  105. {
  106. Thread t = new Thread(ListenHeartBeat);
  107. t.Start();
  108. }
  109. static bool Last_Status = false;
  110. static DateTime Last_UpdateTime = DateTime.Now;
  111. private void ListenHeartBeat()
  112. {
  113. do
  114. {
  115. try
  116. {
  117. SimpleReturnObject outInfo = new SimpleReturnObject();
  118. string MainService = ((ArrayList)(MainServiceKeys["ServerCommon"]))[2].ToString() + @"/ServerCommon";
  119. ReturnObjectEx rtn = (ReturnObjectEx)ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "ControlIt", new object[] { MainService }, out outInfo);
  120. if (outInfo.ErrCode == 1314 && rtn.RealObject != null)
  121. {
  122. if (rtn.RealObject.ToString() == MainService)
  123. {
  124. if (!Last_Status || Last_UpdateTime.AddSeconds(30).CompareTo(DateTime.Now) <= 0)
  125. {
  126. LoadExternServices();
  127. Last_UpdateTime = DateTime.Now;
  128. if (ServiceStatusChanged != null)
  129. {
  130. ServiceStatusChanged();
  131. }
  132. }
  133. this.Valid = RemoteServerStatus.Normal;
  134. Last_Status = true;
  135. }
  136. else
  137. {
  138. this.Valid = RemoteServerStatus.ControlByOthers;
  139. }
  140. }
  141. else
  142. {
  143. if (Last_Status)
  144. {
  145. if (ServiceStatusChanged != null)
  146. {
  147. ServiceStatusChanged();
  148. }
  149. }
  150. this.Valid = RemoteServerStatus.Unable;
  151. Last_Status = false;
  152. }
  153. }
  154. catch
  155. {
  156. if (Last_Status)
  157. {
  158. if (ServiceStatusChanged != null)
  159. {
  160. ServiceStatusChanged();
  161. }
  162. }
  163. this.Valid = RemoteServerStatus.Unable;
  164. Last_Status = false;
  165. }
  166. Thread.Sleep(5000);
  167. } while (true);
  168. }
  169. public void LoadExternServices()
  170. {
  171. try
  172. {
  173. SimpleReturnObject outInfo = new SimpleReturnObject();
  174. ReturnObjectEx rtn = (ReturnObjectEx)ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "GetServerList", null, out outInfo);
  175. if (outInfo.ErrCode != 0)
  176. {
  177. throw new Exception("获取服务器服务列表失败:" + outInfo.ErrMessage);
  178. }
  179. DataSet ds = (DataSet)(UnBoxing(rtn, out outInfo));
  180. if (outInfo.ErrCode != 0)
  181. {
  182. throw new Exception("获取服务器服务列表失败:" + outInfo.ErrMessage);
  183. }
  184. if (ds != null && ds.Tables.Contains("ServiceList"))
  185. {
  186. foreach (DataRow dr in ds.Tables["ServiceList"].Rows)
  187. {
  188. string ServerName = dr["servername"].ToString();
  189. CreateServiceInfo(ServerName, dr["URL"].ToString() + "/" + ServerName, dr["MD5_KEY"].ToString(), dr["SHA256_KEY"].ToString());
  190. if (MainServiceKeys.ContainsKey(ServerName))
  191. {
  192. ArrayList keys = (ArrayList)(MainServiceKeys[ServerName]);
  193. CompareMainService(ServerName, keys[0].ToString(), keys[1].ToString());
  194. }
  195. else
  196. {
  197. CompareMainService(ServerName, ".", ".");
  198. }
  199. }
  200. ArrayList UnexistsSrv = new ArrayList();
  201. foreach (string ServerName in _htServiceUrlList.Keys)
  202. {
  203. DataRow[] drs = ds.Tables[0].Select(string.Format("servername = '{0}'", ServerName));
  204. if (drs == null || drs.GetLength(0) == 0)
  205. {
  206. UnexistsSrv.Add(ServerName);
  207. }
  208. }
  209. if (UnexistsSrv.Count > 0)
  210. {
  211. foreach (string ServerName in UnexistsSrv)
  212. {
  213. DeleteServiceInfo(ServerName);
  214. }
  215. }
  216. }
  217. if (ds != null && ds.Tables.Contains("CONFIG"))
  218. {
  219. DataRow[] drs = ds.Tables["CONFIG"].Select("NAME='PriorityFactor'");
  220. if (drs != null && drs.GetLength(0) > 0)
  221. {
  222. this._priorityFactor = (double)drs[0]["VALUE"];
  223. }
  224. }
  225. }
  226. catch (Exception ex)
  227. {
  228. Debug.Print(ex.Message);
  229. }
  230. }
  231. public ReturnObjectEx ExecuteMethod(CallingMessage par, out string errorInfo)
  232. {
  233. SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
  234. CallingMessageEx parEx = new CallingMessageEx();
  235. parEx.FromCallingMessage(par);
  236. ReturnObjectEx obj = ExecuteMethod(parEx, out outInfo);
  237. errorInfo = outInfo.ErrMessage;
  238. return obj;
  239. }
  240. public ReturnObjectEx ExecuteMethod(string serverName, string className, string methodName, object[] args, out string errorInfo)
  241. {
  242. //== 解析参数
  243. CallingMessageEx parEx = new CallingMessageEx();
  244. parEx.ServerName = serverName;
  245. parEx.ClassName = className;
  246. parEx.MethodName = methodName;
  247. parEx.args = args;
  248. parEx.RedirectLimit = 0;
  249. parEx.ServerType = MesServerType.IComponentContainServer;
  250. parEx.TransType = NetWorkTransType.Remoting;
  251. parEx.visitType = VisitType.Method;
  252. SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
  253. ReturnObjectEx obj = ExecuteMethod(parEx, out outInfo);
  254. errorInfo = outInfo.ErrMessage;
  255. return obj;
  256. }
  257. public ReturnObjectEx ExecuteMethod(string serverName, string className, string methodName, object[] args, out SimpleReturnObject outInfo)
  258. {
  259. //== 解析参数
  260. CallingMessageEx parEx = new CallingMessageEx();
  261. parEx.ServerName = serverName;
  262. parEx.ClassName = className;
  263. parEx.MethodName = methodName;
  264. parEx.args = args;
  265. parEx.RedirectLimit = 0;
  266. parEx.ServerType = MesServerType.IComponentContainServer;
  267. parEx.TransType = NetWorkTransType.Remoting;
  268. parEx.visitType = VisitType.Method;
  269. outInfo.ErrCode = 0;
  270. outInfo.ErrMessage = "";
  271. return ExecuteMethod(parEx, out outInfo);
  272. }
  273. public ReturnObjectEx ExecuteMethod(CallingMessage par, out SimpleReturnObject outInfo)
  274. {
  275. outInfo = new SimpleReturnObject();
  276. outInfo.ErrCode = 0;
  277. outInfo.ErrMessage = "";
  278. CallingMessageEx parEx = new CallingMessageEx();
  279. parEx.FromCallingMessage(par);
  280. return ExecuteMethod(parEx, out outInfo);
  281. }
  282. public ReturnObjectEx ExecuteMethod(CallingMessageEx parEx, out SimpleReturnObject outInfo)
  283. {
  284. outInfo = new SimpleReturnObject();
  285. outInfo.ErrCode = 0;
  286. outInfo.ErrMessage = "";
  287. if (!FindRemoteServer(parEx.ServerName))
  288. {
  289. outInfo.ErrCode = 10012;
  290. outInfo.ErrMessage = "未找到请求的服务![3]";
  291. return new ReturnObjectEx(null, outInfo.ErrCode, outInfo.ErrMessage);
  292. }
  293. try
  294. {
  295. ICommon executor = ((ServiceObject)(_htServices[parEx.ServerName])).instance;
  296. do
  297. {
  298. ReturnObjectEx rtnObj = executor.MethodHandlerEx(parEx, new ValidateInfo());
  299. if (!string.IsNullOrEmpty(rtnObj.ReDirectURL) && parEx.RedirectLimit == 0)
  300. {
  301. outInfo.ErrCode = 1;
  302. outInfo.ErrMessage = "重定位次数过多!";
  303. return new ReturnObjectEx(null, outInfo.ErrCode, outInfo.ErrMessage);
  304. }
  305. else if (!string.IsNullOrEmpty(rtnObj.ReDirectURL))
  306. {
  307. parEx.RedirectLimit--;
  308. try
  309. {
  310. executor = (ICommon)Activator.GetObject(typeof(ICommon), rtnObj.ReDirectURL);
  311. }
  312. catch
  313. {
  314. executor = ((ServiceObject)(_htServices[parEx.ServerName])).instance;
  315. parEx.RedirectLimit = 0;
  316. }
  317. }
  318. else
  319. {
  320. outInfo.ErrCode = rtnObj.ErrCode;
  321. outInfo.ErrMessage = rtnObj.ErrMessage;
  322. return rtnObj;
  323. }
  324. } while (parEx.RedirectLimit >= 0);
  325. return new ReturnObjectEx(null, 1, "未能运行服务!");
  326. }
  327. catch (Exception ex)
  328. {
  329. outInfo.ErrCode = 1;
  330. outInfo.ErrMessage = ex.Message;
  331. return new ReturnObjectEx(null, outInfo.ErrCode, outInfo.ErrMessage);
  332. }
  333. }
  334. public bool FindRemoteServer(string serverName)
  335. {
  336. if (!_htServices.Contains(serverName))
  337. return GetServerUrl(serverName);
  338. return true;
  339. }
  340. private bool GetServerUrl(string serverName)
  341. {
  342. if (!_htServiceUrlList.Contains(serverName))
  343. return false;
  344. return GetServer(serverName);
  345. }
  346. private bool GetServer(string serverName)
  347. {
  348. try
  349. {
  350. if (!_htServices.Contains(serverName))
  351. {
  352. CreateServiceInfo(serverName, _htServiceUrlList[serverName].ToString(), "", "");
  353. }
  354. }
  355. catch (Exception ex)
  356. {
  357. Console.WriteLine(ex.Message);
  358. return false;
  359. }
  360. return true;
  361. }
  362. public void InitServiceUrlList(string serverName, string url)
  363. {
  364. if (_htServiceUrlList.Count == 0)
  365. _htServiceUrlList.Add(serverName, url);
  366. }
  367. public object UnBoxing(ReturnObjectEx rtnObj, out SimpleReturnObject outInfo)
  368. {
  369. ReturnObject rtn = new ReturnObject();
  370. rtn.ErrCode = rtnObj.ErrCode;
  371. rtn.ErrMessage = rtnObj.ErrMessage;
  372. rtn.RealDataSet = rtnObj.RealDataSet;
  373. rtn.RealObject = rtnObj.RealObject;
  374. return UnBoxing(rtn, out outInfo);
  375. }
  376. public object UnBoxing(ReturnObject rtnObj, out SimpleReturnObject outInfo)
  377. {
  378. outInfo.ErrCode = 0;
  379. outInfo.ErrMessage = "";
  380. try
  381. {
  382. if (rtnObj.RealDataSet != null && rtnObj.RealDataSet.Tables.Count > 0 &&
  383. rtnObj.RealDataSet.Tables.Contains("RETURN_RESULT"))
  384. {
  385. DataTable dt_rus = rtnObj.RealDataSet.Tables["RETURN_RESULT"];
  386. if (dt_rus.Rows.Count > 0)
  387. {
  388. int IsCompressed = (int)dt_rus.Rows[0]["IsCompressed"];
  389. int UnCompress_size = (int)dt_rus.Rows[0]["UnCompress_size"];
  390. int Compress_size = (int)dt_rus.Rows[0]["Compress_size"];
  391. if (IsCompressed == 1)
  392. {
  393. byte[] _rtn_comp = (byte[])rtnObj.RealObject;
  394. if (_rtn_comp.GetLength(0) != Compress_size)
  395. {
  396. outInfo.ErrCode = 1;
  397. outInfo.ErrMessage = "接受的数据大小和服务端发送的数据大小不一致,请重试! [可能是网络原因造成]";
  398. return null;
  399. }
  400. byte[] _rtn = Utility.Decompress(_rtn_comp);
  401. if (_rtn.GetLength(0) != UnCompress_size)
  402. {
  403. outInfo.ErrCode = 1;
  404. outInfo.ErrMessage = "接受的数据大小和服务端发送的数据大小不一致,请重试! [可能是网络原因造成]";
  405. return null;
  406. }
  407. rtnObj.RealDataSet.Tables.Remove("RETURN_RESULT");
  408. rtnObj.RealDataSet.AcceptChanges();
  409. rtnObj.RealObject = Utility.ReSerializable(_rtn);
  410. }
  411. }
  412. }
  413. return rtnObj.RealObject;
  414. }
  415. catch (Exception ex)
  416. {
  417. outInfo.ErrCode = 1;
  418. outInfo.ErrMessage = ex.Message;
  419. return null;
  420. }
  421. }
  422. }
  423. public enum RemoteServerStatus { Normal = 1, Unable = 2, PauseServer = 3, ControlByOthers = 4, };
  424. public enum ServiceObjectStatus { Normal = 11, Unable = 12, PauseService = 13, UnMatch = 14, ServiceLost = 15 };
  425. public class ServiceObject
  426. {
  427. public string ServerName;
  428. public string URL;
  429. public string MD5_KEY;
  430. public string SHA256_KEY;
  431. public bool Enable = true;
  432. public ServiceObjectStatus Valid = ServiceObjectStatus.Normal;
  433. public ICommon instance = null;
  434. }
  435. }