终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataBus_Currency.cs 6.0 KiB

2 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. 
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. /* ***********************************************
  10.  * subject 通用数据总线,采取订阅推送模式
  11.  * author 张原川
  12.  * date   2019/6/3 15:03:10
  13.  * ***********************************************/
  14. namespace BPASmartClient.Bus.DataBus
  15. {
  16. public class DataBus_Currency<TData> : ISimpleDataBus<TData>
  17. {
  18. //接收数据缓冲
  19. //protected CircularBuffer<TData> _dataPool = new CircularBuffer<TData>(1 * 1024 * 1024);
  20. protected ConcurrentQueue<TData> _dataPool = new ConcurrentQueue<TData>();
  21. //订阅数据回调集合
  22. protected List<Action<TData[]>> _multDataHandlers = new List<Action<TData[]>>();
  23. protected List<Action<TData>> _singleDataHandlers = new List<Action<TData>>();
  24. //订阅与推送数据信号量
  25. protected AutoResetEvent _are = new AutoResetEvent(false);
  26. //运行标识
  27. protected bool _running = false;
  28. public int MultSubscriberCount { get { return _multDataHandlers.Count; } }
  29. public int SingleSubscriberCount { get { return _singleDataHandlers.Count; } }
  30. public int DataCount { get { return _dataPool.Count; } }
  31. /// <summary>
  32. /// 终止事件总线
  33. /// </summary>
  34. public void Dispose()
  35. {
  36. StopBus();
  37. }
  38. /// <summary>
  39. /// 数据存入
  40. /// </summary>
  41. public void Put(TData data)
  42. {
  43. if (!_running)
  44. return;
  45. if (_multDataHandlers.Count > 0 || _singleDataHandlers.Count > 0)
  46. _dataPool.Enqueue(data);
  47. }
  48. /// <summary>
  49. /// 数据存入
  50. /// </summary>
  51. public void Put(TData[] data)
  52. {
  53. if (!_running)
  54. return;
  55. if (_multDataHandlers.Count > 0 || _singleDataHandlers.Count > 0)
  56. foreach (var item in data)
  57. _dataPool.Enqueue(item);
  58. }
  59. /// <summary>
  60. /// 开启总线
  61. /// </summary>
  62. public void StartBus()
  63. {
  64. _running = true;
  65. List<TData> items = new List<TData>();
  66. _are.Set();
  67. while (_running)
  68. {
  69. int count = _dataPool.Count;
  70. items.Clear();
  71. if (_singleDataHandlers.Count > 0)
  72. {
  73. _are.WaitOne(TimeSpan.FromMilliseconds(10));
  74. for (int i = 0; i < count; i++)
  75. {
  76. TData data = default(TData);
  77. while (!_dataPool.TryDequeue(out data)) ;
  78. uint msgId = Convert.ToUInt32(data.GetType().GetProperty("msgid").GetValue(data));
  79. if (msgId == 316)
  80. {
  81. }
  82. //var item = _dataPool.Get();
  83. //Parallel.ForEach(_singleDataHandlers, p => p.Invoke(items[items.Count - 1]));
  84. for (int j = 0; j < _singleDataHandlers.Count; j++)
  85. {
  86. _singleDataHandlers[j].Invoke(data);
  87. }
  88. items.Add(data);
  89. }
  90. }
  91. if (_multDataHandlers.Count > 0)
  92. {
  93. if (items.Count <= 0)
  94. {
  95. TData data = default(TData);
  96. for (int i = 0; i < count; i++)
  97. {
  98. while (!_dataPool.TryDequeue(out data)) ;
  99. items.Add(data);
  100. }
  101. //items.AddRange(_dataPool.Get(count));
  102. }
  103. _are.WaitOne(TimeSpan.FromMilliseconds(10));
  104. //Parallel.For(0, _multDataHandlers.Count, (i) =>
  105. //{
  106. // _multDataHandlers[i].Invoke(items.ToArray());
  107. //});
  108. for (int i = _multDataHandlers.Count - 1; i >= 0; i--)
  109. {
  110. _multDataHandlers[i].Invoke(items.ToArray());
  111. }
  112. }
  113. Thread.Sleep(10);
  114. _are.Set();
  115. }
  116. }
  117. public void StopBus()
  118. {
  119. _running = false;
  120. }
  121. /// <summary>
  122. /// 数据订阅
  123. /// </summary>
  124. /// <param name="action">接收数据回调</param>
  125. public void Subscribe(Action<TData> action)
  126. {
  127. if (_singleDataHandlers.Contains(action))
  128. return;
  129. _are.Reset();
  130. _singleDataHandlers.Add(action);
  131. _are.Set();
  132. }
  133. /// <summary>
  134. /// 数据订阅
  135. /// </summary>
  136. /// <param name="action">接收数据回调</param>
  137. public void Subscribe(Action<TData[]> action)
  138. {
  139. if (_multDataHandlers.Contains(action))
  140. return;
  141. _are.Reset();
  142. _multDataHandlers.Add(action);
  143. _are.Set();
  144. }
  145. /// <summary>
  146. /// 取消数据订阅
  147. /// </summary>
  148. /// <param name="action">接收数据回调</param>
  149. public void UnSubcribe(Action<TData> action)
  150. {
  151. if (!_singleDataHandlers.Contains(action))
  152. return;
  153. _are.Reset();
  154. _singleDataHandlers.Remove(action);
  155. _are.Set();
  156. }
  157. /// <summary>
  158. /// 取消数据订阅
  159. /// </summary>
  160. /// <param name="action">接收数据回调</param>
  161. public void UnSubcribe(Action<TData[]> action)
  162. {
  163. if (!_multDataHandlers.Contains(action))
  164. return;
  165. _are.Reset();
  166. _multDataHandlers.Remove(action);
  167. _are.Set();
  168. }
  169. }
  170. }