-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.cpp
executable file
·165 lines (134 loc) · 3.04 KB
/
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
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
#include "Packet.hpp"
#include "memory.hpp"
#include <cstdlib>
#include <cstring>
#include <WinSock2.h>
#include "msvcfix.hpp"
Packet::Packet()
{
init();
}
Packet::Packet(int type)
{
init();
putInt(type);
}
Packet::Packet(
char *dataWithHeader,
int sizeWithoutHeader)
{
pos = 4;
size = alloc = sizeWithoutHeader + sizeof(int);
data = static_cast<unsigned char *>(malloc(alloc));
memcpy(data, dataWithHeader, size);
memcpy(data, &size, sizeof(int));
}
void Packet::init()
{
pos = 4;
size = 4;
alloc = 32 + sizeof(int);
data = static_cast<unsigned char*>(malloc(alloc));
memset(data, 0, alloc);
memcpy(data, &size, sizeof(int));
}
Packet::~Packet()
{
free(data);
}
//////////////////////////////////////////////////////////////////////////
void Packet::putChar(char value)
{
expand(1);
data[size++] = value;
}
void Packet::putShort(short value)
{
expand(2);
data[size++] = (value & 0xFF);
data[size++] = (value >> 8 & 0xFF);
}
void Packet::putInt(int value)
{
expand(4);
data[size++] = (value & 0xFF);
data[size++] = (value >> 8 & 0xFF);
data[size++] = (value >> 16 & 0xFF);
data[size++] = (value >> 24 & 0xFF);
}
void Packet::putFloat(float value)
{
expand(sizeof(float));
memcpy(data + size, &value, sizeof(float));
size += sizeof(float);
}
void Packet::putString(const std::string &value)
{
const char *toPut = value.c_str();
// Make room for the null terminator too
expand(static_cast<int>(strlen(toPut) + 1));
while (*toPut != '\0')
data[size++] = *(toPut++);
data[size++] = '\0';
}
//////////////////////////////////////////////////////////////////////////
char Packet::getChar()
{
return data[pos++];
}
short Packet::getShort()
{
short ret =
(static_cast<unsigned>(data[pos ])) |
(static_cast<unsigned>(data[pos + 1])) << 8;
pos += 2;
return ret;
}
int Packet::getInt()
{
int ret =
(static_cast<unsigned>(data[pos ])) |
(static_cast<unsigned>(data[pos + 1])) << 8 |
(static_cast<unsigned>(data[pos + 2])) << 16 |
(static_cast<unsigned>(data[pos + 3])) << 24;
pos += 4;
return ret;
}
float Packet::getFloat()
{
float ret;
memcpy(&ret, data + pos, sizeof(float));
pos += sizeof(float);
return ret;
}
std::string Packet::getString()
{
std::string ret("");
while (data[pos] != '\0')
ret += data[pos++];
++pos; // Skip over the null terminator
return ret;
}
//////////////////////////////////////////////////////////////////////////
int Packet::getSize() const
{
return size;
}
const char *Packet::getData()
{
// About to pass this data over the network, so encode the size also
memcpy(data, &size, sizeof(int));
return reinterpret_cast<char *>(data);
}
void Packet::expand(int numBytes)
{
while (size + numBytes >= alloc)
alloc *= 2;
data = static_cast<unsigned char *>(realloc(data, alloc));
}
Packet *Packet::clone()
{
return NEW Packet(
reinterpret_cast<char *>(this->data),
size - sizeof(int));
}