终端一体化运控平台
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_Byte.cs 2.8 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Byte类型数据总线,在订阅推送模式基础上
  11. * 增加主动获取
  12.  * author 张原川
  13.  * date   2019/6/3 14:44:36
  14.  * ***********************************************/
  15. namespace BPASmartClient.Bus.DataBus
  16. {
  17. public class DataBus_Byte : DataBus_Currency<byte>, IGivenDataBus<byte>
  18. {
  19. //接收数据缓冲(用以Get)
  20. //protected CircularBuffer<byte> _givenDataPool = new CircularBuffer<byte>(1 * 1024 * 1024);
  21. protected ConcurrentQueue<byte> _givenDataPool = new ConcurrentQueue<byte>();
  22. public new int DataCount { get { return _givenDataPool.Count; } }
  23. /// <summary>
  24. /// 重写Put方法,加入givenDataPool
  25. /// </summary>
  26. public new void Put(byte data)
  27. {
  28. if (!_running)
  29. return;
  30. if (_multDataHandlers.Count > 0 || _singleDataHandlers.Count > 0)
  31. _dataPool.Enqueue(data);
  32. _givenDataPool.Enqueue(data);
  33. }
  34. /// <summary>
  35. /// 重写Put方法,加入givenDataPool
  36. /// </summary>
  37. public new void Put(byte[] data)
  38. {
  39. if (!_running)
  40. return;
  41. if (_multDataHandlers.Count > 0 || _singleDataHandlers.Count > 0)
  42. foreach (var item in data)
  43. _dataPool.Enqueue(item);
  44. foreach (var item in data)
  45. _givenDataPool.Enqueue(item);
  46. }
  47. /// <summary>
  48. /// 数据取出
  49. /// </summary>
  50. public byte Get()
  51. {
  52. agin:
  53. if (_givenDataPool.Count <= 0)
  54. {
  55. Thread.Sleep(5);
  56. goto agin;
  57. }
  58. byte res;
  59. while (!_givenDataPool.TryDequeue(out res)) ;
  60. return res;
  61. }
  62. /// <summary>
  63. /// 数据取出
  64. /// </summary>
  65. public byte[] Get(int count)
  66. {
  67. agin:
  68. if (_givenDataPool.Count < count)
  69. {
  70. Thread.Sleep(5);
  71. goto agin;
  72. }
  73. //Console.WriteLine(_givenDataPool.Size + "===========" + _dataPool.Size);
  74. //for (int i = 0; i < count; i++) {
  75. // _givenDataPool.TryDequeue
  76. //}
  77. int i = 0;
  78. byte[] result = new byte[count];
  79. while (i < count)
  80. {
  81. if (_givenDataPool.TryDequeue(out result[i]))
  82. {
  83. i++;
  84. }
  85. }
  86. return result;// _givenDataPool.Get(count);
  87. }
  88. }
  89. }