msgpack.cpp

来自「小型短信网关系统」· C++ 代码 · 共 62 行

CPP
62
字号
#include 	<netinet/in.h>#include	"MsgPack.h"#include	"Logger.h"MsgPack::MsgPack(){}MsgPack::~MsgPack(){}UInt	MsgPack::packMsg(	UInt msgType,	UInt seqNo,	Octets bodyData,	UInt bodyLength,	Octets msgData){	UInt	nMsgType, nSeqNo, nTotalLength, totalLength;	Octets	ptr;	totalLength=12+bodyLength;	nTotalLength=htonl(totalLength);	nMsgType=htonl(msgType);	nSeqNo=htonl(seqNo);	memset(msgData, 0, sizeof(msgData));	ptr=msgData;		memcpy(ptr, &nTotalLength, 4);	ptr=ptr+4;	memcpy(ptr, &nMsgType, 4);	ptr=ptr+4;	memcpy(ptr, &nSeqNo, 4);	ptr=ptr+4;	memcpy(ptr, bodyData, bodyLength );	return totalLength;}UInt    MsgPack::unpackMsg(UInt& msgType, UInt& seqNo, Octets dataBody, Octets msgData, UInt    length){	UInt	nMsgType, nSeqNo, nTotalLength, totalLength;		memcpy(&nTotalLength, msgData, 4);		totalLength=ntohl(nTotalLength);	if(totalLength != length)	{		Logger::debug("TotalLength dos not equal receive length!");		exit(1);	}		memcpy(&nMsgType, msgData+4, 4);	msgType=ntohl(nMsgType);	memcpy(&nSeqNo, msgData+8, 4);	seqNo=ntohl(nSeqNo);	memset(dataBody, 0, sizeof(dataBody));	memcpy(dataBody, msgData+12, totalLength-12);	return totalLength;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?