2 // This file is part of the aMule Project.
4 // Copyright (c) 2005-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "DeadSourceList.h"
28 #include <common/Macros.h>
30 #include "updownclient.h" // Needed for CUpDownClient
32 #define CLEANUPTIME MIN2MS(60)
34 #define BLOCKTIME (::GetTickCount() + (m_bGlobalList ? MIN2MS(30) : MIN2MS(45)))
35 #define BLOCKTIMEFW (::GetTickCount() + (m_bGlobalList ? MIN2MS(45) : MIN2MS(60)))
37 ///////////////////////////////////////////////////////////////////////////////////////
41 CDeadSourceList::CDeadSource::CDeadSource(uint32 ID
, uint16 Port
, uint32 ServerIP
, uint16 KadPort
)
46 m_ServerIP
= ServerIP
;
51 void CDeadSourceList::CDeadSource::SetTimeout( uint32 t
)
57 uint32
CDeadSourceList::CDeadSource::GetTimeout() const
63 bool CDeadSourceList::CDeadSource::operator==(const CDeadSource
& other
) const
65 if ( m_ID
== other
.m_ID
) {
66 if ( m_Port
== other
.m_Port
|| m_KadPort
== other
.m_KadPort
) {
67 if ( IsLowID( m_ID
) ) {
68 return m_ServerIP
== other
.m_ServerIP
;
79 ///////////////////////////////////////////////////////////////////////////////////////
82 CDeadSourceList::CDeadSourceList(bool isGlobal
)
84 m_dwLastCleanUp
= ::GetTickCount();
85 m_bGlobalList
= isGlobal
;
89 uint32
CDeadSourceList::GetDeadSourcesCount() const
91 return m_sources
.size();
95 bool CDeadSourceList::IsDeadSource(const CUpDownClient
* client
)
98 client
->GetUserIDHybrid(),
99 client
->GetUserPort(),
100 client
->GetServerIP(),
105 DeadSourcePair range
= m_sources
.equal_range( client
->GetUserIDHybrid() );
106 for ( ; range
.first
!= range
.second
; range
.first
++ ) {
107 if ( range
.first
->second
== source
) {
108 // Check if the entry is still valid
109 if ( range
.first
->second
.GetTimeout() > GetTickCount() ) {
113 // The source is no longer dead, so remove it to reduce the size of the list
114 m_sources
.erase( range
.first
);
123 void CDeadSourceList::AddDeadSource( const CUpDownClient
* client
)
126 client
->GetUserIDHybrid(),
127 client
->GetUserPort(),
128 client
->GetServerIP(),
132 // Set the timeout for the new source
133 source
.SetTimeout( client
->HasLowID() ? BLOCKTIMEFW
: BLOCKTIME
);
135 // Check if the source is already listed
136 DeadSourcePair range
= m_sources
.equal_range( client
->GetUserIDHybrid() );
137 for ( ; range
.first
!= range
.second
; range
.first
++ ) {
138 if ( range
.first
->second
== source
) {
139 range
.first
->second
= source
;
144 m_sources
.insert( DeadSourceMap::value_type( client
->GetUserIDHybrid(), source
) );
146 // Check if we should cleanup the list. This is
147 // done to avoid a buildup of stale entries.
148 if ( GetTickCount() - m_dwLastCleanUp
> CLEANUPTIME
) {
154 void CDeadSourceList::CleanUp()
156 m_dwLastCleanUp
= ::GetTickCount();
158 DeadSourceIterator it
= m_sources
.begin();
159 for ( ; it
!= m_sources
.end(); ) {
160 DeadSourceIterator it1
= it
++;
161 if ( it1
->second
.GetTimeout() < m_dwLastCleanUp
) {
162 m_sources
.erase( it1
);
166 // File_checked_for_headers