NetflowTool.cs 4.5 KB

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