ServerDlg.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // ServerDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Server.h"
  5. #include "ServerDlg.h"
  6. #include "afxdialogex.h"
  7. // CServerDlg dialog
  8. const LPCTSTR CServerDlg::ADDRESS = _T("0.0.0.0");
  9. const USHORT CServerDlg::PORT = 5555;
  10. CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
  11. : CDialogEx(CServerDlg::IDD, pParent), m_Server(this)
  12. {
  13. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  14. }
  15. void CServerDlg::DoDataExchange(CDataExchange* pDX)
  16. {
  17. CDialogEx::DoDataExchange(pDX);
  18. DDX_Control(pDX, IDC_INFO, m_Info);
  19. DDX_Control(pDX, IDC_START, m_Start);
  20. DDX_Control(pDX, IDC_STOP, m_Stop);
  21. DDX_Control(pDX, IDC_ADDRESS, m_Address);
  22. DDX_Control(pDX, IDC_CONN_ID, m_ConnID);
  23. DDX_Control(pDX, IDC_DISCONNECT, m_DisConn);
  24. }
  25. BEGIN_MESSAGE_MAP(CServerDlg, CDialogEx)
  26. ON_WM_PAINT()
  27. ON_WM_QUERYDRAGICON()
  28. ON_BN_CLICKED(IDC_START, &CServerDlg::OnBnClickedStart)
  29. ON_BN_CLICKED(IDC_STOP, &CServerDlg::OnBnClickedStop)
  30. ON_MESSAGE(USER_INFO_MSG, OnUserInfoMsg)
  31. ON_BN_CLICKED(IDC_DISCONNECT, &CServerDlg::OnBnClickedDisconnect)
  32. ON_EN_CHANGE(IDC_CONN_ID, &CServerDlg::OnEnChangeConnId)
  33. ON_WM_VKEYTOITEM()
  34. END_MESSAGE_MAP()
  35. // CServerDlg message handlers
  36. BOOL CServerDlg::OnInitDialog()
  37. {
  38. CDialogEx::OnInitDialog();
  39. // Set the icon for this dialog. The framework does this automatically
  40. // when the application's main window is not a dialog
  41. SetIcon(m_hIcon, TRUE); // Set big icon
  42. SetIcon(m_hIcon, FALSE); // Set small icon
  43. // TODO: Add extra initialization here
  44. CString strTitle;
  45. CString strOriginTitle;
  46. GetWindowText(strOriginTitle);
  47. strTitle.Format(_T("%s - (%s:%d)"), strOriginTitle, ADDRESS, PORT);
  48. SetWindowText(strTitle);
  49. ::SetMainWnd(this);
  50. ::SetInfoList(&m_Info);
  51. SetAppState(ST_STOPPED);
  52. return TRUE; // return TRUE unless you set the focus to a control
  53. }
  54. // If you add a minimize button to your dialog, you will need the code below
  55. // to draw the icon. For MFC applications using the document/view model,
  56. // this is automatically done for you by the framework.
  57. void CServerDlg::OnPaint()
  58. {
  59. if (IsIconic())
  60. {
  61. CPaintDC dc(this); // device context for painting
  62. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  63. // Center icon in client rectangle
  64. int cxIcon = GetSystemMetrics(SM_CXICON);
  65. int cyIcon = GetSystemMetrics(SM_CYICON);
  66. CRect rect;
  67. GetClientRect(&rect);
  68. int x = (rect.Width() - cxIcon + 1) / 2;
  69. int y = (rect.Height() - cyIcon + 1) / 2;
  70. // Draw the icon
  71. dc.DrawIcon(x, y, m_hIcon);
  72. }
  73. else
  74. {
  75. CDialogEx::OnPaint();
  76. }
  77. }
  78. // The system calls this function to obtain the cursor to display while the user drags
  79. // the minimized window.
  80. HCURSOR CServerDlg::OnQueryDragIcon()
  81. {
  82. return static_cast<HCURSOR>(m_hIcon);
  83. }
  84. BOOL CServerDlg::PreTranslateMessage(MSG* pMsg)
  85. {
  86. if (
  87. pMsg->message == WM_KEYDOWN
  88. &&( pMsg->wParam == VK_ESCAPE
  89. || pMsg->wParam == VK_CANCEL
  90. || pMsg->wParam == VK_RETURN
  91. ))
  92. return TRUE;
  93. return CDialog::PreTranslateMessage(pMsg);
  94. }
  95. void CServerDlg::SetAppState(EnAppState state)
  96. {
  97. m_enState = state;
  98. if(this->GetSafeHwnd() == nullptr)
  99. return;
  100. m_Start.EnableWindow(m_enState == ST_STOPPED);
  101. m_Stop.EnableWindow(m_enState == ST_STARTED);
  102. m_Address.EnableWindow(m_enState == ST_STOPPED);
  103. m_DisConn.EnableWindow(m_enState == ST_STARTED && m_ConnID.GetWindowTextLength() > 0);
  104. }
  105. void CServerDlg::OnBnClickedStart()
  106. {
  107. //m_Server.SetWorkerThreadCount(2);
  108. //m_Server.SetMaxShutdownWaitTime(100000000);
  109. //m_Server.SetMaxDatagramSize(50);
  110. //m_Server.SetDetectAttempts(0);
  111. //m_Server.SetDetectAttempts(0);
  112. //m_Server.SetDetectInterval(0);
  113. //m_Server.SetSendPolicy(SP_SAFE);
  114. m_Address.GetWindowText(m_strAddress);
  115. m_strAddress.Trim();
  116. SetAppState(ST_STARTING);
  117. if(m_Server.Start(ADDRESS, PORT))
  118. {
  119. ::LogServerStart(ADDRESS, PORT);
  120. SetAppState(ST_STARTED);
  121. }
  122. else
  123. {
  124. ::LogServerStartFail(m_Server.GetLastError(), m_Server.GetLastErrorDesc());
  125. SetAppState(ST_STOPPED);
  126. }
  127. }
  128. void CServerDlg::OnBnClickedStop()
  129. {
  130. SetAppState(ST_STOPPING);
  131. if(m_Server.Stop())
  132. {
  133. ::LogServerStop();
  134. SetAppState(ST_STOPPED);
  135. }
  136. else
  137. {
  138. ASSERT(FALSE);
  139. }
  140. }
  141. void CServerDlg::OnBnClickedDisconnect()
  142. {
  143. CString strConnID;
  144. m_ConnID.GetWindowText(strConnID);
  145. CONNID dwConnID = (CONNID)_ttoi(strConnID);
  146. if(m_Server.Disconnect(dwConnID))
  147. ::LogDisconnect(dwConnID);
  148. else
  149. ::LogDisconnectFail(dwConnID);
  150. }
  151. void CServerDlg::OnEnChangeConnId()
  152. {
  153. m_DisConn.EnableWindow(m_enState == ST_STARTED && m_ConnID.GetWindowTextLength() > 0);
  154. }
  155. int CServerDlg::OnVKeyToItem(UINT nKey, CListBox* pListBox, UINT nIndex)
  156. {
  157. if(nKey == 'C')
  158. pListBox->ResetContent();
  159. return __super::OnVKeyToItem(nKey, pListBox, nIndex);
  160. }
  161. LRESULT CServerDlg::OnUserInfoMsg(WPARAM wp, LPARAM lp)
  162. {
  163. info_msg* msg = (info_msg*)wp;
  164. ::LogInfoMsg(msg);
  165. return 0;
  166. }
  167. EnHandleResult CServerDlg::OnPrepareListen(SOCKET soListen)
  168. {
  169. TCHAR szAddress[40];
  170. int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
  171. USHORT usPort;
  172. m_Server.GetListenAddress(szAddress, iAddressLen, usPort);
  173. ::PostOnPrepareListen(szAddress, usPort);
  174. return HR_OK;
  175. }
  176. EnHandleResult CServerDlg::OnAccept(CONNID dwConnID, const SOCKADDR_IN* pSockAddr)
  177. {
  178. BOOL bPass = TRUE;
  179. TCHAR szAddress[40];
  180. int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
  181. USHORT usPort;
  182. m_Server.GetRemoteAddress(dwConnID, szAddress, iAddressLen, usPort);
  183. if(!m_strAddress.IsEmpty())
  184. {
  185. if(m_strAddress.CompareNoCase(szAddress) == 0)
  186. bPass = FALSE;
  187. }
  188. ::PostOnAccept(dwConnID, szAddress, usPort, bPass);
  189. return bPass ? HR_OK : HR_ERROR;
  190. }
  191. EnHandleResult CServerDlg::OnSend(CONNID dwConnID, const BYTE* pData, int iLength)
  192. {
  193. //static int t = 0;
  194. //if(++t % 3 == 0) return HR_ERROR;
  195. ::PostOnSend(dwConnID, pData, iLength);
  196. return HR_OK;
  197. }
  198. EnHandleResult CServerDlg::OnReceive(CONNID dwConnID, const BYTE* pData, int iLength)
  199. {
  200. //static int t = 0;
  201. //if(++t % 3 == 0) return HR_ERROR;
  202. ::PostOnReceive(dwConnID, pData, iLength);
  203. if(m_Server.Send(dwConnID, pData, iLength))
  204. return HR_OK;
  205. else
  206. return HR_ERROR;
  207. }
  208. EnHandleResult CServerDlg::OnClose(CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode)
  209. {
  210. iErrorCode == SE_OK ? ::PostOnClose(dwConnID) :
  211. ::PostOnError(dwConnID, enOperation, iErrorCode);
  212. return HR_OK;
  213. }
  214. EnHandleResult CServerDlg::OnShutdown()
  215. {
  216. ::PostOnShutdown();
  217. return HR_OK;
  218. }