RawServer.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:6k
源码类别:
P2P编程
开发平台:
Visual C++
- // RawServer.cpp: implementation of the CRawServer class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testBT.h"
- #include "RawServer.h"
- #include "Choker.h"
- #include "Connector.h"
- #include "DownloaderFeedback.h"
- #include "StorageWrapper.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CRawServer::CRawServer(HANDLE hevDone, HANDLE hevUnPause, CEncrypter* pEncrypter, CChoker* pChoker,
- CConnector* pConnector, CDownloaderFeedback* pDownloaderFeedback, CStorageWrapper* pStorageWrapper)
- {
- m_pConnector = pConnector;
- m_pChoker = pChoker;
- m_pEncrypter = pEncrypter;
- m_hevUnPause = hevUnPause;
- m_hevDone = hevDone;
- m_pDownloaderFeedback = pDownloaderFeedback;
- m_pStorageWrapper = pStorageWrapper;
- m_hevAccept = CreateEvent(0, false, false, 0);
- m_hevHttpDone = 0;
- m_hserver = INVALID_SOCKET;
- }
- CRawServer::~CRawServer()
- {
- CloseHandle(m_hevAccept);
- // CloseHandle(m_hevHttpDone);
- if (m_hserver != INVALID_SOCKET)
- closesocket(m_hserver);
- }
- long CRawServer::Bind(long lminport, long lmaxport)
- {
- assert (m_hserver == INVALID_SOCKET);
- SOCKET hserver = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 0, WSA_FLAG_OVERLAPPED);
- if (hserver == INVALID_SOCKET)
- return false;
- long lport = 0;
- int iRet = -1;
- sockaddr_in addr = {0};
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = htonl (INADDR_ANY);
- for (lport=lminport; lport<=lmaxport; lport++)
- {
- addr.sin_port = htons(lport);
- iRet = bind(hserver, (sockaddr*)&addr, sizeof(addr));
- if (iRet == 0) break;
- // else
- //errorFunc("bind() server error:bind to port error: " + WSAShowError());
- }
- if (iRet != 0)
- {
- closesocket(hserver);
- return false;
- }
- iRet = listen(hserver, 10);
- if (iRet != 0)
- {
- closesocket(hserver);
- return false;
- }
- iRet = WSAEventSelect(hserver, m_hevAccept, FD_ACCEPT);
- if (iRet == SOCKET_ERROR)
- {
- assert(false);
- closesocket(m_hserver);
- return false;
- }
- m_hserver = hserver;
- return lport;
- }
- void CRawServer::listen_forever(CRerequester* pRequest)
- {
- assert (m_hserver != INVALID_SOCKET);
- m_pRequester = pRequest;
- if (IsEventSet(m_hevDone))
- return;
- m_hevHttpDone = m_pRequester->GetHttpEvent();
- const time_t ltimewait = 2;
- time_t tLast = 0, ltime = 0;
- time(&tLast);
- while (WaitForSingleObject(m_hevDone, 0) != WAIT_OBJECT_0)
- {
- time(<ime);
- if (ltime < tLast)
- tLast = ltime;
- if ((ltime - tLast) >= ltimewait)
- {
- m_pEncrypter->send_keepalives();
- m_pChoker->round_robin();
- m_pEncrypter->detele_connections_died();
- m_pRequester->Excute();
- m_pConnector->uncap();
- m_pConnector->OnIdle();
- m_pDownloaderFeedback->PutDataTime();
- m_pStorageWrapper->OnIdle();
- if (!IsEventSet(m_hevUnPause))
- {
- // TRACE("rnstorage pausedrn");
- m_pDownloaderFeedback->ShowSystemMessage("用户暂停该任务...", CSystemMsg::eCmd);
- DWORD dwRet = WaitForSingleObject(m_hevUnPause, INFINITE);
- // TRACE("rndownload Unpausedrn");
- m_pDownloaderFeedback->ShowSystemMessage("用户继续该任务...", CSystemMsg::eCmd);
- // assert(dwRet != WAIT_OBJECT_0);
- }
- tLast = ltime;
- }
- //
- // compute handles to wait.
- //
- long lInx = 0;
- long lCount = 3 + m_pEncrypter->m_connections.size() + m_pEncrypter->m_unConnections.size();
- HANDLE* pHandles = new HANDLE[lCount];
- auto_ptr<HANDLE> autohandles(pHandles);
- pHandles[lInx++] = m_hevDone;
- pHandles[lInx++] = m_hevHttpDone;
- pHandles[lInx++] = m_hevAccept;
- for (int i=0; i<m_pEncrypter->m_connections.size(); i++)
- {
- // if (!m_pEncrypter->m_connections[i]->IsPause())
- pHandles[lInx++] = m_pEncrypter->m_connections[i]->m_hReadEvent;
- }
- for (i=0; i<m_pEncrypter->m_unConnections.size(); i++)
- {
- pHandles[lInx++] = m_pEncrypter->m_unConnections[i]->m_hevConnecting;
- }
- //
- // wait handles to signal.
- //
- DWORD dwRet = WaitForMultipleObjects(lCount, pHandles, false, ltimewait * CLOCKS_PER_SEC );
- //event done, exit the loop.
- if (dwRet == WAIT_OBJECT_0)
- return;
- // event http
- else if (dwRet == (WAIT_OBJECT_0 + 1))
- {
- m_pRequester->PostExcute();
- /*
- int iHttpResult = m_pRequester->GetAnnounceResult();
- if (iHttpResult != 0)
- {
- // m_pRequester->postrequest();
- }
- //*/
- }
- // event accept
- else if (dwRet == (WAIT_OBJECT_0 + 2))
- {
- WSANETWORKEVENTS events;
- dwRet = WSAEnumNetworkEvents(m_hserver, m_hevAccept, &events);
- if (dwRet == SOCKET_ERROR)
- {
- string e = WSAShowError();
- assert(false);
- }
- else if (!(events.lNetworkEvents & FD_ACCEPT))
- {
- string e = WSAShowError();
- assert(false);
- }
- else if (events.iErrorCode[FD_ACCEPT_BIT])
- {
- string e = WSAShowError();
- assert(false);
- }
- else //if (events.lNetworkEvents & FD_ACCEPT)
- {
- sockaddr_in addr = {0};
- int iaddrlen = sizeof(addr);
- SOCKET newsocket = accept(m_hserver, (sockaddr *)&addr, &iaddrlen);
- if (newsocket == INVALID_SOCKET)
- {
- string e = WSAShowError();
- assert(false);
- }
- m_pEncrypter->connection_made(newsocket);
- }
- }
- // event read,write and connecting
- else if (dwRet >= (WAIT_OBJECT_0 + 3) && dwRet < (WAIT_OBJECT_0 + lCount))
- {
- int inx = dwRet - (WAIT_OBJECT_0 + 3);
- if (inx < m_pEncrypter->m_connections.size()) // event read and write
- {
- m_pEncrypter->m_connections[inx]->handle_events();
- }
- else // event connecting
- {
- inx = inx - m_pEncrypter->m_connections.size();
- if (inx < m_pEncrypter->m_unConnections.size())
- {
- m_pEncrypter->complete_connecting(inx);
- }
- }
- }
- // wait fail
- else
- {
- if (dwRet == WAIT_TIMEOUT)
- {
- }
- else
- {
- // range out of MAXIMUM_WAIT_OBJECTS.
- long lsize = m_pEncrypter->m_connections.size();
- TRACE("rn%drn", lsize);
- string e = WSAShowError();
- assert(false);
- return;
- }
- }
- } // end while()
- }