a35627b815063aa07bd4af3932f0fb5b606657bd.svn-base 21 KB

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