📄 msgpack.cpp
字号:
#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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -