Panda3D
|
00001 // Filename: datagramIterator.I 00002 // Created by: drose (08May01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: DatagramIterator::Constructor 00018 // Access: Public 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE DatagramIterator:: 00022 DatagramIterator() : 00023 _datagram((Datagram *)NULL), 00024 _current_index(0) { 00025 } 00026 00027 //////////////////////////////////////////////////////////////////// 00028 // Function: DatagramIterator::Constructor 00029 // Access: Public 00030 // Description: 00031 //////////////////////////////////////////////////////////////////// 00032 INLINE DatagramIterator:: 00033 DatagramIterator(const Datagram &datagram, size_t offset) : 00034 _datagram(&datagram), 00035 _current_index(offset) { 00036 nassertv(_current_index <= _datagram->get_length()); 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: DatagramIterator::Copy Constructor 00041 // Access: Public 00042 // Description: 00043 //////////////////////////////////////////////////////////////////// 00044 INLINE DatagramIterator:: 00045 DatagramIterator(const DatagramIterator ©) : 00046 _datagram(copy._datagram), 00047 _current_index(copy._current_index) { 00048 } 00049 00050 //////////////////////////////////////////////////////////////////// 00051 // Function: DatagramIterator::Copy Assignment Operator 00052 // Access: Public 00053 // Description: 00054 //////////////////////////////////////////////////////////////////// 00055 INLINE void DatagramIterator:: 00056 operator = (const DatagramIterator ©) { 00057 _datagram = copy._datagram; 00058 _current_index = copy._current_index; 00059 } 00060 //////////////////////////////////////////////////////////////////// 00061 // Function: DatagramIterator::assign 00062 // Access: Public 00063 // Description: direct Assignment to a Datagram 00064 //////////////////////////////////////////////////////////////////// 00065 INLINE void DatagramIterator::assign(Datagram &datagram, size_t offset) 00066 { 00067 _datagram =&datagram; 00068 _current_index = offset; 00069 } 00070 //////////////////////////////////////////////////////////////////// 00071 // Function: DatagramIterator::Destructor 00072 // Access: Public 00073 // Description: 00074 //////////////////////////////////////////////////////////////////// 00075 INLINE DatagramIterator:: 00076 ~DatagramIterator() { 00077 } 00078 00079 // Various ways to get data and increment the iterator... 00080 // Cut-and-paste-orama 00081 00082 //////////////////////////////////////////////////////////////////// 00083 // Function: DatagramIterator::get_bool 00084 // Access: Public 00085 // Description: Extracts a boolean value. 00086 //////////////////////////////////////////////////////////////////// 00087 INLINE bool DatagramIterator:: 00088 get_bool() { 00089 return get_uint8() != 0; 00090 } 00091 00092 //////////////////////////////////////////////////////////////////// 00093 // Function: DatagramIterator::get_int8 00094 // Access: Public 00095 // Description: Extracts a signed 8-bit integer. 00096 //////////////////////////////////////////////////////////////////// 00097 INLINE PN_int8 DatagramIterator:: 00098 get_int8() { 00099 nassertr(_datagram != (const Datagram *)NULL, 0); 00100 // Avoid reading junk data off the end of the datagram: 00101 nassertr(_current_index < _datagram->get_length(), 0); 00102 // Get the Data: 00103 const char *ptr = (const char *)_datagram->get_data(); 00104 PN_int8 tempvar = (PN_int8)ptr[_current_index]; 00105 ++_current_index; 00106 00107 return tempvar; 00108 } 00109 00110 //////////////////////////////////////////////////////////////////// 00111 // Function: DatagramIterator::get_uint8 00112 // Access: Public 00113 // Description: Extracts an unsigned 8-bit integer. 00114 //////////////////////////////////////////////////////////////////// 00115 INLINE PN_uint8 DatagramIterator:: 00116 get_uint8() { 00117 nassertr(_datagram != (const Datagram *)NULL, 0); 00118 // Avoid reading junk data off the end of the datagram: 00119 nassertr(_current_index < _datagram->get_length(), 0); 00120 // Get the Data: 00121 const char *ptr = (const char *)_datagram->get_data(); 00122 PN_uint8 tempvar = (PN_uint8)ptr[_current_index]; 00123 ++_current_index; 00124 00125 return tempvar; 00126 } 00127 00128 //////////////////////////////////////////////////////////////////// 00129 // Function: DatagramIterator::get_int16 00130 // Access: Public 00131 // Description: Extracts a signed 16-bit integer. 00132 //////////////////////////////////////////////////////////////////// 00133 INLINE PN_int16 DatagramIterator:: 00134 get_int16() { 00135 nassertr(_datagram != (const Datagram *)NULL, 0); 00136 nassertr(_current_index < _datagram->get_length(), 0); 00137 00138 PN_int16 tempvar; 00139 // Avoid reading junk data off the end of the datagram: 00140 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00141 // Get the Data: 00142 LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00143 s.store_value(&tempvar, sizeof(tempvar)); 00144 _current_index += sizeof(tempvar); 00145 00146 return tempvar; 00147 } 00148 00149 //////////////////////////////////////////////////////////////////// 00150 // Function: DatagramIterator::get_int32 00151 // Access: Public 00152 // Description: Extracts a signed 32-bit integer. 00153 //////////////////////////////////////////////////////////////////// 00154 INLINE PN_int32 DatagramIterator:: 00155 get_int32() { 00156 nassertr(_datagram != (const Datagram *)NULL, 0); 00157 nassertr(_current_index < _datagram->get_length(), 0); 00158 00159 PN_int32 tempvar; 00160 // Avoid reading junk data off the end of the datagram: 00161 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00162 // Get the Data: 00163 LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00164 s.store_value(&tempvar, sizeof(tempvar)); 00165 _current_index += sizeof(tempvar); 00166 00167 return tempvar; 00168 } 00169 00170 //////////////////////////////////////////////////////////////////// 00171 // Function: DatagramIterator::get_int64 00172 // Access: Public 00173 // Description: Extracts a signed 64-bit integer. 00174 //////////////////////////////////////////////////////////////////// 00175 INLINE PN_int64 DatagramIterator:: 00176 get_int64() { 00177 nassertr(_datagram != (const Datagram *)NULL, 0); 00178 nassertr(_current_index < _datagram->get_length(), 0); 00179 00180 PN_int64 tempvar; 00181 // Avoid reading junk data off the end of the datagram: 00182 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00183 // Get the Data: 00184 LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00185 s.store_value(&tempvar, sizeof(tempvar)); 00186 _current_index += sizeof(tempvar); 00187 00188 return tempvar; 00189 } 00190 00191 //////////////////////////////////////////////////////////////////// 00192 // Function: DatagramIterator::get_uint16 00193 // Access: Public 00194 // Description: Extracts an unsigned 16-bit integer. 00195 //////////////////////////////////////////////////////////////////// 00196 INLINE PN_uint16 DatagramIterator:: 00197 get_uint16() { 00198 nassertr(_datagram != (const Datagram *)NULL, 0); 00199 nassertr(_current_index < _datagram->get_length(), 0); 00200 00201 PN_uint16 tempvar; 00202 // Avoid reading junk data off the end of the datagram: 00203 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00204 // Get the Data: 00205 LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00206 s.store_value(&tempvar, sizeof(tempvar)); 00207 _current_index += sizeof(tempvar); 00208 00209 return tempvar; 00210 } 00211 00212 //////////////////////////////////////////////////////////////////// 00213 // Function: DatagramIterator::get_uint32 00214 // Access: Public 00215 // Description: Extracts an unsigned 32-bit integer. 00216 //////////////////////////////////////////////////////////////////// 00217 INLINE PN_uint32 DatagramIterator:: 00218 get_uint32() { 00219 nassertr(_datagram != (const Datagram *)NULL, 0); 00220 nassertr(_current_index < _datagram->get_length(), 0); 00221 00222 PN_uint32 tempvar; 00223 // Avoid reading junk data off the end of the datagram: 00224 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00225 // Get the Data: 00226 LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00227 s.store_value(&tempvar, sizeof(tempvar)); 00228 _current_index += sizeof(tempvar); 00229 00230 return tempvar; 00231 } 00232 00233 //////////////////////////////////////////////////////////////////// 00234 // Function: DatagramIterator::get_uint64 00235 // Access: Public 00236 // Description: Extracts an unsigned 64-bit integer. 00237 //////////////////////////////////////////////////////////////////// 00238 INLINE PN_uint64 DatagramIterator:: 00239 get_uint64() { 00240 nassertr(_datagram != (const Datagram *)NULL, 0); 00241 nassertr(_current_index < _datagram->get_length(), 0); 00242 00243 PN_uint64 tempvar; 00244 // Avoid reading junk data off the end of the datagram: 00245 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00246 // Get the Data: 00247 LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00248 s.store_value(&tempvar, sizeof(tempvar)); 00249 _current_index += sizeof(tempvar); 00250 00251 return tempvar; 00252 } 00253 00254 //////////////////////////////////////////////////////////////////// 00255 // Function: DatagramIterator::get_float32 00256 // Access: Public 00257 // Description: Extracts a 32-bit single-precision floating-point 00258 // number. Since this kind of float is not necessarily 00259 // portable across different architectures, special care 00260 // is required. 00261 //////////////////////////////////////////////////////////////////// 00262 INLINE float DatagramIterator:: 00263 get_float32() { 00264 // For now, we assume the float format is portable across all 00265 // architectures we are concerned with. If we come across one that 00266 // is different, we will have to convert. 00267 nassertr(sizeof(float) == 4, 0.0f); 00268 nassertr(_datagram != (const Datagram *)NULL, 0.0); 00269 nassertr(_current_index < _datagram->get_length(), 0.0); 00270 00271 float tempvar; 00272 // Avoid reading junk data off the end of the datagram: 00273 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0.0); 00274 // Get the Data: 00275 LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00276 s.store_value(&tempvar, sizeof(tempvar)); 00277 _current_index += sizeof(tempvar); 00278 00279 return tempvar; 00280 } 00281 00282 //////////////////////////////////////////////////////////////////// 00283 // Function: DatagramIterator::get_float64 00284 // Access: Public 00285 // Description: Extracts a 64-bit floating-point number. 00286 //////////////////////////////////////////////////////////////////// 00287 INLINE PN_float64 DatagramIterator:: 00288 get_float64() { 00289 nassertr(_datagram != (const Datagram *)NULL, 0.0); 00290 nassertr(_current_index < _datagram->get_length(), 0.0); 00291 00292 PN_float64 tempvar; 00293 // Avoid reading junk data off the end of the datagram: 00294 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0.0); 00295 // Get the Data: 00296 LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00297 s.store_value(&tempvar, sizeof(tempvar)); 00298 _current_index += sizeof(tempvar); 00299 00300 return tempvar; 00301 } 00302 00303 //////////////////////////////////////////////////////////////////// 00304 // Function: DatagramIterator::get_be_int16 00305 // Access: Public 00306 // Description: Extracts a signed 16-bit big-endian integer. 00307 //////////////////////////////////////////////////////////////////// 00308 INLINE PN_int16 DatagramIterator:: 00309 get_be_int16() { 00310 nassertr(_datagram != (const Datagram *)NULL, 0); 00311 nassertr(_current_index < _datagram->get_length(), 0); 00312 00313 PN_int16 tempvar; 00314 // Avoid reading junk data off the end of the datagram: 00315 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00316 // Get the Data: 00317 BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00318 s.store_value(&tempvar, sizeof(tempvar)); 00319 _current_index += sizeof(tempvar); 00320 00321 return tempvar; 00322 } 00323 00324 //////////////////////////////////////////////////////////////////// 00325 // Function: DatagramIterator::get_be_int32 00326 // Access: Public 00327 // Description: Extracts a signed 32-bit big-endian integer. 00328 //////////////////////////////////////////////////////////////////// 00329 INLINE PN_int32 DatagramIterator:: 00330 get_be_int32() { 00331 nassertr(_datagram != (const Datagram *)NULL, 0); 00332 nassertr(_current_index < _datagram->get_length(), 0); 00333 00334 PN_int32 tempvar; 00335 // Avoid reading junk data off the end of the datagram: 00336 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00337 // Get the Data: 00338 BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00339 s.store_value(&tempvar, sizeof(tempvar)); 00340 _current_index += sizeof(tempvar); 00341 00342 return tempvar; 00343 } 00344 00345 //////////////////////////////////////////////////////////////////// 00346 // Function: DatagramIterator::get_be_int64 00347 // Access: Public 00348 // Description: Extracts a signed 64-bit big-endian integer. 00349 //////////////////////////////////////////////////////////////////// 00350 INLINE PN_int64 DatagramIterator:: 00351 get_be_int64() { 00352 nassertr(_datagram != (const Datagram *)NULL, 0); 00353 nassertr(_current_index < _datagram->get_length(), 0); 00354 00355 PN_int64 tempvar; 00356 // Avoid reading junk data off the end of the datagram: 00357 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00358 // Get the Data: 00359 BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00360 s.store_value(&tempvar, sizeof(tempvar)); 00361 _current_index += sizeof(tempvar); 00362 00363 return tempvar; 00364 } 00365 00366 //////////////////////////////////////////////////////////////////// 00367 // Function: DatagramIterator::get_be_uint16 00368 // Access: Public 00369 // Description: Extracts an unsigned 16-bit big-endian integer. 00370 //////////////////////////////////////////////////////////////////// 00371 INLINE PN_uint16 DatagramIterator:: 00372 get_be_uint16() { 00373 nassertr(_datagram != (const Datagram *)NULL, 0); 00374 nassertr(_current_index < _datagram->get_length(), 0); 00375 00376 PN_uint16 tempvar; 00377 // Avoid reading junk data off the end of the datagram: 00378 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00379 // Get the Data: 00380 BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00381 s.store_value(&tempvar, sizeof(tempvar)); 00382 _current_index += sizeof(tempvar); 00383 00384 return tempvar; 00385 } 00386 00387 //////////////////////////////////////////////////////////////////// 00388 // Function: DatagramIterator::get_be_uint32 00389 // Access: Public 00390 // Description: Extracts an unsigned 32-bit big-endian integer. 00391 //////////////////////////////////////////////////////////////////// 00392 INLINE PN_uint32 DatagramIterator:: 00393 get_be_uint32() { 00394 nassertr(_datagram != (const Datagram *)NULL, 0); 00395 nassertr(_current_index < _datagram->get_length(), 0); 00396 00397 PN_uint32 tempvar; 00398 // Avoid reading junk data off the end of the datagram: 00399 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00400 // Get the Data: 00401 BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00402 s.store_value(&tempvar, sizeof(tempvar)); 00403 _current_index += sizeof(tempvar); 00404 00405 return tempvar; 00406 } 00407 00408 //////////////////////////////////////////////////////////////////// 00409 // Function: DatagramIterator::get_be_uint64 00410 // Access: Public 00411 // Description: Extracts an unsigned 64-bit big-endian integer. 00412 //////////////////////////////////////////////////////////////////// 00413 INLINE PN_uint64 DatagramIterator:: 00414 get_be_uint64() { 00415 nassertr(_datagram != (const Datagram *)NULL, 0); 00416 nassertr(_current_index < _datagram->get_length(), 0); 00417 00418 PN_uint64 tempvar; 00419 // Avoid reading junk data off the end of the datagram: 00420 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00421 // Get the Data: 00422 BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00423 s.store_value(&tempvar, sizeof(tempvar)); 00424 _current_index += sizeof(tempvar); 00425 00426 return tempvar; 00427 } 00428 00429 //////////////////////////////////////////////////////////////////// 00430 // Function: DatagramIterator::get_be_float32 00431 // Access: Public 00432 // Description: Extracts a 32-bit big-endian single-precision 00433 // floating-point number. Since this kind of float is 00434 // not necessarily portable across different 00435 // architectures, special care is required. 00436 //////////////////////////////////////////////////////////////////// 00437 INLINE float DatagramIterator:: 00438 get_be_float32() { 00439 // For now, we assume the float format is portable across all 00440 // architectures we are concerned with. If we come across one that 00441 // is different, we will have to convert. 00442 nassertr(sizeof(float) == 4, 0.0f); 00443 nassertr(_datagram != (const Datagram *)NULL, 0.0); 00444 nassertr(_current_index < _datagram->get_length(), 0.0); 00445 00446 float tempvar; 00447 // Avoid reading junk data off the end of the datagram: 00448 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0); 00449 // Get the Data: 00450 BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00451 s.store_value(&tempvar, sizeof(tempvar)); 00452 _current_index += sizeof(tempvar); 00453 00454 return tempvar; 00455 } 00456 00457 //////////////////////////////////////////////////////////////////// 00458 // Function: DatagramIterator::get_be_float64 00459 // Access: Public 00460 // Description: Extracts a 64-bit big-endian floating-point number. 00461 //////////////////////////////////////////////////////////////////// 00462 INLINE PN_float64 DatagramIterator:: 00463 get_be_float64() { 00464 nassertr(_datagram != (const Datagram *)NULL, 0.0); 00465 nassertr(_current_index < _datagram->get_length(), 0.0); 00466 00467 PN_float64 tempvar; 00468 // Avoid reading junk data off the end of the datagram: 00469 nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0.0); 00470 // Get the Data: 00471 BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar)); 00472 s.store_value(&tempvar, sizeof(tempvar)); 00473 _current_index += sizeof(tempvar); 00474 00475 return tempvar; 00476 } 00477 00478 //////////////////////////////////////////////////////////////////// 00479 // Function: DatagramIterator::skip_bytes 00480 // Access: Public 00481 // Description: Skips over the indicated number of bytes in the 00482 // datagram. 00483 //////////////////////////////////////////////////////////////////// 00484 INLINE void DatagramIterator:: 00485 skip_bytes(size_t size) { 00486 nassertv(_datagram != (const Datagram *)NULL); 00487 nassertv((int)size >= 0); 00488 #ifndef NDEBUG 00489 if (_current_index + size > _datagram->get_length()) { 00490 nout << "datagram overflow: current_index = " << _current_index 00491 << " size = " << size << " length = " << _datagram->get_length() << "\n"; 00492 _datagram->dump_hex(nout); 00493 } 00494 #endif 00495 nassertv(_current_index + size <= _datagram->get_length()); 00496 _current_index += size; 00497 } 00498 00499 //////////////////////////////////////////////////////////////////// 00500 // Function: DatagramIterator::get_remaining_bytes 00501 // Access: Public 00502 // Description: Returns the remaining bytes in the datagram as a 00503 // string, but does not extract them from the iterator. 00504 //////////////////////////////////////////////////////////////////// 00505 INLINE string DatagramIterator:: 00506 get_remaining_bytes() const { 00507 nassertr(_datagram != (const Datagram *)NULL, ""); 00508 nassertr(_current_index <= _datagram->get_length(), ""); 00509 00510 const char *ptr = (const char *)_datagram->get_data(); 00511 int remaining_size = _datagram->get_length() - _current_index; 00512 return string(ptr + _current_index, remaining_size); 00513 } 00514 00515 //////////////////////////////////////////////////////////////////// 00516 // Function: DatagramIterator::get_remaining_size 00517 // Access: Public 00518 // Description: Return the bytes left in the datagram. 00519 //////////////////////////////////////////////////////////////////// 00520 INLINE int DatagramIterator:: 00521 get_remaining_size() const { 00522 return _datagram->get_length() - _current_index; 00523 } 00524 00525 //////////////////////////////////////////////////////////////////// 00526 // Function: DatagramIterator::get_datagram 00527 // Access: Public 00528 // Description: Return the datagram of this iterator. 00529 //////////////////////////////////////////////////////////////////// 00530 INLINE const Datagram &DatagramIterator:: 00531 get_datagram() const { 00532 return *_datagram; 00533 } 00534 00535 //////////////////////////////////////////////////////////////////// 00536 // Function: DatagramIterator::get_current_index 00537 // Access: Public 00538 // Description: Returns the current position within the datagram of the 00539 // next piece of data to extract. 00540 //////////////////////////////////////////////////////////////////// 00541 INLINE size_t DatagramIterator:: 00542 get_current_index() const { 00543 return _current_index; 00544 } 00545 00546 INLINE void 00547 generic_read_datagram(bool &result, DatagramIterator &source) { 00548 result = source.get_bool(); 00549 } 00550 00551 INLINE void 00552 generic_read_datagram(int &result, DatagramIterator &source) { 00553 result = source.get_int32(); 00554 } 00555 00556 INLINE void 00557 generic_read_datagram(float &result, DatagramIterator &source) { 00558 result = source.get_float32(); 00559 } 00560 00561 INLINE void 00562 generic_read_datagram(double &result, DatagramIterator &source) { 00563 result = source.get_float64(); 00564 } 00565 00566 INLINE void 00567 generic_read_datagram(string &result, DatagramIterator &source) { 00568 result = source.get_string(); 00569 } 00570 00571 INLINE void 00572 generic_read_datagram(wstring &result, DatagramIterator &source) { 00573 result = source.get_wstring(); 00574 } 00575 00576