ClientTestDlg.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // ClientTestDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "ClientTest.h"
  5. #include "ClientTestDlg.h"
  6. #include "struct_def.h"
  7. #include "msgId_def.h"
  8. #include "lzari.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #endif
  12. typedef struct
  13. {
  14. BYTE bsql;
  15. BYTE tabcount;
  16. BYTE code[100];
  17. DWORD count[100];
  18. DWORD length[100];
  19. }SENDHEAD;
  20. BOOL g_bSendOK = FALSE;
  21. BYTE* g_pData2 = NULL;
  22. BYTE *g_pData = NULL;
  23. SENDHEAD g_sendhead;
  24. DWORD g_nLeng = 0;
  25. DWORD g_nLeng2 = 0;
  26. DWORD g_ncount = 0;
  27. static void HandleClientNetEvent(IN SOCKET hSocket,
  28. IN ETransportEvent eEvent,
  29. IN void *pDataBuf,
  30. IN unsigned long nDataLen,
  31. IN int nError,
  32. IN void *pContext);
  33. // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
  34. class CAboutDlg : public CDialog
  35. {
  36. public:
  37. CAboutDlg();
  38. // 对话框数据
  39. enum { IDD = IDD_ABOUTBOX };
  40. protected:
  41. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
  42. // 实现
  43. protected:
  44. DECLARE_MESSAGE_MAP()
  45. };
  46. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  47. {
  48. }
  49. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  50. {
  51. CDialog::DoDataExchange(pDX);
  52. }
  53. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  54. END_MESSAGE_MAP()
  55. // CClientTestDlg 对话框
  56. CClientTestDlg::CClientTestDlg(CWnd* pParent /*=NULL*/)
  57. : CDialog(CClientTestDlg::IDD, pParent)
  58. {
  59. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  60. m_bIsConnected = FALSE;
  61. m_bIsLogined = FALSE;
  62. }
  63. void CClientTestDlg::DoDataExchange(CDataExchange* pDX)
  64. {
  65. CDialog::DoDataExchange(pDX);
  66. }
  67. BEGIN_MESSAGE_MAP(CClientTestDlg, CDialog)
  68. ON_WM_SYSCOMMAND()
  69. ON_WM_PAINT()
  70. ON_WM_DESTROY()
  71. ON_WM_QUERYDRAGICON()
  72. //}}AFX_MSG_MAP
  73. ON_BN_CLICKED(IDOK, &CClientTestDlg::OnBnClickedOk)
  74. ON_BN_CLICKED(IDCANCEL, &CClientTestDlg::OnBnClickedCancel)
  75. ON_BN_CLICKED(IDC_BUTTON2, &CClientTestDlg::OnBnClickedButton2)
  76. ON_BN_CLICKED(IDC_BUTTON3, &CClientTestDlg::OnBnClickedButton3)
  77. END_MESSAGE_MAP()
  78. // CClientTestDlg 消息处理程序
  79. BOOL CClientTestDlg::OnInitDialog()
  80. {
  81. CDialog::OnInitDialog();
  82. // 将“关于...”菜单项添加到系统菜单中。
  83. // IDM_ABOUTBOX 必须在系统命令范围内。
  84. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  85. ASSERT(IDM_ABOUTBOX < 0xF000);
  86. CMenu* pSysMenu = GetSystemMenu(FALSE);
  87. if (pSysMenu != NULL)
  88. {
  89. BOOL bNameValid;
  90. CString strAboutMenu;
  91. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  92. ASSERT(bNameValid);
  93. if (!strAboutMenu.IsEmpty())
  94. {
  95. pSysMenu->AppendMenu(MF_SEPARATOR);
  96. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  97. }
  98. }
  99. CClientTunnel::Transport_Init();
  100. // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  101. // 执行此操作
  102. SetIcon(m_hIcon, TRUE); // 设置大图标
  103. SetIcon(m_hIcon, FALSE); // 设置小图标
  104. // TODO: 在此添加额外的初始化代码
  105. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  106. }
  107. void CClientTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
  108. {
  109. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  110. {
  111. CAboutDlg dlgAbout;
  112. dlgAbout.DoModal();
  113. }
  114. else
  115. {
  116. CDialog::OnSysCommand(nID, lParam);
  117. }
  118. }
  119. // 如果向对话框添加最小化按钮,则需要下面的代码
  120. // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
  121. // 这将由框架自动完成。
  122. void CClientTestDlg::OnPaint()
  123. {
  124. if (IsIconic())
  125. {
  126. CPaintDC dc(this); // 用于绘制的设备上下文
  127. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  128. // 使图标在工作区矩形中居中
  129. int cxIcon = GetSystemMetrics(SM_CXICON);
  130. int cyIcon = GetSystemMetrics(SM_CYICON);
  131. CRect rect;
  132. GetClientRect(&rect);
  133. int x = (rect.Width() - cxIcon + 1) / 2;
  134. int y = (rect.Height() - cyIcon + 1) / 2;
  135. // 绘制图标
  136. dc.DrawIcon(x, y, m_hIcon);
  137. }
  138. else
  139. {
  140. CDialog::OnPaint();
  141. }
  142. }
  143. //当用户拖动最小化窗口时系统调用此函数取得光标
  144. //显示。
  145. HCURSOR CClientTestDlg::OnQueryDragIcon()
  146. {
  147. return static_cast<HCURSOR>(m_hIcon);
  148. }
  149. void CClientTestDlg::OnDestroy()
  150. {
  151. if (g_pData)
  152. delete[]g_pData;
  153. g_pData = NULL;
  154. CClientTunnel::Transport_UnInit();
  155. }
  156. static void HandleClientNetEvent(IN SOCKET hSocket,
  157. IN ETransportEvent eEvent,
  158. IN void *pDataBuf,
  159. IN unsigned long nDataLen,
  160. IN int nError,
  161. IN void *pContext)
  162. {
  163. if (nError != TRANSPORT_OK)
  164. return;
  165. CClientTestDlg *pDlg = (CClientTestDlg *)pContext;
  166. if (NULL == pDlg)
  167. return;
  168. pDlg->ProcessNetEvent(eEvent, pDataBuf, nDataLen);
  169. }
  170. void CClientTestDlg::ProcessNetEvent(int nEventType, void *pRecvMsg, DWORD dwDataLen)
  171. {
  172. if (Transport_ReadEv == nEventType)
  173. {
  174. if (NULL == pRecvMsg) return;
  175. TMessageHeader* pHeader = (TMessageHeader *)pRecvMsg;
  176. char *pDataBuf = (char *)pRecvMsg + MESSAGE_HEADER_LEN;
  177. WORD dwMessageID = pHeader->wMessageId;
  178. switch (dwMessageID)
  179. {
  180. case MSG_LOGIN_RESP:
  181. case (MSG_LOGIN_RESP + 0X2FFF) :
  182. {
  183. LOGIN_RESULT_STRU tLoginResult = { 0 };
  184. memcpy(&tLoginResult, pDataBuf, sizeof(LOGIN_RESULT_STRU));
  185. ProcessLoginResponse(&tLoginResult);
  186. break;
  187. }
  188. case 5:
  189. {
  190. break;
  191. }
  192. case 3:
  193. {
  194. break;
  195. }
  196. case MSG_CHATMESSAGE_RESP:
  197. case (MSG_CHATMESSAGE_RESP + 0X4FFF):
  198. {
  199. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf;
  200. ProcessChatMessageResponse((void *)pChatMessage);
  201. }
  202. default:
  203. {
  204. break;
  205. }
  206. }
  207. }
  208. else if (Transport_CloseEv == nEventType)
  209. {
  210. // OnDisconnect();
  211. }
  212. }
  213. //登录返回
  214. void CClientTestDlg::ProcessLoginResponse(void *pLoginResult)
  215. {
  216. if (NULL == pLoginResult) return;
  217. LOGIN_RESULT_STRU *ptLoginResult = (LOGIN_RESULT_STRU *)pLoginResult;
  218. DWORD dwConnectionID = ptLoginResult->tCommonMsg.dwConnectionID;
  219. BYTE byResult = ptLoginResult->byResult;
  220. if (LOGIN_RESULT_SUC == byResult)
  221. {
  222. m_bIsLogined = TRUE;
  223. OutputDebugString(_T("登录成功\n"));
  224. }
  225. else
  226. OutputDebugString(_T("登录失败\n"));
  227. }
  228. //边接服务器
  229. void CClientTestDlg::OnBnClickedOk()
  230. {
  231. // TODO: 在此添加控件通知处理程序代码
  232. int nResult = m_tClientTunnel.net_OpenSocket(
  233. Transport_Client,
  234. 0,
  235. HandleClientNetEvent,
  236. this);
  237. if (TRANSPORT_OK != nResult)
  238. {
  239. MessageBox(_T("网络初始化失败!"));
  240. return;
  241. }
  242. unsigned long ulIPValue = inet_addr("127.0.0.1");
  243. unsigned short usPort = 5678;
  244. if(m_tClientTunnel.net_Connect(ulIPValue, usPort) == -1)
  245. {
  246. MessageBox(_T("连接失败!"));
  247. return;
  248. }
  249. ((CButton*)GetDlgItem(IDOK))->EnableWindow(FALSE);
  250. m_bIsConnected = TRUE;
  251. }
  252. void CClientTestDlg::ProcessChatMessageResponse(void *pResponse)
  253. {
  254. //printf("Jeff:ProcessChatMessageResponse**************\n\n");//Jeff Printf;
  255. // 1.数据空,直接返回;
  256. if (NULL == pResponse)
  257. return;
  258. // 2.数据解析过程;
  259. //printf("Jeff:ProcessChatMessageResponse**************\数据解析过程\n\n");//Jeff Printf;
  260. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pResponse;
  261. int nMessageLen = pChatMessage->wMessageLen;
  262. int nMessageLen2 = pChatMessage->dwToUserID; // 在服务器中被处理了,一般使dwToUserID=wMessageLen;
  263. // Jeff.dwFromUserID==8888表示的内容是更新客户端skin界面数据,只有两处dwToUserID==211和dwToUserID==21;
  264. // Jeff.其余的大多数都是 dwFromUserID = wMessageLen = dwToUserID 三者相等;
  265. if (nMessageLen == nMessageLen2 && pChatMessage->dwFromUserID != 8888)
  266. {
  267. // 2.1.清除全局变量g_pData数据,用于保存接收的实际数据;
  268. if (g_pData)
  269. delete[]g_pData;
  270. g_pData = NULL;
  271. g_pData = new BYTE[nMessageLen];
  272. memcpy(g_pData, pChatMessage->byFileContent, nMessageLen);
  273. memcpy(&g_sendhead, pChatMessage, sizeof(g_sendhead));
  274. g_nLeng = nMessageLen;
  275. if (nMessageLen <= sizeof(DWORD))
  276. memcpy(&g_ncount, pChatMessage->byFileContent, sizeof(DWORD));
  277. }
  278. else if (nMessageLen == nMessageLen2 && pChatMessage->dwFromUserID == 8888)
  279. {
  280. if (g_pData2)
  281. delete[]g_pData2;
  282. g_pData2 = NULL;
  283. g_pData2 = new BYTE[nMessageLen2];
  284. memcpy(g_pData2, pChatMessage->byFileContent, nMessageLen2);
  285. g_nLeng2 = nMessageLen2;
  286. }
  287. else
  288. {
  289. //登录返回
  290. OutputDebugString(_T("登录返回\n"));
  291. return;
  292. }
  293. if(g_pData != NULL && _tcscmp((char*)g_pData, _T("")) != 0)
  294. {
  295. CArray<CStringArray,CStringArray> AryList;
  296. DataToArray( g_pData, g_nLeng, &AryList);
  297. for(int i=0; i<AryList.GetSize(); i++)
  298. {
  299. CString strMsg = _T("");
  300. for(int j=0; j<AryList.ElementAt(i).GetSize(); j++)
  301. {
  302. strMsg += AryList.ElementAt(i).ElementAt(j);
  303. strMsg += _T(" ");
  304. }
  305. strMsg += _T("\n");
  306. OutputDebugString(strMsg);
  307. }
  308. CString strCount = _T("");
  309. strCount.Format(_T("------------------- %d --------------------\n"), AryList.GetSize());
  310. OutputDebugString(strCount);
  311. }
  312. }
  313. void CClientTestDlg::OnBnClickedCancel()
  314. {
  315. // TODO: 在此添加控件通知处理程序代码
  316. m_tClientTunnel.net_CloseSocket();
  317. OnCancel();
  318. }
  319. void CClientTestDlg::ProcessLoginRequest()
  320. {
  321. // 1.设置通信类型MSG_LOGIN_REQ,为登陆请求;
  322. WORD wMessageId = MSG_LOGIN_REQ + 0X1FFF;
  323. // 2.填充发送数据;
  324. TLOGIN_STRU tLogonInfo = { 0 };
  325. tLogonInfo.tCommonMsg.dwConnectionID = INVALID_SOCKET;
  326. tLogonInfo.tCommonMsg.wMessageId = wMessageId;
  327. TCHAR szHostName[MAX_PATH] = {0};
  328. gethostname(szHostName, MAX_PATH);
  329. strcpy(tLogonInfo.tUserInfo.szUserName, szHostName);
  330. DWORD dwDataLen = sizeof(TLOGIN_STRU);
  331. // 3.填充数据头;
  332. TMessageHeader tHeader = { 0 };
  333. tHeader.wMessageId = wMessageId;
  334. tHeader.dwDataLen = dwDataLen;
  335. // 4.发送数据;
  336. unsigned long ulSendLen = m_tClientTunnel.net_Send((void *)&tHeader, (void *)&tLogonInfo, dwDataLen);
  337. if (ulSendLen == SOCKET_ERROR)
  338. {
  339. MessageBox(_T("发送数据失败"));
  340. return;
  341. }
  342. g_bSendOK = TRUE;
  343. }
  344. CString CClientTestDlg::newGUID()
  345. {
  346. CString str;
  347. GUID guid;
  348. CoInitialize(NULL);
  349. if (S_OK == ::CoCreateGuid(&guid))
  350. {
  351. str.Format(
  352. _T("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
  353. guid.Data1,
  354. guid.Data2,
  355. guid.Data3,
  356. guid.Data4[0], guid.Data4[1],
  357. guid.Data4[2], guid.Data4[3],
  358. guid.Data4[4], guid.Data4[5],
  359. guid.Data4[6], guid.Data4[7]);
  360. }
  361. CoUninitialize();
  362. return str;
  363. }
  364. //登录
  365. void CClientTestDlg::OnBnClickedButton2()
  366. {
  367. // TODO: 在此添加控件通知处理程序代码
  368. if(!m_bIsConnected)
  369. {
  370. MessageBox(_T("没有连接服务端"));
  371. return;
  372. }
  373. //请求登录
  374. ProcessLoginRequest();
  375. }
  376. //读取数据
  377. void CClientTestDlg::OnBnClickedButton3()
  378. {
  379. if(!m_bIsLogined)
  380. {
  381. MessageBox(_T("还没登录"));
  382. return;
  383. }
  384. // TODO: 在此添加控件通知处理程序代码
  385. ZeroMemory(&g_sendhead, sizeof(SENDHEAD));
  386. CHAR szSQL[MAX_PATH] = "status3='未取'";
  387. g_sendhead.bsql = 0;
  388. g_sendhead.code[0] = 225;
  389. g_sendhead.tabcount = 1;
  390. ProcessChatMessageRequest(szSQL, strlen(szSQL) + 1) ;
  391. /*
  392. CHAR szSQL[MAX_PATH] = "dimission='在职'";
  393. g_sendhead.bsql = 0;
  394. g_sendhead.code[0] = 5;
  395. g_sendhead.tabcount = 1;
  396. ProcessChatMessageRequest(szSQL, strlen(szSQL) + 1) ;
  397. g_sendhead.bsql = 1;
  398. CHAR szSQL[MAX_PATH] = _T("select * from dindan");
  399. int nLen = strlen(szSQL) + 1;
  400. szSQL[nLen] = '\0';
  401. ProcessChatMessageRequest((void*)szSQL, strlen(szSQL) + 1);
  402. */
  403. if(g_bSendOK == FALSE)
  404. return;
  405. }
  406. //解析消息
  407. void CClientTestDlg::DataToArray(IN BYTE *pData, IN CONST DWORD &dwLength, IN CArray<CStringArray, CStringArray>* List1array)
  408. {
  409. List1array->RemoveAll();
  410. if (dwLength == 0)return;
  411. if (g_sendhead.code[0])
  412. {
  413. BYTE *lpszOut = NULL;
  414. int nOutSize = 0;
  415. LZARI Lzari;
  416. Lzari.UnCompress(pData, dwLength, (const BYTE*&)lpszOut, nOutSize);
  417. CMemFile memfile;
  418. memfile.Attach(lpszOut, nOutSize);
  419. Lzari.Release();
  420. try
  421. {
  422. CArchive ar(&memfile, CArchive::load);
  423. List1array->SetSize(g_sendhead.count[0]);
  424. for (int i = 0; i < List1array->GetSize(); i++)
  425. {
  426. List1array->ElementAt(i).Serialize(ar);
  427. }
  428. ar.Close();
  429. memfile.Detach();
  430. }
  431. catch (CException* e)
  432. {
  433. e->ReportError();
  434. }
  435. }
  436. else
  437. {
  438. CMemFile memfile;
  439. memfile.Attach(pData, dwLength);
  440. CArchive ar(&memfile, CArchive::load);
  441. List1array->SetSize(g_sendhead.count[0]);
  442. for (int ii = 0; ii < List1array->GetSize(); ii++)
  443. List1array->ElementAt(ii).Serialize(ar);
  444. ar.Close();
  445. memfile.Detach();
  446. }
  447. }
  448. //发送数据
  449. BOOL CClientTestDlg::ProcessChatMessageRequest(void *szDataBuf, int nDataLen)
  450. {
  451. BOOL bRecevie = FALSE;
  452. DWORD dwFromUserID = 0;
  453. WORD wMessageId = MSG_CHATMESSAGE_REQ + 0X3FFF;
  454. DWORD dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + sizeof(TMessageHeader) + nDataLen;
  455. BYTE *pSendData = NULL;
  456. pSendData = new BYTE[dwDataLen];
  457. memset(pSendData, 0, dwDataLen);
  458. TMessageHeader* ptHeader = (TMessageHeader*)pSendData;
  459. ptHeader->wMessageId = wMessageId;
  460. ptHeader->dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + nDataLen;
  461. ptHeader->byVersion = 101;
  462. ptHeader->wHeaderFlag = MESSAGE_HEADER_FLAG;
  463. ptHeader->wReserve = 0;
  464. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU*)(pSendData + sizeof(TMessageHeader));
  465. g_sendhead.length[98] = 987123768;
  466. memcpy(pChatMessage, &g_sendhead, sizeof(g_sendhead));
  467. pChatMessage->tCommonMsg.dwConnectionID = dwFromUserID;
  468. pChatMessage->tCommonMsg.wMessageId = wMessageId;
  469. pChatMessage->dwFromUserID = dwFromUserID;
  470. pChatMessage->dwToUserID = 15;
  471. pChatMessage->wMessageLen = nDataLen;
  472. memcpy(pChatMessage->byFileContent, szDataBuf, nDataLen);
  473. // 6.发送数据;
  474. BOOL bRet = 0;
  475. unsigned long ulSendLen = m_tClientTunnel.net_Send(ptHeader, (void *)pChatMessage, dwDataLen);
  476. if (ulSendLen != SOCKET_ERROR)
  477. bRet = 1;
  478. // 7.发送完毕,释放内存;
  479. delete[] pSendData;
  480. g_bSendOK = TRUE;
  481. return bRet;
  482. }