00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "PongCache.h"
00026 #include "G1Packet.h"
00027
00028 #ifdef _DEBUG
00029 #undef THIS_FILE
00030 static char THIS_FILE[]=__FILE__;
00031 #define new DEBUG_NEW
00032 #endif
00033
00034
00036
00037
00038 CPongCache::CPongCache()
00039 {
00040 m_nTime = 0;
00041 }
00042
00043 CPongCache::~CPongCache()
00044 {
00045 Clear();
00046 }
00047
00049
00050
00051 void CPongCache::Clear()
00052 {
00053 for ( POSITION pos = m_pCache.GetHeadPosition() ; pos ; )
00054 {
00055 CPongItem* pItem = (CPongItem*)m_pCache.GetNext( pos );
00056 delete pItem;
00057 }
00058
00059 m_pCache.RemoveAll();
00060
00061 m_nTime = GetTickCount();
00062 }
00063
00064 BOOL CPongCache::ClearIfOld()
00065 {
00066 if ( GetTickCount() - m_nTime >= Settings.Gnutella1.PongCache )
00067 {
00068 Clear();
00069 return TRUE;
00070 }
00071
00072 return FALSE;
00073 }
00074
00076
00077
00078 CPongItem* CPongCache::Add(CNeighbour* pNeighbour, IN_ADDR* pAddress, WORD nPort, BYTE nHops, DWORD nFiles, DWORD nVolume)
00079 {
00080 for ( POSITION pos = m_pCache.GetHeadPosition() ; pos ; )
00081 {
00082 CPongItem* pItem = (CPongItem*)m_pCache.GetNext( pos );
00083
00084 if ( pItem->m_nPort != nPort ) continue;
00085 if ( pItem->m_nHops != nHops ) continue;
00086
00087 if ( memcmp( &pItem->m_pAddress, pAddress, sizeof(IN_ADDR) ) == 0 )
00088 {
00089 pItem->m_nFiles = nFiles;
00090 pItem->m_nVolume = nVolume;
00091 return pItem;
00092 }
00093 }
00094
00095 CPongItem* pItem = new CPongItem( pNeighbour, pAddress, nPort, nHops, nFiles, nVolume );
00096
00097 m_pCache.AddTail( pItem );
00098
00099 return pItem;
00100 }
00101
00103
00104
00105 CPongItem* CPongCache::Lookup(CNeighbour* pNotFrom, BYTE nHops, CPtrList* pIgnore)
00106 {
00107 for ( POSITION pos = m_pCache.GetHeadPosition() ; pos ; )
00108 {
00109 CPongItem* pItem = (CPongItem*)m_pCache.GetNext( pos );
00110
00111 if ( pItem->m_nHops != nHops ) continue;
00112 if ( pItem->m_pNeighbour == pNotFrom ) continue;
00113
00114 if ( pIgnore && pIgnore->Find( pItem ) ) continue;
00115
00116 return pItem;
00117 }
00118
00119 return NULL;
00120 }
00121
00123
00124
00125 POSITION CPongCache::GetIterator() const
00126 {
00127 return m_pCache.GetHeadPosition();
00128 }
00129
00130 CPongItem* CPongCache::GetNext(POSITION& pos) const
00131 {
00132 return (CPongItem*)m_pCache.GetNext( pos );
00133 }
00134
00135
00137
00138
00139 CPongItem::CPongItem(CNeighbour* pNeighbour, IN_ADDR* pAddress, WORD nPort, BYTE nHops, DWORD nFiles, DWORD nVolume)
00140 {
00141 m_pNeighbour = pNeighbour;
00142 m_pAddress = *pAddress;
00143 m_nPort = nPort;
00144 m_nHops = nHops;
00145 m_nFiles = nFiles;
00146 m_nVolume = nVolume;
00147 }
00148
00149 CPongItem::~CPongItem()
00150 {
00151 }
00152
00154
00155
00156 CG1Packet* CPongItem::ToPacket(int nTTL, GGUID* pGUID)
00157 {
00158 CG1Packet* pPong = CG1Packet::New( G1_PACKET_PONG, nTTL, pGUID );
00159
00160 pPong->m_nHops = m_nHops;
00161
00162 pPong->WriteShortLE( m_nPort );
00163 pPong->WriteLongLE( *(DWORD*)&m_pAddress );
00164 pPong->WriteLongLE( m_nFiles );
00165 pPong->WriteLongLE( m_nVolume );
00166
00167 return pPong;
00168 }