📄 clientlist.cpp
字号:
{
for (POSITION pos = list.GetHeadPosition(); pos != NULL;)
{
CUpDownClient* cur_client = list.GetNext(pos);
if (cur_client->GetIP() == ip && cur_client->GetKadPort() == port)
return cur_client;
}
return 0;
}
//TODO: This needs to change to a random Kad user.
CUpDownClient* CClientList::GetRandomKadClient() const
{
for (POSITION pos = list.GetHeadPosition(); pos != NULL;)
{
CUpDownClient* cur_client = list.GetNext(pos);
if (cur_client->GetKadPort())
return cur_client;
}
return 0;
}
CUpDownClient* CClientList::FindClientByServerID(uint32 uServerIP, uint32 uED2KUserID) const
{
uint32 uHybridUserID = ntohl(uED2KUserID);
for (POSITION pos = list.GetHeadPosition(); pos != NULL;)
{
CUpDownClient* cur_client = list.GetNext(pos);
if (cur_client->GetServerIP() == uServerIP && cur_client->GetUserIDHybrid() == uHybridUserID)
return cur_client;
}
return 0;
}
void CClientList::AddBannedClient(uint32 dwIP){
m_bannedList.SetAt(dwIP, ::GetTickCount());
}
bool CClientList::IsBannedClient(uint32 dwIP) /*const*/
{
uint32 dwBantime;
if (m_bannedList.Lookup(dwIP, dwBantime)){
if (dwBantime + CLIENTBANTIME > ::GetTickCount())
return true;
RemoveBannedClient(dwIP);
}
return false;
}
void CClientList::RemoveBannedClient(uint32 dwIP){
m_bannedList.RemoveKey(dwIP);
}
////////////////////////////////////////
/// Tracked clients
void CClientList::AddTrackClient(CUpDownClient* toadd){
CDeletedClient* pResult = 0;
if (m_trackedClientsList.Lookup(toadd->GetIP(), pResult)){
pResult->m_dwInserted = ::GetTickCount();
for (int i = 0; i != pResult->m_ItemsList.GetCount(); i++){
if (pResult->m_ItemsList[i].nPort == toadd->GetUserPort()){
// already tracked, update
pResult->m_ItemsList[i].pHash = toadd->Credits();
return;
}
}
PORTANDHASH porthash = { toadd->GetUserPort(), toadd->Credits()};
pResult->m_ItemsList.Add(porthash);
}
else{
m_trackedClientsList.SetAt(toadd->GetIP(), new CDeletedClient(toadd));
}
}
// true = everything ok, hash didn't changed
// false = hash changed
bool CClientList::ComparePriorUserhash(uint32 dwIP, uint16 nPort, void* pNewHash){
CDeletedClient* pResult = 0;
if (m_trackedClientsList.Lookup(dwIP, pResult)){
for (int i = 0; i != pResult->m_ItemsList.GetCount(); i++){
if (pResult->m_ItemsList[i].nPort == nPort){
if (pResult->m_ItemsList[i].pHash != pNewHash)
return false;
else
break;
}
}
}
return true;
}
UINT CClientList::GetClientsFromIP(uint32 dwIP) const
{
CDeletedClient* pResult;
if (m_trackedClientsList.Lookup(dwIP, pResult))
return pResult->m_ItemsList.GetCount();
return 0;
}
void CClientList::TrackBadRequest(const CUpDownClient* upcClient, sint32 nIncreaseCounter){
CDeletedClient* pResult = NULL;
if (upcClient->GetIP() == 0){
ASSERT( false );
return;
}
if (m_trackedClientsList.Lookup(upcClient->GetIP(), pResult)){
pResult->m_dwInserted = ::GetTickCount();
pResult->m_cBadRequest += nIncreaseCounter;
}
else{
CDeletedClient* ccToAdd = new CDeletedClient(upcClient);
ccToAdd->m_cBadRequest = nIncreaseCounter;
m_trackedClientsList.SetAt(upcClient->GetIP(), ccToAdd);
}
}
uint32 CClientList::GetBadRequests(const CUpDownClient* upcClient) const{
CDeletedClient* pResult = NULL;
if (upcClient->GetIP() == 0){
ASSERT( false );
return 0;
}
if (m_trackedClientsList.Lookup(upcClient->GetIP(), pResult)){
return pResult->m_cBadRequest;
}
else
return 0;
}
void CClientList::Process(){
const uint32 cur_tick = ::GetTickCount();
if (m_dwLastBannCleanUp + BAN_CLEANUP_TIME < cur_tick){
m_dwLastBannCleanUp = cur_tick;
POSITION pos = m_bannedList.GetStartPosition();
uint32 nKey;
uint32 dwBantime;
while (pos != NULL){
m_bannedList.GetNextAssoc( pos, nKey, dwBantime );
if (dwBantime + CLIENTBANTIME < cur_tick )
RemoveBannedClient(nKey);
}
}
if (m_dwLastTrackedCleanUp + TRACKED_CLEANUP_TIME < cur_tick ){
m_dwLastTrackedCleanUp = cur_tick;
if (thePrefs.GetLogBannedClients())
AddDebugLogLine(false, _T("Cleaning up TrackedClientList, %i clients on List..."), m_trackedClientsList.GetCount());
POSITION pos = m_trackedClientsList.GetStartPosition();
uint32 nKey;
CDeletedClient* pResult;
while (pos != NULL){
m_trackedClientsList.GetNextAssoc( pos, nKey, pResult );
if (pResult->m_dwInserted + KEEPTRACK_TIME < cur_tick ){
m_trackedClientsList.RemoveKey(nKey);
delete pResult;
}
}
if (thePrefs.GetLogBannedClients())
AddDebugLogLine(false, _T("...done, %i clients left on list"), m_trackedClientsList.GetCount());
}
//We need to try to connect to the clients in RequestTCPList
//If connected, remove them from the list and send a message back to Kad so we can send a ACK.
//If we don't connect, we need to remove the client..
//The sockets timeout should delete this object.
POSITION pos1, pos2;
for (pos1 = RequestTCPList.GetHeadPosition();( pos2 = pos1 ) != NULL;)
{
RequestTCPList.GetNext(pos1);
CUpDownClient* cur_client = RequestTCPList.GetAt(pos2);
switch(cur_client->GetKadState())
{
case KS_QUEUED_FWCHECK:
//I removed the self check here because we already did it when we added the client object.
cur_client->TryToConnect();
break;
case KS_CONNECTING_FWCHECK:
//Ignore this state as we are just waiting for results.
break;
case KS_CONNECTED_FWCHECK:
//Send the Kademlia client a TCP connection ack!
Kademlia::CKademlia::getUDPListener()->sendNullPacket(KADEMLIA_FIREWALLED_ACK, ntohl(cur_client->GetIP()), cur_client->GetKadPort());
cur_client->SetKadState(KS_NONE);
break;
case KS_QUEUED_BUDDY:
//a client lowID client wanting to be in the Kad network
break;
case KS_CONNECTED_BUDDY:
//a client lowID client wanting to be in the Kad network
//Not working at the moment so just set to none..
cur_client->SetKadState(KS_NONE);
default:
RemoveTCP(cur_client);
}
}
CleanUpClientList();
}
#ifdef _DEBUG
void CClientList::Debug_SocketDeleted(CClientReqSocket* deleted){
for (POSITION pos = list.GetHeadPosition(); pos != NULL;){
CUpDownClient* cur_client = list.GetNext(pos);
if (!AfxIsValidAddress(cur_client, sizeof(CUpDownClient))) {
AfxDebugBreak();
}
if (thePrefs.m_iDbgHeap >= 2)
ASSERT_VALID(cur_client);
if (cur_client->socket == deleted){
AfxDebugBreak();
}
}
}
#endif
bool CClientList::IsValidClient(CUpDownClient* tocheck)
{
if (thePrefs.m_iDbgHeap >= 2)
ASSERT_VALID(tocheck);
return list.Find(tocheck);
}
void CClientList::RequestTCP(Kademlia::CContact* contact)
{
uint32 nContactIP = ntohl(contact->getIPAddress());
//If eMule already knows this client, abort this.. It could cause conflicts.
//Although the odds of this happening is very small, it could still happen.
if (FindClientByIP(nContactIP, contact->getTCPPort()))
{
return;
}
// don't connect ourself
if (theApp.serverconnect->GetLocalIP() == nContactIP && thePrefs.GetPort() == contact->getTCPPort())
return;
//Add client to the lists to be processed.
CUpDownClient* pNewClient = new CUpDownClient(0, contact->getTCPPort(), contact->getIPAddress(), 0, 0, false );
pNewClient->SetKadPort(contact->getUDPPort());
pNewClient->SetKadState(KS_QUEUED_FWCHECK);
RequestTCPList.AddTail(pNewClient);
AddClient(pNewClient);
}
void CClientList::RemoveTCP(CUpDownClient* torem){
POSITION pos = RequestTCPList.Find(torem);
if(pos)
{
RequestTCPList.RemoveAt(pos);
}
}
void CClientList::CleanUpClientList(){
// we remove clients which are not needed any more by time
// this check is also done on CUpDownClient::Disconnected, however it will not catch all
// cases (if a client changes the state without beeing connected
//
// Adding this check directly to every point where any state changes would be more effective,
// is however not compatible with the current code, because there are points where a client has
// no state for some code lines and the code is also not prepared that a client object gets
// invalid while working with it (aka setting a new state)
// so this way is just the easy and safe one to go (as long as emule is basically single threaded)
const uint32 cur_tick = ::GetTickCount();
if (m_dwLastClientCleanUp + CLIENTLIST_CLEANUP_TIME < cur_tick ){
m_dwLastClientCleanUp = cur_tick;
POSITION pos1, pos2;
uint32 cDeleted = 0;
for (pos1 = list.GetHeadPosition();( pos2 = pos1 ) != NULL;){
list.GetNext(pos1);
CUpDownClient* pCurClient = list.GetAt(pos2);
if ((pCurClient->GetUploadState() == US_NONE || pCurClient->GetUploadState() == US_BANNED && !pCurClient->IsBanned())
&& pCurClient->GetDownloadState() == DS_NONE
&& pCurClient->GetChatState() == MS_NONE
&& pCurClient->GetKadState() == KS_NONE
&& pCurClient->socket == NULL)
{
cDeleted++;
delete pCurClient;
}
}
DEBUG_ONLY(AddDebugLogLine(false,_T("Cleaned ClientList, removed %i not used known clients"), cDeleted));
}
}
CDeletedClient::CDeletedClient(const CUpDownClient* pClient)
{
m_cBadRequest = 0;
m_dwInserted = ::GetTickCount();
PORTANDHASH porthash = { pClient->GetUserPort(), pClient->Credits()};
m_ItemsList.Add(porthash);
}
// ZZ:DownloadManager -->
void CClientList::ProcessA4AFClients() {
//if(thePrefs.GetLogA4AF()) AddDebugLogLine(false, _T(">>> Starting A4AF check"));
POSITION pos1, pos2;
for (pos1 = list.GetHeadPosition();( pos2 = pos1 ) != NULL;){
list.GetNext(pos1);
CUpDownClient* cur_client = list.GetAt(pos2);
if(cur_client->GetDownloadState() != DS_DOWNLOADING &&
cur_client->GetDownloadState() != DS_CONNECTED &&
(!cur_client->m_OtherRequests_list.IsEmpty() || !cur_client->m_OtherNoNeeded_list.IsEmpty())) {
//AddDebugLogLine(false, "+++ ZZ:DownloadManager: Trying for better file for source: %s '%s'", cur_client->GetUserName(), cur_client->reqfile->GetFileName());
cur_client->SwapToAnotherFile(_T("Periodic A4AF check CClientList::ProcessA4AFClients()"), false, false, false, NULL, true, false);
}
}
//if(thePrefs.GetLogA4AF()) AddDebugLogLine(false, _T(">>> Done with A4AF check"));
}
// <-- ZZ:DownloadManager
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -