ClientDlg.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // ClientDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Client.h"
  5. #include "ClientDlg.h"
  6. #include "afxdialogex.h"
  7. // CClientDlg dialog
  8. #define DEFAULT_CONTENT _T("text to be sent")
  9. #define DEFAULT_ADDRESS _T("127.0.0.1")
  10. #define DEFAULT_PORT _T("5555")
  11. CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
  12. : CDialogEx(CClientDlg::IDD, pParent), m_Client(this)
  13. , m_sslInitializer(SSL_SM_CLIENT, g_c_iVerifyMode, g_c_lpszPemCertFile, g_c_lpszPemKeyFile, g_c_lpszKeyPasswod, g_c_lpszCAPemCertFileOrPath)
  14. {
  15. VERIFY(m_sslInitializer.IsValid());
  16. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  17. }
  18. void CClientDlg::DoDataExchange(CDataExchange* pDX)
  19. {
  20. CDialogEx::DoDataExchange(pDX);
  21. DDX_Control(pDX, IDC_CONTENT, m_Content);
  22. DDX_Control(pDX, IDC_SEND, m_Send);
  23. DDX_Control(pDX, IDC_INFO, m_Info);
  24. DDX_Control(pDX, IDC_ADDRESS, m_Address);
  25. DDX_Control(pDX, IDC_PORT, m_Port);
  26. DDX_Control(pDX, IDC_ASYNC, m_Async);
  27. DDX_Control(pDX, IDC_START, m_Start);
  28. DDX_Control(pDX, IDC_STOP, m_Stop);
  29. }
  30. BEGIN_MESSAGE_MAP(CClientDlg, CDialogEx)
  31. ON_WM_PAINT()
  32. ON_WM_QUERYDRAGICON()
  33. ON_BN_CLICKED(IDC_SEND, &CClientDlg::OnBnClickedSend)
  34. ON_BN_CLICKED(IDC_START, &CClientDlg::OnBnClickedStart)
  35. ON_BN_CLICKED(IDC_STOP, &CClientDlg::OnBnClickedStop)
  36. ON_MESSAGE(USER_INFO_MSG, OnUserInfoMsg)
  37. ON_WM_VKEYTOITEM()
  38. END_MESSAGE_MAP()
  39. // CClientDlg message handlers
  40. BOOL CClientDlg::OnInitDialog()
  41. {
  42. CDialogEx::OnInitDialog();
  43. // Set the icon for this dialog. The framework does this automatically
  44. // when the application's main window is not a dialog
  45. SetIcon(m_hIcon, TRUE); // Set big icon
  46. SetIcon(m_hIcon, FALSE); // Set small icon
  47. // TODO: Add extra initialization here
  48. m_Content.SetWindowText(DEFAULT_CONTENT);
  49. m_Address.SetWindowText(DEFAULT_ADDRESS);
  50. m_Port.SetWindowText(DEFAULT_PORT);
  51. m_Async.SetCheck(BST_CHECKED);
  52. ::SetMainWnd(this);
  53. ::SetInfoList(&m_Info);
  54. SetAppState(ST_STOPPED);
  55. m_bAsyncConn = FALSE;
  56. return TRUE; // return TRUE unless you set the focus to a control
  57. }
  58. // If you add a minimize button to your dialog, you will need the code below
  59. // to draw the icon. For MFC applications using the document/view model,
  60. // this is automatically done for you by the framework.
  61. void CClientDlg::OnPaint()
  62. {
  63. if (IsIconic())
  64. {
  65. CPaintDC dc(this); // device context for painting
  66. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  67. // Center icon in client rectangle
  68. int cxIcon = GetSystemMetrics(SM_CXICON);
  69. int cyIcon = GetSystemMetrics(SM_CYICON);
  70. CRect rect;
  71. GetClientRect(&rect);
  72. int x = (rect.Width() - cxIcon + 1) / 2;
  73. int y = (rect.Height() - cyIcon + 1) / 2;
  74. // Draw the icon
  75. dc.DrawIcon(x, y, m_hIcon);
  76. }
  77. else
  78. {
  79. CDialogEx::OnPaint();
  80. }
  81. }
  82. // The system calls this function to obtain the cursor to display while the user drags
  83. // the minimized window.
  84. HCURSOR CClientDlg::OnQueryDragIcon()
  85. {
  86. return static_cast<HCURSOR>(m_hIcon);
  87. }
  88. BOOL CClientDlg::PreTranslateMessage(MSG* pMsg)
  89. {
  90. if (
  91. pMsg->message == WM_KEYDOWN
  92. &&( pMsg->wParam == VK_ESCAPE
  93. || pMsg->wParam == VK_CANCEL
  94. || pMsg->wParam == VK_RETURN
  95. ))
  96. return TRUE;
  97. return CDialog::PreTranslateMessage(pMsg);
  98. }
  99. void CClientDlg::SetAppState(EnAppState state)
  100. {
  101. m_enState = state;
  102. if(this->GetSafeHwnd() == nullptr)
  103. return;
  104. m_Async.EnableWindow(m_enState == ST_STOPPED);
  105. m_Start.EnableWindow(m_enState == ST_STOPPED);
  106. m_Stop.EnableWindow(m_enState == ST_STARTED);
  107. m_Send.EnableWindow(m_enState == ST_STARTED);
  108. m_Address.EnableWindow(m_enState == ST_STOPPED);
  109. m_Port.EnableWindow(m_enState == ST_STOPPED);
  110. }
  111. void CClientDlg::OnBnClickedSend()
  112. {
  113. static DWORD SEQ = 0;
  114. USES_CONVERSION;
  115. CString strContent;
  116. m_Content.GetWindowText(strContent);
  117. LPCSTR name = "ÉËÉñС¹ÖÊÞ";
  118. LPCSTR desc = T2A((LPCTSTR)strContent);
  119. int desc_len = (int)strlen(desc) + 1;
  120. int body_len = offsetof(TPkgBody, desc) + desc_len;
  121. TPkgBody* pBody = (TPkgBody*)_alloca(body_len);
  122. memset(pBody, 0, body_len);
  123. pBody->age = 23;
  124. strcpy(pBody->name, name);
  125. strcpy(pBody->desc, desc);
  126. if(m_Client->Send((BYTE*)pBody, body_len))
  127. ::LogSend(m_Client->GetConnectionID(), strContent);
  128. else
  129. ::LogSendFail(m_Client->GetConnectionID(), ::SYS_GetLastError(), ::HP_GetSocketErrorDesc(SE_DATA_SEND));
  130. }
  131. void CClientDlg::OnBnClickedStart()
  132. {
  133. SetAppState(ST_STARTING);
  134. CString strAddress;
  135. CString strPort;
  136. m_Address.GetWindowText(strAddress);
  137. m_Port.GetWindowText(strPort);
  138. USHORT usPort = (USHORT)_ttoi(strPort);
  139. m_bAsyncConn = m_Async.GetCheck();
  140. ::LogClientStarting(strAddress, usPort);
  141. m_Client->SetMaxPackSize(0x01FFF);
  142. m_Client->SetPackHeaderFlag(0x169);
  143. if(m_Client->Start(strAddress, usPort, m_bAsyncConn))
  144. {
  145. }
  146. else
  147. {
  148. ::LogClientStartFail(m_Client->GetLastError(), m_Client->GetLastErrorDesc());
  149. SetAppState(ST_STOPPED);
  150. }
  151. }
  152. void CClientDlg::OnBnClickedStop()
  153. {
  154. SetAppState(ST_STOPPING);
  155. if(m_Client->Stop())
  156. ::LogClientStopping(m_Client->GetConnectionID());
  157. else
  158. ASSERT(FALSE);
  159. }
  160. int CClientDlg::OnVKeyToItem(UINT nKey, CListBox* pListBox, UINT nIndex)
  161. {
  162. if(nKey == 'C')
  163. pListBox->ResetContent();
  164. return __super::OnVKeyToItem(nKey, pListBox, nIndex);
  165. }
  166. LRESULT CClientDlg::OnUserInfoMsg(WPARAM wp, LPARAM lp)
  167. {
  168. info_msg* msg = (info_msg*)wp;
  169. ::LogInfoMsg(msg);
  170. return 0;
  171. }
  172. EnHandleResult CClientDlg::OnConnect(IClient* pClient)
  173. {
  174. TCHAR szAddress[40];
  175. int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
  176. USHORT usPort;
  177. pClient->GetLocalAddress(szAddress, iAddressLen, usPort);
  178. ::PostOnConnect(pClient->GetConnectionID(), szAddress, usPort);
  179. return HR_OK;
  180. }
  181. EnHandleResult CClientDlg::OnHandShake(IClient* pClient)
  182. {
  183. ::PostOnHandShake(pClient->GetConnectionID());
  184. SetAppState(ST_STARTED);
  185. return HR_OK;
  186. }
  187. EnHandleResult CClientDlg::OnSend(IClient* pClient, const BYTE* pData, int iLength)
  188. {
  189. ::PostOnSend(pClient->GetConnectionID(), pData, iLength);
  190. return HR_OK;
  191. }
  192. EnHandleResult CClientDlg::OnReceive(IClient* pClient, const BYTE* pData, int iLength)
  193. {
  194. ::PostOnReceive(pClient->GetConnectionID(), pData, iLength);
  195. return HR_OK;
  196. }
  197. EnHandleResult CClientDlg::OnClose(IClient* pClient, EnSocketOperation enOperation, int iErrorCode)
  198. {
  199. iErrorCode == SE_OK ? ::PostOnClose(pClient->GetConnectionID()) :
  200. ::PostOnError(pClient->GetConnectionID(), enOperation, iErrorCode) ;
  201. SetAppState(ST_STOPPED);
  202. return HR_OK;
  203. }