IClientImpl.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. #include "StdAfx.h"
  2. #include "IClientImpl.h"
  3. #include <comdef.h>
  4. #include <atlbase.h>
  5. #include "crc32.h"
  6. #include "ThreadPool.hpp"
  7. const int AF_IPV4 = 0;
  8. const int AF_IPV6 = 1;
  9. const int SOCK_TCP = SOCK_STREAM-1;
  10. const int SOCK_UDP = SOCK_DGRAM-1;
  11. TCHAR g_szServerIP[MAX_PATH] = _T("127.0.0.1");
  12. TCHAR g_szCmdPort[MAX_PATH] = _T("5678");
  13. SENDHEAD g_tSendhead = {0};
  14. DWORD g_dwSendCode = 0;
  15. DWORD g_dwLeng = 0;
  16. BYTE *g_pData = NULL;
  17. DWORD g_dwLeng2 = 0;
  18. BYTE *g_pData2 = NULL;
  19. DWORD g_dwcount;
  20. BOOL g_bConnLYFZ = 0;
  21. int g_conntype = 0;//正常
  22. BOOL g_bReturned = FALSE;
  23. //IClientImpl* IClientImpl::m_pTcpClient[TCPCLIENTNUM] = {NULL};
  24. void GetFileName(IN const TCHAR *pFullName,OUT TCHAR *pFileName)
  25. {
  26. TCHAR szExt[_MAX_EXT];
  27. _tsplitpath(pFullName, NULL, NULL, pFileName, szExt);
  28. _tcscat(pFileName, szExt);
  29. }
  30. unsigned int VerifyIntegrityPacket(IN void *pIntegrityPacket,IN unsigned int nPacketSize)
  31. {
  32. unsigned int checksum = 0;
  33. //if ( nPacketSize <= sizeof(STProtocolheader) )
  34. //{
  35. // return 0;
  36. //}
  37. unsigned char *pBody = &((unsigned char*)pIntegrityPacket)[sizeof(STProtocolheader)];
  38. if( pBody )
  39. checksum = crc32( 0, pBody, nPacketSize-sizeof(STProtocolheader) );
  40. return checksum;
  41. }
  42. IClientImpl::IClientImpl():m_nMode(AF_IPV4),m_nSockType(SOCK_TCP)
  43. {
  44. m_bSocket = FALSE;
  45. m_bStopbeat = FALSE;
  46. m_hRunObject = NULL;
  47. m_hReConnectSrvThreadHandle = NULL;
  48. m_SocketClient.SetInterface(this);
  49. }
  50. IClientImpl::~IClientImpl()
  51. {
  52. //m_SocketClient.Terminate();
  53. DisConnectServer();
  54. }
  55. void IClientImpl::GetAddress(const SockAddrIn& addrIn, CString& rString) const
  56. {
  57. TCHAR szIPAddr[MAX_PATH] = { 0 };
  58. CSocketHandle::FormatIP(szIPAddr, MAX_PATH, addrIn);
  59. rString.Format(_T("%s : %d"), szIPAddr, static_cast<int>(static_cast<UINT>(ntohs(addrIn.GetPort()))) );
  60. }
  61. void IClientImpl::AppendText(LPCTSTR lpszFormat, ...)
  62. {
  63. // if ( !::IsWindow(m_ctlMsgList.GetSafeHwnd()) ) return;
  64. // TCHAR szBuffer[512];
  65. // HWND hWnd = m_ctlMsgList.GetSafeHwnd();
  66. // DWORD dwResult = 0;
  67. // if (SendMessageTimeout(hWnd, WM_GETTEXTLENGTH, 0, 0, SMTO_NORMAL, 500L, &dwResult) != 0)
  68. // {
  69. // int nLen = (int) dwResult;
  70. // if (SendMessageTimeout(hWnd, EM_SETSEL, nLen, nLen, SMTO_NORMAL, 500L, &dwResult) != 0)
  71. // {
  72. // size_t cb = 0;
  73. // va_list args;
  74. // va_start(args, lpszFormat);
  75. // ::StringCchVPrintfEx(szBuffer, 512, NULL, &cb, 0, lpszFormat, args);
  76. // va_end(args);
  77. // SendMessageTimeout(hWnd, EM_REPLACESEL, FALSE, reinterpret_cast<LPARAM>(szBuffer), SMTO_NORMAL, 500L, &dwResult);
  78. // }
  79. // }
  80. }
  81. bool IClientImpl::GetDestination(SockAddrIn& addrIn) const
  82. {
  83. CString strPort;
  84. int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
  85. return addrIn.CreateFrom(NULL, strPort, nFamily);
  86. }
  87. bool IClientImpl::SetupMCAST()
  88. {
  89. const TCHAR szIPv4MCAST[] = TEXT("239.121.1.2");
  90. const TCHAR szIPv6MCAST[] = TEXT("FF02:0:0:0:0:0:0:1"); // All Nodes local address
  91. bool result = false;
  92. if ( m_nSockType == SOCK_UDP )
  93. {
  94. if ( m_nMode == AF_IPV4 ) {
  95. result = m_SocketClient->AddMembership(szIPv4MCAST, NULL);
  96. } else {
  97. result = m_SocketClient->AddMembership(szIPv6MCAST, NULL);
  98. HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
  99. hr = hr;
  100. }
  101. }
  102. return result;
  103. }
  104. ///////////////////////////////////////////////////////////////////////////////
  105. void IClientImpl::OnThreadBegin(CSocketHandle* pSH)
  106. {
  107. ASSERT( pSH == m_SocketClient );
  108. (pSH);
  109. CString strAddr;
  110. SockAddrIn sockAddr;
  111. m_SocketClient->GetSockName(sockAddr);
  112. GetAddress( sockAddr, strAddr );
  113. InitializeCriticalSection(&pSH->m_hClient2SrvSection);
  114. }
  115. void IClientImpl::OnThreadExit(CSocketHandle* pSH)
  116. {
  117. ASSERT( pSH == m_SocketClient );
  118. DeleteCriticalSection( &pSH->m_hClient2SrvSection );
  119. (pSH);
  120. }
  121. void IClientImpl::OnConnectionDropped(CSocketHandle* pSH)
  122. {
  123. ASSERT( pSH == m_SocketClient );
  124. (pSH);
  125. AppendText( _T("Connection lost with client.\r\n") );
  126. }
  127. void IClientImpl::OnConnectionError(CSocketHandle* pSH, DWORD dwError)
  128. {
  129. ASSERT( pSH == m_SocketClient );
  130. (pSH);
  131. _com_error err(dwError);
  132. AppendText( _T("Communication Error:\r\n%s\r\n"), err.ErrorMessage() );
  133. }
  134. void IClientImpl::OnDataReceived(CSocketHandle* pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr)
  135. {
  136. ASSERT( pSH == m_SocketClient );
  137. (pSH);
  138. if( !m_SocketClient->IsOpen() ) return;
  139. // 处理接收回来的数据;
  140. ToprocessRecivebuf(pSH,pbData,dwCount);
  141. }
  142. void IClientImpl::ToprocessRecivebuf(IN CSocketHandle *pSockHandle, IN const BYTE* pReceivebuf, IN DWORD dwReceiveSize)
  143. {
  144. if (NULL == pReceivebuf) return;
  145. TMessageHeader* pHeader = (TMessageHeader *)pReceivebuf;
  146. char *pDataBuf = (char *)pReceivebuf + MESSAGE_HEADER_LEN;
  147. WORD dwMessageID = pHeader->wMessageId;
  148. switch (dwMessageID)
  149. {
  150. case MSG_LOGIN_RESP:
  151. case (MSG_LOGIN_RESP + 0X2FFF) :
  152. {
  153. LOGIN_RESULT_STRU tLoginResult = { 0 };
  154. memcpy(&tLoginResult, pDataBuf, sizeof(LOGIN_RESULT_STRU));
  155. ProcessLoginResponse(&tLoginResult);
  156. break;
  157. }
  158. case MSG_USERINFO_RESP:
  159. {
  160. TUSERLIST_INFO_STRU tUserListInfo = { 0 };
  161. memcpy(&tUserListInfo, pDataBuf, sizeof(TUSERLIST_INFO_STRU));
  162. // ProcessUserListInfoResponse(&tUserListInfo);
  163. break;
  164. }
  165. case MSG_LOGOUT_RESP:
  166. {
  167. TUSERLIST_INFO_STRU tUserListInfo = { 0 };
  168. memcpy(&tUserListInfo, pDataBuf, sizeof(TUSERLIST_INFO_STRU));
  169. // ProcessLogoutResponse(&tUserListInfo);
  170. break;
  171. }
  172. case MSG_CHATMESSAGE_RESP:
  173. case (MSG_CHATMESSAGE_RESP + 0X4FFF) :
  174. {
  175. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf;
  176. ProcessChatMessageResponse_g((void *)pChatMessage);
  177. break;
  178. }
  179. default:
  180. break;
  181. }
  182. }
  183. BOOL IClientImpl::ProcessLoginRequest()
  184. {
  185. // 1.设置通信类型MSG_LOGIN_REQ,为登陆请求;
  186. WORD wMessageId = 0;
  187. wMessageId = MSG_LOGIN_REQ;
  188. BYTE *bySend = NULL;
  189. INT nDataLen = sizeof(TMessageHeader) + sizeof(TLOGIN_STRU);
  190. bySend = new BYTE[nDataLen];
  191. memset(bySend, 0, nDataLen);
  192. DWORD dwDataLen = sizeof(TLOGIN_STRU);
  193. TMessageHeader *pHeader = (TMessageHeader*)bySend;
  194. pHeader->wMessageId = wMessageId;
  195. pHeader->dwDataLen = sizeof(TLOGIN_STRU);
  196. pHeader->byVersion = 101;
  197. pHeader->wHeaderFlag = MESSAGE_HEADER_FLAG;
  198. TLOGIN_STRU *pLogonInfo = (TLOGIN_STRU*)(bySend + sizeof(TMessageHeader));
  199. pLogonInfo->tCommonMsg.dwConnectionID = 0;
  200. pLogonInfo->tCommonMsg.wMessageId = wMessageId;
  201. CHAR szHostName[MAX_PATH];
  202. gethostname(szHostName,MAX_PATH);
  203. strcpy(pLogonInfo->tUserInfo.szUserName, szHostName);
  204. #if 0
  205. TMessageHeader *pssss = (TMessageHeader*)bySend;
  206. TLOGIN_STRU *pddddd = (TLOGIN_STRU*)(bySend + sizeof(TMessageHeader));
  207. #endif
  208. unsigned long ulSendLen = m_SocketClient.Write(bySend, nDataLen);
  209. delete []bySend;
  210. bySend = NULL;
  211. if (ulSendLen == SOCKET_ERROR)
  212. {
  213. return 0;
  214. }
  215. return 1;
  216. }
  217. void IClientImpl::ProcessLoginResponse(void *pLoginResult)
  218. {
  219. if (NULL == pLoginResult) return;
  220. LOGIN_RESULT_STRU *ptLoginResult = (LOGIN_RESULT_STRU *)pLoginResult;
  221. DWORD dwConnectionID = ptLoginResult->tCommonMsg.dwConnectionID;
  222. if (LOGIN_RESULT_SUC == ptLoginResult->byResult)
  223. {
  224. OutputDebugString(_T("登陆成功~\n"));
  225. }
  226. }
  227. BOOL IClientImpl::ProcessChatMessageRequest(void *szDataBuf, int nDataLen)
  228. {
  229. // 1.获取当前与服务器连接的id;
  230. DWORD dwFromUserID = 0;
  231. // 2.设置通信类型为MSG_CHATMESSAGE_REQ;
  232. WORD wMessageId = 0;
  233. wMessageId = MSG_CHATMESSAGE_REQ;
  234. // 3.计算出欲发送的数据的长度 dwDataLen;
  235. DWORD dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + sizeof(TMessageHeader) + nDataLen ;
  236. BYTE *pSendData = new BYTE[dwDataLen];
  237. memset(pSendData, 0, dwDataLen);
  238. TMessageHeader *ptHeader = (TMessageHeader*)&pSendData;
  239. ptHeader->wMessageId = wMessageId;
  240. ptHeader->dwDataLen = dwDataLen;
  241. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU*)(pSendData + sizeof(TMessageHeader));
  242. int nYearPos = -1;
  243. if (nYearPos == -1)
  244. g_tSendhead.length[99] = nYearPos;
  245. else
  246. g_tSendhead.length[99] = nYearPos * 32142953 + 10966236;
  247. g_tSendhead.length[98] = 987123768;
  248. memcpy(pSendData, &g_tSendhead, sizeof(g_tSendhead));
  249. #ifdef ENTERPRISE_VERSION
  250. if (g_bBranchModify == 0)
  251. strcpy(pChatMessage->szFromUserName, g_branchname);
  252. #endif
  253. // 4.填充发送的数据结构;
  254. pChatMessage->tCommonMsg.dwConnectionID = dwFromUserID;
  255. pChatMessage->tCommonMsg.wMessageId = wMessageId;
  256. pChatMessage->dwFromUserID = dwFromUserID;
  257. pChatMessage->dwToUserID = g_dwSendCode;//▲▲g_nSendCode在服务端同为dwToUserID;
  258. pChatMessage->wMessageLen = nDataLen;
  259. memcpy(pChatMessage->byFileContent, szDataBuf, nDataLen);
  260. // 6.发送数据;
  261. BOOL bRet = 0;
  262. unsigned long ulSendLen = m_SocketClient->Write(pSendData, dwDataLen);
  263. if (ulSendLen != SOCKET_ERROR)
  264. {
  265. bRet = 1;
  266. }
  267. // 7.发送完毕,释放内存;
  268. delete[] pSendData;
  269. pSendData = NULL;
  270. return bRet;
  271. }
  272. BOOL IClientImpl::ProcessChatMessageRequest_g(void *szDataBuf, int nDataLen)
  273. {
  274. memset(&g_tSendhead,0,sizeof(SENDHEAD));
  275. g_bReturned = 0;
  276. DWORD dwFromUserID = 0;
  277. WORD wMessageId = MSG_CHATMESSAGE_REQ;
  278. DWORD dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + sizeof(TMessageHeader) + nDataLen;
  279. BYTE *pSendData = NULL;
  280. pSendData = new BYTE[dwDataLen];
  281. memset(pSendData, 0, dwDataLen);
  282. TMessageHeader* ptHeader = (TMessageHeader*)pSendData;
  283. ptHeader->wMessageId = wMessageId;
  284. ptHeader->dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + nDataLen;
  285. ptHeader->byVersion = 101;
  286. ptHeader->wHeaderFlag = MESSAGE_HEADER_FLAG;
  287. ptHeader->wReserve = 0;
  288. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU*)(pSendData + sizeof(TMessageHeader));
  289. g_tSendhead.length[98] = 987123768;
  290. memcpy(pChatMessage, &g_tSendhead, sizeof(g_tSendhead));
  291. pChatMessage->tCommonMsg.dwConnectionID = dwFromUserID;
  292. pChatMessage->tCommonMsg.wMessageId = wMessageId;
  293. pChatMessage->dwFromUserID = dwFromUserID;
  294. pChatMessage->dwToUserID = g_dwSendCode;
  295. pChatMessage->wMessageLen = nDataLen;
  296. memcpy(pChatMessage->byFileContent, szDataBuf, nDataLen);
  297. #if 1
  298. TMessageHeader *pssss = (TMessageHeader*)pSendData;
  299. TCHAT_MESSAGE_STRU *pddddd = (TCHAT_MESSAGE_STRU*)(pSendData + sizeof(TMessageHeader));
  300. #endif
  301. BOOL bRet = FALSE;
  302. unsigned long ulSendLen = m_SocketClient->Write(pSendData, dwDataLen);
  303. if (ulSendLen != SOCKET_ERROR)
  304. {
  305. bRet = TRUE;
  306. }
  307. if ( ulSendLen == SOCKET_ERROR )
  308. {
  309. TRACE(_T("++++++++++++++++\n"));
  310. }
  311. delete[] pSendData;
  312. pSendData = NULL;
  313. return bRet;
  314. }
  315. void IClientImpl::ProcessChatMessageResponse(void *pResponse)
  316. {
  317. if (NULL == pResponse)
  318. return;
  319. // 2.数据解析过程;
  320. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pResponse;
  321. int nMessageLen = pChatMessage->wMessageLen;
  322. int nMessageLen2 = pChatMessage->dwToUserID; // 在服务器中被处理了,一般使dwToUserID=wMessageLen;
  323. // Jeff.dwFromUserID==8888表示的内容是更新客户端skin界面数据,只有两处dwToUserID==211和dwToUserID==21;
  324. // Jeff.其余的大多数都是 dwFromUserID = wMessageLen = dwToUserID 三者相等;
  325. if (nMessageLen == nMessageLen2 && pChatMessage->dwFromUserID != 8888)
  326. {
  327. // 2.1.清除全局变量g_pData数据,用于保存接收的实际数据;
  328. if (g_pData)
  329. delete[]g_pData;
  330. g_pData = NULL;
  331. g_pData = new BYTE[nMessageLen];
  332. memcpy(g_pData, pChatMessage->byFileContent, nMessageLen);
  333. memcpy(&g_tSendhead, pChatMessage, sizeof(g_tSendhead));
  334. g_dwLeng = nMessageLen;
  335. if (nMessageLen <= sizeof(DWORD))
  336. {
  337. memcpy(&g_dwcount, pChatMessage->byFileContent, sizeof(DWORD));
  338. }
  339. }
  340. else if (nMessageLen == nMessageLen2 && pChatMessage->dwFromUserID == 8888)
  341. {
  342. if (g_pData2)
  343. delete[]g_pData2;
  344. g_pData2 = NULL;
  345. g_pData2 = new BYTE[nMessageLen2];
  346. memcpy(g_pData2, pChatMessage->byFileContent, nMessageLen2);
  347. g_dwLeng2 = nMessageLen2;
  348. }
  349. else // 登陆返回;
  350. {
  351. if (g_bConnLYFZ)
  352. {
  353. if (g_pData)
  354. delete[]g_pData;
  355. g_pData = NULL;
  356. g_pData = new BYTE[nMessageLen];
  357. memcpy(g_pData, pChatMessage->byFileContent, nMessageLen);
  358. memcpy(&g_tSendhead, pChatMessage, sizeof(g_tSendhead));
  359. g_dwLeng = nMessageLen;
  360. if (nMessageLen <= sizeof(DWORD))
  361. {
  362. memcpy(&g_dwcount, pChatMessage->byFileContent, sizeof(DWORD));
  363. }
  364. return;
  365. }
  366. if (g_conntype) // Jeff.g_conntype == 1,表示连接公司服务器的返回,用于集团版解析*.ly.com的IP地址;
  367. {
  368. char *szChatMessage = new char[nMessageLen + 1];
  369. memset(szChatMessage, 0x00, nMessageLen + 1);
  370. memcpy(szChatMessage, pChatMessage->byFileContent, nMessageLen);
  371. szChatMessage;
  372. delete[]szChatMessage;
  373. g_conntype = 0;
  374. // g_serverip=g_server;
  375. return;
  376. }
  377. if (g_pData2 == NULL)
  378. {
  379. g_pData2 = new BYTE[nMessageLen2];
  380. g_dwLeng2 = 0;
  381. }
  382. memcpy(g_pData2 + g_dwLeng2, pChatMessage->byFileContent, nMessageLen);
  383. g_dwLeng2 += nMessageLen;
  384. if (g_dwLeng2 == nMessageLen2)//已收完
  385. {
  386. }
  387. else
  388. {
  389. }
  390. }
  391. }
  392. void IClientImpl::ProcessChatMessageResponse_g(void *pResponse)
  393. {
  394. if (NULL == pResponse) return;
  395. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pResponse;
  396. int nMessageLen = pChatMessage->wMessageLen;
  397. DWORD dwToUserID = pChatMessage->dwToUserID;
  398. char *pStr = new char[nMessageLen + 1];
  399. memset(pStr, 0, nMessageLen + 1);
  400. memcpy(pStr, pChatMessage->byFileContent, nMessageLen);
  401. CString str = pStr;
  402. delete[]pStr;
  403. if (str == "账户或密码错误!")
  404. {
  405. //g_bSendOK = 0;
  406. }
  407. else
  408. {
  409. if (dwToUserID == 0 && str != "发送信息成功!")
  410. ;//g_bSendOK = 0;
  411. else
  412. ;//g_bSendOK = 1;
  413. }
  414. OutputDebugString(str + _T("\n"));
  415. #if 0
  416. int pos = str.Find("authcodexiao");
  417. if (pos != -1)
  418. {
  419. CString authcode = str.Right(str.GetLength() - pos - 12);
  420. str = str.Left(pos);
  421. char path[MAX_PATH];
  422. ::GetSystemDirectory(path, MAX_PATH);
  423. CString sysdir = path;
  424. sysdir += "\\authcode.txt";
  425. CStdioFile fp;
  426. if (fp.Open(sysdir, CFile::modeWrite | CFile::modeCreate))
  427. {
  428. fp.WriteString(authcode);
  429. fp.Close();
  430. }
  431. }
  432. #endif
  433. //g_str = str;
  434. g_bReturned = 1;
  435. }
  436. int IClientImpl::OnIntegrityPacket(IN CSocketHandle *pSockHandle, IN void *pIntegrityPacket)
  437. {
  438. STProtocolheader *pHeader = (STProtocolheader *)pIntegrityPacket;
  439. if( pHeader == NULL || pHeader->bof != PBOF)
  440. {
  441. //LOG4C((LOG_NOTICE,"协议头标识错误"));
  442. return -1;
  443. }
  444. if( pHeader->nDataLen < 0 || pHeader->nDataLen > 65535 )
  445. {
  446. //LOG4C((LOG_NOTICE,"协议长度越界错误"));
  447. return -1;
  448. }
  449. unsigned int tmp = VerifyIntegrityPacket(pHeader, pHeader->nDataLen);
  450. if(tmp != pHeader->nVerify)
  451. {
  452. //LOG4C((LOG_NOTICE,"协议校验值错误:len=%d,%d != %d",pHeader->nDataLen,tmp,pHeader->nVerify));
  453. return -1;
  454. }
  455. switch (pHeader->nCmd)
  456. {
  457. case CMD_HEART: // 公共命令:心跳包
  458. {
  459. // 心跳包,不处理任务事务;
  460. //LOG4C((LOG_NOTICE,"接收到的心跳包"));
  461. }
  462. break;
  463. case CMD_TOCHAT: // 公共命令:文字聊天;
  464. {
  465. STChatbody *tChatbody = (STChatbody*)pIntegrityPacket;
  466. //TRACE("------------------");
  467. //AfxMessageBox(tChatbody->szChat);
  468. //LOG4C((LOG_NOTICE,"聊天内容:%s",tChatbody->szChat));
  469. }
  470. break;
  471. case CMD_DATABASEINFO:
  472. {
  473. // 1.接到服务器返回的数据库连接信息;
  474. // 2.连接数据库;
  475. // 同时,此处做为全局开关,只有获取到了数据库信息才断续其他操作;
  476. // 定义一个全局标识 g_bSuccess;才能启动重连线程的心跳机制等等;
  477. STDatabaseInfobody *pDatabaseInfobody = (STDatabaseInfobody*)pIntegrityPacket;
  478. /*LOG4C((LOG_NOTICE,"数据库源:%s;数据库源端口:%s;数据库用户:%s;数据库密码:%s;数据库名称:%s",
  479. pDatabaseInfobody->szDatabaseServer,
  480. pDatabaseInfobody->szDatabaseTCPPort,
  481. pDatabaseInfobody->szDatabaseAccount,
  482. pDatabaseInfobody->szDatabasePassword,
  483. pDatabaseInfobody->szDatabaseName));*/
  484. }
  485. break;
  486. default:
  487. return 0;
  488. }
  489. return 0;
  490. }
  491. //unsigned int IClientImpl::VerityIntegrityPacket(IN void *pIntegrityPacket,unsigned int nPacketSize)
  492. //{
  493. // unsigned int checksum = 0;
  494. // //if ( nPacketSize <= sizeof(STProtocolheader) )
  495. // //{
  496. // // return 0;
  497. // //}
  498. // unsigned char *pBody = &((unsigned char*)pIntegrityPacket)[sizeof(STProtocolheader)];
  499. //
  500. // if( pBody )
  501. // checksum = crc32( 0, pBody, nPacketSize-sizeof(STProtocolheader) );
  502. //
  503. // return checksum;
  504. //}
  505. BOOL IClientImpl::Initialize()
  506. {
  507. TCHAR szIPAddr[MAX_PATH] = { 0 };
  508. CSocketHandle::GetLocalAddress(szIPAddr, MAX_PATH, AF_INET);
  509. AppendText(_T("Local Address (IPv4): %s\r\n"), szIPAddr);
  510. CSocketHandle::GetLocalAddress(szIPAddr, MAX_PATH, AF_INET6);
  511. AppendText(_T("Local Address (IPv6): %s\r\n"), szIPAddr);
  512. return TRUE;
  513. }
  514. void IClientImpl::StartReConnectSrvThread()
  515. {
  516. // Jeff.启用重连服务端线程.-------------------
  517. m_hRunObject = CreateEvent( NULL, TRUE, FALSE, _T("ClientThreadRun") );
  518. if ( m_hRunObject == NULL )
  519. {
  520. //LOG4C((LOG_NOTICE,"创建事件失败"));
  521. }
  522. m_hReConnectSrvThreadHandle = CreateThread(NULL,0,ReConnectSrvThread,this,0,NULL);
  523. if ( m_hReConnectSrvThreadHandle == NULL )
  524. {
  525. //LOG4C((LOG_NOTICE,"创建线程失败"));
  526. }
  527. }
  528. BOOL IClientImpl::ConnectServer(LPCTSTR strAddr, LPCTSTR strPort)
  529. {
  530. int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
  531. if ( !m_SocketClient.StartClient(NULL, strAddr, strPort, nFamily, (m_nSockType+1) ) )
  532. {
  533. m_bSocket = FALSE;
  534. //LOG4C((LOG_NOTICE, "连接失败 [%s, %s, %d, %d]",strAddr, strPort, nFamily, (m_nSockType+1)));
  535. return FALSE;
  536. }
  537. else
  538. {
  539. m_bSocket = TRUE;
  540. CSocketHandle* pSH = (CSocketHandle *)m_SocketClient;
  541. pSH->m_npendingSize = 0;
  542. memset(pSH->m_szpendingbuf, 0, SOCKET_BUFFSIZE);
  543. SetupMCAST();
  544. //LOG4C((LOG_NOTICE, "连接成功 [%s, %s, %d, %d]",strAddr, strPort, nFamily, (m_nSockType+1)));
  545. return TRUE;
  546. }
  547. }
  548. void IClientImpl::DisConnectServer()
  549. {
  550. if(m_hRunObject)
  551. SetEvent(m_hRunObject);
  552. if( m_hReConnectSrvThreadHandle )
  553. {
  554. if (WaitForSingleObject(m_hReConnectSrvThreadHandle,INFINITE) != WAIT_FAILED)
  555. {
  556. CloseHandle(m_hReConnectSrvThreadHandle);
  557. m_hReConnectSrvThreadHandle = NULL;
  558. }
  559. }
  560. if(m_hRunObject)
  561. CloseHandle( m_hRunObject );
  562. m_hRunObject = NULL;
  563. m_SocketClient.Terminate();
  564. }
  565. void IClientImpl::SendMsg(void *pMsg,const int nLen)
  566. {
  567. if ( m_SocketClient.IsOpen() )
  568. {
  569. USES_CONVERSION;
  570. if (m_nSockType == SOCK_TCP)
  571. {
  572. m_SocketClient.Write((const LPBYTE)(pMsg), nLen, NULL);
  573. }
  574. else
  575. {
  576. SockAddrIn sockAddr;
  577. GetDestination(sockAddr);
  578. m_SocketClient.Write((const LPBYTE)(pMsg), nLen, sockAddr);
  579. }
  580. }
  581. else
  582. {
  583. AfxMessageBox(_T("Socket is not connected"));
  584. }
  585. }
  586. // 静态成员函数,提供全局访问的接口
  587. //IClientImpl* IClientImpl::GetInstancePtr( int iTCPIndex )
  588. //{
  589. // if( NULL == m_pTcpClient[iTCPIndex] )
  590. // {
  591. // m_pTcpClient[iTCPIndex] = new IClientImpl();
  592. // }
  593. //
  594. // return m_pTcpClient[iTCPIndex];
  595. //}
  596. DWORD WINAPI IClientImpl::ReConnectSrvThread(LPVOID pInstance)
  597. {
  598. //LOG4C((LOG_NOTICE,"重连服务器线程"));
  599. IClientImpl *pClientImpl = (IClientImpl*)pInstance;
  600. #if 0
  601. STProtocolheader tProtocolheader;
  602. tProtocolheader.nCmd = CMD_HEART;
  603. tProtocolheader.nCmdType = 1;
  604. tProtocolheader.nDataLen = sizeof(STProtocolheader);
  605. //tProtocolheader.nVerify = crc32(0, reinterpret_cast<const unsigned char*>(&tProtocolheader), sizeof(STProtocolheader));
  606. tProtocolheader.nVerify = VerityIntegrityPacket(&tProtocolheader, sizeof(STProtocolheader));
  607. #else
  608. STChatbody tChatbody;
  609. tChatbody.tPHeader.nCmd = CMD_TOCHAT;
  610. tChatbody.tPHeader.nCmdType = 0;
  611. tChatbody.tPHeader.nDataLen = sizeof(STChatbody);
  612. memset(tChatbody.szChat,99,MAX_CHATLENGTH);
  613. tChatbody.szChat[MAX_CHATLENGTH-1] = '\0';
  614. //tChatbody.tPHeader.nVerify = VerifyIntegrityPacket(&tChatbody, sizeof(STChatbody));
  615. tChatbody.GetVerify();
  616. #endif
  617. do
  618. {
  619. #if 0 // 心跳包;
  620. if( !pClientImpl->m_bStopbeat && pClientImpl->m_bSocket )
  621. {
  622. //LOG4C((LOG_NOTICE,"发送心跳包"));
  623. USES_CONVERSION;
  624. if( pClientImpl->m_SocketClient->Write((const LPBYTE)&tProtocolheader,sizeof(STProtocolheader)) == -1)
  625. pClientImpl->m_bSocket = FALSE;
  626. }
  627. #else
  628. if( !pClientImpl->m_bStopbeat && pClientImpl->m_bSocket )
  629. {
  630. //LOG4C((LOG_NOTICE,"发送心跳包"));
  631. //USES_CONVERSION;
  632. if( pClientImpl->m_SocketClient->Write((const LPBYTE)&tChatbody,sizeof(STChatbody)) == -1)
  633. pClientImpl->m_bSocket = FALSE;
  634. }
  635. #endif
  636. // 检测连接状态;
  637. if ( pClientImpl->m_bSocket == FALSE )
  638. {
  639. if ( pClientImpl->m_SocketClient->IsOpen() == TRUE )
  640. {
  641. pClientImpl->m_SocketClient.Terminate();
  642. }
  643. if ( FALSE == pClientImpl->ConnectServer(g_szServerIP,g_szCmdPort))
  644. {
  645. //LOG4C((LOG_NOTICE,"重连服务器失败"));
  646. pClientImpl->m_bSocket = FALSE;
  647. }
  648. else
  649. {
  650. //LOG4C((LOG_NOTICE,"重连服务器成功"));
  651. pClientImpl->m_bSocket = TRUE;
  652. }
  653. }
  654. } while( WaitForSingleObject(pClientImpl->m_hRunObject,200L) == WAIT_TIMEOUT );
  655. return 0;
  656. }
  657. #if 1
  658. void IClientImpl::SendFile(LPCTSTR lpzFileName,LPCTSTR strPort)
  659. {
  660. CFile file;
  661. CFileException e;
  662. file.Open(lpzFileName, CFile::modeRead|CFile::shareDenyNone, &e);
  663. if (e.m_cause != 0)
  664. {
  665. //LOG4C((LOG_NOTICE,"读取文件失败"));
  666. file.Close();
  667. return;
  668. }
  669. // 1 发送文件信息;
  670. //LOG4C((LOG_NOTICE,"发送文件信息"));
  671. STFileInfobody tfInfo;
  672. //GetFileName(lpzFileName,tfInfo.szFileName);
  673. tfInfo.nFileLength = file.GetLength();
  674. //tfInfo.tPHeader.nCmd = CMD_FILEINFO_TRANSFER;
  675. //tfInfo.tPHeader.nCmdType = 1;
  676. CFileStatus FileStatus ;
  677. if (file.GetStatus(FileStatus) != FALSE)
  678. {
  679. tfInfo.tCreateDate = FileStatus.m_ctime.GetTime();
  680. tfInfo.tModifyDate = FileStatus.m_mtime.GetTime();
  681. }
  682. //tfInfo.tPHeader.nDataLen = sizeof(STFileInfobody);// - sizeof(STProtocolheader);
  683. //tfInfo.tPHeader.nVerify = crc32(0,reinterpret_cast<const unsigned char*>(&tfInfo),sizeof(STFileInfobody));
  684. //tfInfo.tPHeader.nVerify = VerifyIntegrityPacket(&tfInfo,sizeof(STFileInfobody));
  685. tfInfo.GetVerify();
  686. /*LOG4C((LOG_NOTICE,"文件信息:协议标识:%d,协议命令:%d,协议类型:%d,协议长度:%d,协议校验:%d,文件大小:%d",
  687. tfInfo.tPHeader.bof,
  688. tfInfo.tPHeader.nCmd,
  689. tfInfo.tPHeader.nCmdType,
  690. tfInfo.tPHeader.nDataLen,
  691. tfInfo.tPHeader.nVerify,
  692. tfInfo.nFileLength));*/
  693. m_bStopbeat = TRUE;
  694. if( m_bSocket )
  695. {
  696. USES_CONVERSION;
  697. if ( m_SocketClient->Write((const LPBYTE)&tfInfo,sizeof(STFileInfobody)) == -1)
  698. {
  699. //LOG4C((LOG_NOTICE,"发送文件信息失败"));
  700. m_bStopbeat = FALSE;
  701. return;
  702. }
  703. }
  704. //return ;
  705. Sleep(100); // 异步传输时这里的文件传输会出现发包乱序;
  706. // 2 发送文件数据;
  707. //LOG4C((LOG_NOTICE,"发送文件数据"));
  708. STFileContextbody tFileContextbody;
  709. //tFileContextbody.tPHeader.nCmd = CMD_FILECONT_TRANSFER;
  710. //tFileContextbody.tPHeader.nCmdType = 0;
  711. //tFileContextbody.tPHeader.nDataLen = sizeof(STFileContextbody);
  712. do
  713. {
  714. USES_CONVERSION;
  715. //memset(tFileContextbody.szFileContext,0,MAX_FILETRANSFERLENGTH);
  716. tFileContextbody.nFileContextLen = file.Read(tFileContextbody.szFileContext,MAX_FILETRANSFERLENGTH);
  717. //tFileContextbody.tPHeader.nVerify = VerifyIntegrityPacket(&tFileContextbody,sizeof(STFileContextbody));
  718. tFileContextbody.GetVerify();
  719. /*LOG4C((LOG_NOTICE,"文件内容:协议标识:%d,协议命令:%d,协议类型:%d,协议长度:%d,协议校验:%d",
  720. tFileContextbody.tPHeader.bof,
  721. tFileContextbody.tPHeader.nCmd,
  722. tFileContextbody.tPHeader.nCmdType,
  723. tFileContextbody.tPHeader.nDataLen,
  724. tFileContextbody.tPHeader.nVerify));*/
  725. if ( m_SocketClient->Write((const LPBYTE)&tFileContextbody,sizeof(STFileContextbody)) == -1)
  726. {
  727. //LOG4C((LOG_NOTICE,"文件内容传输出错"));
  728. break;
  729. }
  730. if ( tFileContextbody.nFileContextLen < MAX_FILETRANSFERLENGTH )
  731. {
  732. break;
  733. }
  734. tfInfo.nFileLength -= MAX_FILETRANSFERLENGTH;
  735. //Sleep(200);
  736. } while (tfInfo.nFileLength);
  737. //LOG4C((LOG_NOTICE,"发送文件结束"));
  738. m_bStopbeat = FALSE;
  739. }
  740. #endif