NetflowTool.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. public bool Start(int interval = 1000)
  72. {
  73. if (Init() && !IsStart)
  74. {
  75. DataCounterInterval = interval;
  76. IsStart = true;
  77. Task.Factory.StartNew(() =>
  78. {
  79. while (IsStart)
  80. {
  81. DataMonitorEvent?.Invoke(this);
  82. try
  83. {
  84. _UploadDataCount += _UploadData;
  85. _UploadData = 0;
  86. foreach (var uc in UploadCounter)
  87. {
  88. _UploadData += (int)uc?.NextValue();
  89. }
  90. _DownloadDataCount += _DownloadData;
  91. _DownloadData = 0;
  92. foreach (var dc in DownloadCounter)
  93. {
  94. _DownloadData += (int)dc?.NextValue();
  95. }
  96. }
  97. catch (Exception e)
  98. {
  99. }
  100. Thread.Sleep(DataCounterInterval);
  101. }
  102. });
  103. return true;
  104. }
  105. return false;
  106. }
  107. public void Restart()
  108. {
  109. if (IsStart)
  110. {
  111. foreach (var uc in UploadCounter)
  112. {
  113. uc?.Close();
  114. }
  115. foreach (var dc in DownloadCounter)
  116. {
  117. dc?.Close();
  118. }
  119. }
  120. Init();
  121. }
  122. public void Stop()
  123. {
  124. if (IsStart)
  125. {
  126. IsStart = false;
  127. foreach (var uc in UploadCounter)
  128. {
  129. uc?.Close();
  130. }
  131. foreach (var dc in DownloadCounter)
  132. {
  133. dc?.Close();
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// 终结器
  139. /// </summary>
  140. ~NetFlowTool()
  141. {
  142. Stop();
  143. }
  144. }
  145. }