RawServer.cpp
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:6k
源码类别:

P2P编程

开发平台:

Visual C++

  1. // RawServer.cpp: implementation of the CRawServer class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "testBT.h"
  6. #include "RawServer.h"
  7. #include "Choker.h"
  8. #include "Connector.h"
  9. #include "DownloaderFeedback.h"
  10. #include "StorageWrapper.h"
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CRawServer::CRawServer(HANDLE hevDone, HANDLE hevUnPause, CEncrypter* pEncrypter, CChoker* pChoker, 
  15.    CConnector* pConnector, CDownloaderFeedback* pDownloaderFeedback, CStorageWrapper* pStorageWrapper)
  16. {
  17. m_pConnector = pConnector;
  18. m_pChoker = pChoker;
  19. m_pEncrypter = pEncrypter;
  20. m_hevUnPause = hevUnPause;
  21. m_hevDone = hevDone;
  22. m_pDownloaderFeedback = pDownloaderFeedback;
  23. m_pStorageWrapper = pStorageWrapper;
  24. m_hevAccept = CreateEvent(0, false, false, 0);
  25. m_hevHttpDone = 0;
  26. m_hserver = INVALID_SOCKET;
  27. }
  28. CRawServer::~CRawServer()
  29. {
  30. CloseHandle(m_hevAccept);
  31. // CloseHandle(m_hevHttpDone);
  32. if (m_hserver != INVALID_SOCKET)
  33. closesocket(m_hserver);
  34. }
  35. long CRawServer::Bind(long lminport, long lmaxport)
  36. {
  37. assert (m_hserver == INVALID_SOCKET);
  38. SOCKET hserver = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 0, WSA_FLAG_OVERLAPPED);
  39. if (hserver == INVALID_SOCKET)
  40. return false;
  41. long lport = 0;
  42. int iRet = -1;
  43. sockaddr_in addr = {0};
  44. addr.sin_family = AF_INET;
  45. addr.sin_addr.s_addr = htonl (INADDR_ANY);
  46. for (lport=lminport; lport<=lmaxport; lport++)
  47. {
  48. addr.sin_port = htons(lport);
  49. iRet = bind(hserver, (sockaddr*)&addr, sizeof(addr));
  50. if (iRet  == 0) break;
  51. // else
  52. //errorFunc("bind() server error:bind to port error: " + WSAShowError());
  53. }
  54. if (iRet != 0) 
  55. {
  56. closesocket(hserver);
  57. return false;
  58. }
  59. iRet = listen(hserver, 10);
  60. if (iRet != 0)
  61. {
  62. closesocket(hserver);
  63. return false;
  64. }
  65. iRet = WSAEventSelect(hserver, m_hevAccept, FD_ACCEPT);
  66. if (iRet == SOCKET_ERROR) 
  67. {
  68. assert(false);
  69. closesocket(m_hserver);
  70. return false;
  71. }
  72. m_hserver = hserver;
  73. return lport;
  74. }
  75. void CRawServer::listen_forever(CRerequester* pRequest)
  76. {
  77. assert (m_hserver != INVALID_SOCKET);
  78. m_pRequester = pRequest;
  79. if (IsEventSet(m_hevDone))
  80. return;
  81. m_hevHttpDone = m_pRequester->GetHttpEvent();
  82. const time_t ltimewait = 2;
  83. time_t tLast = 0, ltime = 0;
  84. time(&tLast);
  85. while (WaitForSingleObject(m_hevDone, 0) != WAIT_OBJECT_0)
  86. {
  87. time(&ltime);
  88. if (ltime < tLast)
  89. tLast = ltime;
  90. if ((ltime - tLast) >= ltimewait)
  91. {
  92. m_pEncrypter->send_keepalives();
  93. m_pChoker->round_robin();
  94. m_pEncrypter->detele_connections_died();
  95. m_pRequester->Excute();
  96. m_pConnector->uncap();
  97. m_pConnector->OnIdle();
  98. m_pDownloaderFeedback->PutDataTime();
  99. m_pStorageWrapper->OnIdle();
  100. if (!IsEventSet(m_hevUnPause))
  101. {
  102. // TRACE("rnstorage pausedrn");
  103. m_pDownloaderFeedback->ShowSystemMessage("用户暂停该任务...", CSystemMsg::eCmd);
  104. DWORD dwRet = WaitForSingleObject(m_hevUnPause, INFINITE);
  105. // TRACE("rndownload Unpausedrn");
  106. m_pDownloaderFeedback->ShowSystemMessage("用户继续该任务...", CSystemMsg::eCmd);
  107. // assert(dwRet != WAIT_OBJECT_0);
  108. }
  109. tLast = ltime;
  110. }
  111. //
  112. // compute handles to wait.
  113. //
  114. long lInx = 0;
  115. long lCount = 3 + m_pEncrypter->m_connections.size() + m_pEncrypter->m_unConnections.size();
  116. HANDLE* pHandles = new HANDLE[lCount];
  117. auto_ptr<HANDLE> autohandles(pHandles);
  118. pHandles[lInx++] = m_hevDone;
  119. pHandles[lInx++] = m_hevHttpDone;
  120. pHandles[lInx++] = m_hevAccept;
  121. for (int i=0; i<m_pEncrypter->m_connections.size(); i++)
  122. {
  123. // if (!m_pEncrypter->m_connections[i]->IsPause())
  124. pHandles[lInx++] = m_pEncrypter->m_connections[i]->m_hReadEvent;
  125. }
  126. for (i=0; i<m_pEncrypter->m_unConnections.size(); i++)
  127. {
  128. pHandles[lInx++] = m_pEncrypter->m_unConnections[i]->m_hevConnecting;
  129. }
  130. //
  131. // wait handles to signal.
  132. //
  133. DWORD dwRet = WaitForMultipleObjects(lCount, pHandles, false, ltimewait * CLOCKS_PER_SEC );
  134. //event done, exit the loop.
  135. if (dwRet == WAIT_OBJECT_0)
  136. return;
  137. // event http
  138. else if (dwRet == (WAIT_OBJECT_0 + 1))
  139. {
  140. m_pRequester->PostExcute();
  141. /*
  142. int iHttpResult = m_pRequester->GetAnnounceResult();
  143. if (iHttpResult != 0)
  144. {
  145. // m_pRequester->postrequest();
  146. }
  147. //*/
  148. }
  149. // event accept
  150. else if (dwRet == (WAIT_OBJECT_0 + 2))
  151. {
  152. WSANETWORKEVENTS events;
  153. dwRet = WSAEnumNetworkEvents(m_hserver, m_hevAccept, &events);
  154. if (dwRet == SOCKET_ERROR) 
  155. {
  156. string e = WSAShowError();
  157. assert(false);
  158. }
  159. else if (!(events.lNetworkEvents & FD_ACCEPT))
  160. {
  161. string e = WSAShowError();
  162. assert(false);
  163. }
  164. else if (events.iErrorCode[FD_ACCEPT_BIT])
  165. {
  166. string e = WSAShowError();
  167. assert(false);
  168. }
  169. else //if (events.lNetworkEvents & FD_ACCEPT)
  170. {
  171. sockaddr_in addr = {0};
  172. int iaddrlen = sizeof(addr);
  173. SOCKET newsocket = accept(m_hserver, (sockaddr *)&addr, &iaddrlen);
  174. if (newsocket == INVALID_SOCKET)  
  175. {
  176. string e = WSAShowError();
  177. assert(false);
  178. }
  179. m_pEncrypter->connection_made(newsocket);
  180. }
  181. }
  182. // event  read,write and connecting
  183. else if (dwRet >= (WAIT_OBJECT_0 + 3) && dwRet < (WAIT_OBJECT_0 + lCount))
  184. {
  185. int inx = dwRet - (WAIT_OBJECT_0 + 3);
  186. if (inx < m_pEncrypter->m_connections.size()) // event read and write
  187. {
  188. m_pEncrypter->m_connections[inx]->handle_events();
  189. }
  190. else // event connecting
  191. {
  192. inx = inx - m_pEncrypter->m_connections.size();
  193. if (inx < m_pEncrypter->m_unConnections.size())
  194. {
  195. m_pEncrypter->complete_connecting(inx);
  196. }
  197. }
  198. }
  199. // wait fail
  200. else
  201. {
  202. if (dwRet == WAIT_TIMEOUT)
  203. {
  204. }
  205. else
  206. {
  207. // range out of MAXIMUM_WAIT_OBJECTS.
  208. long lsize = m_pEncrypter->m_connections.size();
  209. TRACE("rn%drn", lsize);
  210. string e = WSAShowError();
  211. assert(false);
  212. return;
  213. }
  214. }
  215. } // end while()
  216. }