Panda3D
 All Classes Functions Variables Enumerations
datagram.h
1 // Filename: datagram.h
2 // Created by: drose (06Jun00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef DATAGRAM_H
16 #define DATAGRAM_H
17 
18 #include "pandabase.h"
19 
20 #include "numeric_types.h"
21 #include "typedObject.h"
22 #include "littleEndian.h"
23 #include "bigEndian.h"
24 #include "pta_uchar.h"
25 
26 ////////////////////////////////////////////////////////////////////
27 // Class : Datagram
28 // Description : An ordered list of data elements, formatted in memory
29 // for transmission over a socket or writing to a data
30 // file.
31 //
32 // Data elements should be added one at a time, in
33 // order, to the Datagram. The nature and contents of
34 // the data elements are totally up to the user. When a
35 // Datagram has been transmitted and received, its data
36 // elements may be extracted using a DatagramIterator;
37 // it is up to the caller to know the correct type of
38 // each data element in order.
39 //
40 // A Datagram is itself headerless; it is simply a
41 // collection of data elements.
42 ////////////////////////////////////////////////////////////////////
43 class EXPCL_PANDAEXPRESS Datagram : public TypedObject {
44 PUBLISHED:
45  INLINE Datagram();
46  INLINE Datagram(const void *data, size_t size);
47  INLINE Datagram(const string &data);
48  INLINE Datagram(const Datagram &copy);
49  INLINE void operator = (const Datagram &copy);
50 
51 #ifdef USE_MOVE_SEMANTICS
52  INLINE Datagram(Datagram &&from) NOEXCEPT;
53  INLINE void operator = (Datagram &&from) NOEXCEPT;
54 #endif
55 
56  virtual ~Datagram();
57 
58  virtual void clear();
59  void dump_hex(ostream &out, unsigned int indent=0) const;
60 
61  INLINE void add_bool(bool value);
62  INLINE void add_int8(PN_int8 value);
63  INLINE void add_uint8(PN_uint8 value);
64 
65  // The default numeric packing is little-endian.
66  INLINE void add_int16(PN_int16 value);
67  INLINE void add_int32(PN_int32 value);
68  INLINE void add_int64(PN_int64 value);
69  INLINE void add_uint16(PN_uint16 value);
70  INLINE void add_uint32(PN_uint32 value);
71  INLINE void add_uint64(PN_uint64 value);
72  INLINE void add_float32(PN_float32 value);
73  INLINE void add_float64(PN_float64 value);
74  INLINE void add_stdfloat(PN_stdfloat value);
75 
76  // These functions pack numbers big-endian, in case that's desired.
77  INLINE void add_be_int16(PN_int16 value);
78  INLINE void add_be_int32(PN_int32 value);
79  INLINE void add_be_int64(PN_int64 value);
80  INLINE void add_be_uint16(PN_uint16 value);
81  INLINE void add_be_uint32(PN_uint32 value);
82  INLINE void add_be_uint64(PN_uint64 value);
83  INLINE void add_be_float32(PN_float32 value);
84  INLINE void add_be_float64(PN_float64 value);
85 
86  INLINE void add_string(const string &str);
87  INLINE void add_string32(const string &str);
88  INLINE void add_z_string(string str);
89  INLINE void add_fixed_string(const string &str, size_t size);
90  void add_wstring(const wstring &str);
91 
92  void pad_bytes(size_t size);
93  void append_data(const void *data, size_t size);
94  INLINE void append_data(const string &data);
95 
96  void assign(const void *data, size_t size);
97 
98  INLINE string get_message() const;
99  INLINE const void *get_data() const;
100  INLINE size_t get_length() const;
101 
102  INLINE void set_array(PTA_uchar data);
103  INLINE void copy_array(CPTA_uchar data);
104  INLINE CPTA_uchar get_array() const;
105  INLINE PTA_uchar modify_array();
106 
107  INLINE void set_stdfloat_double(bool stdfloat_double);
108  INLINE bool get_stdfloat_double() const;
109 
110  INLINE bool operator == (const Datagram &other) const;
111  INLINE bool operator != (const Datagram &other) const;
112  INLINE bool operator < (const Datagram &other) const;
113 
114  void output(ostream &out) const;
115  void write(ostream &out, unsigned int indent=0) const;
116 
117 private:
118  PTA_uchar _data;
119  bool _stdfloat_double;
120 
121 public:
122 
123  static TypeHandle get_class_type() {
124  return _type_handle;
125  }
126  static void init_type() {
128  register_type(_type_handle, "Datagram",
129  TypedObject::get_class_type());
130  }
131  virtual TypeHandle get_type() const {
132  return get_class_type();
133  }
134  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
135 
136 
137 private:
138  static TypeHandle _type_handle;
139 };
140 
141 // These generic functions are primarily for writing a value to a
142 // datagram from within a template in which the actual type of the
143 // value is not known. If you do know the type, it's preferable to
144 // use the explicit add_*() method from above instead.
145 
146 INLINE void
147 generic_write_datagram(Datagram &dest, bool value);
148 INLINE void
149 generic_write_datagram(Datagram &dest, int value);
150 INLINE void
151 generic_write_datagram(Datagram &dest, float value);
152 INLINE void
153 generic_write_datagram(Datagram &dest, double value);
154 INLINE void
155 generic_write_datagram(Datagram &dest, const string &value);
156 INLINE void
157 generic_write_datagram(Datagram &dest, const wstring &value);
158 
159 
160 #include "datagram.I"
161 
162 #endif
static void init_type()
This function is declared non-inline to work around a compiler bug in g++ 2.96.
Definition: typedObject.cxx:52
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43