MsgPool.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using log4net;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace OpenSEMI.Core.Msg
  6. {
  7. public class MsgPool
  8. {
  9. internal class Msg
  10. {
  11. public string id
  12. {
  13. get;
  14. private set;
  15. }
  16. public string Description
  17. {
  18. get;
  19. set;
  20. }
  21. public Action<MsgPool, object> Handler
  22. {
  23. get;
  24. set;
  25. }
  26. public Action<MsgPool, object, object> WithParameterHandler
  27. {
  28. get;
  29. set;
  30. }
  31. public object Sender
  32. {
  33. get;
  34. private set;
  35. }
  36. public object Param
  37. {
  38. get;
  39. private set;
  40. }
  41. public bool HasParam
  42. {
  43. get;
  44. private set;
  45. }
  46. public Msg()
  47. {
  48. id = Guid.NewGuid().ToString();
  49. }
  50. public Msg(Action<MsgPool, object> handler = null, object sender = null, string desc = null)
  51. : this()
  52. {
  53. id = Guid.NewGuid().ToString();
  54. Sender = sender;
  55. Handler = handler;
  56. Description = desc;
  57. HasParam = false;
  58. }
  59. public Msg(Action<MsgPool, object, object> handler = null, object param = null, object sender = null, string desc = null)
  60. : this()
  61. {
  62. id = Guid.NewGuid().ToString();
  63. Sender = sender;
  64. WithParameterHandler = handler;
  65. Description = desc;
  66. Param = param;
  67. HasParam = true;
  68. }
  69. }
  70. private ManualResetEvent waitevent = new ManualResetEvent(false);
  71. private Thread context;
  72. private Action<MsgPool> Handler = null;
  73. private Queue<Msg> PostMsgs = new Queue<Msg>();
  74. private Stack<Msg> SendMsgs = new Stack<Msg>();
  75. private readonly object lockMsg = new object();
  76. private readonly object lockSubscribers = new object();
  77. private MsgPool lazylooper;
  78. private bool LazySupported;
  79. private static object _lock = new object();
  80. private static List<MsgPool> Loopers = new List<MsgPool>();
  81. private ILog log;
  82. private uint lastInvervalTime;
  83. public string LooperName
  84. {
  85. get;
  86. set;
  87. }
  88. public bool IsRuning
  89. {
  90. get;
  91. private set;
  92. }
  93. public Dictionary<string, string> ExecutionResults
  94. {
  95. get;
  96. private set;
  97. }
  98. public int Interval
  99. {
  100. get;
  101. set;
  102. }
  103. public MsgPool(string loopname = null, bool lazySupported = false)
  104. {
  105. Interval = 500;
  106. LazySupported = lazySupported;
  107. ExecutionResults = new Dictionary<string, string>();
  108. LooperName = loopname;
  109. }
  110. public MsgPool(int interval, Action<MsgPool> handler, string loopername = null, bool lazySupported = false)
  111. : this(loopername, lazySupported)
  112. {
  113. Interval = interval;
  114. Handler = handler;
  115. LazySupported = lazySupported;
  116. }
  117. public void SetLog(ILog _log)
  118. {
  119. log = _log;
  120. }
  121. public int getCount()
  122. {
  123. return PostMsgs.Count + SendMsgs.Count;
  124. }
  125. public void Run()
  126. {
  127. if (!IsRuning)
  128. {
  129. IsRuning = true;
  130. if (LazySupported)
  131. {
  132. lazylooper = new MsgPool(null, false);
  133. lazylooper.Run();
  134. }
  135. context = new Thread(Loop);
  136. context.Start();
  137. lock (_lock)
  138. {
  139. Loopers.Add(this);
  140. }
  141. }
  142. }
  143. private void Loop()
  144. {
  145. lastInvervalTime = GetTickCount();
  146. while (true)
  147. {
  148. uint millisecondsTimeout = (uint)(((uint)Interval <= GetElapsedMS(lastInvervalTime)) ? 1 : (Interval - (int)GetElapsedMS(lastInvervalTime)));
  149. if (IsRuning)
  150. {
  151. bool flag = waitevent.WaitOne((int)millisecondsTimeout);
  152. if (IsRuning)
  153. {
  154. if (flag)
  155. {
  156. Msg msg = null;
  157. lock (lockMsg)
  158. {
  159. if (SendMsgs.Count > 0)
  160. {
  161. msg = SendMsgs.Pop();
  162. }
  163. else if (PostMsgs.Count > 0)
  164. {
  165. msg = PostMsgs.Dequeue();
  166. }
  167. }
  168. if (msg != null)
  169. {
  170. try
  171. {
  172. if (!msg.HasParam && msg.Handler != null)
  173. {
  174. msg.Handler(this, msg.Sender);
  175. }
  176. else if (msg.WithParameterHandler != null)
  177. {
  178. msg.WithParameterHandler(this, msg.Param, msg.Sender);
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. ExecutionResults.Add(msg.id, ex.Message);
  184. if (log != null)
  185. {
  186. log.Error("Msg:" + ex.Message + ",CallStack:" + ex.StackTrace);
  187. }
  188. }
  189. }
  190. if (SendMsgs.Count <= 0 && PostMsgs.Count <= 0)
  191. {
  192. waitevent.Reset();
  193. }
  194. }
  195. else
  196. {
  197. if (Handler != null)
  198. {
  199. Handler(this);
  200. }
  201. lastInvervalTime = GetTickCount();
  202. }
  203. continue;
  204. }
  205. }
  206. break;
  207. }
  208. }
  209. public void Terminate(bool force = false)
  210. {
  211. if (!force)
  212. {
  213. if (LazySupported)
  214. {
  215. LazyMsg(delegate
  216. {
  217. lazylooper.IsRuning = false;
  218. }, this, "Terminate lazy looper");
  219. lazylooper.context.Join();
  220. }
  221. PostMsg(delegate
  222. {
  223. IsRuning = false;
  224. }, this, "Terminate looper");
  225. }
  226. else
  227. {
  228. if (LazySupported)
  229. {
  230. lazylooper.IsRuning = false;
  231. lazylooper.waitevent.Set();
  232. }
  233. IsRuning = false;
  234. waitevent.Set();
  235. }
  236. lock (_lock)
  237. {
  238. Loopers.Remove(this);
  239. }
  240. }
  241. public static void TerminateAll(bool force = false, MsgPool looper = null)
  242. {
  243. lock (_lock)
  244. {
  245. if (looper != null)
  246. {
  247. looper.Terminate(force);
  248. }
  249. else
  250. {
  251. foreach (MsgPool looper2 in Loopers)
  252. {
  253. if (!force)
  254. {
  255. if (looper2.LazySupported)
  256. {
  257. looper2.LazyMsg(delegate
  258. {
  259. looper2.lazylooper.IsRuning = false;
  260. }, looper2, "Terminate lazy looper");
  261. }
  262. looper2.PostMsg(delegate
  263. {
  264. looper2.IsRuning = false;
  265. }, looper2, "Terminate looper");
  266. }
  267. else
  268. {
  269. if (looper2.LazySupported)
  270. {
  271. looper2.lazylooper.IsRuning = false;
  272. looper2.lazylooper.waitevent.Set();
  273. }
  274. looper2.IsRuning = false;
  275. looper2.waitevent.Set();
  276. }
  277. }
  278. Loopers.Clear();
  279. }
  280. }
  281. }
  282. public string PostMsg(Action<MsgPool, object> handler, object sender = null, string description = "handler")
  283. {
  284. lock (lockMsg)
  285. {
  286. if (!IsRuning)
  287. {
  288. return string.Empty;
  289. }
  290. Msg msg = new Msg(handler, sender, description);
  291. PostMsgs.Enqueue(msg);
  292. waitevent.Set();
  293. return msg.id;
  294. }
  295. }
  296. public string PostMsg(Action<MsgPool, object, object> handler, object param, object sender = null, string description = "handler")
  297. {
  298. lock (lockMsg)
  299. {
  300. if (!IsRuning)
  301. {
  302. return string.Empty;
  303. }
  304. Msg msg = new Msg(handler, param, sender, description);
  305. PostMsgs.Enqueue(msg);
  306. waitevent.Set();
  307. return msg.id;
  308. }
  309. }
  310. public string SendMsg(Action<MsgPool, object> handler, object sender = null, string description = "handler")
  311. {
  312. lock (lockMsg)
  313. {
  314. if (!IsRuning)
  315. {
  316. return string.Empty;
  317. }
  318. Msg msg = new Msg(handler, sender, description);
  319. SendMsgs.Push(msg);
  320. waitevent.Set();
  321. return msg.id;
  322. }
  323. }
  324. public string SendMsg(Action<MsgPool, object, object> handler, object param, object sender = null, string description = "handler")
  325. {
  326. lock (lockMsg)
  327. {
  328. if (!IsRuning)
  329. {
  330. return string.Empty;
  331. }
  332. Msg msg = new Msg(handler, param, sender, description);
  333. SendMsgs.Push(msg);
  334. waitevent.Set();
  335. return msg.id;
  336. }
  337. }
  338. public string LazyMsg(Action<MsgPool, object> handler, object sender = null, string description = "handler")
  339. {
  340. if (!IsRuning)
  341. {
  342. return string.Empty;
  343. }
  344. if (LazySupported)
  345. {
  346. return lazylooper.PostMsg(handler, sender, description);
  347. }
  348. return null;
  349. }
  350. public string LazyMsg(Action<MsgPool, object, object> handler, object param, object sender = null, string description = "handler")
  351. {
  352. if (!IsRuning)
  353. {
  354. return string.Empty;
  355. }
  356. if (LazySupported)
  357. {
  358. return lazylooper.PostMsg(handler, param, sender, description);
  359. }
  360. return null;
  361. }
  362. private uint GetElapsedMS(uint startMS)
  363. {
  364. uint tickCount = GetTickCount();
  365. if (startMS > tickCount)
  366. {
  367. return (uint)(-1 - (int)startMS + (int)tickCount + 1);
  368. }
  369. return tickCount - startMS;
  370. }
  371. private uint GetTickCount()
  372. {
  373. return (uint)Environment.TickCount;
  374. }
  375. }
  376. }