ClientDlg.cpp 6.9 KB

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