⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 managedsearch.cpp

📁 著名的下载软件核心Shareaza
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// ManagedSearch.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2004.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "StdAfx.h"
#include "Shareaza.h"
#include "Settings.h"
#include "SearchManager.h"
#include "ManagedSearch.h"
#include "QuerySearch.h"
#include "Network.h"
#include "HostCache.h"
#include "Neighbours.h"
#include "Datagrams.h"
#include "G1Neighbour.h"
#include "G2Neighbour.h"
#include "G1Packet.h"
#include "G2Packet.h"
#include "EDPacket.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// CManagedSearch construction

CManagedSearch::CManagedSearch(CQuerySearch* pSearch, int nPriority)
{
	m_pSearch		= pSearch ? pSearch : new CQuerySearch();
	m_nPriority		= nPriority;
	m_bAllowG2		= TRUE;
	m_bAllowG1		= TRUE;
	m_bAllowED2K	= TRUE;
	
	m_bActive		= FALSE;
	m_bReceive		= TRUE;
	m_tStarted		= 0;
	m_nHubs			= 0;
	m_nLeaves		= 0;
	m_nHits			= 0;
	m_tLastED2K		= 0;
}

CManagedSearch::~CManagedSearch()
{
	Stop();
	if ( m_pSearch ) delete m_pSearch;
}

//////////////////////////////////////////////////////////////////////
// CManagedSearch serialize

void CManagedSearch::Serialize(CArchive& ar)
{
	int nVersion = 3;

	if ( ar.IsStoring() )
	{
		ar << nVersion;

		m_pSearch->Serialize( ar );
		ar << m_nPriority;
		ar << m_bActive;
		ar << m_bReceive;
		ar << m_bAllowG2;
		ar << m_bAllowG1;
		ar << m_bAllowED2K;
	}
	else
	{
		ar >> nVersion;
		if ( nVersion < 2 ) AfxThrowUserException();
		
		if ( m_pSearch ) delete m_pSearch;
		m_pSearch = new CQuerySearch();
		m_pSearch->Serialize( ar );
		
		ar >> m_nPriority;
		ar >> m_bActive;
		ar >> m_bReceive;
		
		m_bActive = m_bReceive = FALSE;
		
		if ( nVersion >= 3 )
		{
			ar >> m_bAllowG2;
			ar >> m_bAllowG1;
			ar >> m_bAllowED2K;
		}
	}
}

//////////////////////////////////////////////////////////////////////
// CManagedSearch start and stop

void CManagedSearch::Start()
{
	if ( m_bActive ) return;
	m_bActive = TRUE;
	
	CSingleLock pLock( &SearchManager.m_pSection );
	pLock.Lock( 1000 );
	
	m_tStarted	= time( NULL );
	m_tExecute	= 0;
	m_tLastED2K	= 0;
	
	m_pNodes.RemoveAll();
	
	SearchManager.Add( this );
}

void CManagedSearch::Stop()
{
	if ( m_bActive )
	{
		m_bActive = FALSE;
	    Datagrams.PurgeToken( this );
	}
	
	CSingleLock pLock( &SearchManager.m_pSection );
	pLock.Lock( 1000 );
	SearchManager.Remove( this );
}

//////////////////////////////////////////////////////////////////////
// CManagedSearch execute

BOOL CManagedSearch::Execute()
{
	if ( ! m_bActive || ! m_pSearch ) return FALSE;
	
	DWORD tTicks	= GetTickCount();
	DWORD tSecs		= time( NULL );
	
	if ( m_nPriority == spLowest )
	{
		if ( tTicks - m_tExecute < 30000 ) return FALSE;
		m_tExecute = tTicks;
	}
	else
	{
		if ( tTicks - m_tExecute < 200 ) return FALSE;
		m_tExecute = tTicks;
	}
	
	BOOL bSuccess = ExecuteNeighbours( tTicks, tSecs );
	
	if ( Settings.Gnutella2.EnableToday && m_bAllowG2 )
	{
		bSuccess |= ExecuteG2Mesh( tTicks, tSecs );
	}
	
	if ( Settings.eDonkey.EnableToday && Settings.eDonkey.ServerWalk && m_bAllowED2K && Network.IsListening() )
	{
		if ( tTicks > m_tLastED2K && tTicks - m_tLastED2K >= Settings.eDonkey.QueryGlobalThrottle )
		{
			bSuccess |= ExecuteDonkeyMesh( tTicks, tSecs );
			m_tLastED2K = tTicks;
		}
	}
	
	return bSuccess;
}

//////////////////////////////////////////////////////////////////////
// CManagedSearch execute the search on G1 and G2 neighbours

BOOL CManagedSearch::ExecuteNeighbours(DWORD tTicks, DWORD tSecs)
{
	BOOL bIsOld = ( tSecs - m_tStarted ) >= 5;
	
	int nCount = 0;
	
	for ( POSITION pos = Neighbours.GetIterator() ; pos ; )
	{
		CNeighbour* pNeighbour = Neighbours.GetNext( pos );
		DWORD nPeriod;
		
		// Must be connected
		
		if ( pNeighbour->m_nState != nrsConnected ) continue;
		
		// Check network flags

		switch ( pNeighbour->m_nProtocol )
		{
		case PROTOCOL_G1:
			if ( ! m_bAllowG1 ) continue;
			break;
		case PROTOCOL_G2:
			if ( ! m_bAllowG2 ) continue;
			break;
		case PROTOCOL_ED2K:
			if ( ! m_bAllowED2K ) continue;
			break;
		}
		
		// Must be stable for 15 seconds, or longer for G1 low priority searches
		
		nPeriod = bIsOld ? 15 : 5;
		if ( bIsOld && pNeighbour->m_nProtocol == PROTOCOL_G1 ) nPeriod += 120 + ( 15 * m_nPriority );
		
		if ( tTicks - pNeighbour->m_tConnected < nPeriod * 1000 ) continue;
		
		// Do not hammer neighbours for search results
		
		if ( bIsOld )
		{
			if ( pNeighbour->m_nProtocol == PROTOCOL_G1 )
			{
				if ( tSecs - pNeighbour->m_tLastQuery <
					 Settings.Gnutella1.QueryThrottle ) continue;
			}
			else if ( pNeighbour->m_nProtocol == PROTOCOL_G2 )
			{
				if ( tSecs - pNeighbour->m_tLastQuery <
					 Settings.Gnutella2.QueryHostThrottle / 4 ) continue;
			}
		}
		
		// Lookup the host
		
		DWORD nAddress = pNeighbour->m_pHost.sin_addr.S_un.S_addr;
		
		if ( m_pNodes.Lookup( (LPVOID)nAddress, (LPVOID&)nPeriod ) )
		{
			DWORD nFrequency = 0;
			
			if ( pNeighbour->m_nProtocol == PROTOCOL_G1 )
			{
				nFrequency = Settings.Gnutella1.RequeryDelay;
				nFrequency *= ( m_nPriority + 1 );
			}
			else if ( pNeighbour->m_nProtocol == PROTOCOL_G2 )
			{
				nFrequency = Settings.Gnutella2.RequeryDelay;
				nFrequency *= ( m_nPriority + 1 );
			}
			else if ( pNeighbour->m_nProtocol == PROTOCOL_ED2K )
			{
				nFrequency = Settings.eDonkey.RequeryDelay;
				nFrequency *= ( m_nPriority + 1 );
			}
			
			if ( tSecs - nPeriod < nFrequency ) continue;
		}
		
		// Set the last query time for this host for this search
		
		m_pNodes.SetAt( (LPVOID)nAddress, (LPVOID)tSecs );
		
		// Create the appropriate packet type
		
		CPacket* pPacket = NULL;
		
		if ( pNeighbour->m_nProtocol == PROTOCOL_G1 )
		{
			pPacket = m_pSearch->ToG1Packet();
		}
		else if ( pNeighbour->m_nProtocol == PROTOCOL_G2 )
		{
			m_pSearch->m_bAndG1 = ( Settings.Gnutella1.EnableToday && m_bAllowG1 );
			pPacket = m_pSearch->ToG2Packet( Datagrams.IsStable() ? &Network.m_pHost : NULL, 0 );
		}
		else if ( pNeighbour->m_nProtocol == PROTOCOL_ED2K )
		{
			pPacket = m_pSearch->ToEDPacket( FALSE );
		}
		else
		{
			ASSERT( FALSE );
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -