-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMsgHead.cpp
More file actions
65 lines (49 loc) · 1.21 KB
/
MsgHead.cpp
File metadata and controls
65 lines (49 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "MsgHead.h"
bool MsgHead::encodeHead(char *buffer)
{
if (!buffer)
{
return false;
}
int offset = 0;
msgLen = htonl(msgLen);
memcpy(&buffer[offset], &msgLen, sizeof(msgLen));
offset += sizeof(msgLen);
memcpy(&buffer[offset], &msgType, sizeof(msgType));
offset += sizeof(msgType);
memcpy(&buffer[offset], &option, sizeof(option));
offset += sizeof(option);
memcpy(&buffer[offset], &reserve, sizeof(reserve));
offset += sizeof(reserve);
cmdID = htonl(cmdID);
memcpy(&buffer[offset], &cmdID, sizeof(cmdID));
offset += sizeof(cmdID);
reqID = htonl(reqID);
memcpy(&buffer[offset], &reqID, sizeof(reqID));
offset += sizeof(reqID);
return true;
}
bool MsgHead::decodeHead(const char *data)
{
if (!data)
{
return false;
}
int offset = 0;
msgLen = *((int*)&data[offset]);
msgLen = ntohl(msgLen);
offset += sizeof(msgLen);
msgType = *((char*)&data[offset]);
offset += sizeof(msgType);
option = *((char*)&data[offset]);
offset += sizeof(option);
reserve = *((short*)&data[offset]);
offset += sizeof(reserve);
cmdID = *((int*)&data[offset]);
cmdID = ntohl(cmdID);
offset += sizeof(cmdID);
reqID = *((int*)&data[offset]);
reqID = ntohl(reqID);
offset += sizeof(reqID);
return true;
}