123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- #include "stdafx.h"
- #include "Client2SrvType.h"
- #include "TcpParse.h"
- #include "crc32.h"
- #include "SocketHandle.h"
- #include "Global.h"
- CStringArray g_saAllData;
- //判断是否已经插入
- bool IsExistInsert( CString sData )
- {
- bool bRet = false;
- CString str;
- int nSize = g_saAllData.GetCount();
- for( int i=0;i<nSize;i++ )
- {
- str = g_saAllData.GetAt( i );
- if( str==sData )
- {
- bRet = true;
- break;
- }
- }
- if( !bRet )
- g_saAllData.Add( sData );
- return bRet;
- }
- CTcpParse::CTcpParse(void)
- {
- }
- CTcpParse::~CTcpParse(void)
- {
- }
- unsigned int CTcpParse::CalcCheckSum( void *pData, unsigned int nSize )
- {
- unsigned int checksum = 0;
- if ( nSize <= sizeof( ProtocolHeader ) )
- {
- return 0;
- }
- unsigned char *pBody = &( ( unsigned char* )pData )[ sizeof( ProtocolHeader ) ];
- nSize -= sizeof( ProtocolHeader );
- checksum = crc32( 0, pBody, nSize );
- return checksum;
- }
- void CTcpParse::ProcessHeart(void *pSocketHandle, void *pData, int nLen)
- {
- ((CSocketHandle *)pSocketHandle)->Write((const LPBYTE)pData, nLen, NULL);
- }
- void CTcpParse::ProcessReadSingleVarData(void *pSocketHandle, void *pData, int nLen)
- {
- RomoteDBData *pTmp = (RomoteDBData *)pData;
-
- //CHAR szID[MAX_ID] = {0};
- //int nRet = CDBInterface::GetInstancePtr()->GetSummaryInfo((char *)(LPCTSTR)pTmp->sDevUid,
- // (char *)(LPCTSTR)pTmp->sVarName,szID);
- CString sData;
- sData.Format( "%s %s",pTmp->sDevUid,pTmp->sVarName );
- if( IsExistInsert(sData) )
- {
- CDBInterface::GetInstancePtr()->EditSummaryRecord(
- (char *)pTmp->sDevNmae,
- (char *)pTmp->sTime,
- "",
- (char *)pTmp->sDesc,
- pTmp->Data,
- (char *)pTmp->sDevUid,
- (char *)pTmp->sVarName
- );
- }
- else
- {
- CDBInterface::GetInstancePtr()->InsertSummaryRecord(
- (char *)pTmp->sDevNmae,
- (char *)pTmp->sTime,
- "",
- (char *)pTmp->sDesc,
- pTmp->Data,
- (char *)pTmp->sDevUid,
- (char *)pTmp->sVarName
- );
- }
- }
- int CTcpParse::OnCmdProcess(void *pSocketHandle, void *pData)
- {
- ProtocolHeader *pHeader = (ProtocolHeader *)pData;
- if( pHeader == NULL ) return -1;
- if( pHeader->nLen < 0 || pHeader->nLen > 65535 ) return -1;
- unsigned int tmp = CalcCheckSum(pHeader, pHeader->nLen);
- if( tmp != pHeader->Verify )
- {
- return -1;
- }
- switch( pHeader->nCmd )
- {
- case CMD_READ_SINGLE_VAR_DATA: // 请求单个变量内容回应
- ProcessReadSingleVarData(pSocketHandle, pData, pHeader->nLen);
- break;
- }
- return 0;
- }
- void CTcpParse::ProcessData(void *pSocketHandle, const BYTE* pData, DWORD nLen)
- {
- DWORD nBuffIndex = 0;
- CSocketHandle* pSH = (CSocketHandle *)pSocketHandle;
- EnterCriticalSection( &(pSH->m_hClient2SrvSection) );
- while( nBuffIndex < nLen )
- {
- //LOG4C((LOG_NOTICE, "接收长度=%d, 处理长度=%d, PendingSize=%d", nLen, nBuffIndex, pSH->m_nPendingSize));
- ProtocolHeader *pHeader; //当前协议包头
- DWORD nProcessedLen = 0; //当前循环处理了多少个字节
- if( pSH->m_nPendingSize > 0 ) // 开始组包
- {
- //LOG4C((LOG_NOTICE, "开始组包"));
- pHeader = (ProtocolHeader *)pSH->m_PendingBuffer;
- if( pSH->m_nPendingSize < sizeof(ProtocolHeader) ) //上一次接收到的长度小于包头
- {
- DWORD nLinkHeaderLen = sizeof( ProtocolHeader ) - pSH->m_nPendingSize;
- if( nLinkHeaderLen <= nLen ) //这次可以收完包头
- {
- memcpy( &pSH->m_PendingBuffer[ pSH->m_nPendingSize ], pData, nLinkHeaderLen ); //这里已经收完Header
- nProcessedLen = pHeader->nLen - pSH->m_nPendingSize;
- if( nProcessedLen <= nLen ) //如果所需处理的长度小于等于当前包长度
- {
- memcpy( &pSH->m_PendingBuffer[ sizeof( ProtocolHeader ) ],
- & ( ( char *) pData )[ nLinkHeaderLen ],
- pHeader->nLen - sizeof( ProtocolHeader ) );
- //LOG4C((LOG_NOTICE, "收完所需的包"));
- pSH->m_nPendingSize = 0; // 收完所需的包,置m_nPendingSize为0
- }
- else
- {
- int nTemp = nLen - nLinkHeaderLen; //除去头剩余部分的长度
- if ( nTemp > 0 ) //刚好是Header的长度,不用拷贝内存,所以这里加了>0的判断
- {
- memcpy( &pSH->m_PendingBuffer[ sizeof( ProtocolHeader ) ],
- & ( ( char *) pData )[ nLinkHeaderLen ],
- nTemp );
- }
- pSH->m_nPendingSize += nLen;
- }
- }
- else //这次还是没有收完包头, 继续Pending
- {
- //LOG4C((LOG_NOTICE, "这次还是没有收完包头, 继续Pending"));
- memcpy( &pSH->m_PendingBuffer[ pSH->m_nPendingSize ], pData, nLen );
- pSH->m_nPendingSize += nLen;
- nProcessedLen = nLen;
- }
- }
- else //Header部分已经在阻塞的缓冲区中
- {
- nProcessedLen = pHeader->nLen - pSH->m_nPendingSize;
- if ( nProcessedLen <= nLen ) //如果需要处理的长度小于现有包的长度
- {
- memcpy( &pSH->m_PendingBuffer[ pSH->m_nPendingSize ], pData, nProcessedLen );
- pSH->m_nPendingSize = 0;
- }
- else //否则要继续阻塞
- {
- memcpy( &pSH->m_PendingBuffer[ pSH->m_nPendingSize ], pData, nLen );
- pSH->m_nPendingSize += nLen;
- }
- }
- }
- else //第一次接包
- {
- //LOG4C((LOG_NOTICE, "第一次接包"));
- pHeader = (ProtocolHeader *)&( (unsigned char *)pData )[nBuffIndex];
- if( nLen - nBuffIndex < sizeof(ProtocolHeader) ) // 没有收够包头,先记录当前收到的Buffer
- {
- //如果第一次接包就没有收够包头,认为是非法包,扔掉,就是说已处理的长度nProcessedLen = 0
- pSH->m_nPendingSize = nLen - nBuffIndex;
- memcpy(pSH->m_PendingBuffer, pHeader, pSH->m_nPendingSize);
- //LOG4C((LOG_NOTICE, "第一次接包就没有收够包头,认为是非法包,扔掉"));
- }
- else
- {
- nProcessedLen = pHeader->nLen;
- if( (int)pHeader->nLen > nLen - nBuffIndex )
- {
- memcpy(pSH->m_PendingBuffer, pHeader, nLen - nBuffIndex);
- //如果第一次接包,pHeader->nLen大于当前包的总长,认为是非法包,扔掉
- if( nBuffIndex == 0 )
- {
- //组包错误,则扔掉当前包
- //LOG4C((LOG_NOTICE, "第一次接包,服务器pHeader->nLen大于当前包的总长,认为是非法包,扔掉\r\n"));
- }
- pSH->m_nPendingSize = nLen - nBuffIndex;
- nProcessedLen = nLen - nBuffIndex;
- }
- else
- {
- //LOG4C((LOG_NOTICE, "正常包"));
- pSH->m_nPendingSize = 0;
- }
- }
- }
- if ( nProcessedLen == 0 )
- {
- // 没有收够包头,认为是非法包,扔掉
- //LOG4C((LOG_NOTICE, "没有收够包头,认为是非法包,扔掉"));
- break;
- }
- if ( pSH->m_nPendingSize == 0 )
- {
- if ( pHeader->nLen > SOCKET_BUFFSIZE )
- {
- // 包长度超过限制
- //LOG4C((LOG_NOTICE, "pHeader->nLen超过限制"));
- }
- if(-1 == OnCmdProcess( pSocketHandle, pHeader ))
- {
- //MessageBox( NULL, "Error OnCmdProcess", NULL, MB_OK );
- //LOG4C((LOG_NOTICE, "Error OnCmdProcess"));
- break;
- }
- }
- nBuffIndex += nProcessedLen;
- }
- LeaveCriticalSection( &(pSH->m_hClient2SrvSection) );
- }
|