BOOL CDownloadTransferED2K::SendPrimaryRequest()
{
	ASSERT( m_pClient != NULL );
	const DWORD tNow = GetTickCount();

	//if ( m_pDownload->GetVolumeRemaining() == 0 )
	//{
	//	theApp.Message( MSG_INFO, IDS_DOWNLOAD_FRAGMENT_END, (LPCTSTR)m_sAddress );
	//	Close( TRI_TRUE );
	//	return FALSE;
	//}

	// This source is current requesting
	SetState( dtsRequesting );

	// Set the 'last requested' time
	m_tRequest = tNow;

	ClearRequests();

	// Send ed2k file request
	CEDPacket* pPacket = CEDPacket::New( ED2K_C2C_FILEREQUEST );
	pPacket->Write( m_pDownload->m_oED2K );

	if ( Settings.eDonkey.ExtendedRequest >= 1 && m_pClient->m_bEmRequest >= 1 )
		m_pClient->WritePartStatus( pPacket, m_pDownload );

	// It's not very accurate
	if ( Settings.eDonkey.ExtendedRequest >= 2 && m_pClient->m_bEmRequest >= 2 )
		pPacket->WriteShortLE( (WORD)m_pDownload->GetED2KCompleteSourceCount() );

	Send( pPacket );

	if ( m_pDownload->m_nSize <= ED2K_PART_SIZE )
	{
		// Don't ask for status - if the client answers, we know the file is complete anyway
	}
	else
	{
		// Send ed2k status request
		pPacket = CEDPacket::New( ED2K_C2C_FILESTATUSREQUEST );
		pPacket->Write( m_pDownload->m_oED2K );
		Send( pPacket );
	}

	if ( m_pDownload->GetSourceCount() < Settings.Downloads.SourcesWanted &&	// We want more sources
		 tNow > m_tSources && tNow > m_tSources + 30 * 60 * 1000 &&				// We have not asked for at least 30 minutes
		 m_pClient->m_bEmule && Network.IsListening() )							// Remote client is eMule compatible and we are accepting packets
	{
		// Set 'last asked for sources' time
		m_tSources = tNow;
		// Send ed2k request for sources packet
		pPacket = CEDPacket::New( ED2K_C2C_REQUESTSOURCES, ED2K_PROTOCOL_EMULE );
		pPacket->Write( m_pDownload->m_oED2K );
		Send( pPacket );
	}

	return TRUE;
}
BOOL CDownloadTransferED2K::OnStartUpload(CEDPacket* /*pPacket*/)
{
	SetState( dtsDownloading );
	m_pClient->m_mInput.tLast = GetTickCount();

	ClearRequests();

	return SendFragmentRequests();
}
void CDownloadTransferED2K::SetQueueRank(int nRank)
{
	m_tRequest	= m_tRanking = GetTickCount();
	m_nQueuePos	= nRank;
	m_bUDP		= FALSE;

	SetState( dtsQueued );
	ClearRequests();

	theApp.Message( MSG_INFO, IDS_DOWNLOAD_QUEUED, (LPCTSTR)m_sAddress, m_nQueuePos, m_nQueueLen, (LPCTSTR)m_sQueueName );	//_T("eDonkey2000") );
}
CDownloadTransferED2K::~CDownloadTransferED2K()
{
	ASSUME_LOCK( Transfers.m_pSection );

	// This never happens
	if ( m_pClient ) m_pClient->m_mInput.pLimit = m_pClient->m_mOutput.pLimit = NULL;

	ClearRequests();
	delete m_pInflateBuffer;

	ASSERT( m_pClient == NULL );
	ASSERT( ! EDClients.IsMyDownload( this ) );
}
Exemple #5
0
void BaseWorker::HandleRequest()
{
  //if (Id!=0) printf("HANDLE.Id:%d\n",);
  if (State == RUNNING && Requests.empty())
  {
    Calculate();
    CollabSync();
    SendCalculations();
    CollabSync();
    ReceiveCalculations();
    IterNumber++;
    if (IterNumber == IterCount)
    {
      Requests.push_front(BaseRequest("STOP"));
    }
  }

  if (Requests.empty())
    return;

  if (Requests.front() == "RUN" && State == STOPPED)
  {
    IterNumber = 0;
    IterCount = Requests.front().GetIterCount();
    State = RUNNING;
    Requests.pop_front();
  }
  else if (Requests.front() == "STOP" && State == RUNNING)
  {
    SendFinalReport();
    State = STOPPED;
    Requests.pop_front();
    ClearRequests();
    State = ENDED;
  }
  else if (Requests.front() == "QUIT")
  {
    State = ENDED;
  }  
  else {
    HandleOtherRequests();
  }
}
BOOL CDownloadTransferED2K::SendSecondaryRequest()
{
	ASSERT( m_pClient != NULL );
	ASSERT( m_nState > dtsConnecting );
	// ASSERT( m_nState == dtsRequesting || m_nState == dtsHashset );

	if ( ! m_pDownload->PrepareFile() )
	{
		Close( TRI_TRUE );
		return FALSE;
	}

	if ( m_bHashset == FALSE && m_pDownload->NeedHashset() )
	{
		CEDPacket* pPacket = CEDPacket::New( ED2K_C2C_HASHSETREQUEST );
		pPacket->Write( m_pDownload->m_oED2K );
		Send( pPacket );

		SetState( dtsHashset );
		m_pClient->m_mInput.tLast = GetTickCount();
	}
	else if ( m_pSource->HasUsefulRanges() )
	{
		CEDPacket* pPacket = CEDPacket::New( ED2K_C2C_QUEUEREQUEST );
		pPacket->Write( m_pDownload->m_oED2K );
		Send( pPacket );

		SetState( dtsEnqueue );
		m_tRequest = GetTickCount();
	}
	else
	{
		m_pSource->m_tAttempt = GetTickCount() + Settings.eDonkey.ReAskTime * 500;
		m_pSource->SetAvailableRanges( NULL );
		theApp.Message( MSG_INFO, IDS_DOWNLOAD_FRAGMENT_END, (LPCTSTR)m_sAddress );
		Close( TRI_TRUE );
		return FALSE;
	}

	ClearRequests();

	return TRUE;
}
BOOL CDownloadTransferED2K::SendPrimaryRequest()
{
	ASSERT( m_pClient != NULL );
	DWORD tNow = GetTickCount();
	
	/*
	if ( m_pDownload->GetVolumeRemaining() == 0 )
	{
		theApp.Message( MSG_INFO, IDS_DOWNLOAD_FRAGMENT_END, (LPCTSTR)m_sAddress );
		Close( TRI_TRUE );
		return FALSE;
	}
	*/

	//This source is current requesting
	SetState( dtsRequesting );

	//Set the 'last requested' time
	m_tRequest	= tNow;		

	ClearRequests();
	
	//Send ed2k file request
	CEDPacket* pPacket = CEDPacket::New( ED2K_C2C_FILEREQUEST );
	pPacket->Write( m_pDownload->m_oED2K );

	if ( Settings.eDonkey.ExtendedRequest >= 1 && m_pClient->m_bEmRequest >= 1 )
	{
		m_pClient->WritePartStatus( pPacket, m_pDownload );
	}

	//It's not very accurate
	if ( Settings.eDonkey.ExtendedRequest >= 2 && m_pClient->m_bEmRequest >= 2 ) 
	{
		pPacket->WriteShortLE( (WORD) m_pDownload->GetED2KCompleteSourceCount() );
	}

	Send( pPacket );
	
	if ( m_pDownload->m_nSize <= ED2K_PART_SIZE )
	{
		// Don't ask for status - if the client answers, we know the file is complete anyway
	}
	else
	{
		//Send ed2k status request
		pPacket = CEDPacket::New( ED2K_C2C_FILESTATUSREQUEST );
		pPacket->Write( m_pDownload->m_oED2K );
		Send( pPacket );
	}
	
	// TODO: Add new option "SourceExchangePeriod" (default: 10 minutes) like BitTorrent has
	if ( ( m_pDownload->GetSourceCount() < Settings.Downloads.SourcesWanted ) &&
		 ( tNow >= m_tSourceRequest + 10 * 60 * 1000 ) &&
		 m_pClient->m_bEmule )
	{
		// Set 'last asked for sources' time
		m_tSourceRequest = tNow;
		// Send ed2k request for sources packet
		pPacket = CEDPacket::New( ED2K_C2C_REQUESTSOURCES, ED2K_PROTOCOL_EMULE );
		pPacket->Write( m_pDownload->m_oED2K );
		Send( pPacket );
	}
	
	return TRUE;
}