NetflowTool.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 DataMonitorSwitch = 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() && !DataMonitorSwitch)
  74. {
  75. DataCounterInterval = interval;
  76. DataMonitorSwitch = true;
  77. Task.Factory.StartNew(() =>
  78. {
  79. while (DataMonitorSwitch)
  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. foreach (var uc in UploadCounter)
  110. {
  111. uc?.Close();
  112. }
  113. foreach (var dc in DownloadCounter)
  114. {
  115. dc?.Close();
  116. }
  117. Init();
  118. }
  119. public void Stop()
  120. {
  121. DataMonitorSwitch = false;
  122. foreach (var uc in UploadCounter)
  123. {
  124. uc?.Close();
  125. }
  126. foreach (var dc in DownloadCounter)
  127. {
  128. dc?.Close();
  129. }
  130. }
  131. /// <summary>
  132. /// 终结器
  133. /// </summary>
  134. ~NetFlowTool()
  135. {
  136. Stop();
  137. }
  138. }
  139. }