📄 corruptionblackbox.cpp
字号:
//this file is part of eMule
//Copyright (C)2002-2004 Merkur ( devs@emule-project.net / http://www.emule-project.net )
//
//This program 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.
//
//This program 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 this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "StdAfx.h"
#include "corruptionblackbox.h"
#include "knownfile.h"
#include "updownclient.h"
#include "log.h"
#include "emule.h"
#include "clientlist.h"
#include "opcodes.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CBB_BANTHRESHOLD 32 //% max corrupted data
CCBBRecord::CCBBRecord(uint32 nStartPos, uint32 nEndPos, uint32 dwIP, EBBRStatus BBRStatus){
if (nStartPos > nEndPos){
ASSERT( false );
return;
}
m_nStartPos = nStartPos;
m_nEndPos = nEndPos;
m_dwIP = dwIP;
m_BBRStatus = BBRStatus;
}
CCBBRecord& CCBBRecord::operator=(const CCBBRecord& cv)
{
m_nStartPos = cv.m_nStartPos;
m_nEndPos = cv.m_nEndPos;
m_dwIP = cv.m_dwIP;
m_BBRStatus = cv.m_BBRStatus;
return *this;
}
bool CCBBRecord::Merge(uint32 nStartPos, uint32 nEndPos, uint32 dwIP, EBBRStatus BBRStatus){
if (m_dwIP == dwIP && m_BBRStatus == BBRStatus && (nStartPos == m_nEndPos + 1 || nEndPos + 1 == m_nStartPos)){
if (nStartPos == m_nEndPos + 1)
m_nEndPos = nEndPos;
else if (nEndPos + 1 == m_nStartPos)
m_nStartPos = nStartPos;
else
ASSERT( false );
return true;
}
else
return false;
}
bool CCBBRecord::CanMerge(uint32 nStartPos, uint32 nEndPos, uint32 dwIP, EBBRStatus BBRStatus){
if (m_dwIP == dwIP && m_BBRStatus == BBRStatus && (nStartPos == m_nEndPos + 1 || nEndPos + 1 == m_nStartPos)){
return true;
}
else
return false;
}
void CCorruptionBlackBox::Init(uint32 nFileSize) {
m_aaRecords.SetSize(((uint64)nFileSize + (PARTSIZE - 1)) / PARTSIZE);
}
void CCorruptionBlackBox::Free() {
m_aaRecords.RemoveAll();
m_aaRecords.FreeExtra();
}
void CCorruptionBlackBox::TransferredData(uint32 nStartPos, uint32 nEndPos, const CUpDownClient* pSender){
if (nEndPos - nStartPos >= PARTSIZE){
ASSERT( false );
return;
}
if (nStartPos > nEndPos){
ASSERT( false );
return;
}
uint32 dwSenderIP = pSender->GetIP();
// we store records seperated for each part, so we don't have to search all entries everytime
// convert pos to relative block pos
uint16 nPart = nStartPos / PARTSIZE;
uint32 nRelStartPos = nStartPos - nPart*PARTSIZE;
uint32 nRelEndPos = nEndPos - nPart*PARTSIZE;
if (nRelEndPos >= PARTSIZE){
// data crosses the partborder, split it
nRelEndPos = PARTSIZE-1;
uint32 nTmpStartPos = nPart*PARTSIZE + nRelEndPos + 1;
ASSERT( nTmpStartPos % PARTSIZE == 0); // remove later
TransferredData(nTmpStartPos, nEndPos, pSender);
}
if (nPart >= m_aaRecords.GetCount()){
//ASSERT( false );
m_aaRecords.SetSize(nPart+1);
}
int posMerge = -1;
uint32 ndbgRewritten = 0;
for (int i= 0; i < m_aaRecords[nPart].GetCount(); i++){
if (m_aaRecords[nPart][i].CanMerge(nRelStartPos, nRelEndPos, dwSenderIP, BBR_NONE)){
posMerge = i;
}
// check if there is already an pending entry and overwrite it
else if (m_aaRecords[nPart][i].m_BBRStatus == BBR_NONE){
if (m_aaRecords[nPart][i].m_nStartPos >= nRelStartPos && m_aaRecords[nPart][i].m_nEndPos <= nRelEndPos){
// old one is included in new one -> delete
ndbgRewritten += (m_aaRecords[nPart][i].m_nEndPos-m_aaRecords[nPart][i].m_nStartPos)+1;
m_aaRecords[nPart].RemoveAt(i);
i--;
}
else if (m_aaRecords[nPart][i].m_nStartPos < nRelStartPos && m_aaRecords[nPart][i].m_nEndPos > nRelEndPos){
// old one includes new one
// check if the old one and new one have the same ip
if (dwSenderIP != m_aaRecords[nPart][i].m_dwIP){
// different IP, means we have to split it 2 times
uint32 nTmpEndPos1 = m_aaRecords[nPart][i].m_nEndPos;
uint32 nTmpStartPos1 = nRelEndPos + 1;
uint32 nTmpStartPos2 = m_aaRecords[nPart][i].m_nStartPos;
uint32 nTmpEndPos2 = nRelStartPos - 1;
m_aaRecords[nPart][i].m_nEndPos = nRelEndPos;
m_aaRecords[nPart][i].m_nStartPos = nRelStartPos;
uint32 dwOldIP = m_aaRecords[nPart][i].m_dwIP;
m_aaRecords[nPart][i].m_dwIP = dwSenderIP;
m_aaRecords[nPart].Add(CCBBRecord(nTmpStartPos1,nTmpEndPos1, dwOldIP));
m_aaRecords[nPart].Add(CCBBRecord(nTmpStartPos2,nTmpEndPos2, dwOldIP));
// and are done then
}
AddDebugLogLine(DLP_DEFAULT, false, _T("CorruptionBlackBox: Debug: %i bytes were rewritten and records replaced with new stats (1)"), (nRelEndPos - nRelStartPos)+1);
return;
}
else if (m_aaRecords[nPart][i].m_nStartPos >= nRelStartPos && m_aaRecords[nPart][i].m_nStartPos <= nRelEndPos){
// old one laps over new one on the right site
ASSERT( nRelEndPos - m_aaRecords[nPart][i].m_nStartPos > 0 );
ndbgRewritten += nRelEndPos - m_aaRecords[nPart][i].m_nStartPos;
m_aaRecords[nPart][i].m_nStartPos = nRelEndPos + 1;
}
else if (m_aaRecords[nPart][i].m_nEndPos >= nRelStartPos && m_aaRecords[nPart][i].m_nEndPos <= nRelEndPos){
// old one laps over new one on the left site
ASSERT( m_aaRecords[nPart][i].m_nEndPos - nRelStartPos > 0 );
ndbgRewritten += m_aaRecords[nPart][i].m_nEndPos - nRelStartPos;
m_aaRecords[nPart][i].m_nEndPos = nRelStartPos - 1;
}
}
}
if (posMerge != (-1) ){
VERIFY( m_aaRecords[nPart][posMerge].Merge(nRelStartPos, nRelEndPos, dwSenderIP, BBR_NONE) );
}
else
m_aaRecords[nPart].Add(CCBBRecord(nRelStartPos, nRelEndPos, dwSenderIP, BBR_NONE));
if (ndbgRewritten > 0){
AddDebugLogLine(DLP_DEFAULT, false, _T("CorruptionBlackBox: Debug: %i bytes were rewritten and records replaced with new stats (2)"), ndbgRewritten);
}
}
void CCorruptionBlackBox::VerifiedData(uint32 nStartPos, uint32 nEndPos){
if (nEndPos - nStartPos >= PARTSIZE){
ASSERT( false );
return;
}
// convert pos to relative block pos
uint16 nPart = nStartPos / PARTSIZE;
uint32 nRelStartPos = nStartPos - nPart*PARTSIZE;
uint32 nRelEndPos = nEndPos - nPart*PARTSIZE;
if (nRelEndPos >= PARTSIZE){
ASSERT( false );
return;
}
if (nPart >= m_aaRecords.GetCount()){
//ASSERT( false );
m_aaRecords.SetSize(nPart+1);
}
uint32 nDbgVerifiedBytes = 0;
uint32 nDbgOldEntries = m_aaRecords[nPart].GetCount();
#ifdef _DEBUG
CMap<int, int, int, int> mapDebug;
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -