Browse Source

express: Add peek_int16 and peek_uint16 to DatagramIterator

rdb 3 years ago
parent
commit
d6ebd62034
2 changed files with 34 additions and 13 deletions
  1. 31 13
      panda/src/express/datagramIterator.I
  2. 3 0
      panda/src/express/datagramIterator.h

+ 31 - 13
panda/src/express/datagramIterator.I

@@ -82,10 +82,10 @@ get_uint8() {
 }
 
 /**
- * Extracts a signed 16-bit integer.
+ * Extracts a signed 16-bit integer without advancing the iterator.
  */
 INLINE int16_t DatagramIterator::
-get_int16() {
+peek_int16() {
   nassertr(_datagram != nullptr, 0);
   nassertr(_current_index < _datagram->get_length(), 0);
 
@@ -95,11 +95,38 @@ get_int16() {
   // Get the Data:
   LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
   s.store_value(&tempvar, sizeof(tempvar));
-  _current_index += sizeof(tempvar);
 
   return tempvar;
 }
 
+/**
+ * Extracts an unsigned 16-bit integer without advancing the iterator.
+ */
+INLINE uint16_t DatagramIterator::
+peek_uint16() {
+  nassertr(_datagram != nullptr, 0);
+  nassertr(_current_index < _datagram->get_length(), 0);
+
+  uint16_t tempvar;
+  // Avoid reading junk data off the end of the datagram:
+  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
+  // Get the Data:
+  LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
+  s.store_value(&tempvar, sizeof(tempvar));
+
+  return tempvar;
+}
+
+/**
+ * Extracts a signed 16-bit integer.
+ */
+INLINE int16_t DatagramIterator::
+get_int16() {
+  int16_t tempvar = peek_int16();
+  _current_index += sizeof(tempvar);
+  return tempvar;
+}
+
 /**
  * Extracts a signed 32-bit integer.
  */
@@ -143,17 +170,8 @@ get_int64() {
  */
 INLINE uint16_t DatagramIterator::
 get_uint16() {
-  nassertr(_datagram != nullptr, 0);
-  nassertr(_current_index < _datagram->get_length(), 0);
-
-  uint16_t tempvar;
-  // Avoid reading junk data off the end of the datagram:
-  nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
-  // Get the Data:
-  LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
-  s.store_value(&tempvar, sizeof(tempvar));
+  uint16_t tempvar = peek_uint16();
   _current_index += sizeof(tempvar);
-
   return tempvar;
 }
 

+ 3 - 0
panda/src/express/datagramIterator.h

@@ -39,6 +39,9 @@ PUBLISHED:
   INLINE int8_t get_int8();
   INLINE uint8_t get_uint8();
 
+  INLINE int16_t peek_int16();
+  INLINE uint16_t peek_uint16();
+
   INLINE int16_t get_int16();
   INLINE int32_t get_int32();
   INLINE int64_t get_int64();