📄 iocppacketheader.cpp
字号:
// IocpPacketHeader.cpp: implementation of the CIocpPacketHeader class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IocpPacketHeader.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
IMPLEMENT_SERIAL(CIocpPacketHeader, CObject, 1)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CIocpPacketHeader::CIocpPacketHeader()
{
m_wTag = PACKETTAG;
m_nVer = PACKET_VERSION_CURRENT;
m_gdHeader = GUID_NULL;
ResetHeader();
}
CIocpPacketHeader::~CIocpPacketHeader()
{
}
void CIocpPacketHeader::ResetHeader()
{
m_nCmd = m_wParam = m_lParam = m_nReserved = 0;
m_nErrCode = ERROR_NONE;
m_nDataLen = 0;
}
void CIocpPacketHeader::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
if (ar.IsLoading())
{
ar >> m_wTag;
ar >> m_nVer;
ar.Read(&m_gdHeader, sizeof(GUID));
ar >> m_nCmd;
ar >> m_wParam;
ar >> m_lParam;
ar >> m_nErrCode;
ar >> m_nDataLen;
ar >> m_nReserved;
}
else
{
ar << m_wTag;
ar << m_nVer;
ar.Write(&m_gdHeader, sizeof(GUID));
ar << m_nCmd;
ar << m_wParam;
ar << m_lParam;
ar << m_nErrCode;
ar << m_nDataLen;
ar << m_nReserved;
}
m_aFullName.Serialize(ar);
m_aFileName.Serialize(ar);
m_aFileLength.Serialize(ar);
}
void CIocpPacketHeader::operator=(const CIocpPacketHeader &srcHeader)
{
if (this==&srcHeader) return;
m_wTag = srcHeader.m_wTag;
m_nVer = srcHeader.m_nVer;
m_gdHeader = srcHeader.m_gdHeader;
m_nCmd = srcHeader.m_nCmd;
m_wParam = srcHeader.m_wParam;
m_lParam = srcHeader.m_lParam;
m_nErrCode = srcHeader.m_nErrCode;
m_nDataLen = srcHeader.m_nDataLen;
m_nReserved = srcHeader.m_nReserved;
m_aFullName.RemoveAll();
m_aFileName.RemoveAll();
m_aFileLength.RemoveAll();
for (int i=0; i<srcHeader.m_aFullName.GetSize(); i++)
{
CString strFullName=srcHeader.m_aFullName[i];
AddPacketFile(strFullName);
}
}
int CIocpPacketHeader::AddPacketFile(CString strFullName)
{
int nErr=ERROR_NONE;
if (strFullName.IsEmpty())
{
nErr = ERROR_FUNC_PARAM_INVALID;
return nErr;
}
ASSERT(m_aFullName.GetSize()==m_aFileLength.GetSize() &&
m_aFullName.GetSize()==m_aFileName.GetSize());
for (int i=0; i<m_aFullName.GetSize(); i++)
{
CString strFileName=m_aFullName[i];
if (strFileName.CompareNoCase(strFullName)==0)
break;
}
CFileStatus rStatus;
if (i>=m_aFullName.GetSize() &&
CFile::GetStatus(strFullName, rStatus))
{
m_aFullName.Add(strFullName);
m_aFileName.Add(CPublicFunc::GetShortName(strFullName));
m_aFileLength.Add(rStatus.m_size);
}
else
{
nErr = ERROR_IOCPPACKET_ADD_FILE;
}
return nErr;
}
int CIocpPacketHeader::RemovePacketFile(CString strFullName)
{
int nErr=ERROR_NONE;
if (strFullName.IsEmpty())
{
nErr = ERROR_FUNC_PARAM_INVALID;
return nErr;
}
ASSERT(m_aFullName.GetSize()==m_aFileLength.GetSize() &&
m_aFullName.GetSize()==m_aFileName.GetSize());
for (int i=0; i<m_aFullName.GetSize(); i++)
{
CString strFileName=m_aFullName[i];
if (strFileName.CompareNoCase(strFullName)==0)
{
m_aFullName.RemoveAt(i);
m_aFileName.RemoveAt(i);
m_aFileLength.RemoveAt(i);
break;
}
}
if (i>=m_aFullName.GetSize())
nErr = ERROR_IOCPPACKET_REMOVE_FILE;
return nErr;
}
int CIocpPacketHeader::DeleteAllFile(WORD wType, BOOL bDeleteAfterSend)
{
int nRet=0;
ASSERT(m_aFullName.GetSize()==m_aFileLength.GetSize() &&
m_aFullName.GetSize()==m_aFileName.GetSize());
for (int i=m_aFullName.GetSize()-1; i>=0; i--)
{
CString strFullName=m_aFullName[i];
CString strExtName=CPublicFunc::GetExtName(strFullName);
if (wType==PACKETTYPE_WRITE && bDeleteAfterSend)
{
//若是发送数据包则根据bDeleteAfterSend删除文件
nRet += (::DeleteFile(strFullName)?1:0);
}
else if (wType==PACKETTYPE_READ && strExtName.CompareNoCase(_T("tmp"))==0)
{
//若是接收数据包则将后缀名为tmp的文件删除
nRet += (::DeleteFile(strFullName)?1:0);
}
}
m_aFullName.RemoveAll();
m_aFileName.RemoveAll();
m_aFileLength.RemoveAll();
return nRet;
}
BOOL CIocpPacketHeader::GetPacketFile(int nIndex, CString& strFullName)
{
BOOL bRet=FALSE;
ASSERT(m_aFullName.GetSize()==m_aFileLength.GetSize() &&
m_aFullName.GetSize()==m_aFileName.GetSize());
if (nIndex>=0 && nIndex<m_aFullName.GetSize())
{
bRet = TRUE;
CString strOldName=m_aFullName[nIndex];
if (strFullName.IsEmpty())
{
//若传入文件名为空则直接返回原来的全路径名称
strFullName = strOldName;
}
else if (strOldName.CompareNoCase(strFullName)!=0)
{
if (::GetFileAttributes(strFullName)!=0xFFFFFFFF)
bRet = ::DeleteFile(strFullName); //若文件已存在则删除
if (bRet) bRet = ::MoveFile(strOldName, strFullName);
if (!bRet)
{
Logging4(ERRORLEVEL_ERROR, CErrorManager::Instance()->WSAErrorCodeToText(GetLastError()),
DOMAIN_NAME, CLASS_NAME_IOCPPACKET);
}
}
}
return bRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -