etNetworkTest.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "etNetworkTest.h"
  2. #include "ui_etNetworkTest.h"
  3. #include "CAppInfo.h"
  4. #include "logproc.h"
  5. #include <QScrollBar>
  6. #include <QHBoxLayout>
  7. #include <QLabel>
  8. #include <QProcess>
  9. #include <regex>
  10. #include "winsock2.h"
  11. #include "windows.h"
  12. #include "winsock.h"
  13. #include "iphlpapi.h"
  14. etNetworkTest::etNetworkTest(QWidget *parent) :
  15. QWidget(parent),
  16. ui(new Ui::etNetworkTest)
  17. {
  18. ui->setupUi(this);
  19. setStyleSheet(g_appInfoPtr->m_sQssStr);
  20. bSpeedPass = false;
  21. bDelayPass = false;
  22. connect(this, &etNetworkTest::checkInfo, this, [&](QString speed, QString sTime) {
  23. if (!speed.isEmpty())
  24. {
  25. if (speed.toInt() > 5)
  26. {
  27. bSpeedPass = true;
  28. ui->tablewt_envTest->item(0, 0)->setText(QString::fromLocal8Bit("电脑当前下载速度"));
  29. ui->tablewt_envTest->item(0, 1)->setText(speed + "kb/s");
  30. QWidget *widget = new QWidget;
  31. QHBoxLayout *layout = new QHBoxLayout;
  32. widget->setLayout(layout);
  33. QLabel *icon = new QLabel;
  34. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  35. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  36. layout->addWidget(icon);
  37. layout->addStretch();
  38. ui->tablewt_envTest->setCellWidget(0, 2, widget);
  39. }
  40. }
  41. if(!sTime.isEmpty())
  42. {
  43. if (sTime.toInt() < 500)
  44. {
  45. bDelayPass = true;
  46. }
  47. ui->tablewt_envTest->item(1, 0)->setText(QString::fromLocal8Bit("电脑当前网络延迟"));
  48. ui->tablewt_envTest->item(1, 1)->setText(sTime+"ms");
  49. QWidget *widget = new QWidget;
  50. QHBoxLayout *layout = new QHBoxLayout;
  51. widget->setLayout(layout);
  52. QLabel *icon = new QLabel;
  53. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  54. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  55. layout->addWidget(icon);
  56. layout->addStretch();
  57. ui->tablewt_envTest->setCellWidget(1, 2, widget);
  58. }
  59. if (bSpeedPass && bDelayPass)
  60. {
  61. m_bIsRun = false;
  62. }
  63. });
  64. m_bIsRun = true;
  65. m_thread = std::thread(std::bind(&etNetworkTest::threadProc, this));
  66. }
  67. etNetworkTest::~etNetworkTest()
  68. {
  69. m_bIsRun = false;
  70. m_thread.join();
  71. delete ui;
  72. }
  73. void etNetworkTest::threadProc()
  74. {
  75. PMIB_IFTABLE m_pTable = NULL;
  76. DWORD m_dwAdapters = 0;
  77. ULONG uRetCode = GetIfTable(m_pTable, &m_dwAdapters, TRUE);
  78. if (uRetCode == ERROR_NOT_SUPPORTED)
  79. {
  80. return;
  81. }
  82. if (uRetCode == ERROR_INSUFFICIENT_BUFFER)
  83. {
  84. m_pTable = (PMIB_IFTABLE)new BYTE[65535]; //假设端口数不超过65535个
  85. }
  86. DWORD dwLastIn = 0; //上一秒钟的接收字节数
  87. DWORD dwLastOut = 0; //上一秒钟的发送字节数
  88. DWORD dwBandIn = 0; //下载速度
  89. DWORD dwBandOut = 0;
  90. while(m_bIsRun)
  91. {
  92. QString sFileName = sAudioUrl.right(sAudioUrl.length() - sAudioUrl.lastIndexOf("/") - 1);
  93. sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
  94. CHttpRequestPackage hrp;
  95. hrp.sUri = sAudioUrl;
  96. hrp.sCommonStr = sFileName;
  97. hrp.nRequestType = RequestType::rtDownLoadFile;
  98. g_httpBllPtr->downLoad(hrp);
  99. GetIfTable(m_pTable, &m_dwAdapters, TRUE);
  100. DWORD dwInOctets = 0;
  101. DWORD dwOutOctets = 0;
  102. //将所有端口的流量进行统计
  103. for (UINT i = 0; i < m_pTable->dwNumEntries; i++)
  104. {
  105. MIB_IFROW Row = m_pTable->table[i];
  106. dwInOctets += Row.dwInOctets;
  107. dwOutOctets += Row.dwOutOctets;
  108. }
  109. dwBandIn = dwInOctets - dwLastIn; //下载速度
  110. dwBandOut = dwOutOctets - dwLastOut; //上传速速
  111. if (dwLastIn <= 0)
  112. {
  113. dwBandIn = 0;
  114. }
  115. else
  116. {
  117. dwBandIn = dwBandIn / 1024; //b转换成kb
  118. }
  119. if (dwLastOut <= 0)
  120. {
  121. dwBandOut = 0;
  122. }
  123. else
  124. {
  125. dwBandOut = dwBandOut / 1024; //b转换成kb
  126. }
  127. dwLastIn = dwInOctets;
  128. dwLastOut = dwOutOctets;
  129. QProcess process;
  130. process.start("ping www.baidu.com -n 1");
  131. process.waitForFinished(-1); // will wait forever until finished
  132. QString sOutStr = QString::fromLocal8Bit(process.readAllStandardOutput().data());
  133. QString sFindStr = QString::fromLocal8Bit("平均 =");
  134. int nIndex = sOutStr.indexOf(sFindStr);
  135. if (nIndex < 0)
  136. {
  137. nIndex = sOutStr.indexOf(QString::fromLocal8Bit("Average ="));
  138. }
  139. QString sTimeNum = "";
  140. if (nIndex > 0)
  141. {
  142. QString sTime = sOutStr.mid(nIndex + sFindStr.length(), sOutStr.length() - nIndex);
  143. int nTimeIndex = sTime.indexOf("ms");
  144. sTimeNum = sTime.left(nTimeIndex);
  145. }
  146. emit checkInfo(QString::number(dwBandIn), sTimeNum);
  147. Sleep(1000);
  148. }
  149. }
  150. int etNetworkTest::setUI(const int nLeft, const int nTop, const int nWidth)
  151. {
  152. setGeometry(nLeft, nTop, nWidth, g_appInfoPtr->m_fRate*146);
  153. ui->tablewt_envTest->setGeometry(g_appInfoPtr->m_fRate*30, 0, width() - g_appInfoPtr->m_fRate*30*2, height());
  154. ui->tablewt_envTest->setEditTriggers(QAbstractItemView::NoEditTriggers);
  155. //整行选中的方式
  156. ui->tablewt_envTest->setSelectionBehavior(QAbstractItemView::SelectRows);
  157. //设置为只能选中一行
  158. ui->tablewt_envTest->setSelectionMode(QAbstractItemView::SingleSelection);
  159. //隐藏列表头
  160. ui->tablewt_envTest->verticalHeader()->setVisible(false);
  161. //隐藏边框
  162. ui->tablewt_envTest->setShowGrid(false);
  163. //表头不高亮显示
  164. ui->tablewt_envTest->horizontalHeader()->setHighlightSections(false);
  165. //设置行数
  166. ui->tablewt_envTest->setRowCount(2);
  167. //设置列数
  168. ui->tablewt_envTest->setColumnCount(3);
  169. ui->tablewt_envTest->setHorizontalScrollMode(QTableWidget::ScrollPerPixel);
  170. ui->tablewt_envTest->horizontalScrollBar()->setSingleStep(g_appInfoPtr->m_fRate*5);
  171. QStringList sHeadStr;
  172. sHeadStr << QString::fromLocal8Bit("检测项") <<
  173. QString::fromLocal8Bit("值") <<
  174. QString::fromLocal8Bit("状态");
  175. ui->tablewt_envTest->setHorizontalHeaderLabels(sHeadStr);
  176. ui->tablewt_envTest->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  177. ui->tablewt_envTest->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
  178. ui->tablewt_envTest->verticalHeader()->setDefaultSectionSize(g_appInfoPtr->m_fRate*48);
  179. ui->tablewt_envTest->setColumnWidth(0, g_appInfoPtr->m_fRate*525);
  180. ui->tablewt_envTest->setColumnWidth(1, g_appInfoPtr->m_fRate*170);
  181. ui->tablewt_envTest->setColumnWidth(2, g_appInfoPtr->m_fRate*24);
  182. ui->tablewt_envTest->horizontalHeader()->setStretchLastSection(true);
  183. for(int i = 0; i < 2; i++)
  184. {
  185. ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("")));
  186. ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("")));
  187. /* QWidget *widget = new QWidget;
  188. QHBoxLayout *layout = new QHBoxLayout;
  189. widget->setLayout(layout);
  190. QLabel *icon = new QLabel;
  191. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
  192. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
  193. layout->addWidget(icon);
  194. layout->addStretch();
  195. ui->tablewt_envTest->setCellWidget(i, 2, widget);*/
  196. }
  197. ui->tablewt_envTest->item(0, 0)->setText(QString::fromLocal8Bit("电脑当前下载速度"));
  198. ui->tablewt_envTest->item(1, 0)->setText(QString::fromLocal8Bit("电脑当前网络延迟"));
  199. return height();
  200. }
  201. int etNetworkTest::widgetHeight()
  202. {
  203. return height();
  204. }
  205. int etNetworkTest::getCheckStatus()
  206. {
  207. return (bSpeedPass && bDelayPass);
  208. }