BOOL AnalysisController::AnalsisFile(wstring pFileName)
{
m_Data->GetData().clear();
CTime start = CTime::GetCurrentTime();
tagNextDataInfo nextDataInfo;
m_pLogFile.OpenFile(pFileName);
m_pLogFile.ReadFileHeader();
// 读取数据
while(m_pLogFile.GetNoreadLen() > (INT64)0)
{
m_pLogFile.ReadDataHeaderAndEthernet(m_Data, nextDataInfo);
m_pLogFile.ReadData(m_Data, nextDataInfo);
m_pLogFile.MoveNextPos();
}
HANDLE m_hCMD;
CLogFile::CLogFile(void):
m_pMapAddrBegin(0)
,m_pFileAddr(0)
,m_noreadFileSize(0)
,m_n64ReadSize(0)
,m_n64MapOffsetBegin(0)
,m_n32MapNum(0)
,m_nEveryDataLenExceptEthernet(0)
{
SYSTEM_INFO sinf;
GetSystemInfo(&sinf);
m_dwAllocationGranularity = sinf.dwAllocationGranularity * 16 * 512;
}
CLogFile::~CLogFile(void)
{
//UnmapViewOfFile(m_pMapAddrBegin);
m_pMapAddrBegin = 0;
//CloseHandle(m_hFileMap);
}
BOOL CLogFile::NeedNextFilemap()
{
return m_n64ReadSize > m_n32MapNum * m_dwAllocationGranularity - 1;
}
bool CLogFile::OpenFile(wstring wsPath)
{
// 创建文件对象
HANDLE hFile = CreateFileW(wsPath.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(INVALID_HANDLE_VALUE != hFile)
{
// 获取文件大小
GetFileSizeEx(hFile, (PLARGE_INTEGER)&m_noreadFileSize);
// 创建文件映射对象
m_hFileMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
// 释放文件内核对象
CloseHandle(hFile);
if(NULL != m_hFileMap)
{
CreateViewofFile();
m_pFileAddr = m_pMapAddrBegin;
m_n32MapNum = m_n32MapNum + 1;
}
}
return false;
}
void CLogFile::CreateViewofFile()
{
UINT64 u64NewMapOffsetBegin = ((UINT64)m_dwAllocationGranularity) * m_n32MapNum;
m_pMapAddrBegin = (char*)MapViewOfFile(m_hFileMap, FILE_MAP_READ, UINT32(u64NewMapOffsetBegin >> 32) ,
UINT32(u64NewMapOffsetBegin & 0xffffffff), m_noreadFileSize < m_dwAllocationGranularity * 2 - 1 ? m_noreadFileSize
: m_dwAllocationGranularity * 2 - 1);
m_pMapAddrBegin = m_pMapAddrBegin + (m_n64ReadSize - u64NewMapOffsetBegin);
}
void CLogFile::MoveNextPos(UINT32 nMoveSize)
{
m_pFileAddr = m_pFileAddr + nMoveSize;
m_noreadFileSize = m_noreadFileSize - nMoveSize;
m_n64ReadSize = m_n64ReadSize + nMoveSize;
if(NeedNextFilemap())
{
UnmapViewOfFile(m_pMapAddrBegin);
CreateViewofFile();
m_n32MapNum = m_n32MapNum + 1;
m_pFileAddr = m_pMapAddrBegin;
}
}
void CLogFile::ReadFileHeader()
{
MoveNextPos(24);
}
void CLogFile::MoveNextPos()
{
MoveNextPos(m_nEveryDataLenExceptEthernet);
}
void CLogFile::ReadDataHeaderAndEthernet(IUBO* pUBO, tagNextDataInfo& nextData)
{
CDataHeadAndEthernet* header = (CDataHeadAndEthernet*)m_pFileAddr;
CreateNextDataInfo(pUBO, nextData, header);
PutHeaderAndEthernetDataToUBO(pUBO, header);
m_nEveryDataLenExceptEthernet = 16 + header->header.caplen;
}
void CLogFile::ReadData(IUBO* pUBO, tagNextDataInfo& nextData)
{
CEthernetPacketFactory* pParserFactory = CEthernetPacketFactory::CreateInstance();
ICustomPacket* pParser = pParserFactory->GetInstance(nextData.u16ProtocolType);
if(NULL != pParser)
{
pParser->Parse(nextData.pDataPos, nextData.nDataLen, pUBO, nextData);
ReadData(pUBO,nextData);
}
}
void CLogFile::CreateNextDataInfo(IUBO* pUBO, tagNextDataInfo &nextData, CDataHeadAndEthernet* header)
{
nextData.pDataPos = m_pFileAddr + sizeof(CDataHeadAndEthernet);
nextData.n64ProtocolIndexofSize = m_n64ReadSize + sizeof(CDataHeadAndEthernet);
// 获取下一条数据的长度和类型
USHORT type = ntohs(header->ethernet.type);
UINT32 nLen = header->header_caplen;
unsigned short u16NextDataType = 0;
nextData.u16ProtocolType = u16NextDataType;
nextData.nDataLen = nLen - EthernetIILen;
}
void CLogFile::PutHeaderAndEthernetDataToUBO( IUBO* pUBO, CDataHeadAndEthernet* header )
{
static UINT32 n32DataIndex = 0;
n32DataIndex ++;
pUBO->AddDataIndex(n32DataIndex);
pUBO->AddField(HeaderAndEthernet_ID,((UINT64)m_n64ReadSize << 16) + header->header.caplen);
pUBO->AddField(EthernetIIPacket_ID,(((UINT64)m_n64ReadSize + 16) << 16) + header->header.caplen - 16);
pUBO->AddField(EthernetIIPacket_ID);
}
INT64 CLogFile::GetNoreadLen()
{
return m_noreadFileSize;
}
void* CLogFile::GetMapofFilePoint(UINT64 ReadSize)
{
UINT64 modValue = ReadSize % m_dwAllocationGranularity;
UINT64 mapBeginIndex = ReadSize - modValue;
UINT64 distance = mapBeginIndex - m_n64MapOffsetBegin;
if( distance >= 0 && distance < m_dwAllocationGranularity)
{
m_pFileAddr = m_pMapAddrBegin + distance + modValue;
}else
{
UnmapViewOfFile(m_pMapAddrBegin);
UINT64 u64NewMapOffsetBegin = ReadSize - modValue;
m_pMapAddrBegin = (char*)MapViewOfFile(m_hFileMap, FILE_MAP_READ, UINT32(u64NewMapOffsetBegin >> 32) ,
UINT32(u64NewMapOffsetBegin & 0xffffffff), m_noreadFileSize < m_dwAllocationGranularity * 2 - 1 ? m_noreadFileSize
: m_dwAllocationGranularity * 2 - 1);
m_pMapAddrBegin = m_pMapAddrBegin + modValue;
m_pFileAddr = m_pMapAddrBegin;
}
return (void*)m_pFileAddr;
}