#include "StdAfx.h"
|
#include "MBThreadDownloadDisk.h"
|
#include "MBTransExtendEvents.h"
|
|
CMBThreadDownloadDisk::CMBThreadDownloadDisk(void)
|
{
|
m_bStop = false;
|
m_pTaskLoopMgr = NULL;
|
m_pTransMgr = NULL;
|
m_pSvrMgr = NULL;
|
m_nDownloadQueueSize = 4;
|
}
|
|
|
|
|
CMBThreadDownloadDisk::~CMBThreadDownloadDisk(void)
|
{
|
}
|
|
|
// ¿ªÊ¼Ïß³Ì
|
void CMBThreadDownloadDisk::StartThread()
|
{
|
if(IsRunning())
|
return;
|
|
BeginThread();
|
}
|
|
// ½áÊøÏß³Ì
|
void CMBThreadDownloadDisk::OverThread()
|
{
|
m_bStop = true;
|
|
CMBDownloadQueue *pDownloadQueue = m_pTransMgr->GetDownloadQueue(); // ÏÂÔØ¶ÓÁÐ
|
if( pDownloadQueue != NULL )
|
{
|
pDownloadQueue->SetEventShutdownSet();
|
}
|
|
if(!IsRunning())
|
return;
|
|
EndThread();
|
}
|
|
void CMBThreadDownloadDisk::SetTransMgr(CMBTransMgr *pTransMgr)
|
{
|
m_pTransMgr = pTransMgr;
|
}
|
|
void CMBThreadDownloadDisk::SetSvrMgr( CMBServerMgr *pSvrMgr)
|
{
|
m_pSvrMgr = pSvrMgr;
|
}
|
|
void CMBThreadDownloadDisk::SetTaskLoopMgr(CMBTaskLoopMgr *pTaskLoopMgr)
|
{
|
m_pTaskLoopMgr = pTaskLoopMgr;
|
}
|
|
|
UINT CMBThreadDownloadDisk::Run()
|
{
|
CMBDownloadQueue *pDownloadQueue = m_pTransMgr->GetDownloadQueue(); // ÏÂÔØ¶ÓÁÐ
|
if( pDownloadQueue == NULL )
|
return 0;
|
|
HANDLE handlesToWaitFor[2];
|
DWORD dwResult;
|
handlesToWaitFor[0] = pDownloadQueue->GetEventExeHandle();
|
handlesToWaitFor[1] = pDownloadQueue->GetEventShutdownHandle();
|
|
pDownloadQueue->SetEventExeSet();
|
|
while ( true )
|
{
|
#ifdef _DEBUG
|
dwResult = ::WaitForMultipleObjects( 2, handlesToWaitFor, false, 6000 );
|
#else
|
dwResult = ::WaitForMultipleObjects( 2, handlesToWaitFor, false, 300000 );
|
#endif
|
if ( dwResult == WAIT_OBJECT_0 )
|
{
|
if( m_bStop )
|
break;
|
// δ³õʼ»¯²»ÈÃÆäÏÂÔØ
|
if(m_pSvrMgr->GetFileSvrClientMgr()->IsInit())
|
{
|
// ÏÂÔØ
|
while(TRUE){
|
Sleep(1000);
|
TransTimerDownload(pDownloadQueue,m_bStop );
|
if( pDownloadQueue->GetWaitingUserCount() == 0)
|
break;
|
if(m_bStop)
|
break;
|
}
|
pDownloadQueue->SetEventExeReset();
|
}
|
}
|
else if ( dwResult == WAIT_OBJECT_0 + 1 )
|
{
|
// Time to shutdown
|
break;
|
}
|
else if ( dwResult == WAIT_TIMEOUT )
|
{
|
pDownloadQueue->SetEventExeSet();
|
}
|
} // End of while ( true )
|
|
return 0;
|
}
|
|
|
// ´«Êä
|
bool CMBThreadDownloadDisk::TransTimerDownload(CMBDownloadQueue *pDownloadQueue ,BOOL &bThreadStop)
|
{
|
CString strErrInfo;
|
if(!m_pTransMgr->ReDownloadToWaitingList(bThreadStop,strErrInfo ))
|
{
|
return false;
|
}
|
|
if(!pDownloadQueue->RemoveUnUseFromDownloadQueue( bThreadStop ))
|
{
|
return false;
|
}
|
if(!pDownloadQueue->RemoveFinishFromWaitingQueue( bThreadStop ))
|
{
|
return false;
|
}
|
if(!pDownloadQueue->ReDownloadQueue( m_nDownloadQueueSize,bThreadStop ))
|
{
|
return false;
|
}
|
//////////////////////ÏÂÔØ///////////////////////////////
|
CDownClientAutoPtrList listDownloading;
|
pDownloadQueue->CopyDownloadingQueue( listDownloading );
|
|
CDownClientAutoPtrList::iterator it;
|
CAutoRefPtr<CMBDownloadFileInfo> curClient = NULL;
|
|
for( it = listDownloading.begin();it != listDownloading.end();it++ )
|
{
|
if(bThreadStop)
|
return false;
|
curClient = *it;
|
if (curClient->GetStatus() == CMBDownloadFileInfo::Status_OnDownQueue)
|
{
|
curClient->SetStatus(CMBDownloadFileInfo::Status_CalledDownServer);
|
|
if(curClient->m_strFrom.CompareNoCase(FCTN_AM) == 0)
|
{
|
EventAMDownloadFile *pEvt = new EventAMDownloadFile(NULL);
|
curClient.CopyTo(&pEvt->m_pDownloadFileInfo);
|
SNotifyCenter::getSingleton().FireEventAsync(pEvt);
|
pEvt->Release();
|
}
|
else
|
{
|
EventDownloadFile *pEvt = new EventDownloadFile(NULL);
|
curClient.CopyTo(&pEvt->m_pDownloadFileInfo);
|
SNotifyCenter::getSingleton().FireEventAsync(pEvt);
|
pEvt->Release();
|
}
|
|
break;
|
}
|
}
|
|
listDownloading.clear();
|
|
return true;
|
}
|
|
|
|