Panda3D

datagram.h

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 &copy);
00049   INLINE void operator = (const Datagram &copy);
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(float value);
00068   INLINE void add_float64(PN_float64 value);
00069 
00070   // These functions pack numbers big-endian, in case that's desired.
00071   INLINE void add_be_int16(PN_int16 value);
00072   INLINE void add_be_int32(PN_int32 value);
00073   INLINE void add_be_int64(PN_int64 value);
00074   INLINE void add_be_uint16(PN_uint16 value);
00075   INLINE void add_be_uint32(PN_uint32 value);
00076   INLINE void add_be_uint64(PN_uint64 value);
00077   INLINE void add_be_float32(float value);
00078   INLINE void add_be_float64(PN_float64 value);
00079 
00080   INLINE void add_string(const string &str);
00081   INLINE void add_string32(const string &str);
00082   INLINE void add_z_string(string str);
00083   INLINE void add_fixed_string(const string &str, size_t size);
00084   void add_wstring(const wstring &str);
00085 
00086   void pad_bytes(size_t size);
00087   void append_data(const void *data, size_t size);
00088   INLINE void append_data(const string &data);
00089 
00090   void assign(const void *data, size_t size);
00091 
00092   INLINE string get_message() const;
00093   INLINE const void *get_data() const;
00094   INLINE size_t get_length() const;
00095 
00096   INLINE void set_array(PTA_uchar data);
00097   INLINE void copy_array(CPTA_uchar data);
00098   INLINE CPTA_uchar get_array() const;
00099   INLINE PTA_uchar modify_array();
00100 
00101   INLINE bool operator == (const Datagram &other) const;
00102   INLINE bool operator != (const Datagram &other) const;
00103   INLINE bool operator < (const Datagram &other) const;
00104 
00105   void output(ostream &out) const;
00106   void write(ostream &out, unsigned int indent=0) const;
00107 
00108 private:
00109   PTA_uchar _data;
00110 
00111 
00112 public:
00113 
00114   static TypeHandle get_class_type() {
00115     return _type_handle;
00116   }
00117   static void init_type() {
00118     TypedObject::init_type();
00119     register_type(_type_handle, "Datagram",
00120                   TypedObject::get_class_type());
00121   }
00122   virtual TypeHandle get_type() const {
00123     return get_class_type();
00124   }
00125   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00126 
00127 
00128 private:
00129   static TypeHandle _type_handle;
00130 };
00131 
00132 // These generic functions are primarily for writing a value to a
00133 // datagram from within a template in which the actual type of the
00134 // value is not known.  If you do know the type, it's preferable to
00135 // use the explicit add_*() method from above instead.
00136 
00137 INLINE void
00138 generic_write_datagram(Datagram &dest, bool value);
00139 INLINE void
00140 generic_write_datagram(Datagram &dest, int value);
00141 INLINE void
00142 generic_write_datagram(Datagram &dest, float value);
00143 INLINE void
00144 generic_write_datagram(Datagram &dest, double value);
00145 INLINE void
00146 generic_write_datagram(Datagram &dest, const string &value);
00147 INLINE void
00148 generic_write_datagram(Datagram &dest, const wstring &value);
00149 
00150 
00151 #include "datagram.I"
00152 
00153 #endif
 All Classes Functions Variables Enumerations