language: C++11 (gcc-4.7.2)
date: 465 days 9 hours ago
link:
visibility: public
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
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <vector>
#include <stdexcept>
#include <cstdint>
 
class Packet
{
private:
        std::vector<uint8_t> buffer;
        int byteIndex;
        int maxByteIndex;
 
public:
        Packet() : byteIndex(0), maxByteIndex(0)
        {
        }
 
        char* GetBuffer()
        {
                return reinterpret_cast<char*>(&buffer[0]);
        }
 
        int GetLength()
        {
                return buffer.size();
        }
 
        int GetByteIndex()
        {
                return byteIndex;
        }
 
        void SetByteIndex(int value)
        {
                if (value < 0 || value >= buffer.size())
                {
                        throw std::out_of_range("ByteIndex must be an index in the Buffer");
                }
                byteIndex = value;
        }
 
        bool HasHeader()
        {
                return byteIndex >= 2;
        }
 
        bool HasData()
        {
                return byteIndex == maxByteIndex;
        }
 
        void Begin()
        {
                buffer.push_back(0);
                buffer.push_back(0);
                byteIndex += 2;
        }
 
        void End()
        {
                buffer[0] = static_cast<uint8_t>(buffer.size()) >> 8;
                buffer[1] = static_cast<uint8_t>(buffer.size());
        }
 
        bool ReadHeader()
        {
                int previousByteIndex = byteIndex;
                byteIndex = 0;
                uint16_t length;
                ReadUInt16(length);
                maxByteIndex = length - 1;
                byteIndex = previousByteIndex;
                return maxByteIndex > 2;
        }
 
        void WriteEventID(uint8_t value)
        {
                buffer.push_back(value);
                byteIndex++;
        }
 
        void WriteByte(uint8_t value)
        {
                buffer.push_back(value);
                byteIndex++;
        }
 
        void WriteUInt16(uint16_t value)
        {
                buffer.push_back(static_cast<uint8_t>(value >> 8));
                buffer.push_back(static_cast<uint8_t>(value));
                byteIndex += 2;
        }
 
        void WriteString(std::string value, int maxLength = 255)
        {
                if (maxLength < 0 || maxLength > 65535)
                {
                        throw std::out_of_range("maxLength must be in the range [0, 65535]");
                }
                if (value.length() > maxLength)
                {
                        throw std::out_of_range("String length was over the given max length");
                }
                WriteUInt16(static_cast<uint16_t>(value.length()));
                for (int i = 0; i < value.length(); ++i)
                {
                        WriteByte(value[i]);
                }
        }
 
        bool ReadEventID(uint8_t& value)
        {
                return ReadByte(value);
        }
 
        bool ReadByte(uint8_t& value)
        {
                value = 0;
                if (byteIndex >= buffer.size())
                {
                        return false;
                }
                value = buffer[byteIndex];
                byteIndex++;
                return true;
        }
 
        bool ReadUInt16(uint16_t& value)
        {
                value = 0;
                if (byteIndex + 1 >= buffer.size())
                {
                        return false;
                }
                value = static_cast<uint16_t>(buffer[byteIndex] << 8);
                value |= static_cast<uint16_t>(buffer[byteIndex + 1]);
                byteIndex += 2;
                return true;
        }
 
        bool ReadString(std::string& value, int maxLength = 255)
        {
                value = "";
                uint16_t length;
                if (!ReadUInt16(length) || length > maxLength || byteIndex + length > buffer.size())
                {
                        return false;
                }
                for (int i = 0; i < length; ++i)
                {
                        value += static_cast<char>(buffer[byteIndex + i]);
                }
                byteIndex += length;
                return true;
        }
};
 
int main()
{
        Packet packet;
        packet.Begin();
        packet.WriteEventID(42);
        packet.WriteByte(43);
        packet.WriteUInt16(65535);
        packet.WriteString("Hello World", 20);
        packet.End();
 
        packet.SetByteIndex(2);
        uint8_t eventID;
        if (!packet.ReadEventID(eventID)) { std::cout << "Error reading" << std::endl; return -1; }
        std::cout << static_cast<int>(eventID) << std::endl;
        uint8_t byteValue;
        if (!packet.ReadByte(byteValue)) { std::cout << "Error reading" << std::endl; return -1; }
        std::cout << static_cast<int>(byteValue) << std::endl;
        uint16_t ushortValue;
        if (!packet.ReadUInt16(ushortValue)) { std::cout << "Error reading" << std::endl; return -1; }
        std::cout << ushortValue << std::endl;
        std::string stringValue;
        if (!packet.ReadString(stringValue)) { std::cout << "Error reading" << std::endl; return -1; }
        std::cout << stringValue << std::endl;
 
        /*
        To read from a TCP stream:
        // packet is a per end point object that persists through read callbacks
        for (int readByteIndex = 0; readByteIndex < receiveBufferSize; ++readByteIndex)
        {
                packet.WriteByte(receiveBuffer[readByteIndex]);
                if (!packet.HasHeader())
                {
                        if (!packet.ReadHeader())
                        {
                                DisconnectEndPoint();
                        }
                }
                if (!packet.HasData())
                {
                        // Packets is a ConcurrentQueue
                        endPoint.Packets.Enqueue(packet);
                        packet = new Packet();
                }
        }
        */
 
        return 0;
}