NetflowTool.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Azylee.Core.DataUtils.CollectionUtils;
  2. using Azylee.Core.WindowsUtils.InfoUtils;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace Azylee.Core.NetUtils
  10. {
  11. public class NetFlowTool
  12. {
  13. /// <summary>
  14. /// 上行数据流量
  15. /// </summary>
  16. public int UploadData { get { return _UploadData; } }
  17. private int _UploadData;
  18. /// <summary>
  19. /// 上行数据总流量
  20. /// </summary>
  21. public long UploadDataCount { get { return _UploadDataCount; } }
  22. private long _UploadDataCount;
  23. /// <summary>
  24. /// 下行数据流量
  25. /// </summary>
  26. public int DownloadData { get { return _DownloadData; } }
  27. private int _DownloadData;
  28. /// <summary>
  29. /// 下行数据总流量
  30. /// </summary>
  31. public long DownloadDataCount { get { return _DownloadDataCount; } }
  32. private long _DownloadDataCount;
  33. private List<PerformanceCounter> UploadCounter, DownloadCounter;//上行、下行流量计数器
  34. private int DataCounterInterval = 1000;//数据流量计数器计数周期
  35. private bool IsStart = false;
  36. public delegate void MonitorEvent(NetFlowTool n);
  37. public MonitorEvent DataMonitorEvent;
  38. public string[] Instances { get { return _Instances; } }
  39. private string[] _Instances;
  40. private bool Init()
  41. {
  42. _Instances = NetCardInfoTool.GetInstanceNames();
  43. if (ListTool.HasElements(_Instances))
  44. {
  45. UploadCounter = new List<PerformanceCounter>();
  46. DownloadCounter = new List<PerformanceCounter>();
  47. for (int i = 0; i < _Instances.Count(); i++)
  48. {
  49. try
  50. {
  51. // 添加 上行流量计数器
  52. UploadCounter.Add(new PerformanceCounter("Network Interface", "Bytes Sent/sec", _Instances[i]));
  53. }
  54. catch { }
  55. try
  56. {
  57. // 添加 下行流量计数器
  58. DownloadCounter.Add(new PerformanceCounter("Network Interface", "Bytes Received/sec", _Instances[i]));
  59. }
  60. catch { }
  61. }
  62. }
  63. if (ListTool.HasElements(UploadCounter) && ListTool.HasElements(DownloadCounter))
  64. {
  65. return true;
  66. }
  67. return false;
  68. }
  69. /// <summary>
  70. /// 启动流量监测
  71. /// </summary>
  72. /// <param name="interval"></param>
  73. /// <returns></returns>
  74. public bool Start(int interval = 1000)
  75. {
  76. if (Init() && !IsStart)
  77. {
  78. DataCounterInterval = interval;
  79. IsStart = true;
  80. Task.Factory.StartNew(() =>
  81. {
  82. while (IsStart)
  83. {
  84. DataMonitorEvent?.Invoke(this);
  85. try
  86. {
  87. _UploadDataCount += _UploadData;
  88. _UploadData = 0;
  89. foreach (var uc in UploadCounter)
  90. {
  91. _UploadData += (int)uc?.NextValue();
  92. }
  93. _DownloadDataCount += _DownloadData;
  94. _DownloadData = 0;
  95. foreach (var dc in DownloadCounter)
  96. {
  97. _DownloadData += (int)dc?.NextValue();
  98. }
  99. }
  100. catch (Exception e)
  101. {
  102. }
  103. Thread.Sleep(DataCounterInterval);
  104. }
  105. });
  106. return true;
  107. }
  108. return false;
  109. }
  110. /// <summary>
  111. /// 重启流量计数器
  112. /// </summary>
  113. public void Restart()
  114. {
  115. if (IsStart)
  116. {
  117. foreach (var uc in UploadCounter)
  118. {
  119. uc?.Close();
  120. }
  121. foreach (var dc in DownloadCounter)
  122. {
  123. dc?.Close();
  124. }
  125. }
  126. Init();
  127. }
  128. /// <summary>
  129. /// 重置流量表数
  130. /// </summary>
  131. public void Reset()
  132. {
  133. _UploadData = 0;
  134. _UploadDataCount = 0;
  135. _DownloadData = 0;
  136. _DownloadDataCount = 0;
  137. }
  138. /// <summary>
  139. /// 停止流量监测
  140. /// </summary>
  141. public void Stop()
  142. {
  143. if (IsStart)
  144. {
  145. IsStart = false;
  146. foreach (var uc in UploadCounter)
  147. {
  148. uc?.Close();
  149. }
  150. foreach (var dc in DownloadCounter)
  151. {
  152. dc?.Close();
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// 终结器
  158. /// </summary>
  159. ~NetFlowTool()
  160. {
  161. Stop();
  162. }
  163. }
  164. }