TDME2  1.9.200
LogicNetworkPacket.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstring>
4 #include <set>
5 #include <sstream>
6 #include <string>
7 
8 #include <tdme/tdme.h>
10 #include <tdme/math/Quaternion.h>
11 #include <tdme/math/Vector3.h>
13 #include <tdme/utilities/Console.h>
14 #include <tdme/utilities/Time.h>
15 
16 using std::memcpy;
17 using std::set;
18 using std::string;
19 using std::stringstream;
20 
26 
28 private:
29  int64_t time;
30  bool safe { false };
31  uint32_t logicTypeId { 0 };
32  bool processed { false };
33  bool reinjected { false };
34  uint32_t messageId { 0 };
35  uint8_t retryCount { 0 };
36  uint8_t position { 0 };
37  uint8_t data[255];
38  string sender;
39  set<string> recipients;
40 
41 public:
42  static constexpr uint32_t LOGIC_TYPEID_NONE { 0 };
43 
44  /**
45  * Public constructor, used to create a network packet
46  * @param safe safe
47  * @param logicTypeId logic network packet type id
48  */
50  this->time = Time::getCurrentMillis();
51  this->safe = safe;
52  this->logicTypeId = logicTypeId;
53  }
54 
55  /**
56  * Public constructor
57  * @param messageId message id
58  * @param safe safe
59  * @param retryCount retry count
60  * @param logicTypeId logic network packet type id
61  * @param packet packet
62  * @param size size
63  */
64  inline LogicNetworkPacket(uint32_t messageId, bool safe, uint8_t retryCount, uint32_t logicTypeId, const UDPPacket* packet, uint8_t size) {
65  this->time = Time::getCurrentMillis();
66  this->messageId = messageId;
67  this->safe = safe;
68  this->retryCount = retryCount;
69  this->logicTypeId = logicTypeId;
70  packet->getBytes(data, size);
71  }
72 
73  /**
74  * @return time in milliseconds
75  */
76  inline int64_t getTime() {
77  return time;
78  }
79 
80  /**
81  * @return if packet has been processed
82  */
83  inline bool isProcessed() {
84  return processed;
85  }
86 
87  /**
88  * Set if packet has been processed
89  * @param processed processed
90  */
91  inline void setProcessed(bool processed) {
92  this->processed = processed;
93  }
94 
95 
96  /**
97  * @return if reinjected
98  */
99  inline bool isReinjected() const {
100  return reinjected;
101  }
102 
103  /**
104  * Set reinjected
105  */
106  inline void setReinjected() {
107  this->reinjected = true;
108  }
109 
110  /**
111  * @return if message is safe message
112  */
113  inline bool getSafe() {
114  return safe;
115  }
116 
117  /**
118  * @return logic type id
119  */
120  inline uint32_t getLogicTypeId() {
121  return logicTypeId;
122  }
123 
124  /**
125  * Set logic type id
126  * @param logicTypeId logic type id
127  */
128  inline void setLogicTypeId(uint32_t logicTypeId) {
129  this->logicTypeId = logicTypeId;
130  }
131 
132  /**
133  * Add recipients, no recipients means broadcast, multiple recipients means multi cast, single recipient means unicast
134  * @param recipient recipient
135  */
136  inline void addRecipient(const string& recipient) {
137  recipients.insert(recipient);
138  }
139 
140  /**
141  * @return recipients
142  */
143  inline set<string>& getRecipients() {
144  return recipients;
145  }
146 
147  /**
148  * @return sender
149  */
150  inline const string& getSender() {
151  return sender;
152  }
153 
154  /**
155  * Set sender
156  * @param sender sender
157  */
158  inline void setSender(const string& sender) {
159  this->sender = sender;
160  }
161 
162  /**
163  * @return message id
164  */
165  inline uint32_t getMessageId() {
166  return messageId;
167  }
168 
169  /**
170  * Get data
171  */
172  inline uint8_t* getData() {
173  return data;
174  }
175 
176  /**
177  * Get position
178  */
179  inline uint8_t getPosition() {
180  return position;
181  }
182 
183  /**
184  * Reset position for read
185  */
186  inline void reset() {
187  position = 0;
188  }
189 
190  /**
191  * Get a byte from packet
192  * @return value
193  */
194  inline uint8_t getByte() {
195  if (position == 255) {
196  Console::println("LogicNetworkPacket::getByte(): position out of range");
197  return 0;
198  }
199  return data[position++];
200  }
201 
202  /**
203  * Puts a byte into packet
204  * @param value value
205  * @return this
206  */
207  inline LogicNetworkPacket& putByte(uint8_t value) {
208  if (position == 255) {
209  Console::println("LogicNetworkPacket::putByte(): position out of range");
210  return *this;
211  }
212  data[position++] = value;
213  return *this;
214  }
215 
216  /**
217  * Get a bool from packet
218  * @return value
219  */
220  inline bool getBool() {
221  return getByte() == 1;
222  }
223 
224  /**
225  * Puts a bool into packet
226  * @param value value
227  * @return this
228  */
229  inline LogicNetworkPacket& putBool(bool value) {
230  putByte(value == true?1:0);
231  return *this;
232  }
233 
234  /**
235  * Get a int16 from packet
236  * @return value
237  */
238  inline uint16_t getInt16() {
239  uint16_t value = 0;
240  value+= (uint32_t)getByte();
241  value+= (uint32_t)getByte() << 8;
242  return value;
243  }
244 
245  /**
246  * Puts a int16 into packet
247  * @param value value
248  * @return this
249  */
250  inline LogicNetworkPacket& putInt16(uint16_t value) {
251  putByte((value) & 0xFF);
252  putByte((value >> 8) & 0xFF);
253  return *this;
254  }
255 
256  /**
257  * Get a int from packet
258  * @return value
259  */
260  inline uint32_t getInt() {
261  uint32_t value = 0;
262  value+= (uint32_t)getByte();
263  value+= (uint32_t)getByte() << 8;
264  value+= (uint32_t)getByte() << 16;
265  value+= (uint32_t)getByte() << 24;
266  return value;
267  }
268 
269  /**
270  * Puts a int into packet
271  * @param value value
272  * @return this
273  */
274  inline LogicNetworkPacket& putInt(uint32_t value) {
275  putByte((value) & 0xFF);
276  putByte((value >> 8) & 0xFF);
277  putByte((value >> 16) & 0xFF);
278  putByte((value >> 24) & 0xFF);
279  return *this;
280  }
281 
282  /**
283  * Get a small int from packet
284  * @return value
285  */
286  inline uint16_t getSmallInt() {
287  uint16_t value = 0;
288  value+= (uint16_t)getByte();
289  value+= (uint16_t)getByte() << 8;
290  return value;
291  }
292 
293  /**
294  * Puts a small int into packet
295  * @param value value
296  * @return this
297  */
298  inline LogicNetworkPacket& putSmallInt(uint16_t value) {
299  putByte((value) & 0xFF);
300  putByte((value >> 8) & 0xFF);
301  return *this;
302  }
303 
304  /**
305  * Get a float from packet
306  * @return value
307  */
308  inline float getFloat() {
309  uint32_t floatAsInt = getInt();
310  return *((float*)&floatAsInt);
311  }
312 
313  /**
314  * Puts a float into packet
315  * @param value value
316  * @return this
317  */
318  inline LogicNetworkPacket& putFloat(float value) {
319  uint32_t* floatAsInt = ((uint32_t*)&value);
320  putInt(*floatAsInt);
321  return *this;
322  }
323 
324  /**
325  * Get a string value
326  * @return value
327  */
328  inline string getString() {
329  string value;
330  uint8_t length = getByte();
331  for (auto i = 0; i < length; i++) value+= getByte();
332  return value;
333  }
334 
335  /**
336  * Puts a string into packet
337  * @param value value
338  * @return this
339  */
340  inline LogicNetworkPacket& putString(const string& value) {
341  putByte(value.size());
342  for (auto i = 0; i < value.size(); i++) {
343  putByte(value[i]);
344  }
345  return *this;
346  }
347 
348  /**
349  * Get a vector3 from packet
350  * @return value
351  */
352  inline Vector3 getVector3() {
353  Vector3 value;
354  value.setX(getFloat());
355  value.setY(getFloat());
356  value.setZ(getFloat());
357  return value;
358  }
359 
360  /**
361  * Puts a vector3 into packet
362  * @param value value
363  * @return this
364  */
365  inline LogicNetworkPacket& putVector3(const Vector3& value) {
366  putFloat(value.getX());
367  putFloat(value.getY());
368  putFloat(value.getZ());
369  return *this;
370  }
371 
372  /**
373  * Get a quaternion from packet
374  * @return value
375  */
377  Quaternion value;
378  value.setX(getFloat());
379  value.setY(getFloat());
380  value.setZ(getFloat());
381  value.setW(getFloat());
382  return value;
383  }
384 
385  /**
386  * Puts a quaternion into packet
387  * @param value value
388  * @return this
389  */
391  putFloat(value.getX());
392  putFloat(value.getY());
393  putFloat(value.getZ());
394  putFloat(value.getW());
395  return *this;
396  }
397 
398 };
uint8_t getByte()
Get a byte from packet.
LogicNetworkPacket & putInt16(uint16_t value)
Puts a int16 into packet.
LogicNetworkPacket & putQuaternion(const Quaternion &value)
Puts a quaternion into packet.
void setSender(const string &sender)
Set sender.
float getFloat()
Get a float from packet.
LogicNetworkPacket & putInt(uint32_t value)
Puts a int into packet.
uint16_t getSmallInt()
Get a small int from packet.
LogicNetworkPacket & putVector3(const Vector3 &value)
Puts a vector3 into packet.
Quaternion getQuaternion()
Get a quaternion from packet.
LogicNetworkPacket & putString(const string &value)
Puts a string into packet.
void setProcessed(bool processed)
Set if packet has been processed.
LogicNetworkPacket & putFloat(float value)
Puts a float into packet.
LogicNetworkPacket(uint32_t messageId, bool safe, uint8_t retryCount, uint32_t logicTypeId, const UDPPacket *packet, uint8_t size)
Public constructor.
uint32_t getInt()
Get a int from packet.
void setLogicTypeId(uint32_t logicTypeId)
Set logic type id.
uint16_t getInt16()
Get a int16 from packet.
void addRecipient(const string &recipient)
Add recipients, no recipients means broadcast, multiple recipients means multi cast,...
Vector3 getVector3()
Get a vector3 from packet.
LogicNetworkPacket & putSmallInt(uint16_t value)
Puts a small int into packet.
void reset()
Reset position for read.
LogicNetworkPacket & putBool(bool value)
Puts a bool into packet.
LogicNetworkPacket & putByte(uint8_t value)
Puts a byte into packet.
static constexpr uint32_t LOGIC_TYPEID_NONE
LogicNetworkPacket(bool safe, uint32_t logicTypeId=LOGIC_TYPEID_NONE)
Public constructor, used to create a network packet.
bool getBool()
Get a bool from packet.
Quaternion class representing quaternion mathematical structure and operations with x,...
Definition: Quaternion.h:24
float getY() const
Definition: Quaternion.h:174
float getX() const
Definition: Quaternion.h:157
float getZ() const
Definition: Quaternion.h:191
float getW() const
Definition: Quaternion.h:208
Quaternion & setY(float y)
Sets y component.
Definition: Quaternion.h:183
Quaternion & setW(float w)
Sets w component.
Definition: Quaternion.h:217
Quaternion & setZ(float z)
Sets z component.
Definition: Quaternion.h:200
Quaternion & setX(float x)
Sets x component.
Definition: Quaternion.h:166
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
float getY() const
Definition: Vector3.h:117
Vector3 & setX(float x)
Sets x component.
Definition: Vector3.h:109
float getX() const
Definition: Vector3.h:100
float getZ() const
Definition: Vector3.h:134
Vector3 & setY(float y)
Sets y component.
Definition: Vector3.h:126
Vector3 & setZ(float z)
Sets z component.
Definition: Vector3.h:143
const UDPPacket * getBytes(uint8_t *bytes, uint16_t byteCount) const
Get raw bytes from packet.
Definition: UDPPacket.h:268
Console class.
Definition: Console.h:29
Time utility class.
Definition: Time.h:20