ServerDlg.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // ServerDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Server.h"
  5. #include "ServerDlg.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/HPSocket_UD.lib")
  12. #else
  13. #pragma comment(lib, "../../../Bin/HPSocket/x64/HPSocket_U.lib")
  14. #endif
  15. #else
  16. #ifdef _DEBUG
  17. #pragma comment(lib, "../../../Bin/HPSocket/x86/HPSocket_UD.lib")
  18. #else
  19. #pragma comment(lib, "../../../Bin/HPSocket/x86/HPSocket_U.lib")
  20. #endif
  21. #endif
  22. // CServerDlg dialog
  23. #define DEFAULT_ADDRESS _T("0.0.0.0")
  24. #define DEFAULT_PORT _T("5555")
  25. CServerDlg::CServerDlg(CWnd* pParent /*=nullptr*/)
  26. : CDialogEx(CServerDlg::IDD, pParent), m_Server(this)
  27. , m_lClientCount(0L)
  28. {
  29. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  30. }
  31. void CServerDlg::DoDataExchange(CDataExchange* pDX)
  32. {
  33. CDialogEx::DoDataExchange(pDX);
  34. DDX_Control(pDX, IDC_INFO, m_Info);
  35. DDX_Control(pDX, IDC_START, m_Start);
  36. DDX_Control(pDX, IDC_STOP, m_Stop);
  37. DDX_Control(pDX, IDC_PORT, m_Port);
  38. DDX_Control(pDX, IDC_SEND_POLICY, m_SendPolicy);
  39. DDX_Control(pDX, IDC_THREAD_COUNT, m_ThreadCount);
  40. DDX_Control(pDX, IDC_MAX_CONN_COUNT, m_MaxConnCount);
  41. }
  42. BEGIN_MESSAGE_MAP(CServerDlg, CDialogEx)
  43. ON_WM_PAINT()
  44. ON_WM_QUERYDRAGICON()
  45. ON_BN_CLICKED(IDC_START, &CServerDlg::OnBnClickedStart)
  46. ON_BN_CLICKED(IDC_STOP, &CServerDlg::OnBnClickedStop)
  47. ON_MESSAGE(USER_INFO_MSG, OnUserInfoMsg)
  48. ON_WM_VKEYTOITEM()
  49. ON_WM_CLOSE()
  50. END_MESSAGE_MAP()
  51. // CServerDlg message handlers
  52. BOOL CServerDlg::OnInitDialog()
  53. {
  54. CDialogEx::OnInitDialog();
  55. // Set the icon for this dialog. The framework does this automatically
  56. // when the application's main window is not a dialog
  57. SetIcon(m_hIcon, TRUE); // Set big icon
  58. SetIcon(m_hIcon, FALSE); // Set small icon
  59. // TODO: Add extra initialization here
  60. CString strTitle;
  61. CString strOriginTitle;
  62. m_SendPolicy.SetCurSel(0);
  63. m_ThreadCount.SetCurSel(0);
  64. m_MaxConnCount.SetCurSel(0);
  65. m_Port.SetWindowText(DEFAULT_PORT);
  66. ::SetMainWnd(this);
  67. ::SetInfoList(&m_Info);
  68. SetAppState(ST_STOPPED);
  69. return TRUE; // return TRUE unless you set the focus to a control
  70. }
  71. void CServerDlg::OnClose()
  72. {
  73. /*
  74. if(m_Server->GetState() != SS_STOPED)
  75. {
  76. this->MessageBox(_T("stop IOCP Server first, pls !"), _T("forbiddden"));
  77. return;
  78. }
  79. */
  80. ::SetMainWnd(nullptr);
  81. __super::OnClose();
  82. }
  83. // If you add a minimize button to your dialog, you will need the code below
  84. // to draw the icon. For MFC applications using the document/view model,
  85. // this is automatically done for you by the framework.
  86. void CServerDlg::OnPaint()
  87. {
  88. if (IsIconic())
  89. {
  90. CPaintDC dc(this); // device context for painting
  91. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  92. // Center icon in client rectangle
  93. int cxIcon = GetSystemMetrics(SM_CXICON);
  94. int cyIcon = GetSystemMetrics(SM_CYICON);
  95. CRect rect;
  96. GetClientRect(&rect);
  97. int x = (rect.Width() - cxIcon + 1) / 2;
  98. int y = (rect.Height() - cyIcon + 1) / 2;
  99. // Draw the icon
  100. dc.DrawIcon(x, y, m_hIcon);
  101. }
  102. else
  103. {
  104. CDialogEx::OnPaint();
  105. }
  106. }
  107. // The system calls this function to obtain the cursor to display while the user drags
  108. // the minimized window.
  109. HCURSOR CServerDlg::OnQueryDragIcon()
  110. {
  111. return static_cast<HCURSOR>(m_hIcon);
  112. }
  113. BOOL CServerDlg::PreTranslateMessage(MSG* pMsg)
  114. {
  115. if (
  116. pMsg->message == WM_KEYDOWN
  117. &&( pMsg->wParam == VK_ESCAPE
  118. || pMsg->wParam == VK_CANCEL
  119. || pMsg->wParam == VK_RETURN
  120. ))
  121. return TRUE;
  122. return CDialog::PreTranslateMessage(pMsg);
  123. }
  124. void CServerDlg::SetAppState(EnAppState state)
  125. {
  126. if(m_enState == state)
  127. return;
  128. m_enState = state;
  129. if(this->GetSafeHwnd() == nullptr)
  130. return;
  131. m_Start.EnableWindow(m_enState == ST_STOPPED);
  132. m_Stop.EnableWindow(m_enState == ST_STARTED);
  133. m_Port.EnableWindow(m_enState == ST_STOPPED);
  134. m_SendPolicy.EnableWindow(m_enState == ST_STOPPED);
  135. m_ThreadCount.EnableWindow(m_enState == ST_STOPPED);
  136. m_MaxConnCount.EnableWindow(m_enState == ST_STOPPED);
  137. }
  138. void CServerDlg::OnBnClickedStart()
  139. {
  140. CString strPort;
  141. m_Port.GetWindowText(strPort);
  142. USHORT usPort = (USHORT)_ttoi(strPort);
  143. if(usPort == 0)
  144. {
  145. MessageBox(_T("Listen Port invalid, pls check!"), _T("Params Error"), MB_OK);
  146. m_Port.SetFocus();
  147. return;
  148. }
  149. EnSendPolicy enSendPolicy = (EnSendPolicy)m_SendPolicy.GetCurSel();
  150. CString strThreadCount;
  151. m_ThreadCount.GetWindowText(strThreadCount);
  152. int iThreadCount = _ttoi(strThreadCount);
  153. if(iThreadCount == 0)
  154. iThreadCount = min((::SysGetNumberOfProcessors() * 2 + 2), 500);
  155. else if(iThreadCount < 0 || iThreadCount > 500)
  156. {
  157. m_ThreadCount.SetFocus();
  158. return;
  159. }
  160. CString strMaxConnCount;
  161. m_MaxConnCount.GetWindowText(strMaxConnCount);
  162. int iMaxConnCount = _ttoi(strMaxConnCount);
  163. if(iMaxConnCount == 0)
  164. iMaxConnCount = 10;
  165. iMaxConnCount *= 1000;
  166. SetAppState(ST_STARTING);
  167. Reset();
  168. m_Server->SetSendPolicy(enSendPolicy);
  169. m_Server->SetWorkerThreadCount(iThreadCount);
  170. m_Server->SetMaxConnectionCount(iMaxConnCount);
  171. //m_Server->SetFreeSocketObjPool(500);
  172. //m_Server->SetFreeSocketObjHold(1500);
  173. //m_Server->SetFreeBufferObjPool(2000);
  174. //m_Server->SetFreeBufferObjHold(6000);
  175. //m_Server->SetSocketListenQueue(2000);
  176. //m_Server->SetAcceptSocketCount(2000);
  177. if(m_Server->Start(DEFAULT_ADDRESS, usPort))
  178. {
  179. ::LogServerStart(DEFAULT_ADDRESS, usPort);
  180. SetAppState(ST_STARTED);
  181. }
  182. else
  183. {
  184. ::LogServerStartFail(m_Server->GetLastError(), m_Server->GetLastErrorDesc());
  185. SetAppState(ST_STOPPED);
  186. }
  187. }
  188. void CServerDlg::OnBnClickedStop()
  189. {
  190. SetAppState(ST_STOPPING);
  191. if(m_Server->Stop())
  192. {
  193. ::LogServerStop();
  194. SetAppState(ST_STOPPED);
  195. }
  196. else
  197. {
  198. ASSERT(FALSE);
  199. }
  200. }
  201. int CServerDlg::OnVKeyToItem(UINT nKey, CListBox* pListBox, UINT nIndex)
  202. {
  203. if(nKey == 'C')
  204. pListBox->ResetContent();
  205. else if(nKey == 'R')
  206. {
  207. Reset();
  208. CString strMsg;
  209. strMsg.Format( _T(" *** Reset Statics: CC - %u, TS - %lld, TR - %lld"),
  210. m_lClientCount, m_llTotalSent, m_llTotalReceived);
  211. ::LogMsg(strMsg);
  212. }
  213. return __super::OnVKeyToItem(nKey, pListBox, nIndex);
  214. }
  215. LRESULT CServerDlg::OnUserInfoMsg(WPARAM wp, LPARAM lp)
  216. {
  217. info_msg* msg = (info_msg*)wp;
  218. ::LogInfoMsg(msg);
  219. return 0;
  220. }
  221. EnHandleResult CServerDlg::OnPrepareListen(SOCKET soListen)
  222. {
  223. TCHAR szAddress[40];
  224. int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
  225. USHORT usPort;
  226. m_Server->GetListenAddress(szAddress, iAddressLen, usPort);
  227. ::PostOnPrepareListen(szAddress, usPort);
  228. return HR_OK;
  229. }
  230. EnHandleResult CServerDlg::OnSend(CONNID dwConnID, const BYTE* pData, int iLength)
  231. {
  232. #ifdef _DEBUG2
  233. ::PostOnSend(dwConnID, pData, iLength);
  234. #endif
  235. #if (_WIN32_WINNT <= _WIN32_WINNT_WS03)
  236. ::InterlockedExchangeAdd((volatile LONG*)&m_llTotalSent, iLength);
  237. #else
  238. ::InterlockedExchangeAdd64(&m_llTotalSent, iLength);
  239. #endif
  240. return HR_OK;
  241. }
  242. EnHandleResult CServerDlg::OnReceive(CONNID dwConnID, const BYTE* pData, int iLength)
  243. {
  244. #ifdef _DEBUG2
  245. ::PostOnReceive(dwConnID, pData, iLength);
  246. #endif
  247. #if (_WIN32_WINNT <= _WIN32_WINNT_WS03)
  248. ::InterlockedExchangeAdd((volatile LONG*)&m_llTotalReceived, iLength);
  249. #else
  250. ::InterlockedExchangeAdd64(&m_llTotalReceived, iLength);
  251. #endif
  252. if(m_Server->Send(dwConnID, pData, iLength))
  253. return HR_OK;
  254. else
  255. return HR_ERROR;
  256. }
  257. EnHandleResult CServerDlg::OnClose(CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode)
  258. {
  259. iErrorCode == SE_OK ? ::PostOnClose(dwConnID) :
  260. ::PostOnError(dwConnID, enOperation, iErrorCode);
  261. Statistics();
  262. return HR_OK;
  263. }
  264. EnHandleResult CServerDlg::OnAccept(CONNID dwConnID, SOCKET soClient)
  265. {
  266. if(m_lClientCount == 0)
  267. {
  268. CCriSecLock lock(m_cs);
  269. if(m_lClientCount == 0)
  270. {
  271. Reset(FALSE);
  272. }
  273. }
  274. ::InterlockedIncrement(&m_lClientCount);
  275. ::PostOnAccept2(dwConnID);
  276. return HR_OK;
  277. }
  278. EnHandleResult CServerDlg::OnShutdown()
  279. {
  280. ::PostOnShutdown();
  281. return HR_OK;
  282. }
  283. void CServerDlg::Statistics()
  284. {
  285. if(m_lClientCount > 0)
  286. {
  287. CCriSecLock lock(m_cs);
  288. if(m_lClientCount > 0)
  289. {
  290. ::InterlockedDecrement(&m_lClientCount);
  291. if(m_lClientCount == 0)
  292. {
  293. ::WaitWithMessageLoop(600L);
  294. ::PostServerStatics((LONGLONG)m_llTotalSent, (LONGLONG)m_llTotalReceived);
  295. }
  296. }
  297. }
  298. }
  299. void CServerDlg::Reset(BOOL bResetClientCount)
  300. {
  301. if(bResetClientCount)
  302. m_lClientCount = 0L;
  303. m_llTotalSent = 0L;
  304. m_llTotalReceived = 0L;
  305. }