Panda3D
datagramIterator.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file datagramIterator.I
10  * @author drose
11  * @date 2001-05-08
12  */
13 
14 /**
15  *
16  */
17 INLINE DatagramIterator::
18 DatagramIterator() :
19  _datagram(nullptr),
20  _current_index(0) {
21 }
22 
23 /**
24  *
25  */
26 INLINE DatagramIterator::
27 DatagramIterator(const Datagram &datagram, size_t offset) :
28  _datagram(&datagram),
29  _current_index(offset) {
30  nassertv(_current_index <= _datagram->get_length());
31 }
32 
33 /**
34  * direct Assignment to a Datagram
35  */
36 INLINE void DatagramIterator::
37 assign(Datagram &datagram, size_t offset) {
38  _datagram = &datagram;
39  _current_index = offset;
40 }
41 
42 // Various ways to get data and increment the iterator... Cut-and-paste-orama
43 
44 /**
45  * Extracts a boolean value.
46  */
47 INLINE bool DatagramIterator::
49  return get_uint8() != 0;
50 }
51 
52 /**
53  * Extracts a signed 8-bit integer.
54  */
55 INLINE int8_t DatagramIterator::
57  nassertr(_datagram != nullptr, 0);
58  // Avoid reading junk data off the end of the datagram:
59  nassertr(_current_index < _datagram->get_length(), 0);
60  // Get the Data:
61  const char *ptr = (const char *)_datagram->get_data();
62  int8_t tempvar = (int8_t)ptr[_current_index];
63  ++_current_index;
64 
65  return tempvar;
66 }
67 
68 /**
69  * Extracts an unsigned 8-bit integer.
70  */
71 INLINE uint8_t DatagramIterator::
73  nassertr(_datagram != nullptr, 0);
74  // Avoid reading junk data off the end of the datagram:
75  nassertr(_current_index < _datagram->get_length(), 0);
76  // Get the Data:
77  const char *ptr = (const char *)_datagram->get_data();
78  uint8_t tempvar = (uint8_t)ptr[_current_index];
79  ++_current_index;
80 
81  return tempvar;
82 }
83 
84 /**
85  * Extracts a signed 16-bit integer.
86  */
87 INLINE int16_t DatagramIterator::
89  nassertr(_datagram != nullptr, 0);
90  nassertr(_current_index < _datagram->get_length(), 0);
91 
92  int16_t tempvar;
93  // Avoid reading junk data off the end of the datagram:
94  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
95  // Get the Data:
96  LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
97  s.store_value(&tempvar, sizeof(tempvar));
98  _current_index += sizeof(tempvar);
99 
100  return tempvar;
101 }
102 
103 /**
104  * Extracts a signed 32-bit integer.
105  */
106 INLINE int32_t DatagramIterator::
108  nassertr(_datagram != nullptr, 0);
109  nassertr(_current_index < _datagram->get_length(), 0);
110 
111  int32_t tempvar;
112  // Avoid reading junk data off the end of the datagram:
113  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
114  // Get the Data:
115  LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
116  s.store_value(&tempvar, sizeof(tempvar));
117  _current_index += sizeof(tempvar);
118 
119  return tempvar;
120 }
121 
122 /**
123  * Extracts a signed 64-bit integer.
124  */
125 INLINE int64_t DatagramIterator::
127  nassertr(_datagram != nullptr, 0);
128  nassertr(_current_index < _datagram->get_length(), 0);
129 
130  int64_t tempvar;
131  // Avoid reading junk data off the end of the datagram:
132  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
133  // Get the Data:
134  LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
135  s.store_value(&tempvar, sizeof(tempvar));
136  _current_index += sizeof(tempvar);
137 
138  return tempvar;
139 }
140 
141 /**
142  * Extracts an unsigned 16-bit integer.
143  */
144 INLINE uint16_t DatagramIterator::
146  nassertr(_datagram != nullptr, 0);
147  nassertr(_current_index < _datagram->get_length(), 0);
148 
149  uint16_t tempvar;
150  // Avoid reading junk data off the end of the datagram:
151  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
152  // Get the Data:
153  LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
154  s.store_value(&tempvar, sizeof(tempvar));
155  _current_index += sizeof(tempvar);
156 
157  return tempvar;
158 }
159 
160 /**
161  * Extracts an unsigned 32-bit integer.
162  */
163 INLINE uint32_t DatagramIterator::
165  nassertr(_datagram != nullptr, 0);
166  nassertr(_current_index < _datagram->get_length(), 0);
167 
168  uint32_t tempvar;
169  // Avoid reading junk data off the end of the datagram:
170  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
171  // Get the Data:
172  LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
173  s.store_value(&tempvar, sizeof(tempvar));
174  _current_index += sizeof(tempvar);
175 
176  return tempvar;
177 }
178 
179 /**
180  * Extracts an unsigned 64-bit integer.
181  */
182 INLINE uint64_t DatagramIterator::
184  nassertr(_datagram != nullptr, 0);
185  nassertr(_current_index < _datagram->get_length(), 0);
186 
187  uint64_t tempvar;
188  // Avoid reading junk data off the end of the datagram:
189  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
190  // Get the Data:
191  LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
192  s.store_value(&tempvar, sizeof(tempvar));
193  _current_index += sizeof(tempvar);
194 
195  return tempvar;
196 }
197 
198 /**
199  * Extracts a 32-bit single-precision floating-point number.
200  */
201 INLINE PN_float32 DatagramIterator::
203  nassertr(_datagram != nullptr, 0.0);
204  nassertr(_current_index < _datagram->get_length(), 0.0);
205 
206  PN_float32 tempvar;
207  // Avoid reading junk data off the end of the datagram:
208  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0.0);
209  // Get the Data:
210  LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
211  s.store_value(&tempvar, sizeof(tempvar));
212  _current_index += sizeof(tempvar);
213 
214  return tempvar;
215 }
216 
217 /**
218  * Extracts a 64-bit floating-point number.
219  */
220 INLINE PN_float64 DatagramIterator::
222  nassertr(_datagram != nullptr, 0.0);
223  nassertr(_current_index < _datagram->get_length(), 0.0);
224 
225  PN_float64 tempvar;
226  // Avoid reading junk data off the end of the datagram:
227  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0.0);
228  // Get the Data:
229  LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
230  s.store_value(&tempvar, sizeof(tempvar));
231  _current_index += sizeof(tempvar);
232 
233  return tempvar;
234 }
235 
236 
237 /**
238  * Extracts either a 32-bit or a 64-bit floating-point number, according to
239  * Datagram::set_stdfloat_double().
240  */
241 INLINE PN_stdfloat DatagramIterator::
243  nassertr(_datagram != nullptr, 0.0);
244  if (_datagram->get_stdfloat_double()) {
245  return (PN_stdfloat)get_float64();
246  } else {
247  return (PN_stdfloat)get_float32();
248  }
249 }
250 
251 /**
252  * Extracts a signed 16-bit big-endian integer.
253  */
254 INLINE int16_t DatagramIterator::
256  nassertr(_datagram != nullptr, 0);
257  nassertr(_current_index < _datagram->get_length(), 0);
258 
259  int16_t tempvar;
260  // Avoid reading junk data off the end of the datagram:
261  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
262  // Get the Data:
263  BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
264  s.store_value(&tempvar, sizeof(tempvar));
265  _current_index += sizeof(tempvar);
266 
267  return tempvar;
268 }
269 
270 /**
271  * Extracts a signed 32-bit big-endian integer.
272  */
273 INLINE int32_t DatagramIterator::
275  nassertr(_datagram != nullptr, 0);
276  nassertr(_current_index < _datagram->get_length(), 0);
277 
278  int32_t tempvar;
279  // Avoid reading junk data off the end of the datagram:
280  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
281  // Get the Data:
282  BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
283  s.store_value(&tempvar, sizeof(tempvar));
284  _current_index += sizeof(tempvar);
285 
286  return tempvar;
287 }
288 
289 /**
290  * Extracts a signed 64-bit big-endian integer.
291  */
292 INLINE int64_t DatagramIterator::
294  nassertr(_datagram != nullptr, 0);
295  nassertr(_current_index < _datagram->get_length(), 0);
296 
297  int64_t tempvar;
298  // Avoid reading junk data off the end of the datagram:
299  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
300  // Get the Data:
301  BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
302  s.store_value(&tempvar, sizeof(tempvar));
303  _current_index += sizeof(tempvar);
304 
305  return tempvar;
306 }
307 
308 /**
309  * Extracts an unsigned 16-bit big-endian integer.
310  */
311 INLINE uint16_t DatagramIterator::
313  nassertr(_datagram != nullptr, 0);
314  nassertr(_current_index < _datagram->get_length(), 0);
315 
316  uint16_t tempvar;
317  // Avoid reading junk data off the end of the datagram:
318  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
319  // Get the Data:
320  BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
321  s.store_value(&tempvar, sizeof(tempvar));
322  _current_index += sizeof(tempvar);
323 
324  return tempvar;
325 }
326 
327 /**
328  * Extracts an unsigned 32-bit big-endian integer.
329  */
330 INLINE uint32_t DatagramIterator::
332  nassertr(_datagram != nullptr, 0);
333  nassertr(_current_index < _datagram->get_length(), 0);
334 
335  uint32_t tempvar;
336  // Avoid reading junk data off the end of the datagram:
337  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
338  // Get the Data:
339  BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
340  s.store_value(&tempvar, sizeof(tempvar));
341  _current_index += sizeof(tempvar);
342 
343  return tempvar;
344 }
345 
346 /**
347  * Extracts an unsigned 64-bit big-endian integer.
348  */
349 INLINE uint64_t DatagramIterator::
351  nassertr(_datagram != nullptr, 0);
352  nassertr(_current_index < _datagram->get_length(), 0);
353 
354  uint64_t tempvar;
355  // Avoid reading junk data off the end of the datagram:
356  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
357  // Get the Data:
358  BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
359  s.store_value(&tempvar, sizeof(tempvar));
360  _current_index += sizeof(tempvar);
361 
362  return tempvar;
363 }
364 
365 /**
366  * Extracts a 32-bit big-endian single-precision floating-point number.
367  */
368 INLINE PN_float32 DatagramIterator::
370  nassertr(_datagram != nullptr, 0.0);
371  nassertr(_current_index < _datagram->get_length(), 0.0);
372 
373  PN_float32 tempvar;
374  // Avoid reading junk data off the end of the datagram:
375  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
376  // Get the Data:
377  BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
378  s.store_value(&tempvar, sizeof(tempvar));
379  _current_index += sizeof(tempvar);
380 
381  return tempvar;
382 }
383 
384 /**
385  * Extracts a 64-bit big-endian floating-point number.
386  */
387 INLINE PN_float64 DatagramIterator::
389  nassertr(_datagram != nullptr, 0.0);
390  nassertr(_current_index < _datagram->get_length(), 0.0);
391 
392  PN_float64 tempvar;
393  // Avoid reading junk data off the end of the datagram:
394  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0.0);
395  // Get the Data:
396  BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
397  s.store_value(&tempvar, sizeof(tempvar));
398  _current_index += sizeof(tempvar);
399 
400  return tempvar;
401 }
402 
403 /**
404  * Extracts a variable-length binary blob.
405  */
406 INLINE vector_uchar DatagramIterator::
408  return extract_bytes(get_uint16());
409 }
410 
411 /**
412  * Extracts a variable-length binary blob with a 32-bit size field.
413  */
414 INLINE vector_uchar DatagramIterator::
416  return extract_bytes(get_uint32());
417 }
418 
419 /**
420  * Skips over the indicated number of bytes in the datagram.
421  */
422 INLINE void DatagramIterator::
423 skip_bytes(size_t size) {
424  nassertv(_datagram != nullptr);
425  nassertv((int)size >= 0);
426 #ifndef NDEBUG
427  if (_current_index + size > _datagram->get_length()) {
428  nout << "datagram overflow: current_index = " << _current_index
429  << " size = " << size << " length = " << _datagram->get_length() << "\n";
430  _datagram->dump_hex(nout);
431  }
432 #endif
433  nassertv(_current_index + size <= _datagram->get_length());
434  _current_index += size;
435 }
436 
437 /**
438  * Returns the remaining bytes in the datagram as a string, but does not
439  * extract them from the iterator.
440  */
441 INLINE vector_uchar DatagramIterator::
443  nassertr(_datagram != nullptr, vector_uchar());
444  nassertr(_current_index <= _datagram->get_length(), vector_uchar());
445 
446  const unsigned char *ptr = (const unsigned char *)_datagram->get_data();
447  return vector_uchar(ptr + _current_index, ptr + _datagram->get_length());
448 }
449 
450 /**
451  * Return the bytes left in the datagram.
452  */
453 INLINE size_t DatagramIterator::
455  return _datagram->get_length() - _current_index;
456 }
457 
458 /**
459  * Return the datagram of this iterator.
460  */
461 INLINE const Datagram &DatagramIterator::
462 get_datagram() const {
463  return *_datagram;
464 }
465 
466 /**
467  * Returns the current position within the datagram of the next piece of data
468  * to extract.
469  */
470 INLINE size_t DatagramIterator::
472  return _current_index;
473 }
474 
475 INLINE void
476 generic_read_datagram(bool &result, DatagramIterator &source) {
477  result = source.get_bool();
478 }
479 
480 INLINE void
481 generic_read_datagram(int &result, DatagramIterator &source) {
482  result = source.get_int32();
483 }
484 
485 INLINE void
486 generic_read_datagram(float &result, DatagramIterator &source) {
487  result = source.get_float32();
488 }
489 
490 INLINE void
491 generic_read_datagram(double &result, DatagramIterator &source) {
492  result = source.get_float64();
493 }
494 
495 INLINE void
496 generic_read_datagram(std::string &result, DatagramIterator &source) {
497  result = source.get_string();
498 }
499 
500 INLINE void
501 generic_read_datagram(std::wstring &result, DatagramIterator &source) {
502  result = source.get_wstring();
503 }
504 
505 INLINE void
506 generic_read_datagram(vector_uchar &result, DatagramIterator &source) {
507  result = source.get_blob();
508 }
vector_uchar get_remaining_bytes() const
Returns the remaining bytes in the datagram as a string, but does not extract them from the iterator.
void assign(Datagram &datagram, size_t offset=0)
direct Assignment to a Datagram
bool get_bool()
Extracts a boolean value.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
uint64_t get_uint64()
Extracts an unsigned 64-bit integer.
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
PN_float64 get_be_float64()
Extracts a 64-bit big-endian floating-point number.
void dump_hex(std::ostream &out, unsigned int indent=0) const
Writes a representation of the entire datagram contents, as a sequence of hex (and ASCII) values.
Definition: datagram.cxx:44
const Datagram & get_datagram() const
Return the datagram of this iterator.
uint64_t get_be_uint64()
Extracts an unsigned 64-bit big-endian integer.
int32_t get_be_int32()
Extracts a signed 32-bit big-endian integer.
bool get_stdfloat_double() const
Returns the stdfloat_double flag.
Definition: datagram.I:403
PN_float32 get_be_float32()
Extracts a 32-bit big-endian single-precision floating-point number.
int32_t get_int32()
Extracts a signed 32-bit integer.
size_t get_remaining_size() const
Return the bytes left in the datagram.
int64_t get_int64()
Extracts a signed 64-bit integer.
std::string get_string()
Extracts a variable-length string.
PN_float32 get_float32()
Extracts a 32-bit single-precision floating-point number.
vector_uchar extract_bytes(size_t size)
Extracts the indicated number of bytes in the datagram and returns them as a string.
uint16_t get_be_uint16()
Extracts an unsigned 16-bit big-endian integer.
size_t get_current_index() const
Returns the current position within the datagram of the next piece of data to extract.
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
vector_uchar get_blob32()
Extracts a variable-length binary blob with a 32-bit size field.
NativeNumericData and ReversedNumericData work together to provide a sneaky interface for automatical...
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
NativeNumericData and ReversedNumericData work together to provide a sneaky interface for automatical...
void store_value(void *dest, size_t length) const
Copies the data, with byte reversal if appropriate, into the indicated numeric variable,...
PN_float64 get_float64()
Extracts a 64-bit floating-point number.
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
vector_uchar get_blob()
Extracts a variable-length binary blob.
int16_t get_int16()
Extracts a signed 16-bit integer.
A class to retrieve the individual data elements previously stored in a Datagram.
int16_t get_be_int16()
Extracts a signed 16-bit big-endian integer.
int8_t get_int8()
Extracts a signed 8-bit integer.
uint32_t get_be_uint32()
Extracts an unsigned 32-bit big-endian integer.
int64_t get_be_int64()
Extracts a signed 64-bit big-endian integer.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
void store_value(void *dest, size_t length) const
Copies the data, with byte reversal if appropriate, into the indicated numeric variable,...
std::wstring get_wstring()
Extracts a variable-length wstring (with a 32-bit length field).
size_t get_length() const
Returns the number of bytes in the datagram.
Definition: datagram.I:335
const void * get_data() const
Returns a pointer to the beginning of the datagram's data.
Definition: datagram.I:327