IServerImpl.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. #include "StdAfx.h"
  2. #include "IServerImpl.h"
  3. #include <comdef.h>
  4. #include <atlbase.h>
  5. #include <strsafe.h>
  6. #include "ThreadPool.hpp"
  7. #include "ClientProcess.h"
  8. // #ifdef _DEBUG
  9. // #define new DEBUG_NEW
  10. // #endif
  11. const int AF_IPV4 = 0;
  12. const int AF_IPV6 = 1;
  13. const int SOCK_TCP = SOCK_STREAM-1;
  14. const int SOCK_UDP = SOCK_DGRAM-1;
  15. namespace ServerSocketImpl
  16. {
  17. IServerImpl* g_pServerSocket = NULL;
  18. IServerImpl::IServerImpl():m_nMode(AF_IPV4)
  19. ,m_nSockType(SOCK_TCP)
  20. ,m_strPort(_T("64320"))
  21. ,m_nSocketIndex(0)
  22. {
  23. m_bStopbeat = FALSE;
  24. InitializeCriticalSection( &m_csProcessData );
  25. m_SocketServer.SetInterface(this);
  26. }
  27. IServerImpl::~IServerImpl()
  28. {
  29. m_SocketServer.Terminate();
  30. DeleteCriticalSection( &m_csProcessData );
  31. }
  32. BOOL IServerImpl::Initialize()
  33. {
  34. TCHAR szIPAddr[MAX_PATH] = { 0 };
  35. CSocketHandle::GetLocalAddress(szIPAddr, MAX_PATH, AF_INET);
  36. //AppendText(_T("Local Address (IPv4): %s\r\n"), szIPAddr);
  37. CSocketHandle::GetLocalAddress(szIPAddr, MAX_PATH, AF_INET6);
  38. //AppendText(_T("Local Address (IPv6): %s\r\n"), szIPAddr);
  39. return TRUE;
  40. }
  41. void IServerImpl::Start(IN LPCTSTR strPort,IN const int &nMode)
  42. {
  43. m_nMode = nMode;
  44. int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
  45. if (!m_SocketServer.StartServer(NULL, strPort, nFamily, (m_nSockType+1)))
  46. {
  47. //OutputDebugString(_T("\n连接服务器失败!\n"));
  48. AfxMessageBox(_T("Failed to start server."), NULL, MB_ICONSTOP);
  49. return;
  50. }
  51. //CClientProcess::GetInstance()->StartMsgWork();
  52. //SyncControls();
  53. }
  54. void IServerImpl::Stop()
  55. {
  56. if(m_hRunObject)
  57. SetEvent(m_hRunObject);
  58. if( m_hClearInvalidateSocketThread )
  59. {
  60. if (WaitForSingleObject(m_hClearInvalidateSocketThread,INFINITE) != WAIT_FAILED)
  61. {
  62. CloseHandle(m_hClearInvalidateSocketThread);
  63. m_hClearInvalidateSocketThread = NULL;
  64. }
  65. }
  66. if ( m_hRunObject )
  67. CloseHandle( m_hRunObject );
  68. m_hRunObject = NULL;
  69. m_SocketServer.Terminate();
  70. //SyncControls();
  71. }
  72. void IServerImpl::Send()
  73. {
  74. if ( m_SocketServer.IsOpen() )
  75. {
  76. CString strMsg;
  77. //m_ctlMessage.GetWindowText( strMsg );
  78. if ( strMsg.IsEmpty() )
  79. {
  80. //AppendText( _T("Please enter the message to send.\r\n") );
  81. return;
  82. }
  83. USES_CONVERSION;
  84. if (m_nSockType == SOCK_TCP)
  85. {
  86. const LPBYTE lpbData = (const LPBYTE)(T2CA(strMsg));
  87. // unsafe access to Socket list!
  88. #ifdef SOCKHANDLE_USE_OVERLAPPED
  89. const SocketContextList& sl = m_SocketServer.GetSocketList();
  90. for(SocketContextList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
  91. #else
  92. const SocketList& sl = m_SocketServer.GetSocketList();
  93. for(SocketList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
  94. #endif
  95. {
  96. CSocketHandle sockHandle;
  97. sockHandle.Attach( (*citer) );
  98. sockHandle.Write(lpbData, strMsg.GetLength(), NULL);
  99. sockHandle.Detach();
  100. }
  101. }
  102. else
  103. {
  104. SockAddrIn servAddr, sockAddr;
  105. m_SocketServer->GetSockName(servAddr);
  106. GetDestination(sockAddr);
  107. if ( servAddr != sockAddr )
  108. {
  109. m_SocketServer.Write((const LPBYTE)(T2CA(strMsg)), strMsg.GetLength(), sockAddr);
  110. }
  111. else
  112. {
  113. //AppendText( _T("Please change the port number to send message to a client.\r\n") );
  114. }
  115. }
  116. }
  117. else
  118. {
  119. AfxMessageBox(_T("Socket is not connected"));
  120. }
  121. }
  122. void IServerImpl::SendAll(CSocketHandle &sockHandle, unsigned char *pMsg, int nLength)
  123. {
  124. if ( m_SocketServer.IsOpen() )
  125. {
  126. USES_CONVERSION;
  127. if (m_nSockType == SOCK_TCP)
  128. {
  129. // unsafe access to Socket list!
  130. const LPBYTE lpbData = (const LPBYTE)(pMsg);
  131. sockHandle.Write(lpbData, nLength, NULL);
  132. }
  133. else
  134. {
  135. SockAddrIn servAddr, sockAddr;
  136. m_SocketServer->GetSockName(servAddr);
  137. GetDestination(sockAddr);
  138. if ( servAddr != sockAddr )
  139. {
  140. m_SocketServer.Write((const LPBYTE)*pMsg, nLength, sockAddr);
  141. }
  142. else
  143. {
  144. }
  145. }
  146. }
  147. else
  148. {
  149. }
  150. }
  151. int IServerImpl::OnIntegrityPacket(IN PerSocketContext &sockHandle, IN void *pIntegrityPacket)
  152. {
  153. return 0;
  154. }
  155. void IServerImpl::GetAddress(const SockAddrIn& addrIn, CString& rString) const
  156. {
  157. TCHAR szIPAddr[MAX_PATH] = { 0 };
  158. CSocketHandle::FormatIP(szIPAddr, MAX_PATH, addrIn);
  159. rString.Format(_T("%s : %d"), szIPAddr, static_cast<int>(static_cast<UINT>(ntohs(addrIn.GetPort()))) );
  160. }
  161. void IServerImpl::AppendText(LPCTSTR lpszFormat, ...)
  162. {
  163. // if ( !::IsWindow(m_ctlMsgList.GetSafeHwnd()) ) return;
  164. // TCHAR szBuffer[512];
  165. // HWND hWnd = m_ctlMsgList.GetSafeHwnd();
  166. // DWORD dwResult = 0;
  167. // if (SendMessageTimeout(hWnd, WM_GETTEXTLENGTH, 0, 0, SMTO_NORMAL, 500L, &dwResult) != 0)
  168. // {
  169. // int nLen = (int) dwResult;
  170. // if (SendMessageTimeout(hWnd, EM_SETSEL, nLen, nLen, SMTO_NORMAL, 500L, &dwResult) != 0)
  171. // {
  172. // size_t cb = 0;
  173. // va_list args;
  174. // va_start(args, lpszFormat);
  175. // ::StringCchVPrintfEx(szBuffer, 512, NULL, &cb, 0, lpszFormat, args);
  176. // va_end(args);
  177. // SendMessageTimeout(hWnd, EM_REPLACESEL, FALSE, reinterpret_cast<LPARAM>(szBuffer), SMTO_NORMAL, 500L, &dwResult);
  178. // }
  179. // }
  180. }
  181. bool IServerImpl::GetDestination(SockAddrIn& addrIn) const
  182. {
  183. CString strPort;
  184. //GetDlgItemText(IDC_SVR_PORT, strPort);
  185. int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
  186. return addrIn.CreateFrom(NULL, strPort, nFamily);
  187. }
  188. bool IServerImpl::SetupMCAST()
  189. {
  190. const TCHAR szIPv4MCAST[] = TEXT("239.121.1.2");
  191. const TCHAR szIPv6MCAST[] = TEXT("FF02:0:0:0:0:0:0:1"); // All Nodes local address
  192. bool result = false;
  193. if ( m_nSockType == SOCK_UDP )
  194. {
  195. if ( m_nMode == AF_IPV4 ) {
  196. result = m_SocketServer->AddMembership(szIPv4MCAST, NULL);
  197. } else {
  198. result = m_SocketServer->AddMembership(szIPv6MCAST, NULL);
  199. HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
  200. hr = hr;
  201. }
  202. }
  203. return result;
  204. }
  205. ///////////////////////////////////////////////////////////////////////////////
  206. void IServerImpl::OnThreadBegin(CSocketHandle* pSH)
  207. {
  208. ASSERT( pSH == m_SocketServer );
  209. (pSH);
  210. CString strAddr;
  211. SockAddrIn sockAddr;
  212. m_SocketServer->GetSockName(sockAddr);
  213. GetAddress( sockAddr, strAddr );
  214. //AppendText( _T("Server Running on: %s\r\n"), strAddr);
  215. }
  216. void IServerImpl::OnThreadExit(CSocketHandle* pSH)
  217. {
  218. ASSERT( pSH == m_SocketServer );
  219. (pSH);
  220. //AppendText( _T("Server Down!\r\n"));
  221. }
  222. void IServerImpl::OnConnectionFailure(CSocketHandle* pSH, SOCKET newSocket)
  223. {
  224. ASSERT( pSH == m_SocketServer );
  225. (pSH);
  226. CString strAddr;
  227. CSocketHandle sockHandle;
  228. SockAddrIn sockAddr;
  229. if (newSocket != INVALID_SOCKET)
  230. {
  231. sockHandle.Attach( newSocket );
  232. sockHandle.GetPeerName( sockAddr );
  233. GetAddress( sockAddr, strAddr );
  234. sockHandle.Close();
  235. //AppendText( _T("Connection abandoned: %s\r\n"), strAddr );
  236. //LOG4C_NO_FILENUM((LOG_NOTICE,"Connection abandoned:%s",strAddr));
  237. //OutputDebugString(_T("\n客户端连接中断:"));
  238. //OutputDebugString(strAddr);
  239. //OutputDebugString(_T("\n\n"));
  240. }
  241. else
  242. {
  243. //OutputDebugString(_T("\n客户端连接中断,不是一个有效的套接字:"));
  244. //OutputDebugString(strAddr);
  245. //OutputDebugString(_T("\n\n"));
  246. //AppendText( _T("Connection abandoned. Not a valid socket.\r\n"), strAddr );
  247. //LOG4C_NO_FILENUM((LOG_NOTICE,"Connection abandoned. Not a valid socket:%s",strAddr));
  248. }
  249. }
  250. void IServerImpl::OnAddConnection(CSocketHandle* pSH, SOCKET newSocket)
  251. {
  252. ASSERT( pSH == m_SocketServer );
  253. (pSH);
  254. CString strAddr;
  255. CSocketHandle sockHandle;
  256. SockAddrIn sockAddr;
  257. sockHandle.Attach( newSocket );
  258. sockHandle.GetPeerName( sockAddr );
  259. GetAddress( sockAddr, strAddr );
  260. sockHandle.Detach();
  261. OutputDebugString(_T("\n新的连接接入:"));
  262. OutputDebugString(strAddr);
  263. OutputDebugString(_T("\n"));
  264. //LOG4C_NO_FILENUM((LOG_NOTICE,"新的连接接入:%s",strAddr));
  265. //AppendText( _T("Connection established: %s\r\n"), strAddr );
  266. }
  267. //void IServerImpl::OnDataReceived(CSocketHandle* pSH, const SOCKET sClient, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr, BYTE **pendingbuf, unsigned int& npendingSize, unsigned int& ncursize)
  268. void IServerImpl::OnDataReceived(LPWSAOVERLAPPED pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr)
  269. {
  270. //TCHAR szcount[10]= {0};
  271. //itoa(dwCount, szcount, 10);
  272. //OutputDebugString(szcount);
  273. //OutputDebugString("\n");
  274. // return;
  275. //ASSERT( pSH == NULL );
  276. if ( pSH == NULL )
  277. return ;
  278. //CString strAddr, strText;
  279. //USES_CONVERSION;
  280. //LPTSTR pszText = strText.GetBuffer(dwCount+1);
  281. //::StringCchCopyN(pszText, dwCount+1, A2CT(reinterpret_cast<LPCSTR>(pbData)), dwCount);
  282. //strText.ReleaseBuffer();
  283. //GetAddress( addr, strAddr );
  284. //AppendText( _T("%s>(%s)\r\n"), strAddr, strText);
  285. //return;
  286. static ULONGLONG ulCount = 0;
  287. ulCount += dwCount;
  288. static TCHAR szcount[40]= {0};
  289. _ui64toa(ulCount, szcount, 10);
  290. //OutputDebugString(szcount);
  291. //OutputDebugString("\n");
  292. LOG4C_NO_FILENUM((LOG_NOTICE,"累积:%s",szcount));
  293. if (m_nSockType == SOCK_TCP)
  294. {
  295. PerSocketContext *psio = (PerSocketContext*)pSH;
  296. EnterCriticalSection( &m_csProcessData );
  297. ToprocessRecivebuf(*psio,pbData,dwCount);
  298. LeaveCriticalSection( &m_csProcessData );
  299. }
  300. else
  301. {
  302. SockAddrIn servAddr, sockAddr;
  303. m_SocketServer->GetSockName(servAddr);
  304. GetDestination(sockAddr);
  305. if ( servAddr != sockAddr )
  306. {
  307. //m_SocketServer.Write((const LPBYTE)(T2CA(strMsg)), strMsg.GetLength(), sockAddr);
  308. }
  309. else
  310. {
  311. //AppendText( _T("Please change the port number to send message to a client.\r\n") );
  312. }
  313. }
  314. }
  315. void IServerImpl::OnConnectionDropped(CSocketHandle* pSH)
  316. {
  317. ASSERT( pSH == m_SocketServer );
  318. (pSH);
  319. CString strAddr;
  320. CSocketHandle sockHandle;
  321. SockAddrIn sockAddr;
  322. //sockHandle.Attach( pSH->GetSocket() );
  323. sockHandle.GetPeerName( sockAddr );
  324. GetAddress( sockAddr, strAddr );
  325. //sockHandle.Detach();
  326. //AppendText( _T("Connection lost with client.\r\n") );
  327. //LOG4C_NO_FILENUM((LOG_NOTICE,"Connection lost with client"));
  328. }
  329. void IServerImpl::OnConnectionError(CSocketHandle* pSH, DWORD dwError)
  330. {
  331. ASSERT( pSH == m_SocketServer );
  332. (pSH);
  333. _com_error err(dwError);
  334. //AppendText( _T("Communication Error:\r\n%s\r\n"), err.ErrorMessage() );
  335. CString strAddr;
  336. CSocketHandle sockHandle;
  337. SockAddrIn sockAddr;
  338. sockHandle.GetPeerName( sockAddr );
  339. GetAddress( sockAddr, strAddr );
  340. //LOG4C((LOG_NOTICE,"IP:%s Communication Error:%s", strAddr, err.ErrorMessage()));
  341. }
  342. #if defined(SOCKHANDLE_USE_OVERLAPPED)
  343. void IServerImpl::OnRemoveConnection(CSocketHandle* pSH, SOCKET dropSocket)
  344. {
  345. return;
  346. ASSERT( pSH == m_SocketServer );
  347. (pSH);
  348. CString strAddr;
  349. CSocketHandle sockHandle;
  350. SockAddrIn sockAddr;
  351. sockHandle.Attach( dropSocket );
  352. sockHandle.GetPeerName( sockAddr );
  353. GetAddress( sockAddr, strAddr );
  354. sockHandle.Detach();
  355. //LOG4C_NO_FILENUM((LOG_NOTICE,"Connection abandoned. Not a valid socket:%s",strAddr));
  356. }
  357. #endif
  358. DWORD IServerImpl::GetClientConnectCount()
  359. {
  360. //DWORD dwClientSize = 0;
  361. //m_SocketServer->GetConnectionCount();
  362. return m_SocketServer.GetConnectionCount();
  363. }
  364. void IServerImpl::ToprocessRecivebuf(IN PerSocketContext &sockHandle, IN const BYTE* pReceivebuf, IN DWORD dwReceiveSize)
  365. {
  366. // 当前接收的包被处理的长度;
  367. DWORD dwIndexOfProcessed = 0;
  368. // 循环一次处理了多少当前包;
  369. DWORD dwOnceProcessedLen = 0;
  370. TheProhead *ptphead;
  371. while( dwIndexOfProcessed < dwReceiveSize )
  372. {
  373. ptphead = NULL;
  374. dwOnceProcessedLen = 0;
  375. // 第一次接收或完整组包后接收剩余包;
  376. if ( sockHandle.ncurSize == 0 )
  377. {
  378. // 接收的数据不足一个包头;
  379. if ( dwReceiveSize - dwIndexOfProcessed < sizeof( TheProhead ) )
  380. {
  381. sockHandle.ncurSize = dwReceiveSize - dwIndexOfProcessed;
  382. sockHandle.SetPendingCurPack(sockHandle.ncurSize);
  383. memcpy(sockHandle.pendingbuf, pReceivebuf+dwIndexOfProcessed, sockHandle.ncurSize);
  384. dwOnceProcessedLen = sockHandle.ncurSize;
  385. //OutputDebugString("A:接收的数据不足一个包头\n");
  386. }
  387. else
  388. {// 接收的数据等于或大于一个包头;
  389. // 先组完一个包头;
  390. ptphead = (TheProhead*)(pReceivebuf+dwIndexOfProcessed);
  391. if ( dwReceiveSize - dwIndexOfProcessed > ptphead->nDataLen + sizeof(TheProhead) )
  392. {
  393. sockHandle.ncurSize = sockHandle.npendingSize = ptphead->nDataLen + sizeof(TheProhead);
  394. sockHandle.SetPendingPack(sockHandle.ncurSize);
  395. memcpy(sockHandle.pendingbuf, pReceivebuf+dwIndexOfProcessed, sockHandle.ncurSize);
  396. dwOnceProcessedLen = sockHandle.ncurSize;
  397. //OutputDebugString("A:接收的数据仍不足组成一个包头\n");
  398. }
  399. else
  400. {
  401. sockHandle.npendingSize = ptphead->nDataLen + sizeof(TheProhead);
  402. sockHandle.ncurSize = dwReceiveSize - dwIndexOfProcessed;
  403. sockHandle.SetPendingPack(sockHandle.npendingSize);
  404. memcpy(sockHandle.pendingbuf, pReceivebuf+dwIndexOfProcessed, sockHandle.ncurSize);
  405. dwOnceProcessedLen = sockHandle.ncurSize;
  406. //OutputDebugString("A:接收的数据可以组成一个包头\n");
  407. }
  408. }
  409. }
  410. else// 接收整包剩余数据;
  411. {
  412. if ( sockHandle.ncurSize >= sizeof(TheProhead) )
  413. {// 已接收的数据大于一个完整的包头;
  414. ptphead = (TheProhead*)sockHandle.pendingbuf;
  415. if ( sockHandle.npendingSize == ptphead->nDataLen + sizeof(TheProhead) )
  416. {
  417. if ( sockHandle.npendingSize - sockHandle.ncurSize > dwReceiveSize - dwIndexOfProcessed )
  418. {// 接收的数据不足组成一个完整的包;
  419. memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed, dwReceiveSize - dwIndexOfProcessed);
  420. sockHandle.ncurSize += dwReceiveSize - dwIndexOfProcessed;
  421. dwOnceProcessedLen = dwReceiveSize - dwIndexOfProcessed;
  422. //OutputDebugString("B:接收的数据不足组成一个协议包\n");
  423. }
  424. else
  425. {// 接收的数据足够组成一个完整的包;
  426. memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed, sockHandle.npendingSize - sockHandle.ncurSize);
  427. dwOnceProcessedLen = sockHandle.npendingSize - sockHandle.ncurSize;
  428. sockHandle.ncurSize = sockHandle.npendingSize;
  429. //OutputDebugString("B:接收的数据可以组成一个协议包\n");
  430. }
  431. }
  432. else
  433. {
  434. // 错误数据,丢包;
  435. LOG4C_NO_FILENUM((LOG_NOTICE, "数据包错误,丢包"));
  436. //OutputDebugString("B:数据包错误,丢包\n");
  437. }
  438. }
  439. else
  440. {// 已接收的数据不足一个包头;
  441. if ( sizeof(TheProhead) - sockHandle.ncurSize > dwReceiveSize - dwIndexOfProcessed )
  442. {// 本次接收的数据未能组成一个包头;
  443. sockHandle.ReSetPengingPack(sockHandle.ncurSize + dwReceiveSize - dwIndexOfProcessed);
  444. memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed, dwReceiveSize - dwIndexOfProcessed);
  445. sockHandle.ncurSize += dwReceiveSize - dwIndexOfProcessed;
  446. dwOnceProcessedLen = dwReceiveSize - dwIndexOfProcessed;
  447. //OutputDebugString("C:本次接收的数据未能组成一个包头\n");
  448. }
  449. else
  450. {// 本次接收的数据能组成一个包头;
  451. // 先组完一个包头;
  452. sockHandle.ReSetPengingPack(sizeof(TheProhead));
  453. memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed, sizeof(TheProhead) - sockHandle.ncurSize);
  454. dwOnceProcessedLen = sizeof(TheProhead) - sockHandle.ncurSize;
  455. sockHandle.ncurSize = sizeof(TheProhead);
  456. ptphead = (TheProhead*)sockHandle.pendingbuf;
  457. sockHandle.npendingSize = ptphead->nDataLen + sizeof(TheProhead);
  458. sockHandle.ReSetPengingPack(sockHandle.npendingSize);
  459. if ( sockHandle.npendingSize >= dwReceiveSize - dwIndexOfProcessed - dwOnceProcessedLen )
  460. {// 剩余数据能组成一个完整的包;
  461. memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed + dwOnceProcessedLen, sockHandle.npendingSize - sockHandle.ncurSize);
  462. dwOnceProcessedLen += sockHandle.npendingSize - sockHandle.ncurSize;
  463. sockHandle.ncurSize = sockHandle.npendingSize;
  464. //OutputDebugString("C:接收的数据未能组成一个协议包\n");
  465. }
  466. else
  467. {// 剩余数据未能组成一个完整的包;
  468. memcpy(sockHandle.pendingbuf + sockHandle.ncurSize, pReceivebuf + dwIndexOfProcessed + dwOnceProcessedLen, dwReceiveSize - dwIndexOfProcessed - dwOnceProcessedLen);
  469. sockHandle.ncurSize += dwReceiveSize - dwIndexOfProcessed - dwOnceProcessedLen;
  470. dwOnceProcessedLen = dwReceiveSize - dwIndexOfProcessed;
  471. //OutputDebugString("C:接收的数据可以组成一个协议包\n");
  472. }
  473. }
  474. }
  475. }
  476. if ( sockHandle.ncurSize == sockHandle.npendingSize )
  477. {
  478. //OutputDebugString("D:接收的数据已完成一次处理过程\n");
  479. TheProbody *ptpb = (TheProbody*)sockHandle.pendingbuf;
  480. CFile cf;
  481. static int a = 0;
  482. CString strFile = _T("");
  483. strFile.Format(_T("D:\\接收文件\\%d-%d.dat"), (SOCKET)sockHandle,a);
  484. if ( cf.Open(strFile, CFile::modeCreate|CFile::modeReadWrite) )
  485. {
  486. cf.Write(ptpb->szBody, ptpb->tphead.nDataLen);
  487. cf.Close();
  488. a++;
  489. }
  490. else
  491. {
  492. LOG4C_NO_FILENUM((LOG_NOTICE, "创建文件失败"));
  493. }
  494. delete []sockHandle.pendingbuf;
  495. sockHandle.pendingbuf = NULL;
  496. sockHandle.ncurSize = sockHandle.npendingSize = 0;
  497. }
  498. dwIndexOfProcessed += dwOnceProcessedLen;
  499. }
  500. }
  501. };