-
Notifications
You must be signed in to change notification settings - Fork 0
/
Packet.cpp
52 lines (44 loc) · 1023 Bytes
/
Packet.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
//Packet.cpp: 数据包处理
#include "pch.h"
#include "Packet.h"
Packet::Packet(char* DataBase, int& offset)
{
int temp = 0;
Size = DecodeVarInt((int8_t*)DataBase, offset);
ID = DecodeVarInt((int8_t*)DataBase + offset, temp);
offset += temp;
Data = (int8_t*)DataBase;
}
long long Packet::GetVarInt(int Start, int& Size)
{
int8_t* temp = new int8_t[8];
for (int i = 0; i < 8; i++)
{
temp[i] = Data[Start + i];
}
int Result = DecodeVarInt(temp, Size);
delete[] temp;
return Result;
}
std::string Packet::GetString(int Start, int &Size)
{
int VarIntSize = 0;
int8_t* VarInt = new int8_t[4];
for (int i = 0; i < 4; i++)
{
VarInt[i] = Data[i + Start];
}
int offset = 0;
long long StringSize = DecodeVarInt(VarInt, offset);
delete[] VarInt;
char* temp = new char[StringSize + 1];
memset(temp, 0, StringSize + 1);
for (int i = 0; i < StringSize; i++)
{
temp[i] = Data[i + Start + offset];
}
Size = StringSize + offset;
std::string result(temp, Size);
delete[] temp;
return result;
}