Panda3D

datagram.I

00001 // Filename: datagram.I
00002 // Created by:  drose (06Jun00)
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 //     Function: Datagram::Constructor
00017 //       Access: Public
00018 //  Description: Constructs an empty datagram.
00019 ////////////////////////////////////////////////////////////////////
00020 INLINE Datagram::
00021 Datagram() :
00022 #ifdef STDFLOAT_DOUBLE
00023   _stdfloat_double(true)
00024 #else
00025   _stdfloat_double(false)
00026 #endif
00027 {
00028 }
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: Datagram::Constructor
00032 //       Access: Public
00033 //  Description: Constructs a datagram from an existing block of data.
00034 ////////////////////////////////////////////////////////////////////
00035 INLINE Datagram::
00036 Datagram(const void *data, size_t size) :
00037 #ifdef STDFLOAT_DOUBLE
00038   _stdfloat_double(true)
00039 #else
00040   _stdfloat_double(false)
00041 #endif
00042 {
00043   append_data(data, size);
00044 }
00045 
00046 ////////////////////////////////////////////////////////////////////
00047 //     Function: Datagram::Constructor
00048 //       Access: Public
00049 //  Description: Constructs a datagram from an existing block of data.
00050 ////////////////////////////////////////////////////////////////////
00051 INLINE Datagram::
00052 Datagram(const string &data) :
00053 #ifdef STDFLOAT_DOUBLE
00054   _stdfloat_double(true)
00055 #else
00056   _stdfloat_double(false)
00057 #endif
00058 {
00059   append_data(data);
00060 }
00061 
00062 ////////////////////////////////////////////////////////////////////
00063 //     Function: Datagram::Copy Constructor
00064 //       Access: Public
00065 //  Description:
00066 ////////////////////////////////////////////////////////////////////
00067 INLINE Datagram::
00068 Datagram(const Datagram &copy) :
00069   _data(copy._data),
00070   _stdfloat_double(copy._stdfloat_double)
00071 {
00072 }
00073 
00074 ////////////////////////////////////////////////////////////////////
00075 //     Function: Datagram::Copy Assignment Operator
00076 //       Access: Public
00077 //  Description:
00078 ////////////////////////////////////////////////////////////////////
00079 INLINE void Datagram::
00080 operator = (const Datagram &copy) {
00081   _data = copy._data;
00082   _stdfloat_double = copy._stdfloat_double;
00083 }
00084 
00085 ////////////////////////////////////////////////////////////////////
00086 //     Function: Datagram::add_bool
00087 //       Access: Public
00088 //  Description: Adds a boolean value to the datagram.
00089 ////////////////////////////////////////////////////////////////////
00090 INLINE void Datagram::
00091 add_bool(bool b) {
00092   add_uint8(b);
00093 }
00094 
00095 ////////////////////////////////////////////////////////////////////
00096 //     Function: Datagram::add_int8
00097 //       Access: Public
00098 //  Description: Adds a signed 8-bit integer to the datagram.
00099 ////////////////////////////////////////////////////////////////////
00100 INLINE void Datagram::
00101 add_int8(PN_int8 value) {
00102   append_data(&value, 1);
00103 }
00104 
00105 ////////////////////////////////////////////////////////////////////
00106 //     Function: Datagram::add_uint8
00107 //       Access: Public
00108 //  Description: Adds an unsigned 8-bit integer to the datagram.
00109 ////////////////////////////////////////////////////////////////////
00110 INLINE void Datagram::
00111 add_uint8(PN_uint8 value) {
00112   append_data(&value, 1);
00113 }
00114 
00115 ////////////////////////////////////////////////////////////////////
00116 //     Function: Datagram::add_int16
00117 //       Access: Public
00118 //  Description: Adds a signed 16-bit integer to the datagram.
00119 ////////////////////////////////////////////////////////////////////
00120 INLINE void Datagram::
00121 add_int16(PN_int16 value) {
00122   LittleEndian s(&value, sizeof(value));
00123   append_data(s.get_data(), sizeof(value));
00124 }
00125 
00126 ////////////////////////////////////////////////////////////////////
00127 //     Function: Datagram::add_int32
00128 //       Access: Public
00129 //  Description: Adds a signed 32-bit integer to the datagram.
00130 ////////////////////////////////////////////////////////////////////
00131 INLINE void Datagram::
00132 add_int32(PN_int32 value) {
00133   LittleEndian s(&value, sizeof(value));
00134   append_data(s.get_data(), sizeof(value));
00135 }
00136 
00137 ////////////////////////////////////////////////////////////////////
00138 //     Function: Datagram::add_int64
00139 //       Access: Public
00140 //  Description: Adds a signed 64-bit integer to the datagram.
00141 ////////////////////////////////////////////////////////////////////
00142 INLINE void Datagram::
00143 add_int64(PN_int64 value) {
00144   LittleEndian s(&value, sizeof(value));
00145   append_data(s.get_data(), sizeof(value));
00146 }
00147 
00148 ////////////////////////////////////////////////////////////////////
00149 //     Function: Datagram::add_uint16
00150 //       Access: Public
00151 //  Description: Adds an unsigned 16-bit integer to the datagram.
00152 ////////////////////////////////////////////////////////////////////
00153 INLINE void Datagram::
00154 add_uint16(PN_uint16 value) {
00155   LittleEndian s(&value, sizeof(value));
00156   append_data(s.get_data(), sizeof(value));
00157 }
00158 
00159 ////////////////////////////////////////////////////////////////////
00160 //     Function: Datagram::add_uint32
00161 //       Access: Public
00162 //  Description: Adds an unsigned 32-bit integer to the datagram.
00163 ////////////////////////////////////////////////////////////////////
00164 INLINE void Datagram::
00165 add_uint32(PN_uint32 value) {
00166   LittleEndian s(&value, sizeof(value));
00167   append_data(s.get_data(), sizeof(value));
00168 }
00169 
00170 ////////////////////////////////////////////////////////////////////
00171 //     Function: Datagram::add_uint64
00172 //       Access: Public
00173 //  Description: Adds an unsigned 64-bit integer to the datagram.
00174 ////////////////////////////////////////////////////////////////////
00175 INLINE void Datagram::
00176 add_uint64(PN_uint64 value) {
00177   LittleEndian s(&value, sizeof(value));
00178   append_data(s.get_data(), sizeof(value));
00179 }
00180 
00181 ////////////////////////////////////////////////////////////////////
00182 //     Function: Datagram::add_float32
00183 //       Access: Public
00184 //  Description: Adds a 32-bit single-precision floating-point number
00185 //               to the datagram.  Since this kind of float is not
00186 //               necessarily portable across different architectures,
00187 //               special care is required.
00188 ////////////////////////////////////////////////////////////////////
00189 INLINE void Datagram::
00190 add_float32(PN_float32 value) {
00191   LittleEndian s(&value, sizeof(value));
00192   append_data(s.get_data(), sizeof(value));
00193 }
00194 
00195 ////////////////////////////////////////////////////////////////////
00196 //     Function: Datagram::add_float64
00197 //       Access: Public
00198 //  Description: Adds a 64-bit floating-point number to the datagram.
00199 ////////////////////////////////////////////////////////////////////
00200 INLINE void Datagram::
00201 add_float64(PN_float64 value) {
00202   LittleEndian s(&value, sizeof(value));
00203   append_data(s.get_data(), sizeof(value));
00204 }
00205 
00206 ////////////////////////////////////////////////////////////////////
00207 //     Function: Datagram::add_stdfloat
00208 //       Access: Public
00209 //  Description: Adds either a 32-bit or a 64-bit floating-point
00210 //               number, according to set_stdfloat_double().
00211 ////////////////////////////////////////////////////////////////////
00212 INLINE void Datagram::
00213 add_stdfloat(PN_stdfloat value) {
00214   if (_stdfloat_double) {
00215     add_float64(value);
00216   } else {
00217     add_float32(value);
00218   }
00219 }
00220 
00221 ////////////////////////////////////////////////////////////////////
00222 //     Function: Datagram::add_be_int16
00223 //       Access: Public
00224 //  Description: Adds a signed 16-bit big-endian integer to the
00225 //               datagram.
00226 ////////////////////////////////////////////////////////////////////
00227 INLINE void Datagram::
00228 add_be_int16(PN_int16 value) {
00229   BigEndian s(&value, sizeof(value));
00230   append_data(s.get_data(), sizeof(value));
00231 }
00232 
00233 ////////////////////////////////////////////////////////////////////
00234 //     Function: Datagram::add_be_int32
00235 //       Access: Public
00236 //  Description: Adds a signed 32-bit big-endian integer to the
00237 //               datagram.
00238 ////////////////////////////////////////////////////////////////////
00239 INLINE void Datagram::
00240 add_be_int32(PN_int32 value) {
00241   BigEndian s(&value, sizeof(value));
00242   append_data(s.get_data(), sizeof(value));
00243 }
00244 
00245 ////////////////////////////////////////////////////////////////////
00246 //     Function: Datagram::add_be_int64
00247 //       Access: Public
00248 //  Description: Adds a signed 64-bit big-endian integer to the
00249 //               datagram.
00250 ////////////////////////////////////////////////////////////////////
00251 INLINE void Datagram::
00252 add_be_int64(PN_int64 value) {
00253   BigEndian s(&value, sizeof(value));
00254   append_data(s.get_data(), sizeof(value));
00255 }
00256 
00257 ////////////////////////////////////////////////////////////////////
00258 //     Function: Datagram::add_be_uint16
00259 //       Access: Public
00260 //  Description: Adds an unsigned 16-bit big-endian integer to the
00261 //               datagram.
00262 ////////////////////////////////////////////////////////////////////
00263 INLINE void Datagram::
00264 add_be_uint16(PN_uint16 value) {
00265   BigEndian s(&value, sizeof(value));
00266   append_data(s.get_data(), sizeof(value));
00267 }
00268 
00269 ////////////////////////////////////////////////////////////////////
00270 //     Function: Datagram::add_be_uint32
00271 //       Access: Public
00272 //  Description: Adds an unsigned 32-bit big-endian integer to the
00273 //               datagram.
00274 ////////////////////////////////////////////////////////////////////
00275 INLINE void Datagram::
00276 add_be_uint32(PN_uint32 value) {
00277   BigEndian s(&value, sizeof(value));
00278   append_data(s.get_data(), sizeof(value));
00279 }
00280 
00281 ////////////////////////////////////////////////////////////////////
00282 //     Function: Datagram::add_be_uint64
00283 //       Access: Public
00284 //  Description: Adds an unsigned 64-bit big-endian integer to the
00285 //               datagram.
00286 ////////////////////////////////////////////////////////////////////
00287 INLINE void Datagram::
00288 add_be_uint64(PN_uint64 value) {
00289   BigEndian s(&value, sizeof(value));
00290   append_data(s.get_data(), sizeof(value));
00291 }
00292 
00293 ////////////////////////////////////////////////////////////////////
00294 //     Function: Datagram::add_be_float32
00295 //       Access: Public
00296 //  Description: Adds a 32-bit single-precision big-endian
00297 //               floating-point number to the datagram.
00298 ////////////////////////////////////////////////////////////////////
00299 INLINE void Datagram::
00300 add_be_float32(PN_float32 value) {
00301   BigEndian s(&value, sizeof(value));
00302   append_data(s.get_data(), sizeof(value));
00303 }
00304 
00305 ////////////////////////////////////////////////////////////////////
00306 //     Function: Datagram::add_be_float64
00307 //       Access: Public
00308 //  Description: Adds a 64-bit big-endian floating-point number to the
00309 //               datagram.
00310 ////////////////////////////////////////////////////////////////////
00311 INLINE void Datagram::
00312 add_be_float64(PN_float64 value) {
00313   BigEndian s(&value, sizeof(value));
00314   append_data(s.get_data(), sizeof(value));
00315 }
00316 
00317 ////////////////////////////////////////////////////////////////////
00318 //     Function: Datagram::add_string
00319 //       Access: Public
00320 //  Description: Adds a variable-length string to the datagram.  This
00321 //               actually adds a count followed by n bytes.
00322 ////////////////////////////////////////////////////////////////////
00323 INLINE void Datagram::
00324 add_string(const string &str) {
00325   // The max sendable length for a string is 2^16.
00326   nassertv(str.length() <= (PN_uint16)0xffff);
00327 
00328   // Strings always are preceded by their length
00329   add_uint16(str.length());
00330 
00331   // Add the string
00332   append_data(str);
00333 }
00334 
00335 ////////////////////////////////////////////////////////////////////
00336 //     Function: Datagram::add_string32
00337 //       Access: Public
00338 //  Description: Adds a variable-length string to the datagram, using
00339 //               a 32-bit length field to allow very long strings.
00340 ////////////////////////////////////////////////////////////////////
00341 INLINE void Datagram::
00342 add_string32(const string &str) {
00343   // Strings always are preceded by their length
00344   add_uint32(str.length());
00345 
00346   // Add the string
00347   append_data(str);
00348 }
00349 
00350 ////////////////////////////////////////////////////////////////////
00351 //     Function: Datagram::add_z_string
00352 //       Access: Public
00353 //  Description: Adds a variable-length string to the datagram, as a
00354 //               NULL-terminated string.
00355 ////////////////////////////////////////////////////////////////////
00356 INLINE void Datagram::
00357 add_z_string(string str) {
00358   // We must not have any nested null characters in the string.
00359   size_t null_pos = str.find('\0');
00360   // Add the string (sans the null character).
00361   append_data(str.substr(0, null_pos));
00362 
00363   // And the null character.
00364   add_uint8('\0');
00365 }
00366 
00367 ////////////////////////////////////////////////////////////////////
00368 //     Function: Datagram::add_fixed_string
00369 //       Access: Public
00370 //  Description: Adds a fixed-length string to the datagram.  If the
00371 //               string given is less than the requested size, this
00372 //               will pad the string out with zeroes; if it is greater
00373 //               than the requested size, this will silently truncate
00374 //               the string.
00375 ////////////////////////////////////////////////////////////////////
00376 INLINE void Datagram::
00377 add_fixed_string(const string &str, size_t size) {
00378   if (str.length() < size) {
00379     append_data(str);
00380     pad_bytes(size - str.length());
00381 
00382   } else { // str.length() >= size
00383     append_data(str.substr(0, size));
00384   }
00385 }
00386 
00387 ////////////////////////////////////////////////////////////////////
00388 //     Function: Datagram::append_data
00389 //       Access: Public
00390 //  Description: Appends some more raw data to the end of the
00391 //               datagram.
00392 ////////////////////////////////////////////////////////////////////
00393 INLINE void Datagram::
00394 append_data(const string &data) {
00395   append_data(data.data(), data.length());
00396 }
00397 
00398 ////////////////////////////////////////////////////////////////////
00399 //     Function: Datagram::get_message
00400 //       Access: Public
00401 //  Description: Returns the datagram's data as a string.
00402 ////////////////////////////////////////////////////////////////////
00403 INLINE string Datagram::
00404 get_message() const {
00405   // Silly special case for gcc 3.2, which can't tolerate string(NULL, 0).
00406   if (_data.size() == 0) {
00407     return string();
00408   } else {
00409     return string((const char *)_data.p(), _data.size());
00410   }
00411 }
00412 
00413 ////////////////////////////////////////////////////////////////////
00414 //     Function: Datagram::get_data
00415 //       Access: Public
00416 //  Description: Returns a pointer to the beginning of the datagram's
00417 //               data.
00418 ////////////////////////////////////////////////////////////////////
00419 INLINE const void *Datagram::
00420 get_data() const {
00421   return _data.p();
00422 }
00423 
00424 ////////////////////////////////////////////////////////////////////
00425 //     Function: Datagram::get_length
00426 //       Access: Public
00427 //  Description: Returns the number of bytes in the datagram.
00428 ////////////////////////////////////////////////////////////////////
00429 INLINE size_t Datagram::
00430 get_length() const {
00431   return _data.size();
00432 }
00433 
00434 ////////////////////////////////////////////////////////////////////
00435 //     Function: Datagram::set_array
00436 //       Access: Public
00437 //  Description: Replaces the data in the Datagram with the data in
00438 //               the indicated PTA_uchar.  This is assignment by
00439 //               reference: subsequent changes to the Datagram will
00440 //               also change the source PTA_uchar.
00441 ////////////////////////////////////////////////////////////////////
00442 INLINE void Datagram::
00443 set_array(PTA_uchar data) {
00444   _data = data;
00445 }
00446 
00447 ////////////////////////////////////////////////////////////////////
00448 //     Function: Datagram::copy_array
00449 //       Access: Public
00450 //  Description: Replaces the data in the Datagram with a copy of the
00451 //               data in the indicated CPTA_uchar.  Unlike
00452 //               set_array(), a complete copy is made of the data;
00453 //               subsequent changes to the Datagram will *not* change
00454 //               the source CPTA_uchar.
00455 ////////////////////////////////////////////////////////////////////
00456 INLINE void Datagram::
00457 copy_array(CPTA_uchar data) {
00458   _data.clear();
00459   _data.v() = data.v();
00460 }
00461 
00462 ////////////////////////////////////////////////////////////////////
00463 //     Function: Datagram::get_array
00464 //       Access: Public
00465 //  Description: Returns a const pointer to the actual data in
00466 //               the Datagram.
00467 ////////////////////////////////////////////////////////////////////
00468 INLINE CPTA_uchar Datagram::
00469 get_array() const {
00470   return _data;
00471 }
00472 
00473 ////////////////////////////////////////////////////////////////////
00474 //     Function: Datagram::modify_array
00475 //       Access: Public
00476 //  Description: Returns a modifiable pointer to the actual data in
00477 //               the Datagram.
00478 ////////////////////////////////////////////////////////////////////
00479 INLINE PTA_uchar Datagram::
00480 modify_array() {
00481   return _data;
00482 }
00483 
00484 ////////////////////////////////////////////////////////////////////
00485 //     Function: Datagram::set_stdfloat_double
00486 //       Access: Public
00487 //  Description: Changes the stdfloat_double flag, which defines the
00488 //               operation performed by add_stdfloat() and
00489 //               DatagramIterator::get_stdfloat().  When this is true,
00490 //               add_stdfloat() adds a 64-bit floating-point number;
00491 //               when it is false, it adds a 32-bit floating-point
00492 //               number.  The default is based on the STDFLOAT_DOUBLE
00493 //               compilation flag.
00494 ////////////////////////////////////////////////////////////////////
00495 INLINE void Datagram::
00496 set_stdfloat_double(bool stdfloat_double) {
00497   _stdfloat_double = stdfloat_double;
00498 }
00499 
00500 ////////////////////////////////////////////////////////////////////
00501 //     Function: Datagram::get_stdfloat_double
00502 //       Access: Public
00503 //  Description: Returns the stdfloat_double flag.  See
00504 //               set_stdfloat_double().
00505 ////////////////////////////////////////////////////////////////////
00506 INLINE bool Datagram::
00507 get_stdfloat_double() const {
00508   return _stdfloat_double;
00509 }
00510 
00511 ////////////////////////////////////////////////////////////////////
00512 //     Function: Datagram::operator ==
00513 //       Access: Public
00514 //  Description:
00515 ////////////////////////////////////////////////////////////////////
00516 INLINE bool Datagram::
00517 operator == (const Datagram &other) const {
00518   if (_data == other._data) {
00519     return true;
00520   }
00521   if (_data != (uchar *)NULL && other._data != (uchar *)NULL) {
00522     return _data.v() == other._data.v();
00523   }
00524   return false;
00525 }
00526 
00527 ////////////////////////////////////////////////////////////////////
00528 //     Function: Datagram::operator !=
00529 //       Access: Public
00530 //  Description:
00531 ////////////////////////////////////////////////////////////////////
00532 INLINE bool Datagram::
00533 operator != (const Datagram &other) const {
00534   return !operator == (other);
00535 }
00536 
00537 ////////////////////////////////////////////////////////////////////
00538 //     Function: Datagram::operator <
00539 //       Access: Public
00540 //  Description:
00541 ////////////////////////////////////////////////////////////////////
00542 INLINE bool Datagram::
00543 operator < (const Datagram &other) const {
00544   if (_data == other._data) {
00545     // Same pointers.
00546     return false;
00547   }
00548 
00549   if (_data != (uchar *)NULL && other._data != (uchar *)NULL) {
00550     // Different pointers, neither NULL.
00551     return _data.v() < other._data.v();
00552   }
00553 
00554   // One of the pointers is NULL, but not the other one.
00555   return _data.size() < other._data.size();
00556 }
00557 
00558 INLINE void
00559 generic_write_datagram(Datagram &dest, bool value) {
00560   dest.add_bool(value);
00561 }
00562 
00563 INLINE void
00564 generic_write_datagram(Datagram &dest, int value) {
00565   dest.add_int32(value);
00566 }
00567 
00568 INLINE void
00569 generic_write_datagram(Datagram &dest, float value) {
00570   dest.add_float32(value);
00571 }
00572 
00573 INLINE void
00574 generic_write_datagram(Datagram &dest, double value) {
00575   dest.add_float64(value);
00576 }
00577 
00578 INLINE void
00579 generic_write_datagram(Datagram &dest, const string &value) {
00580   dest.add_string(value);
00581 }
00582 
00583 INLINE void
00584 generic_write_datagram(Datagram &dest, const wstring &value) {
00585   dest.add_wstring(value);
00586 }
 All Classes Functions Variables Enumerations