| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- #include "StdAfx.h"
- #include "PduEntity.h"
- #include "PduEntityHead.h"
- #include "PduDataFormat.h"
- #include "PduDataFormatOne.h"
- /*****************************************************************
- **【函数名称】 CPduEntity
- **【函数功能】 默认构造函数
- **【参数】
- **【返回值】
- ****************************************************************/
- CPduEntity::CPduEntity()
- {
- m_nCmdType = PDU_CMD_UNKNOWN;
- // 初始化消息头
- InitData();
- }
- /*****************************************************************
- **【函数名称】 CPduEntity
- **【函数功能】 构造函数(赋值调用)
- **【参数】
- **【返回值】
- ****************************************************************/
- CPduEntity::CPduEntity(CPduEntity& a_Cmd)
- {
- InitData(a_Cmd);
- }
- /*****************************************************************
- **【函数名称】 operator =
- **【函数功能】 赋值运算符重载
- **【参数】
- **【返回值】
- ****************************************************************/
- CPduEntity& CPduEntity::operator = (CPduEntity& a_Cmd)
- {
- if(m_pDataInfo != NULL) delete m_pDataInfo;
- // 赋值
- InitData(a_Cmd);
- return *this;
- }
- /*****************************************************************
- **【函数名称】 CPduEntity
- **【函数功能】 构造函数(发送时调用)
- **【参数】 a_nCmdType 命令类型
- **【返回值】
- ****************************************************************/
- CPduEntity::CPduEntity(PDU_CMD_TYPE a_nCmdType)
- {
- m_nCmdType = a_nCmdType;
- // 初始化消息头
- InitData();
- // 初始化数据格式
- InitDataFormat(a_nCmdType);
- }
- /*****************************************************************
- **【函数名称】 CPduEntity
- **【函数功能】 构造函数(接收时调用)
- **【参数】 a_pHead 消息头
- a_szData 命令内容(不包括头)
- **【返回值】
- ****************************************************************/
- CPduEntity::CPduEntity(CPduEntityHead* a_pHead, UCHAR a_szData[])
- {
- // 消息头
- m_nCmdType = a_pHead->GetCmdType();
- m_nLocalId = a_pHead->GetSrcDevId();
- m_nLocalType = a_pHead->GetSrcDevType();
- m_nPeerId = a_pHead->GetDstDevId();
- m_nPeerType = a_pHead->GetDstDevType();
- m_bIsExecReturn = a_pHead->IsExecReturn();
- // 初始化数据格式
- InitDataFormat(m_nCmdType);
- // 解析消息内容
- if(m_pDataInfo != NULL) m_pDataInfo->DecodeData((CHAR*)a_szData);
- }
- /*****************************************************************
- **【函数名称】 ~CPduEntity
- **【函数功能】 析构
- **【参数】
- **【返回值】
- ****************************************************************/
- CPduEntity::~CPduEntity(void)
- {
- if(m_pDataInfo != NULL) delete m_pDataInfo;
- }
- /*****************************************************************
- **【函数名称】 InitData
- **【函数功能】 初始化命令基础属性到默认值
- **【参数】
- **【返回值】
- ****************************************************************/
- void CPduEntity::InitData()
- {
- m_nLocalId = 0;
- m_nLocalType = PDU_DEV_TYPE_UNKNOWN;
- m_nPeerId = 0;
- m_nPeerType = PDU_DEV_TYPE_UNKNOWN;
- m_bIsExecReturn = FALSE;
- m_hSocket = 0;
- // 初始化数据缓冲区
- m_pDataInfo = NULL;
- }
- /*****************************************************************
- **【函数名称】 InitData
- **【函数功能】 复制原命令内容到当前命令
- **【参数】 a_Cmd 原命令实体
- **【返回值】
- ****************************************************************/
- void CPduEntity::InitData(CPduEntity& a_Cmd)
- {
- // 复制基础属性
- m_nLocalType = a_Cmd.GetLocalDevType();
- m_nLocalId = a_Cmd.GetLocalDevId();
- m_nPeerType = a_Cmd.GetPeerDevType();
- m_nPeerId = a_Cmd.GetPeerDevId();
- m_nCmdType = a_Cmd.GetCmdType();
- m_bIsExecReturn = a_Cmd.GetIsExecReturn();
- m_hSocket = a_Cmd.GetAssoSocket();
-
- // 复制命令内容
- m_pDataInfo = new CPduDataFormatOne(m_nCmdType);
- a_Cmd.CopyDataMemo(m_pDataInfo);
- }
- /*****************************************************************
- **【函数名称】 InitDataFormat
- **【函数功能】 根据命令类型初始化数据格式
- **【参数】
- **【返回值】
- ****************************************************************/
- void CPduEntity::InitDataFormat(PDU_CMD_TYPE a_nCmdType)
- {
- // 得到当前命令的数据格式
- CPduDataFormat* pInstance = CPduDataFormat::getInstance();
- m_pDataInfo = pInstance->Clone(a_nCmdType);
- }
- /*****************************************************************
- **【函数名称】 CreatePackge
- **【函数功能】 生成要发送的数据包
- **【参数】
- **【返回值】 a_szBuf[] 数据包内容(HEAD+DATA)
- USHORT 数据包长度
- ****************************************************************/
- USHORT CPduEntity::CreatePackge(UCHAR a_szBuf[])
- {
- // 如果命令初始化失败
- ASSERT(m_pDataInfo != NULL);
- if(m_pDataInfo == NULL) return 0;
- // 填充数据
- USHORT nDataLen = 0;
- nDataLen = m_pDataInfo->IncodeData((CHAR*)&a_szBuf[PDU_HEAD_LEN]);
- // 打包包头
- CPduEntityHead head;
- head.Initialization(m_nLocalId, m_nLocalType, m_nPeerId, m_nPeerType, m_nCmdType, m_bIsExecReturn, nDataLen);
- head.Incode(a_szBuf);
- return PDU_HEAD_LEN + nDataLen;
- }
- /*****************************************************************
- **【函数名称】 CopyDataMemo
- **【函数功能】 拷贝所有数据
- **【参数】 a_pDataMemo 要拷贝数据的目标方
- **【返回值】
- ****************************************************************/
- void CPduEntity::CopyDataMemo(CPduDataFormatOne* a_pDataMemo)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return;
- // 开始复制数据
- for(int i=0; i<PDU_CMD_MAX_DATA_COUNT; i++)
- {
- if(m_pDataInfo->m_pDataMemoArray[i] == NULL) break;
-
- // 拷贝数据
- PduDataMemo* pDataMemo = new PduDataMemo;
- memcpy(pDataMemo, m_pDataInfo->m_pDataMemoArray[i], sizeof(PduDataMemo));
- a_pDataMemo->m_pDataMemoArray[i] = pDataMemo;
- } // end for
- }
- /*****************************************************************
- **【函数名称】 SetLocalDevInfo
- **【函数功能】 设置本端设备信息
- **【参数】
- **【返回值】
- ****************************************************************/
- void CPduEntity::SetLocalDevInfo(PDU_DEV_TYPE a_nLocalType, int a_nLocalId)
- {
- m_nLocalType = a_nLocalType;
- m_nLocalId = a_nLocalId;
- }
- /*****************************************************************
- **【函数名称】 SetPeerDevInfo
- **【函数功能】 设置对端设备信息
- **【参数】
- **【返回值】
- ****************************************************************/
- void CPduEntity::SetPeerDevInfo(PDU_DEV_TYPE a_nPeerType, int a_nPeerId)
- {
- m_nPeerType = a_nPeerType;
- m_nPeerId = a_nPeerId;
- }
- /*****************************************************************
- **【函数名称】 SetDataInt
- **【函数功能】 设置整型字段的内容
- **【参数】 a_nIndex 字段索引
- a_nData 数据内容
- **【返回值】
- ****************************************************************/
- void CPduEntity::SetDataInt(int a_nIndex, int a_nData)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return;
- // 开始写数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%d", a_nData);
- if(!m_pDataInfo->SetData(a_nIndex, 0, szBuf)) ASSERT(FALSE);
- }
- /*****************************************************************
- **【函数名称】 SetDataUInt
- **【函数功能】 设置无符号整型字段的内容
- **【参数】 a_nIndex 字段索引
- a_nData 数据内容
- **【返回值】
- ****************************************************************/
- void CPduEntity::SetDataUInt(int a_nIndex, unsigned int a_nData)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return;
- // 开始写数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%lu", a_nData);
- if(!m_pDataInfo->SetData(a_nIndex, 1, szBuf)) ASSERT(FALSE);
- }
- /*****************************************************************
- **【函数名称】 SetDataLong
- **【函数功能】 设置长整型字段的内容
- **【参数】 a_nIndex 字段索引
- a_nData 数据内容
- **【返回值】
- ****************************************************************/
- void CPduEntity::SetDataLong(int a_nIndex, long a_nData)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return;
- // 开始写数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%ld", a_nData);
- if(!m_pDataInfo->SetData(a_nIndex, 2, szBuf)) ASSERT(FALSE);
- }
- /*****************************************************************
- **【函数名称】 SetDataULong
- **【函数功能】 设置无符号长整型字段的内容
- **【参数】 a_nIndex 字段索引
- a_nData 数据内容
- **【返回值】
- ****************************************************************/
- void CPduEntity::SetDataULong(int a_nIndex, unsigned long a_nData)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return;
- // 开始写数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%lu", a_nData);
- if(!m_pDataInfo->SetData(a_nIndex, 3, szBuf)) ASSERT(FALSE);
- }
- /*****************************************************************
- **【函数名称】 SetDataBool
- **【函数功能】 设置布而型字段的内容
- **【参数】 a_nIndex 字段索引
- a_bData 数据内容
- **【返回值】
- ****************************************************************/
- void CPduEntity::SetDataBool(int a_nIndex, bool a_bData)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return;
- // 开始写数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- if(a_bData)
- {
- sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%d", 1);
- }
- else
- {
- sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%d", 0);
- } // end if
- if(!m_pDataInfo->SetData(a_nIndex, 4, szBuf)) ASSERT(FALSE);
- }
- /*****************************************************************
- **【函数名称】 SetDataLong
- **【函数功能】 设置长整型字段的内容
- **【参数】 a_nIndex 字段索引
- a_strData 数据内容
- **【返回值】
- ****************************************************************/
- void CPduEntity::SetDataString(int a_nIndex, LPCTSTR a_strData)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return;
- // 开始写数据
- if(!m_pDataInfo->SetData(a_nIndex, 5, a_strData)) ASSERT(FALSE);
- }
- /*****************************************************************
- **【函数名称】 GetDataInt
- **【函数功能】 读取整型字段的内容
- **【参数】 a_nIndex 字段索引
- **【返回值】
- ****************************************************************/
- int CPduEntity::GetDataInt(int a_nIndex)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return 0;
- // 开始读数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- if(!m_pDataInfo->GetData(a_nIndex, 0, szBuf)) ASSERT(FALSE);
- int nRet = -1;
- sscanf_s(szBuf, "%d", &nRet);
- return nRet;
- }
- /*****************************************************************
- **【函数名称】 GetDataUInt
- **【函数功能】 读取无符号整型字段的内容
- **【参数】 a_nIndex 字段索引
- **【返回值】
- ****************************************************************/
- unsigned int CPduEntity::GetDataUInt(int a_nIndex)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return 0;
-
- // 开始读数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- if(!m_pDataInfo->GetData(a_nIndex, 1, szBuf)) ASSERT(FALSE);
- unsigned int nRet = 0;
- sscanf_s(szBuf, "%lu", &nRet);
- return nRet;
- }
- /*****************************************************************
- **【函数名称】 GetDataLong
- **【函数功能】 读取长整型字段的内容
- **【参数】 a_nIndex 字段索引
- **【返回值】
- ****************************************************************/
- long CPduEntity::GetDataLong(int a_nIndex)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return 0;
- // 开始读数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- if(!m_pDataInfo->GetData(a_nIndex, 2, szBuf)) ASSERT(FALSE);
- long nRet = -1;
- sscanf_s(szBuf, "%ld", &nRet);
- return nRet;
- }
- /*****************************************************************
- **【函数名称】 GetDataULong
- **【函数功能】 读取无符号长整型字段的内容
- **【参数】 a_nIndex 字段索引
- **【返回值】
- ****************************************************************/
- unsigned long CPduEntity::GetDataULong(int a_nIndex)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return 0;
- // 开始读数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- if(!m_pDataInfo->GetData(a_nIndex, 3, szBuf)) ASSERT(FALSE);
- unsigned long nRet = 0;
- sscanf_s(szBuf, "%lu", &nRet);
- return nRet;
- }
- /*****************************************************************
- **【函数名称】 GetDataBool
- **【函数功能】 读取布而型字段的内容
- **【参数】 a_nIndex 字段索引
- **【返回值】
- ****************************************************************/
- bool CPduEntity::GetDataBool(int a_nIndex)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return false;
- // 开始读数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- if(!m_pDataInfo->GetData(a_nIndex, 4, szBuf)) ASSERT(FALSE);
- return strcmp(szBuf, "0") == 0 ? false : true;
- }
- /*****************************************************************
- **【函数名称】 GetDataString
- **【函数功能】 读取字符串型字段的内容
- **【参数】 a_nIndex 字段索引
- **【返回值】
- ****************************************************************/
- CString CPduEntity::GetDataString(int a_nIndex)
- {
- // 如果命令初始化失败
- if(m_pDataInfo == NULL) return "";
- // 开始读数据
- CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
- ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
- if(!m_pDataInfo->GetData(a_nIndex, 5, szBuf)) ASSERT(FALSE);
- return szBuf;
- }
|