IClientImpl.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #include "StdAfx.h"
  2. #include "IClientImpl.h"
  3. #include <comdef.h>
  4. #include <atlbase.h>
  5. #include "ThreadPool.hpp"
  6. const int AF_IPV4 = 0;
  7. const int AF_IPV6 = 1;
  8. const int SOCK_TCP = SOCK_STREAM-1;
  9. const int SOCK_UDP = SOCK_DGRAM-1;
  10. //IClientImpl* IClientImpl::m_pTcpClient[TCPCLIENTNUM] = {NULL};
  11. IClientImpl::IClientImpl():m_nMode(AF_IPV4)
  12. ,m_nSockType(SOCK_TCP)
  13. {
  14. m_bSocket = FALSE;
  15. m_bStopbeat = FALSE;
  16. m_SocketClient.SetInterface(this);
  17. }
  18. IClientImpl::~IClientImpl()
  19. {
  20. //m_SocketClient.Terminate();
  21. DisConnectServer();
  22. }
  23. void IClientImpl::GetAddress(const SockAddrIn& addrIn, CString& rString) const
  24. {
  25. TCHAR szIPAddr[MAX_PATH] = { 0 };
  26. CSocketHandle::FormatIP(szIPAddr, MAX_PATH, addrIn);
  27. rString.Format(_T("%s : %d"), szIPAddr, static_cast<int>(static_cast<UINT>(ntohs(addrIn.GetPort()))) );
  28. }
  29. void IClientImpl::AppendText(LPCTSTR lpszFormat, ...)
  30. {
  31. // if ( !::IsWindow(m_ctlMsgList.GetSafeHwnd()) ) return;
  32. // TCHAR szBuffer[512];
  33. // HWND hWnd = m_ctlMsgList.GetSafeHwnd();
  34. // DWORD dwResult = 0;
  35. // if (SendMessageTimeout(hWnd, WM_GETTEXTLENGTH, 0, 0, SMTO_NORMAL, 500L, &dwResult) != 0)
  36. // {
  37. // int nLen = (int) dwResult;
  38. // if (SendMessageTimeout(hWnd, EM_SETSEL, nLen, nLen, SMTO_NORMAL, 500L, &dwResult) != 0)
  39. // {
  40. // size_t cb = 0;
  41. // va_list args;
  42. // va_start(args, lpszFormat);
  43. // ::StringCchVPrintfEx(szBuffer, 512, NULL, &cb, 0, lpszFormat, args);
  44. // va_end(args);
  45. // SendMessageTimeout(hWnd, EM_REPLACESEL, FALSE, reinterpret_cast<LPARAM>(szBuffer), SMTO_NORMAL, 500L, &dwResult);
  46. // }
  47. // }
  48. }
  49. bool IClientImpl::GetDestination(SockAddrIn& addrIn) const
  50. {
  51. CString strPort;
  52. int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
  53. return addrIn.CreateFrom(NULL, strPort, nFamily);
  54. }
  55. bool IClientImpl::SetupMCAST()
  56. {
  57. const TCHAR szIPv4MCAST[] = TEXT("239.121.1.2");
  58. const TCHAR szIPv6MCAST[] = TEXT("FF02:0:0:0:0:0:0:1"); // All Nodes local address
  59. bool result = false;
  60. if ( m_nSockType == SOCK_UDP )
  61. {
  62. if ( m_nMode == AF_IPV4 ) {
  63. result = m_SocketClient->AddMembership(szIPv4MCAST, NULL);
  64. } else {
  65. result = m_SocketClient->AddMembership(szIPv6MCAST, NULL);
  66. HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
  67. hr = hr;
  68. }
  69. }
  70. return result;
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////
  73. void IClientImpl::OnThreadBegin(CSocketHandle* pSH)
  74. {
  75. ASSERT( pSH == m_SocketClient );
  76. (pSH);
  77. CString strAddr;
  78. SockAddrIn sockAddr;
  79. m_SocketClient->GetSockName(sockAddr);
  80. GetAddress( sockAddr, strAddr );
  81. InitializeCriticalSection(&pSH->m_hClient2SrvSection);
  82. }
  83. void IClientImpl::OnThreadExit(CSocketHandle* pSH)
  84. {
  85. ASSERT( pSH == m_SocketClient );
  86. DeleteCriticalSection( &pSH->m_hClient2SrvSection );
  87. (pSH);
  88. }
  89. void IClientImpl::OnConnectionDropped(CSocketHandle* pSH)
  90. {
  91. ASSERT( pSH == m_SocketClient );
  92. (pSH);
  93. AppendText( _T("Connection lost with client.\r\n") );
  94. }
  95. void IClientImpl::OnConnectionError(CSocketHandle* pSH, DWORD dwError)
  96. {
  97. ASSERT( pSH == m_SocketClient );
  98. (pSH);
  99. _com_error err(dwError);
  100. AppendText( _T("Communication Error:\r\n%s\r\n"), err.ErrorMessage() );
  101. }
  102. void IClientImpl::OnDataReceived(CSocketHandle* pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr)
  103. {
  104. ASSERT( pSH == m_SocketClient );
  105. (pSH);
  106. if( !m_SocketClient->IsOpen() ) return;
  107. // 处理接收回来的数据;
  108. ToprocessRecivebuf(pSH,pbData,dwCount);
  109. }
  110. void IClientImpl::ToprocessRecivebuf(IN CSocketHandle *pSockHandle, IN const BYTE* pReceivebuf, IN DWORD dwReceiveSize)
  111. {
  112. DWORD dwIndexOfProcessed = 0; // 当前接收的包被处理的长度;
  113. DWORD dwOnceProcessedLen = 0; // 循环一次处理了多少当前包;
  114. STProtocolheader *pstProtocolheader;
  115. while( dwIndexOfProcessed < dwReceiveSize)
  116. {
  117. pstProtocolheader = NULL;
  118. dwOnceProcessedLen = 0;
  119. if ( pSockHandle->m_npendingSize == 0) //----------------------------------------------// 1.第一次接收或完整组包后接收剩余包;
  120. {
  121. pstProtocolheader = (STProtocolheader *)&((unsigned char *)pReceivebuf)[dwIndexOfProcessed];
  122. if( dwReceiveSize - dwIndexOfProcessed < sizeof(STProtocolheader) ) // 1.1.第一次接收或剩余数据不足一个包头;
  123. {
  124. // 如果第一次接包就没有收够包头,认为是非法包,扔掉,就是说已处理的长度dwOnceProcessedLen = 0;
  125. pSockHandle->m_npendingSize = dwReceiveSize - dwIndexOfProcessed;
  126. memcpy(pSockHandle->m_szpendingbuf, pstProtocolheader, pSockHandle->m_npendingSize);
  127. }
  128. else // 1.2.第一次接收或剩余数据大于一个包头;
  129. {
  130. // dwOnceProcessedLen = pstProtocolheader->nDataLen;
  131. if( (int)pstProtocolheader->nDataLen > dwReceiveSize - dwIndexOfProcessed ) // 1.2.1.第一次接收或剩余数据不足一个完整的协议包;
  132. {
  133. memcpy(pSockHandle->m_szpendingbuf, pstProtocolheader, dwReceiveSize - dwIndexOfProcessed);
  134. //如果第一次接包,pstProtocolheader->nDataLen大于当前包的总长,认为是非法包,扔掉(不处理这部分,不一定是非法包)
  135. if( dwIndexOfProcessed == 0 )
  136. {
  137. //组包错误,则扔掉当前包
  138. //LOG4C((LOG_NOTICE, "第一次接包,服务器pstProtocolheader->nDataLen大于当前包的总长,认为是非法包,扔掉\r\n"));
  139. }
  140. pSockHandle->m_npendingSize = dwReceiveSize - dwIndexOfProcessed;
  141. dwOnceProcessedLen = dwReceiveSize - dwIndexOfProcessed; // 该次处理的数据长度;
  142. }
  143. else // ---------------------------------------------------------------------// 1.2.2.第一次接收或剩余数据大于一个完整的协议包;
  144. {
  145. //LOG4C((LOG_NOTICE, "正常包"));
  146. pSockHandle->m_npendingSize = 0;
  147. dwOnceProcessedLen = pstProtocolheader->nDataLen; // 该次处理的数据长度;
  148. }
  149. }
  150. }
  151. else //------------------------------------------------------------------------------// 2.第n+1次接收数据包;
  152. {
  153. pstProtocolheader = (STProtocolheader *)pSockHandle->m_szpendingbuf;
  154. if( pSockHandle->m_npendingSize < sizeof(STProtocolheader) ) // 2.1.pengingbuf数据小于包头
  155. {
  156. DWORD dwRestheader = sizeof(STProtocolheader) - pSockHandle->m_npendingSize;
  157. if( dwRestheader < dwReceiveSize ) // 2.1.1.当前包大小 > 剩下包头长度,可以组成一个完整包头;
  158. {
  159. // -1.先收完一个完整的包头;
  160. memcpy( &pSockHandle->m_szpendingbuf[pSockHandle->m_npendingSize], pReceivebuf, dwRestheader);
  161. // -2.处理剩余数据;
  162. dwOnceProcessedLen = pstProtocolheader->nDataLen - pSockHandle->m_npendingSize;
  163. if( dwOnceProcessedLen <= dwReceiveSize ) // 可以组成一个完整的协议包;
  164. {
  165. memcpy( &pSockHandle->m_szpendingbuf[sizeof(STProtocolheader)],
  166. &((char *)pReceivebuf)[dwRestheader],
  167. pstProtocolheader->nDataLen - sizeof(STProtocolheader));
  168. }
  169. else // 未能组成一个完整的协议包,断续接收等待;
  170. {
  171. //int nTemp = dwReceiveSize - dwRestheader; //除去头剩余部分的长度
  172. //if ( nTemp > 0 ) //刚好是Header的长度,不用拷贝内存,所以这里加了>0的判断
  173. //{
  174. //memcpy( &pSockHandle->m_szpendingbuf[sizeof(STProtocolheader)],&((char *)pReceivebuf)[dwRestheader],nTemp );
  175. //}
  176. memcpy( &pSockHandle->m_szpendingbuf[sizeof(STProtocolheader)],&((char *)pReceivebuf)[dwRestheader],dwReceiveSize - dwRestheader);
  177. pSockHandle->m_npendingSize += dwReceiveSize;
  178. }
  179. }
  180. else //------------------------------------------------------// 2.1.2.当前包大小 <= 剩下包头长度,未能或刚好组成一个完整的包头;
  181. {
  182. memcpy( &pSockHandle->m_szpendingbuf[pSockHandle->m_npendingSize], pReceivebuf, dwReceiveSize );
  183. pSockHandle->m_npendingSize += dwReceiveSize;
  184. dwOnceProcessedLen = dwReceiveSize;
  185. }
  186. }
  187. else // --------------------------------------------------------// 2.2.pengingbuf数据大于包头;
  188. {
  189. dwOnceProcessedLen = pstProtocolheader->nDataLen - pSockHandle->m_npendingSize;
  190. if ( dwOnceProcessedLen <= dwReceiveSize ) // 可以组成一个完整的协议包;
  191. {
  192. memcpy( &pSockHandle->m_szpendingbuf[pSockHandle->m_npendingSize], pReceivebuf, dwOnceProcessedLen );
  193. pSockHandle->m_npendingSize = 0;
  194. }
  195. else // 未能组成一个完整协议包;
  196. {
  197. memcpy( &pSockHandle->m_szpendingbuf[pSockHandle->m_npendingSize], pReceivebuf, dwReceiveSize );
  198. pSockHandle->m_npendingSize += dwReceiveSize;
  199. }
  200. }
  201. }
  202. if ( dwOnceProcessedLen == 0 )
  203. {
  204. // 没有收够包头,认为是非法包,扔掉
  205. //LOG4C((LOG_NOTICE, "没有收够包头,认为是非法包,扔掉"));
  206. break;
  207. }
  208. if ( pSockHandle->m_npendingSize == 0 )
  209. {
  210. if ( pstProtocolheader->nDataLen > SOCKET_BUFFSIZE )
  211. {
  212. // 包长度超过限制,暂时不处理;
  213. //LOG4C((LOG_NOTICE, "pstProtocolheader->nDataLen超过限制"));
  214. }
  215. if(-1 == OnIntegrityPacket(pSockHandle, pstProtocolheader))
  216. {
  217. LOG4C((LOG_NOTICE, "Error OnIntegrityPacket"));
  218. break;
  219. }
  220. }
  221. dwIndexOfProcessed += dwOnceProcessedLen;
  222. }
  223. }
  224. int IClientImpl::OnIntegrityPacket(IN CSocketHandle *pSockHandle, IN void *pIntegrityPacket)
  225. {
  226. STProtocolheader *pHeader = (STProtocolheader *)pIntegrityPacket;
  227. if( pHeader == NULL || pHeader->bof != PBOF)
  228. {
  229. LOG4C((LOG_NOTICE,"协议头标识错误"));
  230. return -1;
  231. }
  232. if( pHeader->nDataLen < 0 || pHeader->nDataLen > 65535 )
  233. {
  234. LOG4C((LOG_NOTICE,"协议长度越界错误"));
  235. return -1;
  236. }
  237. unsigned int tmp = VerifyIntegrityPacket(pHeader, pHeader->nDataLen);
  238. if(tmp != pHeader->nVerify)
  239. {
  240. LOG4C((LOG_NOTICE,"协议校验值错误:len=%d,%d != %d",pHeader->nDataLen,tmp,pHeader->nVerify));
  241. return -1;
  242. }
  243. switch (pHeader->nCmd)
  244. {
  245. case CMD_HEART: // 公共命令:心跳包
  246. {
  247. // 心跳包,不处理任务事务;
  248. //LOG4C((LOG_NOTICE,"接收到的心跳包"));
  249. }
  250. break;
  251. case CMD_TOCHAT: // 公共命令:文字聊天;
  252. {
  253. STChatbody *tChatbody = (STChatbody*)pIntegrityPacket;
  254. //TRACE("------------------");
  255. //AfxMessageBox(tChatbody->szChat);
  256. //LOG4C((LOG_NOTICE,"聊天内容:%s",tChatbody->szChat));
  257. }
  258. break;
  259. case CMD_DATABASEINFO:
  260. {
  261. // 1.接到服务器返回的数据库连接信息;
  262. // 2.连接数据库;
  263. // 同时,此处做为全局开关,只有获取到了数据库信息才断续其他操作;
  264. // 定义一个全局标识 g_bSuccess;才能启动重连线程的心跳机制等等;
  265. STDatabaseInfobody *pDatabaseInfobody = (STDatabaseInfobody*)pIntegrityPacket;
  266. LOG4C((LOG_NOTICE,"数据库源:%s;数据库源端口:%s;数据库用户:%s;数据库密码:%s;数据库名称:%s",
  267. pDatabaseInfobody->szDatabaseServer,
  268. pDatabaseInfobody->szDatabaseTCPPort,
  269. pDatabaseInfobody->szDatabaseAccount,
  270. pDatabaseInfobody->szDatabasePassword,
  271. pDatabaseInfobody->szDatabaseName));
  272. }
  273. break;
  274. default:
  275. return 0;
  276. }
  277. return 0;
  278. }
  279. //unsigned int IClientImpl::VerityIntegrityPacket(IN void *pIntegrityPacket,unsigned int nPacketSize)
  280. //{
  281. // unsigned int checksum = 0;
  282. // //if ( nPacketSize <= sizeof(STProtocolheader) )
  283. // //{
  284. // // return 0;
  285. // //}
  286. // unsigned char *pBody = &((unsigned char*)pIntegrityPacket)[sizeof(STProtocolheader)];
  287. //
  288. // if( pBody )
  289. // checksum = crc32( 0, pBody, nPacketSize-sizeof(STProtocolheader) );
  290. //
  291. // return checksum;
  292. //}
  293. BOOL IClientImpl::Initialize()
  294. {
  295. TCHAR szIPAddr[MAX_PATH] = { 0 };
  296. CSocketHandle::GetLocalAddress(szIPAddr, MAX_PATH, AF_INET);
  297. AppendText(_T("Local Address (IPv4): %s\r\n"), szIPAddr);
  298. CSocketHandle::GetLocalAddress(szIPAddr, MAX_PATH, AF_INET6);
  299. AppendText(_T("Local Address (IPv6): %s\r\n"), szIPAddr);
  300. return TRUE;
  301. }
  302. void IClientImpl::StartReConnectSrvThread()
  303. {
  304. // Jeff.启用重连服务端线程.-------------------
  305. m_hRunObject = CreateEvent( NULL, TRUE, FALSE, "ClientThreadRun" );
  306. if ( m_hRunObject == NULL )
  307. {
  308. LOG4C((LOG_NOTICE,"创建事件失败"));
  309. }
  310. m_hReConnectSrvThreadHandle = CreateThread(NULL,0,ReConnectSrvThread,this,0,NULL);
  311. if ( m_hReConnectSrvThreadHandle == NULL )
  312. {
  313. LOG4C((LOG_NOTICE,"创建线程失败"));
  314. }
  315. }
  316. BOOL IClientImpl::ConnectServer(LPCTSTR strAddr, LPCTSTR strPort)
  317. {
  318. int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
  319. if ( !m_SocketClient.StartClient(NULL, strAddr, strPort, nFamily, (m_nSockType+1) ) )
  320. {
  321. m_bSocket = FALSE;
  322. LOG4C((LOG_NOTICE, "连接失败 [%s, %s, %d, %d]",strAddr, strPort, nFamily, (m_nSockType+1)));
  323. return FALSE;
  324. }
  325. else
  326. {
  327. m_bSocket = TRUE;
  328. CSocketHandle* pSH = (CSocketHandle *)m_SocketClient;
  329. pSH->m_npendingSize = 0;
  330. memset(pSH->m_szpendingbuf, 0, SOCKET_BUFFSIZE);
  331. SetupMCAST();
  332. LOG4C((LOG_NOTICE, "连接成功 [%s, %s, %d, %d]",strAddr, strPort, nFamily, (m_nSockType+1)));
  333. return TRUE;
  334. }
  335. }
  336. void IClientImpl::DisConnectServer()
  337. {
  338. if(m_hRunObject)
  339. SetEvent(m_hRunObject);
  340. if( m_hReConnectSrvThreadHandle )
  341. {
  342. if (WaitForSingleObject(m_hReConnectSrvThreadHandle,INFINITE) != WAIT_FAILED)
  343. {
  344. CloseHandle(m_hReConnectSrvThreadHandle);
  345. m_hReConnectSrvThreadHandle = NULL;
  346. }
  347. }
  348. CloseHandle( m_hRunObject );
  349. m_hRunObject = NULL;
  350. m_SocketClient.Terminate();
  351. }
  352. void IClientImpl::SendMsg(void *pMsg,const int nLen)
  353. {
  354. if ( m_SocketClient.IsOpen() )
  355. {
  356. USES_CONVERSION;
  357. if (m_nSockType == SOCK_TCP)
  358. {
  359. m_SocketClient.Write((const LPBYTE)(pMsg), nLen, NULL);
  360. }
  361. else
  362. {
  363. SockAddrIn sockAddr;
  364. GetDestination(sockAddr);
  365. m_SocketClient.Write((const LPBYTE)(pMsg), nLen, sockAddr);
  366. }
  367. }
  368. else
  369. {
  370. AfxMessageBox(_T("Socket is not connected"));
  371. }
  372. }
  373. // 静态成员函数,提供全局访问的接口
  374. //IClientImpl* IClientImpl::GetInstancePtr( int iTCPIndex )
  375. //{
  376. // if( NULL == m_pTcpClient[iTCPIndex] )
  377. // {
  378. // m_pTcpClient[iTCPIndex] = new IClientImpl();
  379. // }
  380. //
  381. // return m_pTcpClient[iTCPIndex];
  382. //}
  383. DWORD WINAPI IClientImpl::ReConnectSrvThread(LPVOID pInstance)
  384. {
  385. LOG4C((LOG_NOTICE,"重连服务器线程"));
  386. IClientImpl *pClientImpl = (IClientImpl*)pInstance;
  387. #if 0
  388. STProtocolheader tProtocolheader;
  389. tProtocolheader.nCmd = CMD_HEART;
  390. tProtocolheader.nCmdType = 1;
  391. tProtocolheader.nDataLen = sizeof(STProtocolheader);
  392. //tProtocolheader.nVerify = crc32(0, reinterpret_cast<const unsigned char*>(&tProtocolheader), sizeof(STProtocolheader));
  393. tProtocolheader.nVerify = VerityIntegrityPacket(&tProtocolheader, sizeof(STProtocolheader));
  394. #else
  395. STChatbody tChatbody;
  396. tChatbody.tPHeader.nCmd = CMD_TOCHAT;
  397. tChatbody.tPHeader.nCmdType = 0;
  398. tChatbody.tPHeader.nDataLen = sizeof(STChatbody);
  399. memset(tChatbody.szChat,99,MAX_CHATLENGTH);
  400. tChatbody.szChat[MAX_CHATLENGTH-1] = '\0';
  401. //tChatbody.tPHeader.nVerify = VerifyIntegrityPacket(&tChatbody, sizeof(STChatbody));
  402. tChatbody.GetVerify();
  403. #endif
  404. do
  405. {
  406. #if 0 // 心跳包;
  407. if( !pClientImpl->m_bStopbeat && pClientImpl->m_bSocket )
  408. {
  409. //LOG4C((LOG_NOTICE,"发送心跳包"));
  410. USES_CONVERSION;
  411. if( pClientImpl->m_SocketClient->Write((const LPBYTE)&tProtocolheader,sizeof(STProtocolheader)) == -1)
  412. pClientImpl->m_bSocket = FALSE;
  413. }
  414. #else
  415. if( !pClientImpl->m_bStopbeat && pClientImpl->m_bSocket )
  416. {
  417. //LOG4C((LOG_NOTICE,"发送心跳包"));
  418. //USES_CONVERSION;
  419. if( pClientImpl->m_SocketClient->Write((const LPBYTE)&tChatbody,sizeof(STChatbody)) == -1)
  420. pClientImpl->m_bSocket = FALSE;
  421. }
  422. #endif
  423. // 检测连接状态;
  424. if ( pClientImpl->m_bSocket == FALSE )
  425. {
  426. if ( pClientImpl->m_SocketClient->IsOpen() == TRUE )
  427. {
  428. pClientImpl->m_SocketClient.Terminate();
  429. }
  430. if ( FALSE == pClientImpl->ConnectServer(_T(g_szServerIP),_T(g_szCmdPort)))
  431. {
  432. LOG4C((LOG_NOTICE,"重连服务器失败"));
  433. pClientImpl->m_bSocket = FALSE;
  434. }
  435. else
  436. {
  437. LOG4C((LOG_NOTICE,"重连服务器成功"));
  438. pClientImpl->m_bSocket = TRUE;
  439. }
  440. }
  441. } while( WaitForSingleObject(pClientImpl->m_hRunObject,200L) == WAIT_TIMEOUT );
  442. return 0;
  443. }
  444. #if 1
  445. void IClientImpl::SendFile(LPCTSTR lpzFileName,LPCTSTR strPort)
  446. {
  447. CFile file;
  448. CFileException e;
  449. file.Open(lpzFileName, CFile::modeRead|CFile::shareDenyNone, &e);
  450. if (e.m_cause != 0)
  451. {
  452. LOG4C((LOG_NOTICE,"读取文件失败"));
  453. file.Close();
  454. return;
  455. }
  456. // 1 发送文件信息;
  457. LOG4C((LOG_NOTICE,"发送文件信息"));
  458. STFileInfobody tfInfo;
  459. GetFileName(lpzFileName,tfInfo.szFileName);
  460. tfInfo.nFileLength = file.GetLength();
  461. //tfInfo.tPHeader.nCmd = CMD_FILEINFO_TRANSFER;
  462. //tfInfo.tPHeader.nCmdType = 1;
  463. CFileStatus FileStatus ;
  464. if (file.GetStatus(FileStatus) != FALSE)
  465. {
  466. tfInfo.tCreateDate = FileStatus.m_ctime.GetTime();
  467. tfInfo.tModifyDate = FileStatus.m_mtime.GetTime();
  468. }
  469. //tfInfo.tPHeader.nDataLen = sizeof(STFileInfobody);// - sizeof(STProtocolheader);
  470. //tfInfo.tPHeader.nVerify = crc32(0,reinterpret_cast<const unsigned char*>(&tfInfo),sizeof(STFileInfobody));
  471. //tfInfo.tPHeader.nVerify = VerifyIntegrityPacket(&tfInfo,sizeof(STFileInfobody));
  472. tfInfo.GetVerify();
  473. LOG4C((LOG_NOTICE,"文件信息:协议标识:%d,协议命令:%d,协议类型:%d,协议长度:%d,协议校验:%d,文件大小:%d",
  474. tfInfo.tPHeader.bof,
  475. tfInfo.tPHeader.nCmd,
  476. tfInfo.tPHeader.nCmdType,
  477. tfInfo.tPHeader.nDataLen,
  478. tfInfo.tPHeader.nVerify,
  479. tfInfo.nFileLength));
  480. m_bStopbeat = TRUE;
  481. if( m_bSocket )
  482. {
  483. USES_CONVERSION;
  484. if ( m_SocketClient->Write((const LPBYTE)&tfInfo,sizeof(STFileInfobody)) == -1)
  485. {
  486. LOG4C((LOG_NOTICE,"发送文件信息失败"));
  487. m_bStopbeat = FALSE;
  488. return;
  489. }
  490. }
  491. //return ;
  492. Sleep(100); // 异步传输时这里的文件传输会出现发包乱序;
  493. // 2 发送文件数据;
  494. LOG4C((LOG_NOTICE,"发送文件数据"));
  495. STFileContextbody tFileContextbody;
  496. //tFileContextbody.tPHeader.nCmd = CMD_FILECONT_TRANSFER;
  497. //tFileContextbody.tPHeader.nCmdType = 0;
  498. //tFileContextbody.tPHeader.nDataLen = sizeof(STFileContextbody);
  499. do
  500. {
  501. USES_CONVERSION;
  502. //memset(tFileContextbody.szFileContext,0,MAX_FILETRANSFERLENGTH);
  503. tFileContextbody.nFileContextLen = file.Read(tFileContextbody.szFileContext,MAX_FILETRANSFERLENGTH);
  504. //tFileContextbody.tPHeader.nVerify = VerifyIntegrityPacket(&tFileContextbody,sizeof(STFileContextbody));
  505. tFileContextbody.GetVerify();
  506. LOG4C((LOG_NOTICE,"文件内容:协议标识:%d,协议命令:%d,协议类型:%d,协议长度:%d,协议校验:%d",
  507. tFileContextbody.tPHeader.bof,
  508. tFileContextbody.tPHeader.nCmd,
  509. tFileContextbody.tPHeader.nCmdType,
  510. tFileContextbody.tPHeader.nDataLen,
  511. tFileContextbody.tPHeader.nVerify));
  512. if ( m_SocketClient->Write((const LPBYTE)&tFileContextbody,sizeof(STFileContextbody)) == -1)
  513. {
  514. LOG4C((LOG_NOTICE,"文件内容传输出错"));
  515. break;
  516. }
  517. if ( tFileContextbody.nFileContextLen < MAX_FILETRANSFERLENGTH )
  518. {
  519. break;
  520. }
  521. tfInfo.nFileLength -= MAX_FILETRANSFERLENGTH;
  522. //Sleep(200);
  523. } while (tfInfo.nFileLength);
  524. LOG4C((LOG_NOTICE,"发送文件结束"));
  525. m_bStopbeat = FALSE;
  526. }
  527. #endif