📄 sgippkg.cpp
字号:
/*
*
*
* SGIPPkg.cpp
*
* 功能:拆包、拼包
*
*
*/
#include "SGIPPkg.h"
#include "sgip.h"
extern unsigned int g_nSeqNo; //系列号
extern char g_cSrcID[11];//节点编号
extern char g_cClientIP[16];
extern int g_iLocalPort;
extern char g_cServerIP[16];
extern int g_iSrvPort;
extern int g_iAcceptNum;
extern char g_cRUserName[16];
extern char g_cRUserPwd[16];
extern char g_cLUserName[16];
extern char g_cLUserPwd[16];
extern char g_cSpNum[21];
extern char g_cCorpID[7];
CSGIPPkg::CSGIPPkg()
{
}
CSGIPPkg::~CSGIPPkg()
{
}
unsigned int CSGIPPkg::GetSeqno(void)
{
if (sizeof(int) == 4)
{
if (g_nSeqNo < 0x7fffffff)
g_nSeqNo++;
else
g_nSeqNo = 0;
}
else
{
if (g_nSeqNo < 0x7fff)
g_nSeqNo++;
else
g_nSeqNo = 0;
}
return g_nSeqNo;
}
int CSGIPPkg::GetPackageType(const char *pFromtcp)
{
SGIP_HEAD MsgHeader;
memset(&MsgHeader, 0, sizeof(MsgHeader));
memcpy( &MsgHeader, pFromtcp, sizeof(MsgHeader));
return ntohl(MsgHeader.iCmdID) ;
}
int CSGIPPkg::GetPackageLen(const char *pBuf)
{
SGIP_HEAD MsgHeader;
memset(&MsgHeader, 0, sizeof(MsgHeader));
memcpy( &MsgHeader, pBuf, sizeof(MsgHeader) );
return ntohl(MsgHeader.iMsgLen);
}
int CSGIPPkg::GetPackageResult(const char *pBuf)
{
SGIP_MSG Msg;
memset(&Msg, 0, sizeof(SGIP_MSG));
memcpy( &Msg, pBuf, sizeof(SGIP_MSG) );
return Msg.Body.Resp.cResult;
}
int CSGIPPkg::NewPackage(unsigned int nPkgType, char *pTotcp)
{
SGIP_MSG sMsg;
memset(&sMsg,0,sizeof(SGIP_MSG));
int nPkgLen = 0;
nPkgLen = GetMessageLen(nPkgType);
time_t tt = time(NULL);
char cTime[11] = "";
memset(cTime, 0, sizeof(cTime));
GetTimeStamp(tt, (char *)cTime);
if (nPkgType < 0x80000000)
{
sMsg.Head.sSeqNo.iSrcID = htonl((u_long)atof(g_cSrcID));
sMsg.Head.sSeqNo.iDate = htonl(atoi(cTime));
sMsg.Head.sSeqNo.iSeqno = htonl(GetSeqno());
}
sMsg.Head.iMsgLen = htonl(nPkgLen);
sMsg.Head.iCmdID = htonl(nPkgType);
memcpy(pTotcp, &(sMsg), nPkgLen);
return 0;
}
void CSGIPPkg::GetTimeStamp(time_t iTime, char *pTimeStamp)
{
//char *p = pTimeStamp;
tm *pTime = localtime(&iTime);
sprintf(pTimeStamp, "%02d%02d%02d%02d%02d",
pTime->tm_mon+1, pTime->tm_mday,
pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
}
int CSGIPPkg::GetMessageLen(unsigned int nPkgType)
{
int nPkgLen = 0;
switch (nPkgType)
{
case SGIP_BIND:// 0x00000001
{
SGIP_BIND_MSG stMsg;
nPkgLen = sizeof(stMsg.Head)+\
sizeof(stMsg.Body.cLoginName)+
sizeof(stMsg.Body.cLoginPwd)+
sizeof(stMsg.Body.cLoginType)+
sizeof(stMsg.Body.cReserve);
break;
}
case SGIP_BIND_RESP:// 0x80000001
{
SGIP_RESP_MSG stMsg;
nPkgLen = sizeof(stMsg.Head)+\
sizeof(stMsg.Body.cResult)+
sizeof(stMsg.Body.cReserve);
break;
}
case SGIP_UNBIND:// 0x00000002
{
nPkgLen = sizeof(SGIP_UNBIND_MSG);
break;
}
case SGIP_UNBIND_RESP:// 0x80000002
{
nPkgLen = sizeof(SGIP_UNBINDRESP_MSG);
break;
}
case SGIP_SUBMIT:// 0x00000003
{
SGIP_SUBMIT_MSG stSubMsg;
nPkgLen = sizeof(stSubMsg.Head)+\
sizeof(stSubMsg.Body.cAgentFlag)+
sizeof(stSubMsg.Body.cChargeNumber)+
sizeof(stSubMsg.Body.cCorpId)+
sizeof(stSubMsg.Body.cExpireTime)+
sizeof(stSubMsg.Body.cFeeType)+
sizeof(stSubMsg.Body.cFeeValue)+
sizeof(stSubMsg.Body.cGivenValue)+
sizeof(stSubMsg.Body.cMessageCoding)+
sizeof(stSubMsg.Body.cMessageContent)+
sizeof(stSubMsg.Body.cMessageType)+
sizeof(stSubMsg.Body.cMorelatetoMTFlag)+
sizeof(stSubMsg.Body.cPriority)+
sizeof(stSubMsg.Body.cReportFlag)+
sizeof(stSubMsg.Body.cReserve)+
sizeof(stSubMsg.Body.cScheduleTime)+
sizeof(stSubMsg.Body.cServiceType)+
sizeof(stSubMsg.Body.cSPNumber)+
sizeof(stSubMsg.Body.cTP_pid)+
sizeof(stSubMsg.Body.cTP_udhi)+
sizeof(stSubMsg.Body.cUserCount)+
sizeof(stSubMsg.Body.cUserNumber)+
sizeof(stSubMsg.Body.iMessageLength);
break;
}
case SGIP_SUBMIT_RESP:// 0x80000003
{
SGIP_RESP_MSG stMsg;
nPkgLen = sizeof(stMsg.Head)+\
sizeof(stMsg.Body.cResult)+
sizeof(stMsg.Body.cReserve);
break;
}
case SGIP_DELIVER:// 0x00000004
{
SGIP_DELIVER_MSG stDeliMsg;
nPkgLen = sizeof(stDeliMsg.Head)+\
sizeof(stDeliMsg.Body.cMessageCoding)+
sizeof(stDeliMsg.Body.cMessageContent)+
sizeof(stDeliMsg.Body.cReserve)+
sizeof(stDeliMsg.Body.cSPNumber)+
sizeof(stDeliMsg.Body.cTP_pid)+
sizeof(stDeliMsg.Body.cTP_udhi)+
sizeof(stDeliMsg.Body.cUserNumber)+
sizeof(stDeliMsg.Body.iMessageLength);
break;
}
case SGIP_DELIVER_RESP:// 0x80000004
{
SGIP_RESP_MSG stMsg;
nPkgLen = sizeof(stMsg.Head)+\
sizeof(stMsg.Body.cResult)+
sizeof(stMsg.Body.cReserve);
break;
}
case SGIP_REPORT:// 0x00000005
{
SGIP_REPORT_MSG stReportMsg;
nPkgLen = sizeof(stReportMsg.Head)+\
sizeof(stReportMsg.Body.cReportType)+
sizeof(stReportMsg.Body.cReserve)+
sizeof(stReportMsg.Body.cState)+
sizeof(stReportMsg.Body.cUserNumber)+
sizeof(stReportMsg.Body.ErrorCode)+
sizeof(stReportMsg.Body.sSubmitSeqNum);
break;
}
case SGIP_REPORT_RESP:// 0x80000005
{
SGIP_RESP_MSG stMsg;
nPkgLen = sizeof(stMsg.Head)+\
sizeof(stMsg.Body.cResult)+
sizeof(stMsg.Body.cReserve);
break;
}
default:
{
nPkgLen = 0;
break;
}
}
return nPkgLen;
}
void CSGIPPkg::ComposePackage
(
unsigned int nPkgType,
SGIP_MSG &stMsg,
const char *pBuf
)
{
if (pBuf == NULL)
return;
char *pTmp = (char *)pBuf;
memcpy(&stMsg.Head, pTmp, sizeof(SGIP_HEAD));
pTmp += sizeof(SGIP_HEAD);
#ifdef __DEBUG_API__STOP_
int nLen = this->GetMessageLen(nPkgType);
char *pp = (char *)pBuf;
printf("=====PACKAGE:%02x===========\n", nPkgType);
for (int i=0; i<nLen; i++)
{
if ((i+1)%16 == 0)
printf("\n");
printf("%02x ", *pp);
pp++;
}
printf("\n============END PACKAGE=============\n");
#endif
switch (nPkgType)
{
case SGIP_DELIVER:
{
SGIP_DELIVER_BODY *pDeliver = (SGIP_DELIVER_BODY *)&stMsg.Body;
memcpy(&pDeliver->cUserNumber, pTmp, sizeof(pDeliver->cUserNumber));
pTmp += sizeof(pDeliver->cUserNumber);
memcpy(&pDeliver->cSPNumber, pTmp, sizeof(pDeliver->cSPNumber));
pTmp += sizeof(pDeliver->cSPNumber);
memcpy(&pDeliver->cTP_pid, pTmp, sizeof(pDeliver->cTP_pid));
pTmp += sizeof(pDeliver->cTP_pid);
memcpy(&pDeliver->cTP_udhi, pTmp, sizeof(pDeliver->cTP_udhi));
pTmp += sizeof(pDeliver->cTP_udhi);
memcpy(&pDeliver->cMessageCoding, pTmp, sizeof(pDeliver->cMessageCoding));
pTmp += sizeof(pDeliver->cMessageCoding);
memcpy(&pDeliver->iMessageLength, pTmp, sizeof(pDeliver->iMessageLength));
pTmp += sizeof(pDeliver->iMessageLength);
memcpy(&pDeliver->cMessageContent, pTmp, ntohl(pDeliver->iMessageLength));
pTmp += ntohl(pDeliver->iMessageLength);
memcpy(&pDeliver->cReserve, pTmp, sizeof(pDeliver->cReserve));
/* memcpy(&stMsg.Body.Deliver.cUserNumber, pTmp, sizeof(stMsg.Body.Deliver.cUserNumber));
pTmp += sizeof(stMsg.Body.Deliver.cUserNumber);
memcpy(&stMsg.Body.Deliver.cSPNumber, pTmp, sizeof(stMsg.Body.Deliver.cSPNumber));
pTmp += sizeof(stMsg.Body.Deliver.cSPNumber);
memcpy(&stMsg.Body.Deliver.cTP_pid, pTmp, sizeof(stMsg.Body.Deliver.cTP_pid));
pTmp += sizeof(stMsg.Body.Deliver.cTP_pid);
memcpy(&stMsg.Body.Deliver.cTP_udhi, pTmp, sizeof(stMsg.Body.Deliver.cTP_udhi));
pTmp += sizeof(stMsg.Body.Deliver.cTP_udhi);
memcpy(&stMsg.Body.Deliver.cMessageCoding, pTmp, sizeof(stMsg.Body.Deliver.cMessageCoding));
pTmp += sizeof(stMsg.Body.Deliver.cMessageCoding);
memcpy(&stMsg.Body.Deliver.iMessageLength, pTmp, sizeof(stMsg.Body.Deliver.iMessageLength));
pTmp += sizeof(stMsg.Body.Deliver.iMessageLength);
memcpy(&stMsg.Body.Deliver.cMessageContent, pTmp, sizeof(stMsg.Body.Deliver.cMessageContent));
//pTmp += sizeof(stMsg.Body.Deliver.cMessageContent);
pTmp += ntohl(stMsg.Body.Deliver.iMessageLength);
memcpy(&stMsg.Body.Deliver.cReserve, pTmp, sizeof(stMsg.Body.Deliver.cReserve));
*/
break;
}
case SGIP_REPORT:
{
SGIP_REPORT_BODY *pReport = (SGIP_REPORT_BODY *)&stMsg.Body;
memcpy(&pReport->sSubmitSeqNum, pTmp, sizeof(pReport->sSubmitSeqNum));
pTmp += sizeof(pReport->sSubmitSeqNum);
memcpy(&pReport->cReportType, pTmp, sizeof(pReport->cReportType));
pTmp += sizeof(stMsg.Body.Report.cReportType);
memcpy(&pReport->cUserNumber, pTmp, sizeof(pReport->cUserNumber));
pTmp += sizeof(pReport->cUserNumber);
memcpy(&pReport->cState, pTmp, sizeof(pReport->cState));
pTmp += sizeof(pReport->cState);
memcpy(&pReport->ErrorCode, pTmp, sizeof(pReport->ErrorCode));
pTmp += sizeof(pReport->ErrorCode);
memcpy(&pReport->cReserve, pTmp, sizeof(pReport->cReserve));
/*
memcpy(&stMsg.Body.Report.sSubmitSeqNum, pTmp, sizeof(stMsg.Body.Report.sSubmitSeqNum));
pTmp += sizeof(stMsg.Body.Report.sSubmitSeqNum);
memcpy(&stMsg.Body.Report.cReportType, pTmp, sizeof(stMsg.Body.Report.cReportType));
pTmp += sizeof(stMsg.Body.Report.cReportType);
memcpy(&stMsg.Body.Report.cUserNumber, pTmp, sizeof(stMsg.Body.Report.cUserNumber));
pTmp += sizeof(stMsg.Body.Report.cUserNumber);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -