Panda3D
|
00001 // Filename: datagram.h 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 #ifndef DATAGRAM_H 00016 #define DATAGRAM_H 00017 00018 #include "pandabase.h" 00019 00020 #include "numeric_types.h" 00021 #include "typedObject.h" 00022 #include "littleEndian.h" 00023 #include "bigEndian.h" 00024 #include "pta_uchar.h" 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Class : Datagram 00028 // Description : An ordered list of data elements, formatted in memory 00029 // for transmission over a socket or writing to a data 00030 // file. 00031 // 00032 // Data elements should be added one at a time, in 00033 // order, to the Datagram. The nature and contents of 00034 // the data elements are totally up to the user. When a 00035 // Datagram has been transmitted and received, its data 00036 // elements may be extracted using a DatagramIterator; 00037 // it is up to the caller to know the correct type of 00038 // each data element in order. 00039 // 00040 // A Datagram is itself headerless; it is simply a 00041 // collection of data elements. 00042 //////////////////////////////////////////////////////////////////// 00043 class EXPCL_PANDAEXPRESS Datagram : public TypedObject { 00044 PUBLISHED: 00045 INLINE Datagram(); 00046 INLINE Datagram(const void *data, size_t size); 00047 INLINE Datagram(const string &data); 00048 INLINE Datagram(const Datagram ©); 00049 INLINE void operator = (const Datagram ©); 00050 00051 virtual ~Datagram(); 00052 00053 virtual void clear(); 00054 void dump_hex(ostream &out, unsigned int indent=0) const; 00055 00056 INLINE void add_bool(bool value); 00057 INLINE void add_int8(PN_int8 value); 00058 INLINE void add_uint8(PN_uint8 value); 00059 00060 // The default numeric packing is little-endian. 00061 INLINE void add_int16(PN_int16 value); 00062 INLINE void add_int32(PN_int32 value); 00063 INLINE void add_int64(PN_int64 value); 00064 INLINE void add_uint16(PN_uint16 value); 00065 INLINE void add_uint32(PN_uint32 value); 00066 INLINE void add_uint64(PN_uint64 value); 00067 INLINE void add_float32(PN_float32 value); 00068 INLINE void add_float64(PN_float64 value); 00069 INLINE void add_stdfloat(PN_stdfloat value); 00070 00071 // These functions pack numbers big-endian, in case that's desired. 00072 INLINE void add_be_int16(PN_int16 value); 00073 INLINE void add_be_int32(PN_int32 value); 00074 INLINE void add_be_int64(PN_int64 value); 00075 INLINE void add_be_uint16(PN_uint16 value); 00076 INLINE void add_be_uint32(PN_uint32 value); 00077 INLINE void add_be_uint64(PN_uint64 value); 00078 INLINE void add_be_float32(PN_float32 value); 00079 INLINE void add_be_float64(PN_float64 value); 00080 00081 INLINE void add_string(const string &str); 00082 INLINE void add_string32(const string &str); 00083 INLINE void add_z_string(string str); 00084 INLINE void add_fixed_string(const string &str, size_t size); 00085 void add_wstring(const wstring &str); 00086 00087 void pad_bytes(size_t size); 00088 void append_data(const void *data, size_t size); 00089 INLINE void append_data(const string &data); 00090 00091 void assign(const void *data, size_t size); 00092 00093 INLINE string get_message() const; 00094 INLINE const void *get_data() const; 00095 INLINE size_t get_length() const; 00096 00097 INLINE void set_array(PTA_uchar data); 00098 INLINE void copy_array(CPTA_uchar data); 00099 INLINE CPTA_uchar get_array() const; 00100 INLINE PTA_uchar modify_array(); 00101 00102 INLINE void set_stdfloat_double(bool stdfloat_double); 00103 INLINE bool get_stdfloat_double() const; 00104 00105 INLINE bool operator == (const Datagram &other) const; 00106 INLINE bool operator != (const Datagram &other) const; 00107 INLINE bool operator < (const Datagram &other) const; 00108 00109 void output(ostream &out) const; 00110 void write(ostream &out, unsigned int indent=0) const; 00111 00112 private: 00113 PTA_uchar _data; 00114 bool _stdfloat_double; 00115 00116 public: 00117 00118 static TypeHandle get_class_type() { 00119 return _type_handle; 00120 } 00121 static void init_type() { 00122 TypedObject::init_type(); 00123 register_type(_type_handle, "Datagram", 00124 TypedObject::get_class_type()); 00125 } 00126 virtual TypeHandle get_type() const { 00127 return get_class_type(); 00128 } 00129 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00130 00131 00132 private: 00133 static TypeHandle _type_handle; 00134 }; 00135 00136 // These generic functions are primarily for writing a value to a 00137 // datagram from within a template in which the actual type of the 00138 // value is not known. If you do know the type, it's preferable to 00139 // use the explicit add_*() method from above instead. 00140 00141 INLINE void 00142 generic_write_datagram(Datagram &dest, bool value); 00143 INLINE void 00144 generic_write_datagram(Datagram &dest, int value); 00145 INLINE void 00146 generic_write_datagram(Datagram &dest, float value); 00147 INLINE void 00148 generic_write_datagram(Datagram &dest, double value); 00149 INLINE void 00150 generic_write_datagram(Datagram &dest, const string &value); 00151 INLINE void 00152 generic_write_datagram(Datagram &dest, const wstring &value); 00153 00154 00155 #include "datagram.I" 00156 00157 #endif