NetflowTool.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. private bool Init()
  41. {
  42. string[] 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. public bool Start()
  70. {
  71. if (Init() && !DataMonitorSwitch)
  72. {
  73. DataMonitorSwitch = true;
  74. Task.Factory.StartNew(() =>
  75. {
  76. while (DataMonitorSwitch)
  77. {
  78. DataMonitorEvent?.Invoke(this);
  79. try
  80. {
  81. _UploadDataCount += _UploadData;
  82. _UploadData = 0;
  83. foreach (var uc in UploadCounter)
  84. {
  85. _UploadData += (int)uc?.NextValue();
  86. }
  87. _DownloadDataCount += _DownloadData;
  88. _DownloadData = 0;
  89. foreach (var dc in DownloadCounter)
  90. {
  91. _DownloadData += (int)dc?.NextValue();
  92. }
  93. }
  94. catch { }
  95. Thread.Sleep(DataCounterInterval);
  96. }
  97. });
  98. return true;
  99. }
  100. return false;
  101. }
  102. public void Stop()
  103. {
  104. DataMonitorSwitch = false;
  105. foreach (var uc in UploadCounter)
  106. {
  107. uc?.Close();
  108. }
  109. foreach (var dc in DownloadCounter)
  110. {
  111. dc?.Close();
  112. }
  113. }
  114. ~NetFlowTool()
  115. {
  116. Stop();
  117. }
  118. }
  119. }