ClientDlg.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // ClientDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Client.h"
  5. #include "ClientDlg.h"
  6. #include "afxdialogex.h"
  7. #include "../../../../Common/Src/WaitFor.h"
  8. #include "../../../../Common/Src/SysHelper.h"
  9. #ifdef _WIN64
  10. #ifdef _DEBUG
  11. #pragma comment(lib, "../../../Bin/HPSocket/x64/static/HPSocket-SSL_UD.lib")
  12. #else
  13. #pragma comment(lib, "../../../Bin/HPSocket/x64/static/HPSocket-SSL_U.lib")
  14. #endif
  15. #else
  16. #ifdef _DEBUG
  17. #pragma comment(lib, "../../../Bin/HPSocket/x86/static/HPSocket-SSL_UD.lib")
  18. #else
  19. #pragma comment(lib, "../../../Bin/HPSocket/x86/static/HPSocket-SSL_U.lib")
  20. #endif
  21. #endif
  22. // CClientDlg dialog
  23. #define DEFAULT_ADDRESS _T("127.0.0.1")
  24. #define DEFAULT_PORT _T("5555")
  25. #define LOCAL_ADDRESS _T("0.0.0.0")
  26. CClientDlg::CClientDlg(CWnd* pParent /*=nullptr*/)
  27. : CDialogEx(CClientDlg::IDD, pParent), m_Agent(this)
  28. , m_sslInitializer(SSL_SM_CLIENT, g_c_iVerifyMode, g_c_lpszPemCertFile, g_c_lpszPemKeyFile, g_c_lpszKeyPasswod, g_c_lpszCAPemCertFileOrPath)
  29. {
  30. VERIFY(m_sslInitializer.IsValid());
  31. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  32. }
  33. void CClientDlg::DoDataExchange(CDataExchange* pDX)
  34. {
  35. CDialogEx::DoDataExchange(pDX);
  36. DDX_Control(pDX, IDC_INFO, m_Info);
  37. DDX_Control(pDX, IDC_ADDRESS, m_Address);
  38. DDX_Control(pDX, IDC_PORT, m_Port);
  39. DDX_Control(pDX, IDC_START, m_Start);
  40. DDX_Control(pDX, IDC_STOP, m_Stop);
  41. DDX_Control(pDX, IDC_TEST_TIMES, m_TestTimes);
  42. DDX_Control(pDX, IDC_SOCK_COUNT, m_SocketCount);
  43. DDX_Control(pDX, IDC_THREAD_COUNT, m_ThreadCount);
  44. DDX_Control(pDX, IDC_DATA_LEN, m_DataLen);
  45. DDX_Control(pDX, IDC_TEST_TIMES_INTERV, m_TestInterv);
  46. DDX_Control(pDX, IDC_SEND_POLICY, m_SendPolicy);
  47. DDX_Control(pDX, IDC_MAX_CONN_COUNT, m_MaxConnCount);
  48. }
  49. BEGIN_MESSAGE_MAP(CClientDlg, CDialogEx)
  50. ON_WM_PAINT()
  51. ON_WM_QUERYDRAGICON()
  52. ON_BN_CLICKED(IDC_START, &CClientDlg::OnBnClickedStart)
  53. ON_BN_CLICKED(IDC_STOP, &CClientDlg::OnBnClickedStop)
  54. ON_MESSAGE(USER_INFO_MSG, OnUserInfoMsg)
  55. ON_WM_VKEYTOITEM()
  56. ON_WM_CLOSE()
  57. END_MESSAGE_MAP()
  58. // CClientDlg message handlers
  59. BOOL CClientDlg::OnInitDialog()
  60. {
  61. CDialogEx::OnInitDialog();
  62. // Set the icon for this dialog. The framework does this automatically
  63. // when the application's main window is not a dialog
  64. SetIcon(m_hIcon, TRUE); // Set big icon
  65. SetIcon(m_hIcon, FALSE); // Set small icon
  66. // TODO: Add extra initialization here
  67. m_TestTimes.SetCurSel(5);
  68. m_TestInterv.SetCurSel(1);
  69. m_SocketCount.SetCurSel(5);
  70. m_ThreadCount.SetCurSel(0);
  71. m_MaxConnCount.SetCurSel(0);
  72. m_DataLen.SetCurSel(5);
  73. m_SendPolicy.SetCurSel(0);
  74. m_Address.SetWindowText(DEFAULT_ADDRESS);
  75. m_Port.SetWindowText(DEFAULT_PORT);
  76. ::SetMainWnd(this);
  77. ::SetInfoList(&m_Info);
  78. SetAppState(ST_STOPPED);
  79. return TRUE; // return TRUE unless you set the focus to a control
  80. }
  81. void CClientDlg::OnClose()
  82. {
  83. ::SetMainWnd(nullptr);
  84. __super::OnClose();
  85. }
  86. // If you add a minimize button to your dialog, you will need the code below
  87. // to draw the icon. For MFC applications using the document/view model,
  88. // this is automatically done for you by the framework.
  89. void CClientDlg::OnPaint()
  90. {
  91. if (IsIconic())
  92. {
  93. CPaintDC dc(this); // device context for painting
  94. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  95. // Center icon in client rectangle
  96. int cxIcon = GetSystemMetrics(SM_CXICON);
  97. int cyIcon = GetSystemMetrics(SM_CYICON);
  98. CRect rect;
  99. GetClientRect(&rect);
  100. int x = (rect.Width() - cxIcon + 1) / 2;
  101. int y = (rect.Height() - cyIcon + 1) / 2;
  102. // Draw the icon
  103. dc.DrawIcon(x, y, m_hIcon);
  104. }
  105. else
  106. {
  107. CDialogEx::OnPaint();
  108. }
  109. }
  110. // The system calls this function to obtain the cursor to display while the user drags
  111. // the minimized window.
  112. HCURSOR CClientDlg::OnQueryDragIcon()
  113. {
  114. return static_cast<HCURSOR>(m_hIcon);
  115. }
  116. BOOL CClientDlg::PreTranslateMessage(MSG* pMsg)
  117. {
  118. if (
  119. pMsg->message == WM_KEYDOWN
  120. &&( pMsg->wParam == VK_ESCAPE
  121. || pMsg->wParam == VK_CANCEL
  122. || pMsg->wParam == VK_RETURN
  123. ))
  124. return TRUE;
  125. return CDialog::PreTranslateMessage(pMsg);
  126. }
  127. void CClientDlg::SetAppState(EnAppState state)
  128. {
  129. if(m_enState == state)
  130. return;
  131. m_enState = state;
  132. if(this->GetSafeHwnd() == nullptr)
  133. return;
  134. m_Start.EnableWindow(m_enState == ST_STOPPED);
  135. m_Stop.EnableWindow(m_enState == ST_STARTED);
  136. m_Address.EnableWindow(m_enState == ST_STOPPED);
  137. m_Port.EnableWindow(m_enState == ST_STOPPED);
  138. m_TestTimes.EnableWindow(m_enState == ST_STOPPED);
  139. m_TestInterv.EnableWindow(m_enState == ST_STOPPED);
  140. m_SocketCount.EnableWindow(m_enState == ST_STOPPED);
  141. m_ThreadCount.EnableWindow(m_enState == ST_STOPPED);
  142. m_MaxConnCount.EnableWindow(m_enState == ST_STOPPED);
  143. m_DataLen.EnableWindow(m_enState == ST_STOPPED);
  144. m_SendPolicy.EnableWindow(m_enState == ST_STOPPED);
  145. }
  146. BOOL CClientDlg::CheckParams()
  147. {
  148. BOOL isOK = TRUE;
  149. if(m_strAddress.IsEmpty())
  150. {
  151. m_Address.SetFocus();
  152. isOK = FALSE;
  153. }
  154. else if(m_usPort == 0)
  155. {
  156. m_Port.SetFocus();
  157. isOK = FALSE;
  158. }
  159. else if(m_iTestTimes <= 0)
  160. {
  161. m_TestTimes.SetFocus();
  162. isOK = FALSE;
  163. }
  164. else if(m_iTestInterv < 0)
  165. {
  166. m_TestInterv.SetFocus();
  167. isOK = FALSE;
  168. }
  169. else if(m_iSocketCount <= 0)
  170. {
  171. m_SocketCount.SetFocus();
  172. isOK = FALSE;
  173. }
  174. else if(m_iThreadCount < 0 || m_iThreadCount > 500)
  175. {
  176. m_ThreadCount.SetFocus();
  177. isOK = FALSE;
  178. }
  179. else if(m_iDataLen <= 0)
  180. {
  181. m_DataLen.SetFocus();
  182. isOK = FALSE;
  183. }
  184. if(!isOK)
  185. MessageBox(_T("One or more settings invalid, pls check!"), _T("Params Error"), MB_OK);
  186. else
  187. {
  188. if(m_iThreadCount == 0)
  189. m_iThreadCount = min((::SysGetNumberOfProcessors() * 2 + 2), 500);
  190. }
  191. return isOK;
  192. }
  193. void CClientDlg::OnBnClickedStart()
  194. {
  195. CString strAddress;
  196. CString strPort;
  197. CString strTestTimes;
  198. CString strTestInterv;
  199. CString strSocketCount;
  200. CString strThreadCount;
  201. CString strDataLen;
  202. m_Address.GetWindowText(strAddress);
  203. m_Port.GetWindowText(strPort);
  204. m_TestTimes.GetWindowText(strTestTimes);
  205. m_TestInterv.GetWindowText(strTestInterv);
  206. m_SocketCount.GetWindowText(strSocketCount);
  207. m_ThreadCount.GetWindowText(strThreadCount);
  208. m_DataLen.GetWindowText(strDataLen);
  209. m_strAddress = strAddress.Trim();
  210. m_usPort = (USHORT)_ttoi(strPort);
  211. m_iTestTimes = _ttoi(strTestTimes);
  212. m_iTestInterv = _ttoi(strTestInterv);
  213. m_iSocketCount = _ttoi(strSocketCount);
  214. m_iThreadCount = _ttoi(strThreadCount);
  215. m_iDataLen = _ttoi(strDataLen);
  216. if(!CheckParams())
  217. return;
  218. EnSendPolicy enSendPolicy = (EnSendPolicy)m_SendPolicy.GetCurSel();
  219. CString strMaxConnCount;
  220. m_MaxConnCount.GetWindowText(strMaxConnCount);
  221. int iMaxConnCount = _ttoi(strMaxConnCount);
  222. if(iMaxConnCount == 0)
  223. iMaxConnCount = 10;
  224. iMaxConnCount *= 1000;
  225. SetAppState(ST_STARTING);
  226. m_hasError = FALSE;
  227. m_dwBeginTickCount = 0;
  228. m_dwTimeconsuming = 0;
  229. m_llTotalReceived = 0;
  230. m_llTotalSent = 0;
  231. m_llExpectReceived = (LONGLONG)m_iTestTimes * (LONGLONG)m_iSocketCount * (LONGLONG)m_iDataLen;
  232. BOOL isOK = FALSE;
  233. m_Agent->SetWorkerThreadCount(m_iThreadCount);
  234. m_Agent->SetMaxConnectionCount(iMaxConnCount);
  235. m_Agent->SetSendPolicy(enSendPolicy);
  236. if(m_Agent->Start(LOCAL_ADDRESS, FALSE))
  237. {
  238. for(int i = 0; i < m_iSocketCount; i++)
  239. {
  240. if(m_Agent->Connect(m_strAddress, m_usPort))
  241. {
  242. if(i == m_iSocketCount - 1)
  243. isOK = TRUE;
  244. }
  245. else
  246. {
  247. ::LogClientStartFail(::SYS_GetLastError(), ::HP_GetSocketErrorDesc(SE_CONNECT_SERVER));
  248. break;
  249. }
  250. }
  251. }
  252. else
  253. ::LogClientStartFail(m_Agent->GetLastError(), m_Agent->GetLastErrorDesc());
  254. if(!isOK)
  255. {
  256. m_Agent->Stop();
  257. SetAppState(ST_STOPPED);
  258. return;
  259. }
  260. ::LogClientStart(m_strAddress, m_usPort);
  261. DWORD dwSendDelay = 3;
  262. CString strMsg;
  263. strMsg.Format(_T(" *** willing to send data after %d seconds ..."), dwSendDelay);
  264. ::LogMsg(strMsg);
  265. ::WaitWithMessageLoop(dwSendDelay * 500);
  266. while((int)m_connIDs.size() < m_iSocketCount)
  267. {
  268. if(m_hasError)
  269. {
  270. m_Agent->Stop();
  271. SetAppState(ST_STOPPED);
  272. return;
  273. }
  274. else
  275. ::WaitWithMessageLoop(1);
  276. }
  277. int iCount = m_Info.GetCount();
  278. if(iCount > 0) m_Info.SetCurSel(iCount - 1);
  279. ::WaitWithMessageLoop(dwSendDelay * 500);
  280. m_sendBuffer.Malloc(m_iDataLen, true);
  281. SetAppState(ST_STARTED);
  282. ::LogMsg(_T(" *** Go Now !"));
  283. m_dwBeginTickCount = ::TimeGetTime();
  284. BOOL bTerminated = FALSE;
  285. for(int i = 0; i < m_iTestTimes; i++)
  286. {
  287. for(int j = 0; j < m_iSocketCount; j++)
  288. {
  289. if(!m_Agent->Send(m_connIDs[j], m_sendBuffer, (int)m_sendBuffer.Size()))
  290. {
  291. ::LogClientSendFail(i + 1, j + 1, ::SYS_GetLastError(), ::HP_GetSocketErrorDesc(SE_DATA_SEND));
  292. bTerminated = TRUE;
  293. break;
  294. }
  295. }
  296. if(bTerminated)
  297. break;
  298. if(m_iTestInterv > 0 && i + 1 < m_iTestTimes)
  299. ::WaitWithMessageLoop(m_iTestInterv);
  300. }
  301. m_sendBuffer.Free();
  302. }
  303. void CClientDlg::OnBnClickedStop()
  304. {
  305. SetAppState(ST_STOPPING);
  306. m_Agent->Stop();
  307. ::WaitWithMessageLoop(100L);
  308. CString strMsg;
  309. strMsg.Format( _T(" *** Summary: expect - %lld, send - %lld, recv - %lld"),
  310. m_llExpectReceived, m_llTotalSent, m_llTotalReceived);
  311. ::LogMsg(strMsg);
  312. if(m_llExpectReceived == m_llTotalReceived)
  313. strMsg.Format(_T(" *** Success: time consuming - %u millisecond !"), m_dwTimeconsuming);
  314. else
  315. strMsg.Format(_T(" *** Fail: manual terminated ? (or data lost)"));
  316. ::LogMsg(strMsg);
  317. SetAppState(ST_STOPPED);
  318. }
  319. int CClientDlg::OnVKeyToItem(UINT nKey, CListBox* pListBox, UINT nIndex)
  320. {
  321. if(nKey == 'C')
  322. pListBox->ResetContent();
  323. return __super::OnVKeyToItem(nKey, pListBox, nIndex);
  324. }
  325. LRESULT CClientDlg::OnUserInfoMsg(WPARAM wp, LPARAM lp)
  326. {
  327. info_msg* msg = (info_msg*)wp;
  328. ::LogInfoMsg(msg);
  329. return 0;
  330. }
  331. EnHandleResult CClientDlg::OnPrepareConnect(CONNID dwConnID, SOCKET socket)
  332. {
  333. return HR_OK;
  334. }
  335. EnHandleResult CClientDlg::OnSend(CONNID dwConnID, const BYTE* pData, int iLength)
  336. {
  337. #ifdef _DEBUG2
  338. ::PostOnSend(dwConnID, pData, iLength);
  339. #endif
  340. #if (_WIN32_WINNT <= _WIN32_WINNT_WS03)
  341. ::InterlockedExchangeAdd((volatile LONG*)&m_llTotalSent, iLength);
  342. #else
  343. ::InterlockedExchangeAdd64(&m_llTotalSent, iLength);
  344. #endif
  345. return HR_OK;
  346. }
  347. EnHandleResult CClientDlg::OnReceive(CONNID dwConnID, const BYTE* pData, int iLength)
  348. {
  349. #ifdef _DEBUG2
  350. ::PostOnReceive(dwConnID, pData, iLength);
  351. #endif
  352. #if (_WIN32_WINNT <= _WIN32_WINNT_WS03)
  353. ::InterlockedExchangeAdd((volatile LONG*)&m_llTotalReceived, iLength);
  354. #else
  355. ::InterlockedExchangeAdd64(&m_llTotalReceived, iLength);
  356. #endif
  357. if(m_llTotalReceived == m_llExpectReceived)
  358. {
  359. m_dwTimeconsuming = ::GetTimeGap32(m_dwBeginTickCount);
  360. ::PostTimeConsuming(m_dwTimeconsuming);
  361. }
  362. ASSERT(m_llTotalReceived <= m_llExpectReceived);
  363. return HR_OK;
  364. }
  365. EnHandleResult CClientDlg::OnClose(CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode)
  366. {
  367. if(iErrorCode == SE_OK)
  368. ::PostOnClose(dwConnID);
  369. else
  370. {
  371. m_hasError = TRUE;
  372. ::PostOnError(dwConnID, enOperation, iErrorCode);
  373. }
  374. return HR_OK;
  375. }
  376. EnHandleResult CClientDlg::OnConnect(CONNID dwConnID)
  377. {
  378. ::PostOnConnect3(dwConnID);
  379. return HR_OK;
  380. }
  381. EnHandleResult CClientDlg::OnHandShake(CONNID dwConnID)
  382. {
  383. {
  384. CCriSecLock locallock(m_cs);
  385. m_connIDs.push_back(dwConnID);
  386. }
  387. ::PostOnHandShake(dwConnID);
  388. return HR_OK;
  389. }
  390. EnHandleResult CClientDlg::OnShutdown()
  391. {
  392. m_connIDs.clear();
  393. ::PostOnShutdown();
  394. return HR_OK;
  395. }