Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

PongCache.cpp

Go to the documentation of this file.
00001 //
00002 // PongCache.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 // CPongCache construction
00037 
00038 CPongCache::CPongCache()
00039 {
00040         m_nTime = 0;
00041 }
00042 
00043 CPongCache::~CPongCache()
00044 {
00045         Clear();
00046 }
00047 
00049 // CPongCache clear
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 // CPongCache add
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 // CPongCache lookup
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 // CPongCache list access
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 // CPongItem construction
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 // CPongItem packet conversion
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 }

Generated on Thu Dec 15 10:39:46 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2