TcpParse.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #include "stdafx.h"
  2. #include "kernel.h"
  3. #include "Client2SrvType.h"
  4. #include "TcpParse.h"
  5. #include "crc32.h"
  6. #include "SocketHandle.h"
  7. #include "Global.h"
  8. CTcpParse::CTcpParse(void)
  9. {
  10. }
  11. CTcpParse::~CTcpParse(void)
  12. {
  13. }
  14. unsigned int CTcpParse::CalcCheckSum( void *pData, unsigned int nSize )
  15. {
  16. unsigned int checksum = 0;
  17. if ( nSize <= sizeof( ProtocolHeader ) )
  18. {
  19. return 0;
  20. }
  21. unsigned char *pBody = &( ( unsigned char* )pData )[ sizeof( ProtocolHeader ) ];
  22. nSize -= sizeof( ProtocolHeader );
  23. checksum = crc32( 0, pBody, nSize );
  24. return checksum;
  25. }
  26. void CTcpParse::ProcessDevOnline(void *pSocketHandle, void *pData, int nLen)
  27. {
  28. ReqDevOnline *pReqDevOnline = (ReqDevOnline *)pData;
  29. CAnalog *pAnalog = pVariantsManager->FindAnalog( pReqDevOnline->nVarID );
  30. pAnalog->m_fData = (float)pReqDevOnline->nOnline;
  31. }
  32. void CTcpParse::ProcessHeart(void *pSocketHandle, void *pData, int nLen)
  33. {
  34. ((CSocketHandle *)pSocketHandle)->Write((const LPBYTE)pData, nLen, NULL);
  35. }
  36. void CTcpParse::ProcessSingleVarOnline(void *pSocketHandle, void *pData, int nLen)
  37. {
  38. }
  39. void CTcpParse::ProcessMulVarOnline(void *pSocketHandle, void *pData, int nLen)
  40. {
  41. }
  42. int CTcpParse::GetVarstatus(float fParaValue, int iUpperLimit, int iLowerLimit, int iNormalState)
  43. {
  44. int iResult = iNormalState;
  45. if ( (iUpperLimit > 0 || iLowerLimit > 0) && iUpperLimit > iLowerLimit )
  46. {
  47. //iResult = (iParaValue >= iUpperLimit || iParaValue <= iLowerLimit);
  48. if( fParaValue > iUpperLimit )
  49. {
  50. iResult = UPPER_LIMIT_ID; // 上限告警
  51. }
  52. if( fParaValue < iLowerLimit )
  53. {
  54. iResult = LOWER_LIMIT_ID; // 下限告警
  55. }
  56. else
  57. {
  58. return iResult;
  59. }
  60. }
  61. else if( (int)fParaValue != iNormalState )
  62. {
  63. iResult = (int)fParaValue;
  64. }
  65. return iResult;
  66. }
  67. void CTcpParse::ProcessReadSingleVarData(void *pSocketHandle, void *pData, int nLen)
  68. {
  69. ReqSingleVarData *pReqSingleVarData = (ReqSingleVarData *)pData;
  70. CAnalog *pAnalog = pVariantsManager->FindAnalog( pReqSingleVarData->nVarID );
  71. pAnalog->m_fData = (float)pReqSingleVarData->Data;
  72. }
  73. void CTcpParse::ProcessReadSpecialMulVarData(void *pSocketHandle, void *pData, int nLen)
  74. {
  75. }
  76. void CTcpParse::ProcessReadSpecialSingleVarData(void *pSocketHandle, void *pData, int nLen)
  77. {
  78. ReqSingleSpecialVarData *pReqSingleSpecialVarData = (ReqSingleSpecialVarData *)pData;
  79. CAnalog *pAnalog = pVariantsManager->FindAnalog( pReqSingleSpecialVarData->iVarid );
  80. pAnalog->m_fData = (float)pReqSingleSpecialVarData->Data;
  81. }
  82. void CTcpParse::ProcessReadMulVarData(void *pSocketHandle, void *pData, int nLen)
  83. {
  84. }
  85. void CTcpParse::ProcessWriteSingleVarData(void *pSocketHandle, void *pData, int nLen)
  86. {
  87. ReqWrSingleVarData *pReqWrSingleVarData = (ReqWrSingleVarData*)pData;
  88. CAnalog *pAnalog = pVariantsManager->FindAnalog( pReqWrSingleVarData->iVarid );
  89. }
  90. void CTcpParse::ProcessWriteMulVarData(void *pSocketHandle, void *pData, int nLen)
  91. {
  92. }
  93. void CTcpParse::ProcessDogData(void *pSocketHandle, void *pData, int nLen)
  94. {
  95. ReqDogData *pReqDogData = (ReqDogData *)pData;
  96. if( pReqDogData->nPastDue==1 )
  97. pDevicesManager->m_bIsPastDue = true;
  98. else
  99. pDevicesManager->m_bIsPastDue = false;
  100. }
  101. void CTcpParse::ProcessAlarmConfirm(void *pSocketHandle, void *pData, int nLen)
  102. {
  103. }
  104. int CTcpParse::OnCmdProcess(void *pSocketHandle, void *pData)
  105. {
  106. ProtocolHeader *pHeader = (ProtocolHeader *)pData;
  107. if( pHeader == NULL ) return -1;
  108. if( pHeader->nLen < 0 || pHeader->nLen > 65535 ) return -1;
  109. unsigned int tmp = CalcCheckSum(pHeader, pHeader->nLen);
  110. if( tmp != pHeader->Verify )
  111. {
  112. return -1;
  113. }
  114. switch( pHeader->nCmd )
  115. {
  116. case CMD_DEV_ONLINE: // 请求设备在线回应
  117. ProcessDevOnline(pSocketHandle, pData, pHeader->nLen);
  118. break;
  119. case CMD_SINGLE_VAR_ONLINE: // 请求单个变量在线回应
  120. ProcessSingleVarOnline(pSocketHandle, pData, pHeader->nLen);
  121. break;
  122. case CMD_MUL_VAR_ONLINE: // 请求多个变量在线回应
  123. ProcessMulVarOnline(pSocketHandle, pData, pHeader->nLen);
  124. break;
  125. case CMD_READ_SINGLE_VAR_DATA: // 请求单个变量内容回应
  126. ProcessReadSingleVarData(pSocketHandle, pData, pHeader->nLen);
  127. break;
  128. case CMD_READ_MUL_VAR_DATA: // 请求多个变量内容回应
  129. ProcessReadMulVarData(pSocketHandle, pData, pHeader->nLen);
  130. break;
  131. case CMD_WRITE_SINGLE_VAR_DATA: // 请求写单个变量回应
  132. ProcessWriteSingleVarData(pSocketHandle, pData, pHeader->nLen);
  133. break;
  134. case CMD_WRITE_MUL_VAR_DATA: // 请求写多个变量回应
  135. ProcessWriteMulVarData(pSocketHandle, pData, pHeader->nLen);
  136. break;
  137. case CMD_READ_SPECIAL_SINGLE_VAR_DATA:
  138. ProcessReadSpecialSingleVarData(pSocketHandle, pData, pHeader->nLen);
  139. break;
  140. case CMD_READ_SPECIAL_MUL_VAR_DATA:
  141. ProcessReadSpecialMulVarData(pSocketHandle, pData, pHeader->nLen);
  142. break;
  143. case CMD_DOG_DATA: // 请求狗数据回应
  144. ProcessDogData(pSocketHandle, pData, pHeader->nLen);
  145. break;
  146. case CMD_ALARM_CONFIRM: // 请求报警确认回应
  147. ProcessAlarmConfirm(pSocketHandle, pData, pHeader->nLen);
  148. break;
  149. case CMD_HEART:
  150. ProcessHeart(pSocketHandle, pData, pHeader->nLen);
  151. break;
  152. case CMD_ALARM_VAR_DATA:
  153. ProcessAlarmData(pSocketHandle, pData, pHeader->nLen);
  154. break;
  155. }
  156. return 0;
  157. }
  158. void CTcpParse::ProcessData(void *pSocketHandle, const BYTE* pData, DWORD nLen)
  159. {
  160. DWORD nBuffIndex = 0;
  161. CSocketHandle* pSH = (CSocketHandle *)pSocketHandle;
  162. EnterCriticalSection( &(pSH->m_hClient2SrvSection) );
  163. while( nBuffIndex < nLen )
  164. {
  165. //LOG4C((LOG_NOTICE, "接收长度=%d, 处理长度=%d, PendingSize=%d", nLen, nBuffIndex, pSH->m_nPendingSize));
  166. ProtocolHeader *pHeader; //当前协议包头
  167. DWORD nProcessedLen = 0; //当前循环处理了多少个字节
  168. if( pSH->m_nPendingSize > 0 ) // 开始组包
  169. {
  170. //LOG4C((LOG_NOTICE, "开始组包"));
  171. pHeader = (ProtocolHeader *)pSH->m_PendingBuffer;
  172. if( pSH->m_nPendingSize < sizeof(ProtocolHeader) ) //上一次接收到的长度小于包头
  173. {
  174. DWORD nLinkHeaderLen = sizeof( ProtocolHeader ) - pSH->m_nPendingSize;
  175. if( nLinkHeaderLen <= nLen ) //这次可以收完包头
  176. {
  177. memcpy( &pSH->m_PendingBuffer[ pSH->m_nPendingSize ], pData, nLinkHeaderLen ); //这里已经收完Header
  178. nProcessedLen = pHeader->nLen - pSH->m_nPendingSize;
  179. if( nProcessedLen <= nLen ) //如果所需处理的长度小于等于当前包长度
  180. {
  181. memcpy( &pSH->m_PendingBuffer[ sizeof( ProtocolHeader ) ],
  182. & ( ( char *) pData )[ nLinkHeaderLen ],
  183. pHeader->nLen - sizeof( ProtocolHeader ) );
  184. //LOG4C((LOG_NOTICE, "收完所需的包"));
  185. pSH->m_nPendingSize = 0; // 收完所需的包,置m_nPendingSize为0
  186. }
  187. else
  188. {
  189. int nTemp = nLen - nLinkHeaderLen; //除去头剩余部分的长度
  190. if ( nTemp > 0 ) //刚好是Header的长度,不用拷贝内存,所以这里加了>0的判断
  191. {
  192. memcpy( &pSH->m_PendingBuffer[ sizeof( ProtocolHeader ) ],
  193. & ( ( char *) pData )[ nLinkHeaderLen ],
  194. nTemp );
  195. }
  196. pSH->m_nPendingSize += nLen;
  197. }
  198. }
  199. else //这次还是没有收完包头, 继续Pending
  200. {
  201. //LOG4C((LOG_NOTICE, "这次还是没有收完包头, 继续Pending"));
  202. memcpy( &pSH->m_PendingBuffer[ pSH->m_nPendingSize ], pData, nLen );
  203. pSH->m_nPendingSize += nLen;
  204. nProcessedLen = nLen;
  205. }
  206. }
  207. else //Header部分已经在阻塞的缓冲区中
  208. {
  209. nProcessedLen = pHeader->nLen - pSH->m_nPendingSize;
  210. if ( nProcessedLen <= nLen ) //如果需要处理的长度小于现有包的长度
  211. {
  212. memcpy( &pSH->m_PendingBuffer[ pSH->m_nPendingSize ], pData, nProcessedLen );
  213. pSH->m_nPendingSize = 0;
  214. }
  215. else //否则要继续阻塞
  216. {
  217. memcpy( &pSH->m_PendingBuffer[ pSH->m_nPendingSize ], pData, nLen );
  218. pSH->m_nPendingSize += nLen;
  219. }
  220. }
  221. }
  222. else //第一次接包
  223. {
  224. //LOG4C((LOG_NOTICE, "第一次接包"));
  225. pHeader = (ProtocolHeader *)&( (unsigned char *)pData )[nBuffIndex];
  226. if( nLen - nBuffIndex < sizeof(ProtocolHeader) ) // 没有收够包头,先记录当前收到的Buffer
  227. {
  228. //如果第一次接包就没有收够包头,认为是非法包,扔掉,就是说已处理的长度nProcessedLen = 0
  229. pSH->m_nPendingSize = nLen - nBuffIndex;
  230. memcpy(pSH->m_PendingBuffer, pHeader, pSH->m_nPendingSize);
  231. LOG4C((LOG_NOTICE, "第一次接包就没有收够包头,认为是非法包,扔掉"));
  232. }
  233. else
  234. {
  235. nProcessedLen = pHeader->nLen;
  236. if( (int)pHeader->nLen > nLen - nBuffIndex )
  237. {
  238. memcpy(pSH->m_PendingBuffer, pHeader, nLen - nBuffIndex);
  239. //如果第一次接包,pHeader->nLen大于当前包的总长,认为是非法包,扔掉
  240. if( nBuffIndex == 0 )
  241. {
  242. //组包错误,则扔掉当前包
  243. LOG4C((LOG_NOTICE, "第一次接包,服务器pHeader->nLen大于当前包的总长,认为是非法包,扔掉\r\n"));
  244. }
  245. pSH->m_nPendingSize = nLen - nBuffIndex;
  246. nProcessedLen = nLen - nBuffIndex;
  247. }
  248. else
  249. {
  250. //LOG4C((LOG_NOTICE, "正常包"));
  251. pSH->m_nPendingSize = 0;
  252. }
  253. }
  254. }
  255. if ( nProcessedLen == 0 )
  256. {
  257. // 没有收够包头,认为是非法包,扔掉
  258. LOG4C((LOG_NOTICE, "没有收够包头,认为是非法包,扔掉"));
  259. break;
  260. }
  261. if ( pSH->m_nPendingSize == 0 )
  262. {
  263. if ( pHeader->nLen > SOCKET_BUFFSIZE )
  264. {
  265. // 包长度超过限制
  266. LOG4C((LOG_NOTICE, "pHeader->nLen超过限制"));
  267. }
  268. if(-1 == OnCmdProcess( pSocketHandle, pHeader ))
  269. {
  270. //MessageBox( NULL, "Error OnCmdProcess", NULL, MB_OK );
  271. LOG4C((LOG_NOTICE, "Error OnCmdProcess"));
  272. break;
  273. }
  274. }
  275. nBuffIndex += nProcessedLen;
  276. }
  277. LeaveCriticalSection( &(pSH->m_hClient2SrvSection) );
  278. }
  279. BOOL CTcpParse::SendNoticeToServer( void *pSocketHandle, int iCmd,int iOperateType,CString sUserName,CString sUID,int iVarID )
  280. {
  281. if( pSocketHandle==NULL )
  282. return FALSE;
  283. switch( iCmd )
  284. {
  285. case 5004:
  286. {
  287. NoticeReadVar tag;
  288. int nLen = sizeof( NoticeReadVar );
  289. memset( &tag,0,nLen );
  290. tag.Header.nLen = nLen;
  291. tag.Header.nCmd = iCmd;
  292. tag.Header.nCmdType = 0;
  293. strcpy( (char *)tag.DevUid, (char *)(LPCTSTR)sUID );
  294. tag.iVarid = iVarID;
  295. tag.iOperateType = iOperateType;
  296. tag.Header.Verify = CalcCheckSum( (void *)&tag,nLen );
  297. ((CSocketHandle *)pSocketHandle)->Write((const LPBYTE)&tag, nLen, NULL);
  298. }
  299. break;
  300. case 5005:
  301. {
  302. NoticeReadDev tag;
  303. int nLen = sizeof( NoticeReadDev );
  304. memset( &tag,0,nLen );
  305. tag.Header.nLen = nLen;
  306. tag.Header.nCmd = iCmd;
  307. tag.Header.nCmdType = 0;
  308. strcpy( (char *)tag.DevUid, (char *)(LPCTSTR)sUID );
  309. tag.iOperateType = iOperateType;
  310. tag.Header.Verify = CalcCheckSum( (void *)&tag,nLen );
  311. ((CSocketHandle *)pSocketHandle)->Write((const LPBYTE)&tag, nLen, NULL);
  312. }
  313. break;
  314. case 5006:
  315. {
  316. NoticeReadUser tag;
  317. int nLen = sizeof( NoticeReadUser );
  318. memset( &tag,0,nLen );
  319. tag.Header.nLen = nLen;
  320. tag.Header.nCmd = iCmd;
  321. tag.Header.nCmdType = 0;
  322. strcpy( (char *)tag.UserName, (char *)(LPCTSTR)sUserName );
  323. tag.iOperateType = iOperateType;
  324. tag.Header.Verify = CalcCheckSum( (void *)&tag,nLen );
  325. ((CSocketHandle *)pSocketHandle)->Write((const LPBYTE)&tag, nLen, NULL);
  326. }
  327. break;
  328. }
  329. return true;
  330. }
  331. BOOL CTcpParse::GetDogInfo( void *pSocketHandle )
  332. {
  333. if( pSocketHandle==NULL )
  334. return FALSE;
  335. Dog_Data tag;
  336. int nLen = sizeof( Dog_Data );
  337. memset( &tag,0,nLen );
  338. tag.Header.nLen = nLen;
  339. tag.Header.nCmd = CMD_DOG_DATA;
  340. tag.Header.nCmdType = 0;
  341. tag.nPastDue = 0;
  342. tag.Header.Verify = CalcCheckSum( (void *)&tag,nLen );
  343. ((CSocketHandle *)pSocketHandle)->Write((const LPBYTE)&tag, nLen, NULL);
  344. return TRUE;
  345. }
  346. BOOL CTcpParse::TestAlarm( void *pSocketHandle )
  347. {
  348. if( pSocketHandle==NULL )
  349. return FALSE;
  350. ProtocolHeader Header;
  351. int nLen = sizeof( ProtocolHeader );
  352. memset( &Header,0,nLen );
  353. Header.nLen = nLen;
  354. Header.nCmd = 5007;
  355. Header.nCmdType = 0;
  356. Header.Verify = CalcCheckSum( (void *)&Header,nLen );
  357. ((CSocketHandle *)pSocketHandle)->Write((const LPBYTE)&Header, nLen, NULL);
  358. return TRUE;
  359. }
  360. BOOL CTcpParse::ConfigAlarm( void *pSocketHandle,CString sUID,int iVarID,int iStatus,int iAlarmIndex )
  361. {
  362. AlarmConfirm tag;
  363. int nLen = sizeof( AlarmConfirm );
  364. memset( &tag,0,nLen );
  365. tag.Header.nLen = nLen;
  366. tag.Header.nCmd = CMD_ALARM_CONFIRM;
  367. tag.Header.nCmdType = 0;
  368. strcpy( (char *)tag.DevUid, (char *)(LPCTSTR)sUID );
  369. tag.iVarid = iVarID;
  370. tag.nStauts = iStatus;
  371. tag.nAlarmIndex = iAlarmIndex;
  372. tag.Header.Verify = CalcCheckSum( (void *)&tag,nLen );
  373. ((CSocketHandle *)pSocketHandle)->Write((const LPBYTE)&tag, nLen, NULL);
  374. return TRUE;
  375. }
  376. void CTcpParse::ProcessAlarmData(void *pSocketHandle, void *pData, int nLen)
  377. {
  378. AutoSendAlarmData *pAlarmData = (AutoSendAlarmData *)pData;
  379. pEventServer->AddAlarmEvent(
  380. pAlarmData->DevUid,
  381. pAlarmData->nVarID,
  382. pAlarmData->nStatus,
  383. pAlarmData->sContent,
  384. pAlarmData->Data,
  385. pAlarmData->nUpperLimit,
  386. pAlarmData->nLowerLimit,
  387. pAlarmData->nNormalState,
  388. pAlarmData->nAlarmState,
  389. pAlarmData->nAlarmIndex
  390. );
  391. }