-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathNetPacket.cpp
65 lines (54 loc) · 973 Bytes
/
NetPacket.cpp
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 "NetPacket.h"
NetPacket::NetPacket()
{
zero();
}
void NetPacket::zero()
{
this->_storage->resize(MSG_HEAD_SIZE);
_wpos = MSG_HEAD_SIZE;
_rpos = MSG_HEAD_SIZE;
}
int32 NetPacket::getHeadSize()
{
return MSG_HEAD_SIZE;
}
int32 NetPacket::getMarkLen()
{
// message head mark length
return getValue<uint32>(MSG_LEN_POS);
}
int NetPacket::getMsgType()
{
return getValue<uint32>(MSG_TYPE_POS);
}
bool NetPacket::isHeadFull()
{
return wpos() >= MSG_HEAD_SIZE;
}
// send msg call
int32 NetPacket::sendSize()
{
return wpos();
}
char * NetPacket::sendStream()
{
return (char *)contents();
}
void NetPacket::writeHead(int msgtype)
{
setValue<uint32>(MSG_LEN_POS, getBodySize());
setValue<uint32>(MSG_TYPE_POS, msgtype);
}
uint32 NetPacket::readHead(const uint8 * p, uint32 size)
{
int rsize = MSG_HEAD_SIZE - wpos();
if (rsize <= 0)
{
return 0;
}
rsize = size >= rsize ? rsize : size;
put(wpos(), p, rsize);
_wpos += rsize;
return rsize;
}