NetflowTool.cs 5.3 KB

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