TDME2  1.9.200
UDPPacket.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <string>
5 
6 #include <tdme/tdme.h>
8 
10 
11 using std::array;
12 using std::string;
13 using std::to_string;
14 
16 
17 /**
18  * UDP Packet
19  * @author Andreas Drewke
20  */
22 private:
23  mutable uint16_t position { 0 };
24  uint16_t size { 0 };
25  array<uint8_t, 512> data;
26 
27 public:
28  static constexpr uint16_t PACKET_MAX_SIZE { 512 };
29 
30  /**
31  * Public constructor
32  */
33  inline UDPPacket() {
34  }
35 
36  /**
37  * Get size of packet
38  */
39  inline uint16_t getSize() const {
40  return size;
41  }
42 
43  /**
44  * Get data
45  */
46  inline const array<uint8_t, 512>& getData() const {
47  return data;
48  }
49 
50  /**
51  * Get position
52  */
53  inline uint16_t getPosition() const {
54  return position;
55  }
56 
57  /**
58  * Set position
59  * @param position position
60  * @return UDP client packet
61  */
62  inline const UDPPacket* setPosition(uint16_t position) const {
63  this->position = position;
64  return this;
65  }
66 
67  /**
68  * Set position
69  * @param position position
70  * @return UDP client packet
71  */
72  inline UDPPacket* setPosition(uint16_t position) {
73  this->position = position;
74  return this;
75  }
76 
77  /**
78  * Reset position for read
79  */
80  inline void reset() const {
81  position = 0;
82  }
83 
84  /**
85  * Get a bool from packet
86  * @return value
87  */
88  inline bool getBool() const {
89  return getByte() == 1;
90  }
91 
92  /**
93  * Puts a bool into packet
94  * @param value value
95  * @return UDP client packet
96  */
97  inline UDPPacket* putBool(bool value) {
98  putByte(value == true?1:0);
99  return this;
100  }
101 
102  /**
103  * Get a byte from packet
104  * @return value
105  */
106  inline uint8_t getByte() const {
107  if (position >= PACKET_MAX_SIZE) {
108  Console::println("UDPPacket::getByte(): position out of range: " + to_string(position) + " >= " + to_string(PACKET_MAX_SIZE));
109  return 0;
110  } else
111  if (position >= size) {
112  Console::println("UDPPacket::getByte(): position out of range: " + to_string(position) + " >= " + to_string(size));
113  return 0;
114  }
115  return data[position++];
116  }
117 
118  /**
119  * Puts a byte into packet
120  * @param value value
121  * @return UDP client packet
122  */
123  inline UDPPacket* putByte(uint8_t value) {
124  if (position >= PACKET_MAX_SIZE) {
125  Console::println("UDPPacket::putByte(): position out of range: " + to_string(position) + " >= " + to_string(PACKET_MAX_SIZE));
126  return this;
127  }
128  data[position++] = value;
129  if (position > size) size = position;
130  return this;
131  }
132 
133  /**
134  * Get a int16 from packet
135  * @return value
136  */
137  inline uint16_t getInt16() const {
138  uint16_t value = 0;
139  value+= (uint32_t)getByte();
140  value+= (uint32_t)getByte() << 8;
141  return value;
142  }
143 
144  /**
145  * Puts a int16 into packet
146  * @param value value
147  * @return UDP client packet
148  */
149  inline UDPPacket* putInt16(uint16_t value) {
150  putByte((value) & 0xFF);
151  putByte((value >> 8) & 0xFF);
152  return this;
153  }
154 
155  /**
156  * Get a int from packet
157  * @return value
158  */
159  inline uint32_t getInt() const {
160  uint32_t value = 0;
161  value+= (uint32_t)getByte();
162  value+= (uint32_t)getByte() << 8;
163  value+= (uint32_t)getByte() << 16;
164  value+= (uint32_t)getByte() << 24;
165  return value;
166  }
167 
168  /**
169  * Puts a int into packet
170  * @param value value
171  * @return UDP client packet
172  */
173  inline UDPPacket* putInt(uint32_t value) {
174  putByte((value) & 0xFF);
175  putByte((value >> 8) & 0xFF);
176  putByte((value >> 16) & 0xFF);
177  putByte((value >> 24) & 0xFF);
178  return this;
179  }
180 
181  /**
182  * Get a int64 from packet
183  * @return value
184  */
185  inline uint32_t getInt64() const {
186  uint64_t value = 0;
187  value+= (uint64_t)getByte();
188  value+= (uint64_t)getByte() << 8;
189  value+= (uint64_t)getByte() << 16;
190  value+= (uint64_t)getByte() << 24;
191  value+= (uint64_t)getByte() << 32;
192  value+= (uint64_t)getByte() << 40;
193  value+= (uint64_t)getByte() << 48;
194  value+= (uint64_t)getByte() << 56;
195  return value;
196  }
197 
198  /**
199  * Puts a int64 into packet
200  * @param value value
201  * @return UDP client packet
202  */
203  inline UDPPacket* putInt64(uint64_t value) {
204  putByte((value) & 0xFF);
205  putByte((value >> 8) & 0xFF);
206  putByte((value >> 16) & 0xFF);
207  putByte((value >> 24) & 0xFF);
208  putByte((value >> 32) & 0xFF);
209  putByte((value >> 40) & 0xFF);
210  putByte((value >> 48) & 0xFF);
211  putByte((value >> 56) & 0xFF);
212  return this;
213  }
214 
215  /**
216  * Get a float from packet
217  * @return value
218  */
219  inline float getFloat() const {
220  uint32_t floatAsInt = getInt();
221  return *((float*)&floatAsInt);
222  }
223 
224  /**
225  * Puts a float into packet
226  * @param value value
227  * @return UDP client packet
228  */
229  inline UDPPacket* putFloat(float value) {
230  uint32_t* floatAsInt = ((uint32_t*)&value);
231  putInt(*floatAsInt);
232  return this;
233  }
234 
235  /**
236  * Get a string value
237  * @return value
238  */
239  inline string getString() const {
240  string value;
241  uint8_t length = getByte();
242  for (auto i = 0; i < length; i++) value+= getByte();
243  return value;
244  }
245 
246  /**
247  * Puts a string into packet
248  * @param value value
249  * @return UDP client packet
250  */
251  inline UDPPacket* putString(const string& value) {
252  if (value.size() > 255) {
253  Console::println("UDPPacket::putString(): string size out of range: string will be clamped to max length of 255 bytes");
254  }
255  putByte(value.size() > 255?255:value.size());
256  for (auto i = 0; i < value.size() && i < 256; i++) {
257  putByte(value[i]);
258  }
259  return this;
260  }
261 
262  /**
263  * Get raw bytes from packet
264  * @param bytes bytes
265  * @param byteCount byte count
266  * @return UDP client packet
267  */
268  inline const UDPPacket* getBytes(uint8_t* bytes, uint16_t byteCount) const {
269  for (auto i = 0; i < byteCount; i++) bytes[i] = getByte();
270  return this;
271  }
272 
273  /**
274  * Get raw bytes from packet
275  * @param bytes bytes
276  * @param byteCount byte count
277  * @return UDP client packet
278  */
279  inline UDPPacket* getBytes(uint8_t* bytes, uint16_t byteCount) {
280  for (auto i = 0; i < byteCount; i++) bytes[i] = getByte();
281  return this;
282  }
283 
284  /**
285  * Puts raw bytes into packet
286  * @param bytes bytes
287  * @param byteCount byte count
288  * @return UDP client packet
289  */
290  inline UDPPacket* putBytes(const uint8_t* bytes, uint16_t byteCount) {
291  for (auto i = 0; i < byteCount; i++) putByte(bytes[i]);
292  return this;
293  }
294 
295  /**
296  * Puts another packet into this packet
297  * @param value value
298  * @return UDP client packet
299  */
300  inline UDPPacket* putPacket(const UDPPacket* packet) {
301  for (auto i = 0; i < packet->getSize(); i++) putByte(packet->data[i]);
302  return this;
303  }
304 
305 };
const UDPPacket * getBytes(uint8_t *bytes, uint16_t byteCount) const
Get raw bytes from packet.
Definition: UDPPacket.h:268
UDPPacket * putInt64(uint64_t value)
Puts a int64 into packet.
Definition: UDPPacket.h:203
const array< uint8_t, 512 > & getData() const
Get data.
Definition: UDPPacket.h:46
array< uint8_t, 512 > data
Definition: UDPPacket.h:25
UDPPacket * putInt16(uint16_t value)
Puts a int16 into packet.
Definition: UDPPacket.h:149
const UDPPacket * setPosition(uint16_t position) const
Set position.
Definition: UDPPacket.h:62
uint32_t getInt() const
Get a int from packet.
Definition: UDPPacket.h:159
uint16_t getSize() const
Get size of packet.
Definition: UDPPacket.h:39
uint16_t getPosition() const
Get position.
Definition: UDPPacket.h:53
UDPPacket * putByte(uint8_t value)
Puts a byte into packet.
Definition: UDPPacket.h:123
uint32_t getInt64() const
Get a int64 from packet.
Definition: UDPPacket.h:185
UDPPacket * setPosition(uint16_t position)
Set position.
Definition: UDPPacket.h:72
UDPPacket * putBool(bool value)
Puts a bool into packet.
Definition: UDPPacket.h:97
bool getBool() const
Get a bool from packet.
Definition: UDPPacket.h:88
UDPPacket * putFloat(float value)
Puts a float into packet.
Definition: UDPPacket.h:229
UDPPacket()
Public constructor.
Definition: UDPPacket.h:33
static constexpr uint16_t PACKET_MAX_SIZE
Definition: UDPPacket.h:28
UDPPacket * getBytes(uint8_t *bytes, uint16_t byteCount)
Get raw bytes from packet.
Definition: UDPPacket.h:279
UDPPacket * putPacket(const UDPPacket *packet)
Puts another packet into this packet.
Definition: UDPPacket.h:300
uint16_t getInt16() const
Get a int16 from packet.
Definition: UDPPacket.h:137
UDPPacket * putBytes(const uint8_t *bytes, uint16_t byteCount)
Puts raw bytes into packet.
Definition: UDPPacket.h:290
void reset() const
Reset position for read.
Definition: UDPPacket.h:80
float getFloat() const
Get a float from packet.
Definition: UDPPacket.h:219
uint8_t getByte() const
Get a byte from packet.
Definition: UDPPacket.h:106
UDPPacket * putString(const string &value)
Puts a string into packet.
Definition: UDPPacket.h:251
UDPPacket * putInt(uint32_t value)
Puts a int into packet.
Definition: UDPPacket.h:173
string getString() const
Get a string value.
Definition: UDPPacket.h:239
Console class.
Definition: Console.h:29