ClientDlg.cpp 7.3 KB

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