-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathBasePacket.cpp
196 lines (168 loc) · 2.96 KB
/
BasePacket.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "BasePacket.h"
BasePacket::BasePacket()
{
// Polymorphism cannot be implemented in constructors
zero();
}
BasePacket::~BasePacket()
{
}
void BasePacket::zero()
{
rpos(0);
wpos(0);
this->storage().resize(0);
}
void BasePacket::release()
{
}
void BasePacket::moveData(BasePacket * packet)
{
rpos(packet->rpos());
wpos(packet->wpos());
swapBuffer(*packet);
packet->zero();
}
void BasePacket::shrink(int isize)
{
// shrink_to_fit()
// swap()
if (_storage->capacity() > isize)
{
std::vector<uint8> vec;
vec.reserve(isize);
_storage->swap(vec);
}
}
int32 BasePacket::getHeadSize()
{
return 0;
}
int32 BasePacket::getBodySize()
{
return wpos() - getHeadSize();
}
const char * BasePacket::getBodyData()
{
return (const char*)(contents() + getHeadSize());
}
const char * BasePacket::readPointer()
{
return (const char *)(contents() + _rpos);
}
void BasePacket::setWriteSize(int size)
{
size += getHeadSize();
_storage->resize(size);
wpos(size);
}
int32 BasePacket::activeSize()
{
return _wpos - _rpos;
}
std::string_view BasePacket::readData()
{
return std::string_view(readPointer(), activeSize());
}
void BasePacket::writeData(std::string_view sv)
{
append(sv.data(), sv.size());
}
// message head mark length
int32 BasePacket::getMarkLen()
{
return 0;
}
int BasePacket::getMsgType()
{
return 0;
}
bool BasePacket::isHeadFull()
{
return false;
}
// send msg call
int32 BasePacket::sendSize()
{
return wpos();
}
char * BasePacket::sendStream()
{
return (char *)contents();
}
int BasePacket::readPos()
{
return rpos();
}
int BasePacket::writePos()
{
return wpos();
}
//------->get
int8 BasePacket::getInt8() {
return popValue<int8>();
}
uint8 BasePacket::getUint8() {
return popValue<uint8>();
}
int16 BasePacket::getInt16() {
return popValue<int16>();
}
uint16 BasePacket::getUint16() {
return popValue<uint16>();
}
int32 BasePacket::getInt32() {
return popValue<int32>();
}
uint32 BasePacket::getUint32() {
return popValue<uint32>();
}
int64 BasePacket::getInt64() {
return popValue<int64>();
}
uint64 BasePacket::getUint64() {
return popValue<uint64>();
}
float BasePacket::getFloat() {
return popValue<float>();
}
double BasePacket::getDouble() {
return popValue<double>();
}
std::string BasePacket::getString() {
return popValue<std::string>();
}
//------->push
void BasePacket::pushInt8(int8 value) {
*this << value;
}
void BasePacket::pushUint8(uint8 value) {
*this << value;
}
void BasePacket::pushInt16(int16 value) {
*this << value;
}
void BasePacket::pushUint16(uint16 value) {
*this << value;
}
void BasePacket::pushInt32(int32 value) {
*this << value;
}
void BasePacket::pushUint32(uint32 value) {
*this << value;
}
void BasePacket::pushInt64(int64 value) {
*this << value;
}
void BasePacket::pushUint64(uint64 value) {
*this << value;
}
void BasePacket::pushFloat(float value) {
*this << value;
}
void BasePacket::pushDouble(double value) {
*this << value;
}
void BasePacket::pushString(std::string value) {
*this << value;
}