升龙物业 老版本 ocx IPO, 加密狗 转值班电话

PduEntity.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #include "StdAfx.h"
  2. #include "PduEntity.h"
  3. #include "PduEntityHead.h"
  4. #include "PduDataFormat.h"
  5. #include "PduDataFormatOne.h"
  6. /*****************************************************************
  7. **【函数名称】 CPduEntity
  8. **【函数功能】 默认构造函数
  9. **【参数】
  10. **【返回值】
  11. ****************************************************************/
  12. CPduEntity::CPduEntity()
  13. {
  14. m_nCmdType = PDU_CMD_UNKNOWN;
  15. // 初始化消息头
  16. InitData();
  17. }
  18. /*****************************************************************
  19. **【函数名称】 CPduEntity
  20. **【函数功能】 构造函数(赋值调用)
  21. **【参数】
  22. **【返回值】
  23. ****************************************************************/
  24. CPduEntity::CPduEntity(CPduEntity& a_Cmd)
  25. {
  26. InitData(a_Cmd);
  27. }
  28. /*****************************************************************
  29. **【函数名称】 operator =
  30. **【函数功能】 赋值运算符重载
  31. **【参数】
  32. **【返回值】
  33. ****************************************************************/
  34. CPduEntity& CPduEntity::operator = (CPduEntity& a_Cmd)
  35. {
  36. if(m_pDataInfo != NULL) delete m_pDataInfo;
  37. // 赋值
  38. InitData(a_Cmd);
  39. return *this;
  40. }
  41. /*****************************************************************
  42. **【函数名称】 CPduEntity
  43. **【函数功能】 构造函数(发送时调用)
  44. **【参数】 a_nCmdType 命令类型
  45. **【返回值】
  46. ****************************************************************/
  47. CPduEntity::CPduEntity(PDU_CMD_TYPE a_nCmdType)
  48. {
  49. m_nCmdType = a_nCmdType;
  50. // 初始化消息头
  51. InitData();
  52. // 初始化数据格式
  53. InitDataFormat(a_nCmdType);
  54. }
  55. /*****************************************************************
  56. **【函数名称】 CPduEntity
  57. **【函数功能】 构造函数(接收时调用)
  58. **【参数】 a_pHead 消息头
  59. a_szData 命令内容(不包括头)
  60. **【返回值】
  61. ****************************************************************/
  62. CPduEntity::CPduEntity(CPduEntityHead* a_pHead, UCHAR a_szData[])
  63. {
  64. // 消息头
  65. m_nCmdType = a_pHead->GetCmdType();
  66. m_nLocalId = a_pHead->GetSrcDevId();
  67. m_nLocalType = a_pHead->GetSrcDevType();
  68. m_nPeerId = a_pHead->GetDstDevId();
  69. m_nPeerType = a_pHead->GetDstDevType();
  70. m_bIsExecReturn = a_pHead->IsExecReturn();
  71. // 初始化数据格式
  72. InitDataFormat(m_nCmdType);
  73. // 解析消息内容
  74. if(m_pDataInfo != NULL) m_pDataInfo->DecodeData((CHAR*)a_szData);
  75. }
  76. /*****************************************************************
  77. **【函数名称】 ~CPduEntity
  78. **【函数功能】 析构
  79. **【参数】
  80. **【返回值】
  81. ****************************************************************/
  82. CPduEntity::~CPduEntity(void)
  83. {
  84. if(m_pDataInfo != NULL) delete m_pDataInfo;
  85. }
  86. /*****************************************************************
  87. **【函数名称】 InitData
  88. **【函数功能】 初始化命令基础属性到默认值
  89. **【参数】
  90. **【返回值】
  91. ****************************************************************/
  92. void CPduEntity::InitData()
  93. {
  94. m_nLocalId = 0;
  95. m_nLocalType = PDU_DEV_TYPE_UNKNOWN;
  96. m_nPeerId = 0;
  97. m_nPeerType = PDU_DEV_TYPE_UNKNOWN;
  98. m_bIsExecReturn = FALSE;
  99. m_hSocket = 0;
  100. // 初始化数据缓冲区
  101. m_pDataInfo = NULL;
  102. }
  103. /*****************************************************************
  104. **【函数名称】 InitData
  105. **【函数功能】 复制原命令内容到当前命令
  106. **【参数】 a_Cmd 原命令实体
  107. **【返回值】
  108. ****************************************************************/
  109. void CPduEntity::InitData(CPduEntity& a_Cmd)
  110. {
  111. // 复制基础属性
  112. m_nLocalType = a_Cmd.GetLocalDevType();
  113. m_nLocalId = a_Cmd.GetLocalDevId();
  114. m_nPeerType = a_Cmd.GetPeerDevType();
  115. m_nPeerId = a_Cmd.GetPeerDevId();
  116. m_nCmdType = a_Cmd.GetCmdType();
  117. m_bIsExecReturn = a_Cmd.GetIsExecReturn();
  118. m_hSocket = a_Cmd.GetAssoSocket();
  119. // 复制命令内容
  120. m_pDataInfo = new CPduDataFormatOne(m_nCmdType);
  121. a_Cmd.CopyDataMemo(m_pDataInfo);
  122. }
  123. /*****************************************************************
  124. **【函数名称】 InitDataFormat
  125. **【函数功能】 根据命令类型初始化数据格式
  126. **【参数】
  127. **【返回值】
  128. ****************************************************************/
  129. void CPduEntity::InitDataFormat(PDU_CMD_TYPE a_nCmdType)
  130. {
  131. // 得到当前命令的数据格式
  132. CPduDataFormat* pInstance = CPduDataFormat::getInstance();
  133. m_pDataInfo = pInstance->Clone(a_nCmdType);
  134. }
  135. /*****************************************************************
  136. **【函数名称】 CreatePackge
  137. **【函数功能】 生成要发送的数据包
  138. **【参数】
  139. **【返回值】 a_szBuf[] 数据包内容(HEAD+DATA)
  140. USHORT 数据包长度
  141. ****************************************************************/
  142. USHORT CPduEntity::CreatePackge(UCHAR a_szBuf[])
  143. {
  144. // 如果命令初始化失败
  145. ASSERT(m_pDataInfo != NULL);
  146. if(m_pDataInfo == NULL) return 0;
  147. // 填充数据
  148. USHORT nDataLen = 0;
  149. nDataLen = m_pDataInfo->IncodeData((CHAR*)&a_szBuf[PDU_HEAD_LEN]);
  150. // 打包包头
  151. CPduEntityHead head;
  152. head.Initialization(m_nLocalId, m_nLocalType, m_nPeerId, m_nPeerType, m_nCmdType, m_bIsExecReturn, nDataLen);
  153. head.Incode(a_szBuf);
  154. return PDU_HEAD_LEN + nDataLen;
  155. }
  156. /*****************************************************************
  157. **【函数名称】 CopyDataMemo
  158. **【函数功能】 拷贝所有数据
  159. **【参数】 a_pDataMemo 要拷贝数据的目标方
  160. **【返回值】
  161. ****************************************************************/
  162. void CPduEntity::CopyDataMemo(CPduDataFormatOne* a_pDataMemo)
  163. {
  164. // 如果命令初始化失败
  165. if(m_pDataInfo == NULL) return;
  166. // 开始复制数据
  167. for(int i=0; i<PDU_CMD_MAX_DATA_COUNT; i++)
  168. {
  169. if(m_pDataInfo->m_pDataMemoArray[i] == NULL) break;
  170. // 拷贝数据
  171. PduDataMemo* pDataMemo = new PduDataMemo;
  172. memcpy(pDataMemo, m_pDataInfo->m_pDataMemoArray[i], sizeof(PduDataMemo));
  173. a_pDataMemo->m_pDataMemoArray[i] = pDataMemo;
  174. } // end for
  175. }
  176. /*****************************************************************
  177. **【函数名称】 SetLocalDevInfo
  178. **【函数功能】 设置本端设备信息
  179. **【参数】
  180. **【返回值】
  181. ****************************************************************/
  182. void CPduEntity::SetLocalDevInfo(PDU_DEV_TYPE a_nLocalType, int a_nLocalId)
  183. {
  184. m_nLocalType = a_nLocalType;
  185. m_nLocalId = a_nLocalId;
  186. }
  187. /*****************************************************************
  188. **【函数名称】 SetPeerDevInfo
  189. **【函数功能】 设置对端设备信息
  190. **【参数】
  191. **【返回值】
  192. ****************************************************************/
  193. void CPduEntity::SetPeerDevInfo(PDU_DEV_TYPE a_nPeerType, int a_nPeerId)
  194. {
  195. m_nPeerType = a_nPeerType;
  196. m_nPeerId = a_nPeerId;
  197. }
  198. /*****************************************************************
  199. **【函数名称】 SetDataInt
  200. **【函数功能】 设置整型字段的内容
  201. **【参数】 a_nIndex 字段索引
  202. a_nData 数据内容
  203. **【返回值】
  204. ****************************************************************/
  205. void CPduEntity::SetDataInt(int a_nIndex, int a_nData)
  206. {
  207. // 如果命令初始化失败
  208. if(m_pDataInfo == NULL) return;
  209. // 开始写数据
  210. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  211. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  212. sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%d", a_nData);
  213. if(!m_pDataInfo->SetData(a_nIndex, 0, szBuf)) ASSERT(FALSE);
  214. }
  215. /*****************************************************************
  216. **【函数名称】 SetDataUInt
  217. **【函数功能】 设置无符号整型字段的内容
  218. **【参数】 a_nIndex 字段索引
  219. a_nData 数据内容
  220. **【返回值】
  221. ****************************************************************/
  222. void CPduEntity::SetDataUInt(int a_nIndex, unsigned int a_nData)
  223. {
  224. // 如果命令初始化失败
  225. if(m_pDataInfo == NULL) return;
  226. // 开始写数据
  227. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  228. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  229. sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%lu", a_nData);
  230. if(!m_pDataInfo->SetData(a_nIndex, 1, szBuf)) ASSERT(FALSE);
  231. }
  232. /*****************************************************************
  233. **【函数名称】 SetDataLong
  234. **【函数功能】 设置长整型字段的内容
  235. **【参数】 a_nIndex 字段索引
  236. a_nData 数据内容
  237. **【返回值】
  238. ****************************************************************/
  239. void CPduEntity::SetDataLong(int a_nIndex, long a_nData)
  240. {
  241. // 如果命令初始化失败
  242. if(m_pDataInfo == NULL) return;
  243. // 开始写数据
  244. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  245. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  246. sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%ld", a_nData);
  247. if(!m_pDataInfo->SetData(a_nIndex, 2, szBuf)) ASSERT(FALSE);
  248. }
  249. /*****************************************************************
  250. **【函数名称】 SetDataULong
  251. **【函数功能】 设置无符号长整型字段的内容
  252. **【参数】 a_nIndex 字段索引
  253. a_nData 数据内容
  254. **【返回值】
  255. ****************************************************************/
  256. void CPduEntity::SetDataULong(int a_nIndex, unsigned long a_nData)
  257. {
  258. // 如果命令初始化失败
  259. if(m_pDataInfo == NULL) return;
  260. // 开始写数据
  261. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  262. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  263. sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%lu", a_nData);
  264. if(!m_pDataInfo->SetData(a_nIndex, 3, szBuf)) ASSERT(FALSE);
  265. }
  266. /*****************************************************************
  267. **【函数名称】 SetDataBool
  268. **【函数功能】 设置布而型字段的内容
  269. **【参数】 a_nIndex 字段索引
  270. a_bData 数据内容
  271. **【返回值】
  272. ****************************************************************/
  273. void CPduEntity::SetDataBool(int a_nIndex, bool a_bData)
  274. {
  275. // 如果命令初始化失败
  276. if(m_pDataInfo == NULL) return;
  277. // 开始写数据
  278. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  279. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  280. if(a_bData)
  281. {
  282. sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%d", 1);
  283. }
  284. else
  285. {
  286. sprintf_s(szBuf, PDU_MAX_DATA_BUF_LEN, "%d", 0);
  287. } // end if
  288. if(!m_pDataInfo->SetData(a_nIndex, 4, szBuf)) ASSERT(FALSE);
  289. }
  290. /*****************************************************************
  291. **【函数名称】 SetDataLong
  292. **【函数功能】 设置长整型字段的内容
  293. **【参数】 a_nIndex 字段索引
  294. a_strData 数据内容
  295. **【返回值】
  296. ****************************************************************/
  297. void CPduEntity::SetDataString(int a_nIndex, LPCTSTR a_strData)
  298. {
  299. // 如果命令初始化失败
  300. if(m_pDataInfo == NULL) return;
  301. // 开始写数据
  302. if(!m_pDataInfo->SetData(a_nIndex, 5, a_strData)) ASSERT(FALSE);
  303. }
  304. /*****************************************************************
  305. **【函数名称】 GetDataInt
  306. **【函数功能】 读取整型字段的内容
  307. **【参数】 a_nIndex 字段索引
  308. **【返回值】
  309. ****************************************************************/
  310. int CPduEntity::GetDataInt(int a_nIndex)
  311. {
  312. // 如果命令初始化失败
  313. if(m_pDataInfo == NULL) return 0;
  314. // 开始读数据
  315. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  316. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  317. if(!m_pDataInfo->GetData(a_nIndex, 0, szBuf)) ASSERT(FALSE);
  318. int nRet = -1;
  319. sscanf_s(szBuf, "%d", &nRet);
  320. return nRet;
  321. }
  322. /*****************************************************************
  323. **【函数名称】 GetDataUInt
  324. **【函数功能】 读取无符号整型字段的内容
  325. **【参数】 a_nIndex 字段索引
  326. **【返回值】
  327. ****************************************************************/
  328. unsigned int CPduEntity::GetDataUInt(int a_nIndex)
  329. {
  330. // 如果命令初始化失败
  331. if(m_pDataInfo == NULL) return 0;
  332. // 开始读数据
  333. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  334. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  335. if(!m_pDataInfo->GetData(a_nIndex, 1, szBuf)) ASSERT(FALSE);
  336. unsigned int nRet = 0;
  337. sscanf_s(szBuf, "%lu", &nRet);
  338. return nRet;
  339. }
  340. /*****************************************************************
  341. **【函数名称】 GetDataLong
  342. **【函数功能】 读取长整型字段的内容
  343. **【参数】 a_nIndex 字段索引
  344. **【返回值】
  345. ****************************************************************/
  346. long CPduEntity::GetDataLong(int a_nIndex)
  347. {
  348. // 如果命令初始化失败
  349. if(m_pDataInfo == NULL) return 0;
  350. // 开始读数据
  351. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  352. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  353. if(!m_pDataInfo->GetData(a_nIndex, 2, szBuf)) ASSERT(FALSE);
  354. long nRet = -1;
  355. sscanf_s(szBuf, "%ld", &nRet);
  356. return nRet;
  357. }
  358. /*****************************************************************
  359. **【函数名称】 GetDataULong
  360. **【函数功能】 读取无符号长整型字段的内容
  361. **【参数】 a_nIndex 字段索引
  362. **【返回值】
  363. ****************************************************************/
  364. unsigned long CPduEntity::GetDataULong(int a_nIndex)
  365. {
  366. // 如果命令初始化失败
  367. if(m_pDataInfo == NULL) return 0;
  368. // 开始读数据
  369. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  370. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  371. if(!m_pDataInfo->GetData(a_nIndex, 3, szBuf)) ASSERT(FALSE);
  372. unsigned long nRet = 0;
  373. sscanf_s(szBuf, "%lu", &nRet);
  374. return nRet;
  375. }
  376. /*****************************************************************
  377. **【函数名称】 GetDataBool
  378. **【函数功能】 读取布而型字段的内容
  379. **【参数】 a_nIndex 字段索引
  380. **【返回值】
  381. ****************************************************************/
  382. bool CPduEntity::GetDataBool(int a_nIndex)
  383. {
  384. // 如果命令初始化失败
  385. if(m_pDataInfo == NULL) return false;
  386. // 开始读数据
  387. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  388. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  389. if(!m_pDataInfo->GetData(a_nIndex, 4, szBuf)) ASSERT(FALSE);
  390. return strcmp(szBuf, "0") == 0 ? false : true;
  391. }
  392. /*****************************************************************
  393. **【函数名称】 GetDataString
  394. **【函数功能】 读取字符串型字段的内容
  395. **【参数】 a_nIndex 字段索引
  396. **【返回值】
  397. ****************************************************************/
  398. CString CPduEntity::GetDataString(int a_nIndex)
  399. {
  400. // 如果命令初始化失败
  401. if(m_pDataInfo == NULL) return "";
  402. // 开始读数据
  403. CHAR szBuf[PDU_MAX_DATA_BUF_LEN];
  404. ZeroMemory(szBuf, PDU_MAX_DATA_BUF_LEN);
  405. if(!m_pDataInfo->GetData(a_nIndex, 5, szBuf)) ASSERT(FALSE);
  406. return szBuf;
  407. }